G27
Self-checkedTier 1 9 min readtoc.pda-cfg-equivalence

PDA and CFG equivalence

Context-free grammars and pushdown automata define the same languages: the one-state PDA built from a grammar (expand variables on the stack, match terminals), the triple construction [pXq] that turns a PDA with acceptance by empty stack into a grammar, a PDA without epsilon-moves from Greibach normal form, the sizes of both constructions, and traced examples of each direction.

Why it exists

Two very different descriptions, grammars (generate strings by rewriting) and pushdown automata (accept strings with a stack), define the same languages: the context-free languages. Each direction is a construction, and GATE asks for the sizes: the number of states, stack symbols and transitions of the PDA built from a grammar, and the number of variables of the grammar built from a PDA. The equivalence also lets us prove properties of CFLs with whichever description is more convenient.

Prerequisites

You need PDAs and their acceptance conditions (PDA) and grammars with derivations (context-free grammars); the Greibach variant uses normal forms.

Precise definitions

Theorem. A language is context-free iff some PDA accepts it (by empty stack, equivalently by final state).

From a grammar to a PDA

Given G=(V,Σ,R,S)G = (V, \Sigma, R, S), build the PDA with one state qq, input alphabet Σ\Sigma, stack alphabet V∪ΣV \cup \Sigma, initial stack content SS (the start variable alone), accepting by empty stack, and the transitions:

  1. for every production A→αA \to \alpha: (q,α)∈δ(q,ε,A)(q, \alpha) \in \delta(q, \varepsilon, A) (pop the variable, push the right side with its first symbol on top);
  2. for every terminal aa: (q,ε)∈δ(q,a,a)(q, \varepsilon) \in \delta(q, a, a) (match a terminal on top of the stack with the input and pop it).

Idea. The stack always holds the part of the current sentential form that has not been matched yet. The machine therefore follows a leftmost derivation: replace the leftmost variable (an ε\varepsilon-move that guesses a production), or match the leftmost terminal. The stack is empty and the input is used up exactly when the derivation has produced the input string.

Size. One state; ∣V∣+∣Σ∣|V| + |\Sigma| stack symbols; ∣R∣+∣Σ∣|R| + |\Sigma| transitions. A string of length nn whose leftmost derivation has dd steps is accepted by a computation of d+nd + n moves.

From a PDA to a grammar (the triple construction)

Given a PDA PP accepting by empty stack with start state q0q_0, bottom symbol Z0Z_0, states QQ and stack alphabet Γ\Gamma, build a grammar with the variables

Sand[p X q]for all p,q∈Q, X∈Γ.S \qquad \text{and} \qquad [p\,X\,q] \quad \text{for all } p, q \in Q,\ X \in \Gamma.

Meaning. [p X q][p\,X\,q] generates exactly the strings ww that the PDA can read while going from state pp to state qq and popping XX (and everything pushed above it later) for good, never touching what lies below XX.

Productions:

  • S→[q0 Z0 p]S \to [q_0\,Z_0\,p] for every p∈Qp \in Q (the whole computation pops Z0Z_0);
  • for every transition (r,Y1Y2⋯Yk)∈δ(q,a,X)(r, Y_1 Y_2 \cdots Y_k) \in \delta(q, a, X) (a∈Σ∪{ε}a \in \Sigma \cup \{\varepsilon\}), and for every choice of states r1,…,rkr_1, \ldots, r_k: [q X rk]→a [r Y1 r1] [r1 Y2 r2]⋯[rk−1 Yk rk];[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]; for k=0k = 0 (a pop): [q X r]→a[q\,X\,r] \to a.

Size. At most ∣Q∣2∣Γ∣+1|Q|^2 |\Gamma| + 1 variables, and for a transition that pushes kk symbols, ∣Q∣k|Q|^k productions. Most of them are useless (the chosen intermediate states never occur in a computation) and disappear when useless symbols are removed.

Grammar in Greibach normal form to a PDA without epsilon-moves

For a grammar in GNF (A→a αA \to a\,\alpha with α\alpha a string of variables), use one state, stack alphabet VV, initial stack SS, accept by empty stack, and for every production A→aαA \to a\alpha the transition (q,α)∈δ(q,a,A)(q, \alpha) \in \delta(q, a, A). Every move reads an input symbol: no ε\varepsilon-moves, and a string of length nn is accepted in exactly nn moves (verified on seven grammars).

