Proofs: CFG to pushdown automaton conversion
These are the human proofs of CFG to pushdown automaton conversion, at full length.
The pushdown automaton built from a grammar accepts exactly the language of the grammar full
This proof shows: correctness.
Let with , and let be the output of lines L1 to L6: one state , stack alphabet , start symbol , an expand move for each production (line L3) and a match move for each (line L5), and no other move. Configurations are with the top of the stack on the left.
Lemma 1 (the moves of ). In a configuration with top symbol , the only moves are expand moves by productions of , and they read no input. In a configuration with top symbol , the only move is the match move, and it exists exactly when the next input symbol is . A configuration with an empty stack has no move.
Proof. Lines L3 and L5 are the only places where moves are added. An expand move has a nonterminal as its top symbol and a match move has a terminal, and .
Lemma 2 (runs are leftmost derivations). For all and ,
Moreover, the number of expand moves in such a run equals the number of steps of the corresponding derivation, and the run makes exactly match moves.
Proof of . Induction on the pair in lexicographic order, where is the number of steps of the leftmost derivation. If , then and the empty run works. If with , a derivation from never changes the first symbol, so with in steps. The match move gives , and the hypothesis for gives the rest. If with , then because has no nonterminal, and the first step of a leftmost derivation rewrites the leftmost nonterminal, which is : by some , and in steps. The expand move gives , and the hypothesis for gives the rest.
Proof of . Induction on the number of moves. If , then and , and the empty derivation works. If , the stack is not empty, and Lemma 1 gives two cases. If with , the first move is the match move and reads . The run reads exactly , so , and the other moves are a run . By induction , so . If with , the first move is an expand move by some , and the other moves are a run . By induction , and since is the leftmost symbol of , .
In both directions each expand move corresponds to one derivation step and each match move to one symbol of , which gives the counts.
Theorem 1 (acceptance by empty stack). .
Proof. Take and in Lemma 2: exactly when . Every derivation of a terminal string from can be reordered into a leftmost one, because both correspond to the same parse tree, so this holds exactly when .
Theorem 2 (acceptance by final state). Let be the output of lines L7 to L10: with new states and , a new stack symbol , the moves and , start state , start symbol and . Then .
Proof. If , Theorem 1 gives a run of . The moves of do not depend on the symbols below the top, so the same moves give in , and accepts.
Conversely, take an accepting run of on . The only move from is the first one, to . The only move into is the one from with on top, and has no moves, so the run ends there and that move happens with all of read. Between these two moves the state is and is never on top, because no move of pops or has as its top, until the stack above is empty. So the middle part is , where stays at the bottom, and removing gives a run of . By Theorem 1, .