Algorithm Search⌘K

Proofs: Equivalence of CFGs and pushdown automata

These are the human proofs of Equivalence of CFGs and pushdown automata, at full length.

Proof that context-free grammars and pushdown automata define the same languages full

This proof shows: statement.

We use the definitions of the statement: a PDA P=(Q,Σ,Γ,δ,q0,Z0,F)P = (Q, \Sigma, \Gamma, \delta, q_0, Z_0, F), configurations (q,w,γ)(q, w, \gamma) with the top of the stack on the left, the move relation ⊢\vdash, L(P)L(P) for acceptance by final state and N(P)N(P) for acceptance by empty stack. We prove 1⇒2⇒3⇒2⇒11 \Rightarrow 2 \Rightarrow 3 \Rightarrow 2 \Rightarrow 1.

Lemma 1 (extra input and extra stack). If (q,x,α)⊢∗(p,ε,β)(q, x, \alpha) \vdash^* (p, \varepsilon, \beta), then (q,xy,αγ)⊢∗(p,y,βγ)(q, xy, \alpha\gamma) \vdash^* (p, y, \beta\gamma) for all y∈Σ∗y \in \Sigma^* and γ∈Γ∗\gamma \in \Gamma^*. Conversely, if (q,xy,Xγ)⊢∗(p,y,γ)(q, xy, X\gamma) \vdash^* (p, y, \gamma) and the stack has more than ∣γ∣|\gamma| symbols at every configuration of the run except the last, then (q,x,X)⊢∗(p,ε,ε)(q, x, X) \vdash^* (p, \varepsilon, \varepsilon) by the same moves.

Proof. A move depends only on the state, the top stack symbol and, if it reads, the next input symbol. In the first claim, every configuration of the run has a nonempty stack before its move, because a configuration with an empty stack has no move. So the top symbol of each configuration lies in the part that comes from α\alpha, and the same move applies with γ\gamma below and yy after the input; it never reads yy, because the run reads exactly xx. In the second claim the moves never pop a symbol of γ\gamma before the last configuration, and they read exactly xx, so removing γ\gamma and yy leaves a valid run. □\square

From grammars to pushdown automata (1⇒21 \Rightarrow 2)

Let G=(V,Σ,R,S)G = (V, \Sigma, R, S). Define the PDA PGP_G with one state qq, stack alphabet V∪ΣV \cup \Sigma, start symbol SS, and the moves

  • (q,α)∈δ(q,ε,A)(q, \alpha) \in \delta(q, \varepsilon, A) for each production A→αA \to \alpha (expand);
  • (q,ε)∈δ(q,a,a)(q, \varepsilon) \in \delta(q, a, a) for each a∈Σa \in \Sigma (match).

Claim. For all x,y∈Σ∗x, y \in \Sigma^* and α∈(V∪Σ)∗\alpha \in (V \cup \Sigma)^*: (q,xy,α)⊢∗(q,y,ε)(q, xy, \alpha) \vdash^* (q, y, \varepsilon) if and only if α⇒lm∗x\alpha \Rightarrow^*_{lm} x.

Proof of "if". Induction on the pair (number kk of steps of the leftmost derivation, ∣α∣|\alpha|), ordered lexicographically. If α=ε\alpha = \varepsilon then x=εx = \varepsilon and zero moves suffice. If α=aα′\alpha = a\alpha' with a∈Σa \in \Sigma, a leftmost derivation from α\alpha never rewrites aa, so x=ax′x = a x' and α′⇒lm∗x′\alpha' \Rightarrow^*_{lm} x' in kk steps; a match move gives (q,ax′y,aα′)⊢(q,x′y,α′)(q, a x' y, a \alpha') \vdash (q, x'y, \alpha'), and the induction hypothesis, with the same kk and a shorter α\alpha, finishes. If α=Aα′\alpha = A\alpha' with A∈VA \in V, then k≥1k \ge 1, because xx has no nonterminal, and the first step rewrites the leftmost nonterminal AA by some A→βA \to \beta: α⇒lmβα′⇒lm∗x\alpha \Rightarrow_{lm} \beta\alpha' \Rightarrow^*_{lm} x in k−1k - 1 further steps. An expand move gives (q,xy,Aα′)⊢(q,xy,βα′)(q, xy, A\alpha') \vdash (q, xy, \beta\alpha'), and the induction hypothesis with k−1k - 1 finishes.