How it works

Example: the PDA built from S→aSb∣εS \to aSb \mid \varepsilon

Transitions: δ(q,ε,S)={(q,aSb),(q,ε)}\delta(q, \varepsilon, S) = \{(q, aSb), (q, \varepsilon)\}, δ(q,a,a)={(q,ε)}\delta(q, a, a) = \{(q, \varepsilon)\}, δ(q,b,b)={(q,ε)}\delta(q, b, b) = \{(q, \varepsilon)\}: 44 transitions =2= 2 productions +2+ 2 terminals. The computation on aabbaabb follows the leftmost derivation and builds its parse tree:

The PDA built from S → aSb | ε reading aabb: start

Parser run by a script, not typed

One-state PDA from a grammar (accepting by empty stack). The stack is on the left with its top first; the tree is the parse tree whose leftmost derivation the stack follows.

  1. 1S → a S b
  2. 2S → ε
stack (top first) (top at the left)
S
unread input
aabb
action start
parse tree so far
S

Step 0

The PDA has a single state. Its stack starts with the grammar's start symbol S. Rule 1: a variable on top is replaced by the right side of one of its productions (an ε-move, guessed by the nondeterminism). Rule 2: a terminal on top is matched against the next input symbol.

0 / 7
computed by script: the machine is simulated or the sets are constructed by scripts/toc_lib.py, one step at a time. python3 scripts/gen-toc3-traces.py

It takes 77 moves: 33 expansions (S→aSbS \to aSb twice and S→εS \to \varepsilon) and 44 matches. The same construction applied to the grammars of the previous pages gave PDAs that accept exactly the same languages (named grammars: anbna^n b^n, palindromes, balanced parentheses, equal counts; and 200200 random grammars).

Example: the grammar built from a PDA

Take the two-state PDA for anbna^n b^n that accepts by empty stack: δ(q0,a,Z)={(q0,AZ)}\delta(q_0, a, Z) = \{(q_0, AZ)\}, δ(q0,a,A)={(q0,AA)}\delta(q_0, a, A) = \{(q_0, AA)\}, δ(q0,b,A)={(q1,ε)}\delta(q_0, b, A) = \{(q_1, \varepsilon)\}, δ(q1,b,A)={(q1,ε)}\delta(q_1, b, A) = \{(q_1, \varepsilon)\}, δ(q0,ε,Z)={(q1,ε)}\delta(q_0, \varepsilon, Z) = \{(q_1, \varepsilon)\}, δ(q1,ε,Z)={(q1,ε)}\delta(q_1, \varepsilon, Z) = \{(q_1, \varepsilon)\}. For each transition the table counts the productions the construction makes and lists the useful ones:

From a PDA to a grammar: the triple construction for a^n b^n: start

Computed by a script, not typed
transitionproductions madeproductions kept (useful)
δ(q0, ε, Z) ∋ (q1, ε)
δ(q0, a, A) ∋ (q0, AA)
δ(q0, a, Z) ∋ (q0, AZ)
δ(q0, b, A) ∋ (q1, ε)
δ(q1, ε, Z) ∋ (q1, ε)
δ(q1, b, A) ∋ (q1, ε)
start variable S

Step 0

The variable [p X q] derives the strings that make the PDA pop X while going from state p to state q. The full construction made 14 productions; after removing useless variables 7 remain. The result generates a^n b^n (checked up to length 10).

0 / 7
computed by script: the machine is simulated or the sets are constructed by scripts/toc_lib.py, one step at a time. python3 scripts/gen-toc3-traces.py

Overall: 99 variables (22⋅2+12^2 \cdot 2 + 1) and 1414 productions are made; after removing the useless ones 55 variables and 77 productions remain:

S→[q0Zq1],[q0Zq1]→ε∣a [q0Aq1] [q1Zq1],[q0Aq1]→a [q0Aq1] [q1Aq1]∣b,[q1Zq1]→ε,[q1Aq1]→b.S \to [q_0 Z q_1], \quad [q_0 Z q_1] \to \varepsilon \mid a\,[q_0 A q_1]\,[q_1 Z q_1], \quad [q_0 A q_1] \to a\,[q_0 A q_1]\,[q_1 A q_1] \mid b, \quad [q_1 Z q_1] \to \varepsilon, \quad [q_1 A q_1] \to b.

