Back to the 2022 paper

Module 2: Context-Free Languages (CFL) and PDA

20227m

Prove that if LL is accepted by a PDA, then LL can be generated by a CFG.

Worked SolutionAI Assisted

Answer

Theorem: LL is accepted by a PDA   ⟺  \iff LL is generated by a CFG (i.e., CFL = NPDA languages).

We prove PDA →\rightarrow CFG using the Triplet Construction (Sipser / standard textbook method).


Given

PDA P=(Q,Σ,Γ,δ,q0,Z0,F)P = (Q, \Sigma, \Gamma, \delta, q_0, Z_0, F) accepting L(P)L(P) by final state (empty stack can be converted).

Assume δ\delta has no ϵ\epsilon-transitions on input (can be eliminated). If ϵ\epsilon-moves exist, handle via closure.


Construction of CFG G=(V,Σ,R,S)G = (V, \Sigma, R, S)

Variables VV:

  • Start symbol SS
  • Triplet variables [pAq][p A q] for every p,q∈Qp,q \in Q and A∈ΓA \in \Gamma
    • Meaning: "Starting in state pp with stack top AA, PP can reach state qq with AA popped off (and any symbols pushed above it matched)."

Productions RR:

  1. Start: S→[q0Z0p]S \rightarrow [q_0 Z_0 p] for each p∈Fp \in F

    • Begin at q0q_0 with initial stack Z0Z_0, end in any final state pp.
  2. From PDA transitions:
    For every transition $\delta(p, a, A)
    i (q, \alpha)where where a \in \Sigma \cup {\epsilon}$:

    • Case α=ϵ\alpha = \epsilon (pop AA, no push):
      [pAq]→a [p A q] \rightarrow a
      (if a=ϵa=\epsilon, RHS is ϵ\epsilon)

    • Case α=B1B2…Bk\alpha = B_1 B_2 \dots B_k (k≥1k \ge 1):
      For all r1,r2,…,rk+1∈Qr_1, r_2, \dots, r_{k+1} \in Q:
      [pArk+1]→a  [qB1r1]  [r1B2r2]…[rkBkrk+1] [p A r_{k+1}] \rightarrow a\; [q B_1 r_1]\; [r_1 B_2 r_2] \dots [r_k B_k r_{k+1}]

      • Push B1…BkB_1\dots B_k; must pop them in order via intermediate states.
      • aa consumed on transition; each [riBi+1ri+1][r_i B_{i+1} r_{i+1}] matches push/pop of Bi+1B_{i+1}.
  3. Empty stack (optional, for empty-stack acceptance):
    Add [pϵp]→ϵ[p \epsilon p] \rightarrow \epsilon for all p∈Qp \in Q (not needed for final-state).


Intuition of Triplet [pAq][p A q]

  • Represents a matching push-pop pair on symbol AA.
  • pp: state where AA is pushed
  • qq: state where AA is popped
  • The derivation inside [pAq][p A q] generates the string read between push and pop of AA.

Proof of Correctness (Sketch)

Soundness (CFG ⊆\subseteq PDA):
Every derivation in GG corresponds to a valid PDA computation:

  • S⇒∗[q0Z0p]S \Rightarrow^* [q_0 Z_0 p] maps to initial config.
  • Production [pAr]→a[qBr1]…[p A r] \rightarrow a [q B r_1] \dots mimics transition (p,a,A)→(q,B… )(p,a,A) \to (q,B\dots).
  • Base case [pAq]→a[p A q] \rightarrow a mimics (p,a,A)→(q,ϵ)(p,a,A) \to (q,\epsilon).
  • Induction on derivation length shows derived string is accepted by PP.

Completeness (PDA ⊆\subseteq CFG):
For any accepting run of PP on ww, build a parse tree in GG:

  • Root S→[q0Z0p]S \to [q_0 Z_0 p].
  • Each push of AA at state pp and corresponding pop at qq gives variable [pAq][p A q].
  • The substring read between push and pop is generated by that variable.
  • By induction on height of push-pop pairs, ww is generated.

Example

PDA for {anbn}\{a^n b^n\}:

  • δ(q0,a,Z0)=(q0,XZ0)\delta(q_0, a, Z_0) = (q_0, XZ_0)
  • δ(q0,a,X)=(q0,XX)\delta(q_0, a, X) = (q_0, XX)
  • δ(q0,b,X)=(q1,ϵ)\delta(q_0, b, X) = (q_1, \epsilon)
  • F={q1}F = \{q_1\}

CFG variables: S,[q0Z0q1],[q0Xq0],[q0Xq1],…S, [q_0 Z_0 q_1], [q_0 X q_0], [q_0 X q_1], \dots
Productions:
S→[q0Z0q1]S \rightarrow [q_0 Z_0 q_1]
[q0Xq1]→a[q0Xq1]b∣ab[q_0 X q_1] \rightarrow a [q_0 X q_1] b \mid ab (after resolving all intermediate states)
[q0Z0q1]→a[q0Xq1]b[q_0 Z_0 q_1] \rightarrow a [q_0 X q_1] b

Yields anbna^n b^n.


Formal Statement

Theorem: LL is accepted by a (nondeterministic) PDA iff LL is generated by a CFG.
Corollary: CFL = languages accepted by NPDA. DPDA ⊂\subset CFL (strict).


Final Answer

Proof via Triplet Construction: Create CFG with variables [pAq][p A q] representing push-pop of AA from state pp to qq. Productions mirror PDA transitions. S→[q0Z0p]S \to [q_0 Z_0 p] for final pp. Induction on derivations/computations proves L(G)=L(P)L(G) = L(P). Hence every PDA language is a CFL.

Similar questions

Official AppAndroid App

PYQDeck for Android

Solve BEU previous-year exam papers anywhere, anytime. Fast search, offline paper viewing, and step-by-step solutions designed specifically for your phone.