Algorithm Search⌘K

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 G=(V,Σ,R,S)G = (V, \Sigma, R, S) with V∩Σ=∅V \cap \Sigma = \emptyset, and let PP be the output of lines L1 to L6: one state qq, stack alphabet V∪ΣV \cup \Sigma, start symbol SS, an expand move (q,α)∈δ(q,ε,A)(q, \alpha) \in \delta(q, \varepsilon, A) for each production A→αA \to \alpha (line L3) and a match move (q,ε)∈δ(q,a,a)(q, \varepsilon) \in \delta(q, a, a) for each a∈Σa \in \Sigma (line L5), and no other move. Configurations are (q,w,γ)(q, w, \gamma) with the top of the stack on the left.

Lemma 1 (the moves of PP). In a configuration with top symbol A∈VA \in V, the only moves are expand moves by productions of AA, and they read no input. In a configuration with top symbol a∈Σa \in \Sigma, the only move is the match move, and it exists exactly when the next input symbol is aa. 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 V∩Σ=∅V \cap \Sigma = \emptyset. □\square

Lemma 2 (runs are leftmost derivations). For all x,y∈Σ∗x, y \in \Sigma^* and α∈(V∪Σ)∗\alpha \in (V \cup \Sigma)^*,

(q,xy,α)⊢∗(q,y,ε)  ⟺  α⇒lm∗x.(q, xy, \alpha) \vdash^* (q, y, \varepsilon) \iff \alpha \Rightarrow^*_{lm} x.

Moreover, the number of expand moves in such a run equals the number of steps of the corresponding derivation, and the run makes exactly ∣x∣|x| match moves.

Proof of ⇐\Leftarrow. Induction on the pair (k,∣α∣)(k, |\alpha|) in lexicographic order, where kk is the number of steps of the leftmost derivation. If α=ε\alpha = \varepsilon, then x=εx = \varepsilon and the empty run works. If α=aα′\alpha = a\alpha' with a∈Σa \in \Sigma, a derivation from α\alpha never changes the first symbol, so x=ax′x = a x' with α′⇒lm∗x′\alpha' \Rightarrow^*_{lm} x' in kk steps. The match move gives (q,ax′y,aα′)⊢(q,x′y,α′)(q, a x' y, a\alpha') \vdash (q, x'y, \alpha'), and the hypothesis for (k,∣α′∣)(k, |\alpha'|) gives the rest. If α=Aα′\alpha = A\alpha' with A∈VA \in V, then k≥1k \ge 1 because xx has no nonterminal, and the first step of a leftmost derivation rewrites the leftmost nonterminal, which is AA: α⇒lmβα′\alpha \Rightarrow_{lm} \beta\alpha' by some A→βA \to \beta, and βα′⇒lm∗x\beta\alpha' \Rightarrow^*_{lm} x in k−1k - 1 steps. The expand move gives (q,xy,Aα′)⊢(q,xy,βα′)(q, xy, A\alpha') \vdash (q, xy, \beta\alpha'), and the hypothesis for k−1k - 1 gives the rest.

Proof of ⇒\Rightarrow. Induction on the number mm of moves. If m=0m = 0, then α=ε\alpha = \varepsilon and x=εx = \varepsilon, and the empty derivation works. If m≥1m \ge 1, the stack is not empty, and Lemma 1 gives two cases. If α=aα′\alpha = a\alpha' with a∈Σa \in \Sigma, the first move is the match move and reads aa. The run reads exactly xx, so x=ax′x = a x', and the other m−1m - 1 moves are a run (q,x′y,α′)⊢∗(q,y,ε)(q, x'y, \alpha') \vdash^* (q, y, \varepsilon). By induction α′⇒lm∗x′\alpha' \Rightarrow^*_{lm} x', so aα′⇒lm∗ax′a\alpha' \Rightarrow^*_{lm} a x'. If α=Aα′\alpha = A\alpha' with A∈VA \in V, the first move is an expand move by some A→βA \to \beta, and the other moves are a run (q,xy,βα′)⊢∗(q,y,ε)(q, xy, \beta\alpha') \vdash^* (q, y, \varepsilon). By induction βα′⇒lm∗x\beta\alpha' \Rightarrow^*_{lm} x, and since AA is the leftmost symbol of α\alpha, α⇒lmβα′⇒lm∗x\alpha \Rightarrow_{lm} \beta\alpha' \Rightarrow^*_{lm} x.

In both directions each expand move corresponds to one derivation step and each match move to one symbol of xx, which gives the counts. □\square

Theorem 1 (acceptance by empty stack). N(P)=L(G)N(P) = L(G).

Proof. Take α=S\alpha = S and y=εy = \varepsilon in Lemma 2: (q,w,S)⊢∗(q,ε,ε)(q, w, S) \vdash^* (q, \varepsilon, \varepsilon) exactly when S⇒lm∗wS \Rightarrow^*_{lm} w. Every derivation of a terminal string from SS can be reordered into a leftmost one, because both correspond to the same parse tree, so this holds exactly when w∈L(G)w \in L(G). □\square

Theorem 2 (acceptance by final state). Let P′P' be the output of lines L7 to L10: PP with new states ss and ff, a new stack symbol ⊥\bot, the moves (q,S⊥)∈δ(s,ε,⊥)(q, S\bot) \in \delta(s, \varepsilon, \bot) and (f,⊥)∈δ(q,ε,⊥)(f, \bot) \in \delta(q, \varepsilon, \bot), start state ss, start symbol ⊥\bot and F={f}F = \{f\}. Then L(P′)=L(G)L(P') = L(G).

Proof. If w∈L(G)w \in L(G), Theorem 1 gives a run (q,w,S)⊢∗(q,ε,ε)(q, w, S) \vdash^* (q, \varepsilon, \varepsilon) of PP. The moves of PP do not depend on the symbols below the top, so the same moves give (q,w,S⊥)⊢∗(q,ε,⊥)(q, w, S\bot) \vdash^* (q, \varepsilon, \bot) in P′P', and (s,w,⊥)⊢(q,w,S⊥)⊢∗(q,ε,⊥)⊢(f,ε,⊥)(s, w, \bot) \vdash (q, w, S\bot) \vdash^* (q, \varepsilon, \bot) \vdash (f, \varepsilon, \bot) accepts.

Conversely, take an accepting run of P′P' on ww. The only move from ss is the first one, to (q,w,S⊥)(q, w, S\bot). The only move into ff is the one from qq with ⊥\bot on top, and ff has no moves, so the run ends there and that move happens with all of ww read. Between these two moves the state is qq and ⊥\bot is never on top, because no move of PP pops ⊥\bot or has ⊥\bot as its top, until the stack above ⊥\bot is empty. So the middle part is (q,w,S⊥)⊢∗(q,ε,⊥)(q, w, S\bot) \vdash^* (q, \varepsilon, \bot), where ⊥\bot stays at the bottom, and removing ⊥\bot gives a run (q,w,S)⊢∗(q,ε,ε)(q, w, S) \vdash^* (q, \varepsilon, \varepsilon) of PP. By Theorem 1, w∈L(G)w \in L(G). ■\blacksquare

Back to CFG to pushdown automaton conversion