Proof of "only if". Induction on the number mm of moves. If m=0m = 0 then α=ε\alpha = \varepsilon and x=εx = \varepsilon. Otherwise look at the first move. If the top of the stack is a terminal aa, the move is a match and reads aa; the run reads exactly xx, so x=ax′x = a x', and the remaining m−1m - 1 moves give (q,x′y,α′)⊢∗(q,y,ε)(q, x'y, \alpha') \vdash^* (q, y, \varepsilon) where α=aα′\alpha = a\alpha'. By induction α′⇒lm∗x′\alpha' \Rightarrow^*_{lm} x', so α⇒lm∗ax′=x\alpha \Rightarrow^*_{lm} a x' = x. If the top is a nonterminal AA, the move is an expand by some A→βA \to \beta and reads nothing, and the remaining moves give (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 α=Aα′\alpha = A\alpha', α⇒lmβα′⇒lm∗x\alpha \Rightarrow_{lm} \beta\alpha' \Rightarrow^*_{lm} x. □\square

With α=S\alpha = S and y=εy = \varepsilon, the claim says w∈N(PG)w \in N(P_G) exactly when S⇒∗wS \Rightarrow^* w, since every derivation of a terminal string can be made leftmost. So N(PG)=L(G)N(P_G) = L(G).

Between the two kinds of acceptance (2⇔32 \Leftrightarrow 3)

From empty stack to final state. Given PP, add a new start state p0p_0, a new final state ff, and a new bottom symbol X0X_0. Add the moves (q0,Z0X0)∈δ′(p0,ε,X0)(q_0, Z_0 X_0) \in \delta'(p_0, \varepsilon, X_0) and (f,ε)∈δ′(q,ε,X0)(f, \varepsilon) \in \delta'(q, \varepsilon, X_0) for each q∈Qq \in Q, keep all moves of PP, and start with X0X_0. Then F′={f}F' = \{f\}. If (q0,w,Z0)⊢∗(q,ε,ε)(q_0, w, Z_0) \vdash^* (q, \varepsilon, \varepsilon) in PP, Lemma 1 gives (p0,w,X0)⊢(q0,w,Z0X0)⊢∗(q,ε,X0)⊢(f,ε,ε)(p_0, w, X_0) \vdash (q_0, w, Z_0 X_0) \vdash^* (q, \varepsilon, X_0) \vdash (f, \varepsilon, \varepsilon). Conversely, an accepting run of P′P' must enter ff, which happens only by a move on top symbol X0X_0. Before that move, the moves of PP never see X0X_0 except at that point, because PP has no move on X0X_0, so the run is (p0,w,X0)⊢(q0,w,Z0X0)⊢∗(q,w′′,X0)⊢(f,w′′,ε)(p_0, w, X_0) \vdash (q_0, w, Z_0 X_0) \vdash^* (q, w'', X_0) \vdash (f, w'', \varepsilon), and ff has no moves, so w′′=εw'' = \varepsilon. By the second part of Lemma 1, (q0,w,Z0)⊢∗(q,ε,ε)(q_0, w, Z_0) \vdash^* (q, \varepsilon, \varepsilon) in PP. So L(P′)=N(P)L(P') = N(P).

From final state to empty stack. Given PP, add a new start state p0p_0, a new bottom symbol X0X_0 and a new state ee. Add (q0,Z0X0)∈δ′(p0,ε,X0)(q_0, Z_0 X_0) \in \delta'(p_0, \varepsilon, X_0), add (e,ε)∈δ′(q,ε,Y)(e, \varepsilon) \in \delta'(q, \varepsilon, Y) for each q∈Fq \in F and Y∈Γ∪{X0}Y \in \Gamma \cup \{X_0\}, and add (e,ε)∈δ′(e,ε,Y)(e, \varepsilon) \in \delta'(e, \varepsilon, Y) for each Y∈Γ∪{X0}Y \in \Gamma \cup \{X_0\}. If PP accepts ww by final state in state q∈Fq \in F with stack γ\gamma, then P′P' runs to (q,ε,γX0)(q, \varepsilon, \gamma X_0) and empties its stack in state ee. Conversely, the stack of P′P' becomes empty only when X0X_0 is popped, which only ee and the states of FF do, by the new moves. Before the first entry into ee or such a pop, the run is a run of PP with X0X_0 below; it ends in a state of FF, and ee reads no input, so all of ww was read when the run of PP reached that state of FF. So N(P′)=L(P)N(P') = L(P).

From pushdown automata to grammars (2⇒12 \Rightarrow 1)

