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), configurations (q,w,γ) with the top of the stack on the left, the move relation ⊢, L(P) for acceptance by final state and N(P) for acceptance by empty stack. We prove 1⇒2⇒3⇒2⇒1.
Lemma 1 (extra input and extra stack). If (q,x,α)⊢∗(p,ε,β), then (q,xy,αγ)⊢∗(p,y,βγ) for all y∈Σ∗ and γ∈Γ∗. Conversely, if (q,xy,Xγ)⊢∗(p,y,γ) and the stack has more than ∣γ∣ symbols at every configuration of the run except the last, then (q,x,X)⊢∗(p,ε,ε) 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 α, and the same move applies with γ below and y after the input; it never reads y, because the run reads exactly x. In the second claim the moves never pop a symbol of γ before the last configuration, and they read exactly x, so removing γ and y leaves a valid run. □
From grammars to pushdown automata (1⇒2)
Let G=(V,Σ,R,S). Define the PDA PG with one state q, stack alphabet V∪Σ, start symbol S, and the moves
- (q,α)∈δ(q,ε,A) for each production A→α (expand);
- (q,ε)∈δ(q,a,a) for each a∈Σ (match).
Claim. For all x,y∈Σ∗ and α∈(V∪Σ)∗: (q,xy,α)⊢∗(q,y,ε) if and only if α⇒lm∗x.
Proof of "if". Induction on the pair (number k of steps of the leftmost derivation, ∣α∣), ordered lexicographically. If α=ε then x=ε and zero moves suffice. If α=aα′ with a∈Σ, a leftmost derivation from α never rewrites a, so x=ax′ and α′⇒lm∗x′ in k steps; a match move gives (q,ax′y,aα′)⊢(q,x′y,α′), and the induction hypothesis, with the same k and a shorter α, finishes. If α=Aα′ with A∈V, then k≥1, because x has no nonterminal, and the first step rewrites the leftmost nonterminal A by some A→β: α⇒lmβα′⇒lm∗x in k−1 further steps. An expand move gives (q,xy,Aα′)⊢(q,xy,βα′), and the induction hypothesis with k−1 finishes.
Proof of "only if". Induction on the number m of moves. If m=0 then α=ε and x=ε. Otherwise look at the first move. If the top of the stack is a terminal a, the move is a match and reads a; the run reads exactly x, so x=ax′, and the remaining m−1 moves give (q,x′y,α′)⊢∗(q,y,ε) where α=aα′. By induction α′⇒lm∗x′, so α⇒lm∗ax′=x. If the top is a nonterminal A, the move is an expand by some A→β and reads nothing, and the remaining moves give (q,xy,βα′)⊢∗(q,y,ε). By induction βα′⇒lm∗x, and since A is the leftmost symbol of α=Aα′, α⇒lmβα′⇒lm∗x. □
With α=S and y=ε, the claim says w∈N(PG) exactly when S⇒∗w, since every derivation of a terminal string can be made leftmost. So N(PG)=L(G).
Between the two kinds of acceptance (2⇔3)
From empty stack to final state. Given P, add a new start state p0, a new final state f, and a new bottom symbol X0. Add the moves (q0,Z0X0)∈δ′(p0,ε,X0) and (f,ε)∈δ′(q,ε,X0) for each q∈Q, keep all moves of P, and start with X0. Then F′={f}. If (q0,w,Z0)⊢∗(q,ε,ε) in P, Lemma 1 gives (p0,w,X0)⊢(q0,w,Z0X0)⊢∗(q,ε,X0)⊢(f,ε,ε). Conversely, an accepting run of P′ must enter f, which happens only by a move on top symbol X0. Before that move, the moves of P never see X0 except at that point, because P has no move on X0, so the run is (p0,w,X0)⊢(q0,w,Z0X0)⊢∗(q,w′′,X0)⊢(f,w′′,ε), and f has no moves, so w′′=ε. By the second part of Lemma 1, (q0,w,Z0)⊢∗(q,ε,ε) in P. So L(P′)=N(P).
From final state to empty stack. Given P, add a new start state p0, a new bottom symbol X0 and a new state e. Add (q0,Z0X0)∈δ′(p0,ε,X0), add (e,ε)∈δ′(q,ε,Y) for each q∈F and Y∈Γ∪{X0}, and add (e,ε)∈δ′(e,ε,Y) for each Y∈Γ∪{X0}. If P accepts w by final state in state q∈F with stack γ, then P′ runs to (q,ε,γX0) and empties its stack in state e. Conversely, the stack of P′ becomes empty only when X0 is popped, which only e and the states of F do, by the new moves. Before the first entry into e or such a pop, the run is a run of P with X0 below; it ends in a state of F, and e reads no input, so all of w was read when the run of P reached that state of F. So N(P′)=L(P).
From pushdown automata to grammars (2⇒1)
Let P accept by empty stack. Define GP with start symbol S and nonterminals [pXq] for p,q∈Q and X∈Γ, and the productions
- S→[q0Z0p] for each p∈Q;
- for each move (r,Y1⋯Yk)∈δ(q,a,X) with a∈Σ∪{ε} and k≥0, and for all states r1,…,rk, the production [qXrk]→a[rY1r1][r1Y2r2]⋯[rk−1Ykrk], where for k=0 the production is [qXr]→a.
Claim. [qXp]⇒∗w if and only if (q,w,X)⊢∗(p,ε,ε).
Proof of "if". Induction on the number m≥1 of moves. The first move uses some (r,Y1⋯Yk)∈δ(q,a,X) and reads a, so w=aw′ and (r,w′,Y1⋯Yk)⊢m−1(p,ε,ε). If k=0 then m=1, r=p, w′=ε, and [qXp]→a is a production. If k≥1, let r0=r, and for i=1,…,k let ri be the state at the first configuration of the remaining run whose stack is Yi+1⋯Yk, and wi the input read between the configurations for i−1 and i. The stack shrinks by at most one symbol per move, so these configurations exist and come in order, and rk=p, w′=w1⋯wk. Between them the stack is longer than k−i, so by the second part of Lemma 1, (ri−1,wi,Yi)⊢∗(ri,ε,ε) in fewer than m moves. By induction [ri−1Yiri]⇒∗wi, and the production [qXp]→a[r0Y1r1]⋯[rk−1Ykrk] gives [qXp]⇒∗aw1⋯wk=w.
Proof of "only if". Induction on the length of the derivation. Its first step uses a production [qXp]→a[r0Y1r1]⋯[rk−1Ykrk] with rk=p that comes from a move (r0,Y1⋯Yk)∈δ(q,a,X), and w=aw1⋯wk with [ri−1Yiri]⇒∗wi by shorter derivations. By induction (ri−1,wi,Yi)⊢∗(ri,ε,ε), and by the first part of Lemma 1, (ri−1,wi⋯wk,Yi⋯Yk)⊢∗(ri,wi+1⋯wk,Yi+1⋯Yk). Chaining these after the move (q,w,X)⊢(r0,w1⋯wk,Y1⋯Yk) gives (q,w,X)⊢∗(p,ε,ε). □
By the claim, S⇒∗w exactly when (q0,w,Z0)⊢∗(p,ε,ε) for some p, that is, L(GP)=N(P).
Conclusion
1⇒2 is the construction of PG, 2⇒3 and 3⇒2 are the two conversions between kinds of acceptance, and 2⇒1 is the construction of GP. All are effective. ■