This is a (renamed) grammar for {anbn}\{a^n b^n\}: [q1Aq1]→b[q_1 A q_1] \to b and [q1Zq1]→ε[q_1 Z q_1] \to \varepsilon can be substituted, giving [q0Aq1]→a[q0Aq1]b∣b[q_0 A q_1] \to a [q_0 A q_1] b \mid b (akbk+1a^k b^{k+1}) and [q0Zq1]→ε∣a[q0Aq1][q_0 Z q_1] \to \varepsilon \mid a [q_0 A q_1] (that is ε\varepsilon or ak+1bk+1a^{k+1} b^{k+1}). The language of this grammar equals that of the PDA on all strings up to length 1010.

Consequences

  • Closure of CFLs under union, concatenation and star is easiest with grammars; closure under intersection with a regular language is easiest with PDAs (run the PDA and a DFA in parallel) (closure properties of CFLs).
  • Acceptance by final state or by empty stack gives the same class of languages.
  • Deterministic PDAs are strictly weaker than nondeterministic ones (textbook theorem, quoted; see the next batch); the grammar-to-PDA construction above is nondeterministic by nature (the production is guessed) (DPDA versus NPDA).

Worked examples

EX 1Size of the PDA from a grammar

Easy

A grammar has 55 variables, 33 terminals and 1212 productions. The PDA has 11 state, 5+3=85 + 3 = 8 stack symbols and 12+3=1512 + 3 = 15 transitions, and accepts by empty stack.

EX 2PDA for palindromes from a grammar

Easy

S→aSa∣bSb∣εS \to aSa \mid bSb \mid \varepsilon gives δ(q,ε,S)={(q,aSa),(q,bSb),(q,ε)}\delta(q, \varepsilon, S) = \{(q, aSa), (q, bSb), (q, \varepsilon)\}, δ(q,a,a)={(q,ε)}\delta(q, a, a) = \{(q, \varepsilon)\}, δ(q,b,b)={(q,ε)}\delta(q, b, b) = \{(q, \varepsilon)\}: 55 transitions. On abbaabba it expands S→aSaS \to aSa, matches aa, expands S→bSbS \to bSb, matches bb, expands S→εS \to \varepsilon, matches bb, matches aa: stack empty. (The language is that of even palindromes: confirmed on all strings up to length 88.)

EX 3Variables of the grammar from a PDA

Medium

A PDA with 33 states and 44 stack symbols gives at most 3⋅3⋅4+1=373 \cdot 3 \cdot 4 + 1 = 37 variables. A transition that pushes 22 symbols gives up to 32=93^2 = 9 productions, a transition that pushes nothing exactly 11. The bound counts all variables made, not only those that survive.

EX 4Read a [pXq] production

Medium

[q0Aq1]→a [q0Aq1] [q1Aq1][q_0 A q_1] \to a\,[q_0 A q_1]\,[q_1 A q_1] comes from the transition (q0,AA)∈δ(q0,a,A)(q_0, AA) \in \delta(q_0, a, A). Reading aa replaces AA by AAA A; to pop the lower AA and end in q1q_1 the machine must pop the new top AA first (going from q0q_0 to some state r1r_1) and then the next one (from r1r_1 to q1q_1). The construction tries every possible middle state r1r_1; only r1=q1r_1 = q_1 occurs in a real computation of this machine.

EX 5Typical questions

GATE level

(a) "A language is accepted by a PDA iff it is generated by:" a context-free grammar. (b) "Number of states of the PDA built from a CFG by the standard construction:" 11. (c) "Number of transitions:" ∣R∣+∣Σ∣|R| + |\Sigma|. (d) "How many variables can the grammar built from an nn-state, mm-stack-symbol PDA have?" At most n2m+1n^2 m + 1. (e) "Which acceptance condition does the standard grammar-to-PDA construction use?" Empty stack. (f) "Does the PDA from a GNF grammar need ε\varepsilon-moves?" No; every move reads a symbol.

