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 , build the PDA with one state , input alphabet , stack alphabet , initial stack content (the start variable alone), accepting by empty stack, and the transitions:
- for every production : (pop the variable, push the right side with its first symbol on top);
- for every terminal : (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 -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; stack symbols; transitions. A string of length whose leftmost derivation has steps is accepted by a computation of moves.
From a PDA to a grammar (the triple construction)
Given a PDA accepting by empty stack with start state , bottom symbol , states and stack alphabet , build a grammar with the variables
Meaning. generates exactly the strings that the PDA can read while going from state to state and popping (and everything pushed above it later) for good, never touching what lies below .
Productions:
- for every (the whole computation pops );
- for every transition (), and for every choice of states : for (a pop): .
Size. At most variables, and for a transition that pushes symbols, 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 ( with a string of variables), use one state, stack alphabet , initial stack , accept by empty stack, and for every production the transition . Every move reads an input symbol: no -moves, and a string of length is accepted in exactly moves (verified on seven grammars).
How it works
Example: the PDA built from
Transitions: , , : transitions productions terminals. The computation on follows the leftmost derivation and builds its parse tree:
The PDA built from S → aSb | ε reading aabb: start
Parser run by a script, not typedOne-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.
- 1S → a S b
- 2S → ε
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.
It takes moves: expansions ( twice and ) and matches. The same construction applied to the grammars of the previous pages gave PDAs that accept exactly the same languages (named grammars: , palindromes, balanced parentheses, equal counts; and random grammars).
Example: the grammar built from a PDA
Take the two-state PDA for that accepts by empty stack: , , , , , . 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| transition | productions made | productions 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).
Overall: variables () and productions are made; after removing the useless ones variables and productions remain:
This is a (renamed) grammar for : and can be substituted, giving () and (that is or ). The language of this grammar equals that of the PDA on all strings up to length .
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
EasyA grammar has variables, terminals and productions. The PDA has state, stack symbols and transitions, and accepts by empty stack.
EX 2PDA for palindromes from a grammar
Easygives , , : transitions. On it expands , matches , expands , matches , expands , matches , matches : stack empty. (The language is that of even palindromes: confirmed on all strings up to length .)
EX 3Variables of the grammar from a PDA
MediumA PDA with states and stack symbols gives at most variables. A transition that pushes symbols gives up to productions, a transition that pushes nothing exactly . The bound counts all variables made, not only those that survive.
EX 4Read a [pXq] production
Mediumcomes from the transition . Reading replaces by ; to pop the lower and end in the machine must pop the new top first (going from to some state ) and then the next one (from to ). The construction tries every possible middle state ; only 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:" . (c) "Number of transitions:" . (d) "How many variables can the grammar built from an -state, -stack-symbol PDA have?" At most . (e) "Which acceptance condition does the standard grammar-to-PDA construction use?" Empty stack. (f) "Does the PDA from a GNF grammar need -moves?" No; every move reads a symbol.
Cheat box
Cheat box
- CFL PDA languages. Grammar PDA: 1 state, stack symbols , transitions , accept by empty stack, simulates a leftmost derivation.
- PDA grammar: variables ( and ); pushed symbols give productions; remove useless ones.
- GNF grammar PDA with no -moves: moves for a string of length .
- Final-state and empty-stack acceptance are interconvertible.
Traps and GATE patterns
Connections
Across subjects
No cross-subject links yet.
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 variables, terminals and productions. How many stack symbols and transitions does the PDA built from it have?
Show answerHide
stack symbols; transitions.
Question 2
What does the variable generate?
Show answerHide
The strings that make the PDA go from state to state while popping (and whatever was pushed above it).
Question 3
How many moves does the PDA from need to accept ?
Show answerHide
expansions ( three times, once) plus matches: moves.
Question 4
Why does the grammar-to-PDA construction accept by empty stack?
Show answerHide
When the whole sentential form has been matched against the input, nothing is left on the stack.
Revision card
One-screen revision
- Grammar PDA: one state, expand variables, match terminals, empty stack; transitions.
- PDA grammar: , variables, productions per transition; prune.
- GNF PDA without -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
My notes (saved in this browser)