2

How do I convert a CFG to a TM? I have a basic idea of how to convert a DFA to a TM, but I can't think of a way to do this one properly.

Can I get some general implementation steps on this?

PTN
  • 1,658
  • 5
  • 24
  • 54
  • You are looking for a parsing algorithm like https://en.wikipedia.org/wiki/CYK_algorithm . Maybe read about it and then implement it on the TM. – Peter Leupold May 03 '17 at 08:04

2 Answers2

4
  1. Construct a pushdown automata (PDA) using a standard algorithm. I won't go into details of any particular construction (a separate question would be a better place to cover that) but you can search "convert cfg to pda" and get results. One example is here.

  2. Construct a two-tape Turing Machine from the PDA as follows:

    • The Turing machine reads the input tape (tape #1) left to right
    • The Turing machine uses the second tape as the stack, moving right when pushing and left when popping.
  3. Construct a vanilla one-tape Turing Machine from the two-tape machine using a standard construction. The proof that one-tape and two-tape TMs are equivalent is a constructive proof and you can follow the implied algorithm to show construct a single-tape TM for the language of your CFG.

Patrick87
  • 27,682
  • 3
  • 38
  • 73
1

Every produced word of the CFG can be written as a parse tree. If you get a string produced by the CFG these are the leafs of your parse tree. To determine whether the string is produced by the CFG you simply go from the leaves to inner nodes till the root. So the general idea is to turn around the production rules. If you can reach the root, then the string is part of the grammar. If you are at a point, where no turned production rule fits, then the word is not produced by the CFG. Note that if there are more than one possibilities you need to try all possibilities and if one works, the string is produced by the CFG. If none works, it is not.

Sebastian Walla
  • 1,104
  • 1
  • 9
  • 23
  • Any naive implementation of this answer will potentially fail to halt if the grammar is left-recursive, even indirectly. – rici May 03 '17 at 01:44