CFG ⟹ Nondeterministic PDA (Empty Stack Acceptance)

CFG ⟹ Nondeterministic PDA (Empty Stack Acceptance)

Verified Sources
Sep 12, 2026

Let G=(V,Σ,R,S)G=(V,\Sigma,R,S) be a context-free grammar (CFG). Its language L(G)L(G) is generated by all terminal strings obtainable from SS via derivations using productions in RR. We prove that there exists a nondeterministic pushdown automaton (PDA) that accepts exactly L(G)L(G) by empty stack.

We will construct a nondeterministic PDA PP 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 P=(Q,Σ,Γ,δ,q0,Z0)P=(Q,\Sigma,\Gamma,\delta,q_0,Z_0) (no final states are needed for empty-stack acceptance). Use:

  • Q={q}Q=\{q\} (single control state),
  • Γ=VΣ{Z0}\Gamma = V \cup \Sigma \cup \{Z_0\},
  • initial stack symbol Z0Z_0,
  • start configuration corresponds to pushing the start symbol SS above Z0Z_0.

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 δ\delta 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)

  1. 1
    Step 1

    From the start, push SS onto the stack (above Z0Z_0) so the stack content represents the current sentential form to be expanded.

  2. 2
    Step 2

    If the top of stack is a nonterminal AVA\in V, nondeterministically choose a production AαA\to\alpha and replace AA by α\alpha on the stack (in reverse order so that the leftmost symbol of α\alpha ends up on top).

  3. 3
    Step 3

    If the top of stack is a terminal aΣa\in\Sigma and the current input symbol is aa, then consume that input symbol and pop aa from the stack.

  4. 4
    Step 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 qq. 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.

  1. Initialization (push SS): [ \delta(q,\epsilon,Z_0)\ni (q,SZ_0) ]

  2. Production step (replace a nonterminal AA):
    For every production AtoalphaA\\to\\alpha in RR where AinVA\\in V and alphain(VcupSigma)\\alpha\\in (V\\cup\\Sigma)^*, add: [ \delta(q,\epsilon,A)\ni \left(q,\text{rev}(\alpha)\right) ] where textrev(alpha)\\text{rev}(\\alpha) denotes the reverse string of alpha\\alpha so that the leftmost symbol of alpha\\alpha becomes the next stack-top to be handled.

  3. Terminal step (match and consume):
    For each ainSigmaa\\in\\Sigma: [ \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 alpha\\alpha in reverse so that the leftmost symbol of alpha\\alpha is on top and processed first."

Correctness Proof

We show L(P)=L(G)L(P)=L(G) under empty-stack acceptance by proving both containments:

  1. If winL(G)w\\in L(G) then PP accepts ww by empty stack.
  2. If PP accepts ww by empty stack then winL(G)w\\in L(G).

The heart of the proof is an invariant: at any point, the stack (ignoring Z0Z_0) is a sentential form reachable from SS, and the PDA’s remaining input corresponds to what still must be matched/produced.

Invariant (sentential form alignment)

Define the stack content (above Z0Z_0) as a string gammain(VcupSigma)\\gamma \\in (V\\cup\\Sigma)^*, representing the “yet-to-be-produced” sentential form. The PDA configuration after some steps has the form: [ (q, ; x, ; \gamma Z_0) ] where xx is the remaining unread input string.

Invariant statement: If gamma\\gamma is the current sentential form in the grammar derivation such that gammaRightarrowx\\gamma \\Rightarrow^* x (producing exactly the remaining terminals in xx), 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 AtoalphaA\\to\\alpha and PDA nondeterministic replacement of top AA by alpha\\alpha,
  • terminal emission of the leftmost terminal symbol and PDA terminal matching move.

(1) If winL(G)w\\in L(G), then PP accepts ww by empty stack

Assume winL(G)w\\in L(G). 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, PP pushes SS, so the stack equals the current sentential form.
  • Whenever the grammar applies a production AtoalphaA\\to\\alpha to the leftmost nonterminal AA in the sentential form, the PDA has AA at the top of stack and can nondeterministically apply the production step to replace AA by alpha\\alpha.
  • Whenever the sentential form has a leftmost terminal aa to be produced next, that terminal appears on top of the PDA stack, and the PDA can apply the terminal matching transition δ(q,a,a)\delta(q,a,a) to consume aa from input and pop it.

At the end of the derivation, the sentential form becomes the terminal string ww and then is consumed symbol-by-symbol. After the entire input ww is read, the stack symbols above Z0Z_0 have all been popped, so the PDA can reach an empty stack configuration (by having popped everything that was representing ww). Hence it accepts by empty stack.

(2) If PP accepts ww by empty stack, then winL(G)w\\in L(G)

Assume the PDA PP accepts ww 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 Z0Z_0):

  • Each time the PDA replaces a nonterminal AA on top of the stack via the rule δ(q,ϵ,A)(q,textrev(alpha))\delta(q,\epsilon,A)\ni(q,\\text{rev}(\\alpha)), we interpret it as a grammar production step AtoalphaA\\to\\alpha.
  • Each time the PDA matches a terminal aa via δ(q,a,a)\delta(q,a,a), 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 Z0Z_0 exactly when it consumes terminals from the input, the sequence of production choices corresponds to a valid derivation from SS that yields ww.

Therefore, SRightarrowwS\\Rightarrow^* w, so winL(G)w\\in L(G).

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$

Start

PDA stack represents initial sentential form."

Expand leftmost nonterminal

Derivation step

Top-of-stack nonterminal AA replaced by production RHS α\alpha."

Match leftmost terminal

Terminal production

Consume input symbol aa and pop aa."

Empty stack

End

After 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

Question 1 of 4
Q1Single choice

In the PDA construction, when the top of the stack is a nonterminal AA, which move is used to simulate the grammar?