Let PP accept by empty stack. Define GPG_P with start symbol SS and nonterminals [pXq][pXq] for p,q∈Qp, q \in Q and X∈ΓX \in \Gamma, and the productions

  • S→[q0Z0p]S \to [q_0 Z_0 p] for each p∈Qp \in Q;
  • for each move (r,Y1⋯Yk)∈δ(q,a,X)(r, Y_1 \cdots Y_k) \in \delta(q, a, X) with a∈Σ∪{ε}a \in \Sigma \cup \{\varepsilon\} and k≥0k \ge 0, and for all states r1,…,rkr_1, \ldots, r_k, the production [qXrk]→a [rY1r1][r1Y2r2]⋯[rk−1Ykrk][q X r_k] \to a\, [r Y_1 r_1][r_1 Y_2 r_2] \cdots [r_{k-1} Y_k r_k], where for k=0k = 0 the production is [qXr]→a[q X r] \to a.

Claim. [qXp]⇒∗w[qXp] \Rightarrow^* w if and only if (q,w,X)⊢∗(p,ε,ε)(q, w, X) \vdash^* (p, \varepsilon, \varepsilon).

Proof of "if". Induction on the number m≥1m \ge 1 of moves. The first move uses some (r,Y1⋯Yk)∈δ(q,a,X)(r, Y_1 \cdots Y_k) \in \delta(q, a, X) and reads aa, so w=aw′w = a w' and (r,w′,Y1⋯Yk)⊢m−1(p,ε,ε)(r, w', Y_1 \cdots Y_k) \vdash^{m-1} (p, \varepsilon, \varepsilon). If k=0k = 0 then m=1m = 1, r=pr = p, w′=εw' = \varepsilon, and [qXp]→a[qXp] \to a is a production. If k≥1k \ge 1, let r0=rr_0 = r, and for i=1,…,ki = 1, \ldots, k let rir_i be the state at the first configuration of the remaining run whose stack is Yi+1⋯YkY_{i+1} \cdots Y_k, and wiw_i the input read between the configurations for i−1i - 1 and ii. The stack shrinks by at most one symbol per move, so these configurations exist and come in order, and rk=pr_k = p, w′=w1⋯wkw' = w_1 \cdots w_k. Between them the stack is longer than k−ik - i, so by the second part of Lemma 1, (ri−1,wi,Yi)⊢∗(ri,ε,ε)(r_{i-1}, w_i, Y_i) \vdash^* (r_i, \varepsilon, \varepsilon) in fewer than mm moves. By induction [ri−1Yiri]⇒∗wi[r_{i-1} Y_i r_i] \Rightarrow^* w_i, and the production [qXp]→a[r0Y1r1]⋯[rk−1Ykrk][qXp] \to a [r_0 Y_1 r_1] \cdots [r_{k-1} Y_k r_k] gives [qXp]⇒∗aw1⋯wk=w[qXp] \Rightarrow^* a w_1 \cdots w_k = w.

Proof of "only if". Induction on the length of the derivation. Its first step uses a production [qXp]→a[r0Y1r1]⋯[rk−1Ykrk][qXp] \to a [r_0 Y_1 r_1] \cdots [r_{k-1} Y_k r_k] with rk=pr_k = p that comes from a move (r0,Y1⋯Yk)∈δ(q,a,X)(r_0, Y_1 \cdots Y_k) \in \delta(q, a, X), and w=aw1⋯wkw = a w_1 \cdots w_k with [ri−1Yiri]⇒∗wi[r_{i-1} Y_i r_i] \Rightarrow^* w_i by shorter derivations. By induction (ri−1,wi,Yi)⊢∗(ri,ε,ε)(r_{i-1}, w_i, Y_i) \vdash^* (r_i, \varepsilon, \varepsilon), and by the first part of Lemma 1, (ri−1,wi⋯wk,Yi⋯Yk)⊢∗(ri,wi+1⋯wk,Yi+1⋯Yk)(r_{i-1}, w_i \cdots w_k, Y_i \cdots Y_k) \vdash^* (r_i, w_{i+1} \cdots w_k, Y_{i+1} \cdots Y_k). Chaining these after the move (q,w,X)⊢(r0,w1⋯wk,Y1⋯Yk)(q, w, X) \vdash (r_0, w_1 \cdots w_k, Y_1 \cdots Y_k) gives (q,w,X)⊢∗(p,ε,ε)(q, w, X) \vdash^* (p, \varepsilon, \varepsilon). □\square

By the claim, S⇒∗wS \Rightarrow^* w exactly when (q0,w,Z0)⊢∗(p,ε,ε)(q_0, w, Z_0) \vdash^* (p, \varepsilon, \varepsilon) for some pp, that is, L(GP)=N(P)L(G_P) = N(P).

Conclusion

1⇒21 \Rightarrow 2 is the construction of PGP_G, 2⇒32 \Rightarrow 3 and 3⇒23 \Rightarrow 2 are the two conversions between kinds of acceptance, and 2⇒12 \Rightarrow 1 is the construction of GPG_P. All are effective. ■\blacksquare

Back to Equivalence of CFGs and pushdown automata