CFG ⟹ Nondeterministic PDA (Empty Stack Acceptance)
Let be a context-free grammar (CFG). Its language is generated by all terminal strings obtainable from via derivations using productions in . We prove that there exists a nondeterministic pushdown automaton (PDA) that accepts exactly by empty stack.
We will construct a nondeterministic PDA that simulates leftmost derivations in a “stack-as-sentential-form” manner: the PDA nondeterministically replaces a nonterminal on the stack using one grammar production, and consumes terminals by matching them against the input. This yields acceptance by empty stack whenever the grammar derivation produces the entire input and empties the sentential form.
Key concepts: context-free grammar , sentential form , leftmost derivation , nondeterministic PDA , empty-stack acceptance.
CFG to PDA (construction intuition)
The construction
Define the PDA (no final states are needed for empty-stack acceptance). Use:
- (single control state),
- ,
- initial stack symbol ,
- start configuration corresponds to pushing the start symbol above .
The PDA uses nondeterminism to choose a production whenever the top-of-stack is a nonterminal. When the top-of-stack is a terminal that matches the current input symbol, the PDA pops it while consuming the symbol. Formally, the transition relation will include two kinds of moves.
We will use the following construction idea: “production simulation” and “terminal matching”.
Build a nondeterministic PDA from a CFG (empty stack)
- 1Step 1
From the start, push onto the stack (above ) so the stack content represents the current sentential form to be expanded.
- 2Step 2
If the top of stack is a nonterminal , nondeterministically choose a production and replace by on the stack (in reverse order so that the leftmost symbol of ends up on top).
- 3Step 3
If the top of stack is a terminal and the current input symbol is , then consume that input symbol and pop from the stack.
- 4Step 4
When the input is fully consumed and the sentential form has been completely expanded and popped, the PDA reaches a configuration with empty stack, so it accepts by empty stack.
Formal transition rules
Let the PDA have a single state . We write transitions as [ \delta(q,; \text{inputSymbol or } \epsilon,; \text{stackTop}) ;\ni; (q,; \text{stackReplacement}) ] where “stackReplacement” is a string that will replace the popped stackTop symbol.
-
Initialization (push ): [ \delta(q,\epsilon,Z_0)\ni (q,SZ_0) ]
-
Production step (replace a nonterminal ):
For every production in where and , add: [ \delta(q,\epsilon,A)\ni \left(q,\text{rev}(\alpha)\right) ] where denotes the reverse string of so that the leftmost symbol of becomes the next stack-top to be handled. -
Terminal step (match and consume):
For each : [ \delta(q,a,a)\ni (q,\epsilon) ]
No other moves are required.
[CalloutBlock tip] type: "tip" title: "Why the reverse string?" content: "When you pop the stack, you effectively process symbols from left to right in the intended sentential form. Since stacks are LIFO, you push the right-hand side in reverse so that the leftmost symbol of is on top and processed first."
Correctness Proof
We show under empty-stack acceptance by proving both containments:
- If then accepts by empty stack.
- If accepts by empty stack then .
The heart of the proof is an invariant: at any point, the stack (ignoring ) is a sentential form reachable from , and the PDA’s remaining input corresponds to what still must be matched/produced.
Invariant (sentential form alignment)
Define the stack content (above ) as a string , representing the “yet-to-be-produced” sentential form. The PDA configuration after some steps has the form: [ (q, ; x, ; \gamma Z_0) ] where is the remaining unread input string.
Invariant statement: If is the current sentential form in the grammar derivation such that (producing exactly the remaining terminals in ), then the PDA can reach and maintain the configuration above through matching and production steps.
This invariant is proven via correspondence between:
- grammar production application and PDA nondeterministic replacement of top by ,
- terminal emission of the leftmost terminal symbol and PDA terminal matching move.
(1) If , then accepts by empty stack
Assume . Then there exists a derivation: [ S \Rightarrow^* w ] Consider a leftmost derivation (every derivation can be refined into a leftmost one). A leftmost derivation expands the leftmost nonterminal at each step, producing terminals in the correct left-to-right order.
We simulate this derivation with the PDA:
- Initially, pushes , so the stack equals the current sentential form.
- Whenever the grammar applies a production to the leftmost nonterminal in the sentential form, the PDA has at the top of stack and can nondeterministically apply the production step to replace by .
- Whenever the sentential form has a leftmost terminal to be produced next, that terminal appears on top of the PDA stack, and the PDA can apply the terminal matching transition to consume from input and pop it.
At the end of the derivation, the sentential form becomes the terminal string and then is consumed symbol-by-symbol. After the entire input is read, the stack symbols above have all been popped, so the PDA can reach an empty stack configuration (by having popped everything that was representing ). Hence it accepts by empty stack.
(2) If accepts by empty stack, then
Assume the PDA accepts by empty stack. Then there exists a computation that, starting from the initial configuration, consumes all input symbols and results in an empty stack.
Track the PDA’s stack evolution (excluding ):
- Each time the PDA replaces a nonterminal on top of the stack via the rule , we interpret it as a grammar production step .
- Each time the PDA matches a terminal via , we interpret it as the grammar having that terminal as part of its leftmost sentential form at that stage.
Because the PDA pops all symbols above exactly when it consumes terminals from the input, the sequence of production choices corresponds to a valid derivation from that yields .
Therefore, , so .
Nonterminal/terminal discipline matters
The PDA must only use production steps when the stack top is a nonterminal and must only use terminal steps when the stack top is a terminal that matches the current input symbol. This discipline is what guarantees the PDA does not generate terminal strings in an order inconsistent with the grammar.
Summary (Language equality)
From the two containments proved above, we conclude: [ L(P)=L(G) ] where acceptance is defined as reaching empty stack after consuming the entire input.
This is the core direction of the CFG–PDA equivalence theorem: CFGs and nondeterministic PDAs describe the same class of languages (the context-free languages), with different but equivalent acceptance conventions (final states vs empty stack).
Correspondence between derivations and PDA moves
Push $S$
StartPDA stack represents initial sentential form."
Expand leftmost nonterminal
Derivation stepTop-of-stack nonterminal replaced by production RHS ."
Match leftmost terminal
Terminal productionConsume input symbol and pop ."
Empty stack
EndAfter input consumed, stack is empty ⇒ accept."
Common edge cases and clarifications
Grammar step vs PDA step
A quick mapping of how each derivation action is simulated.
Knowledge Check
In the PDA construction, when the top of the stack is a nonterminal , which move is used to simulate the grammar?
Explore Related Topics
Which Automaton Accepts Regular Languages? The Correct Answer Is DFA
The deterministic finite automaton (DFA) is the canonical model that exactly accepts regular languages, whereas PDA, LBA, and Turing machines recognize strictly larger language families.
- A language is regular iff some DFA accepts it: .
- DFA ↔ regular languages; PDA ↔ context‑free; LBA ↔ context‑sensitive; Turing machine ↔ recursively enumerable.
- Regular languages form the base of the hierarchy: .
- DFA’s finite memory limits it to patterns like “ends with 01” or “even number of 1’s”, but it cannot handle unbounded counting such as .
Proving That a Grammar Is LL(1) but Not SLR(1)
Constructing a PDA for a Given Grammar and Testing Acceptance of a String