Cheat box

Cheat box

  • CFL == PDA languages. Grammar →\to PDA: 1 state, stack symbols V∪ΣV \cup \Sigma, transitions ∣R∣+∣Σ∣|R| + |\Sigma|, accept by empty stack, simulates a leftmost derivation.
  • PDA →\to grammar: variables [pXq][pXq] (≤n2m+1\le n^2 m + 1 and SS); kk pushed symbols give ∣Q∣k|Q|^k productions; remove useless ones.
  • GNF grammar →\to PDA with no ε\varepsilon-moves: nn moves for a string of length nn.
  • Final-state and empty-stack acceptance are interconvertible.

Traps and GATE patterns

Connections

The equivalence gives two ways to prove closure properties of CFLs (closure properties of CFLs); it also shows that deterministic parsing of programming languages needs a restricted kind of PDA (DPDA versus NPDA); the Greibach variant uses normal forms (normal forms).

PYQs

No PYQs attached to this concept yet

Previous year questions are being added section by section from your PYQ book, each with my own re-checked answer. Nothing is shown from memory.

Self-check

Question 1

A grammar has 44 variables, 22 terminals and 99 productions. How many stack symbols and transitions does the PDA built from it have?

Show answer

4+2=64 + 2 = 6 stack symbols; 9+2=119 + 2 = 11 transitions.

Question 2

What does the variable [p X q][p\,X\,q] generate?

Show answer

The strings that make the PDA go from state pp to state qq while popping XX (and whatever was pushed above it).

Question 3

How many moves does the PDA from S→aSb∣εS \to aSb \mid \varepsilon need to accept aaabbbaaabbb?

Show answer

44 expansions (S→aSbS \to aSb three times, S→εS \to \varepsilon once) plus 66 matches: 1010 moves.

Question 4

Why does the grammar-to-PDA construction accept by empty stack?

Show answer

When the whole sentential form has been matched against the input, nothing is left on the stack.

Revision card

One-screen revision

  • Grammar →\to PDA: one state, expand variables, match terminals, empty stack; ∣R∣+∣Σ∣|R| + |\Sigma| transitions.
  • PDA →\to grammar: [pXq][pXq], ≤n2m+1\le n^2m + 1 variables, ∣Q∣k|Q|^k productions per transition; prune.
  • GNF →\to PDA without ε\varepsilon-moves.
  • CFL == PDA languages.

References

  • ·Sipser, Introduction to the Theory of Computation (3rd edition), Section 2.2 (Theorem 2.20: a language is context-free iff some PDA recognizes it; Lemmas 2.21 and 2.27)
  • ·Hopcroft, Motwani, Ullman, Introduction to Automata Theory, Languages, and Computation (3rd edition), Section 6.3 (equivalence of PDAs and CFGs, Theorems 6.13 and 6.14)

Verification

scripts/enum-checks/toc_cfg.py: the PDA built from a grammar accepts the same language on all strings up to length 8 for the named grammars and for 200 random grammars (strings up to length 5, with a generous stack bound); the grammar built from a PDA by the triple construction generates the same language for named PDAs, for round trips grammar -> PDA -> grammar, and for 200 random empty-stack PDAs (strings up to length 6); for a^n b^n the full construction made 14 productions and 9 variables (n^2 m + 1 with n = 2 states, m = 2 stack symbols), of which 7 productions and 5 variables are useful; the number of transitions of the PDA built from a grammar is the number of productions plus the number of terminals (checked on four grammars); the accepting computation of aabb takes 7 moves (3 expansions and 4 matches); the Greibach-normal-form PDA has no epsilon move and accepts a string of length n in exactly n moves (7 grammars); the run traces were replayed against the grammar. Textbook chapter references are from memory. SECOND PASS (2026-11-02, done by the assistant, not a human check): every Self-check question was solved before the printed answer was read and all agreed; the definitions, worked examples, cheat box and traps were read claim by claim and the numbers re-derived by hand or by a small script (state counts, derivations, parse-tree and move counts, decomposition counts); tables and animations are machine output and were read, not re-run; No other defect found. Status raised to self-checked; verified needs a textbook or human check.

last verified: never