Constructing an NPDA for a Given CFG

Constructing an NPDA for a Given CFG

Verified Sources
Sep 13, 2026

To construct an NPDA from a context-free grammar (CFG), we use the standard correspondence: each CFG production is implemented as a nondeterministic stack rewrite step. Concretely, when the automaton sees a nonterminal XX on top of the stack, it can replace XX by the right-hand side of any production XγX \to \gamma by pushing γ\gamma onto the stack (in reverse order). Simultaneously, terminals are matched by consuming input symbols when the same terminal appears on top of the stack.

This course section applies that construction to the specific grammar:

SαABBαAAAαBBαBbBBA\begin{aligned} S &\to \alpha ABB \mid \alpha AA \\ A &\to \alpha BB \mid \alpha \\ B &\to bBB \mid A \end{aligned}

where α\alpha denotes a (fixed) terminal string that must be read in the input. (If instead α\alpha is meant to be a single terminal, the construction specializes by using that one symbol as the read sequence.)

Key terms

  • CFG
  • Production
  • Stack symbol
  • Epsilon transition
  • Start symbol

NFA/NPDA basics: converting CFGs to pushdown automata (conceptual)

Grammar-to-NPDA construction pattern

The “recipe” for a CFG G=(V,Σ,P,S)G=(V,\Sigma,P,S) to an NPDA MM is:

  1. Use stack to simulate the derivation.
  2. Start with stack containing the start symbol SS (and typically a bottom marker).
  3. For each production XγX \to \gamma add an ϵ\epsilon-move that replaces XX by γ\gamma on the stack.
  4. For each terminal aΣa \in \Sigma add a move that consumes input aa when aa is on the stack top.
  5. Accept by empty stack or by entering a final state after input is consumed (both are standard variants).

Why this works (intuitively)

A leftmost derivation in the CFG corresponds to a sequence of rewrites of the leftmost nonterminal. In the NPDA simulation, “rewriting” is done whenever a nonterminal is on top of the stack, and “reading” is done when a terminal is on top of the stack.

Note: “push reversed” means if γ=Y1Y2Yk\gamma = Y_1Y_2\cdots Y_k then we push Yk,,Y2,Y1Y_k,\ldots,Y_2,Y_1 so that Y1Y_1 becomes the new top (to be expanded/consumed next).

type="tip" title="Pro Tip: Treat terminals as stack symbols too" content="In this construction, terminals in the RHS are pushed to the stack as well; then separate transitions pop them while consuming matching input characters."

type="warning" title="Important: Clarify what 0˘3b1\u03b1 means" content="The grammar uses 0˘3b1\u03b1 on multiple RHSs. The NPDA must read exactly that same fixed terminal string each time it appears in a chosen derivation; if 0˘3b1\u03b1 is actually a single terminal symbol, you should replace each occurrence of the string by that one symbol."

Step-by-step construction of an NPDA for the given grammar

  1. 1
    Step 1

    Let M=(Q,0˘3a3,0˘393,0˘394,q0,Z0,F)M=(Q,\u03a3,\u0393,\u0394,q_0,Z_0,F) with a start state q0q_0, stack alphabet 0˘393\u0393 containing nonterminals 0˘07bS,A,B0˘07d\u007bS,A,B\u007d, terminals from 0˘3a3\u03a3, and a bottom marker Z0Z_0. Acceptance by empty stack (or a final state) is fine; we will use empty stack.

  2. 2
    Step 2

    Use an 0˘3b5\u03b5-move from q0q_0 that pushes SS onto the stack above Z0Z_0.

  3. 3
    Step 3

    For every production XγX\to\gamma, add a transition of the form: when XX is on top of stack, replace it by the string γ\gamma (push its symbols in reverse order).

  4. 4
    Step 4

    For each terminal tt in 0˘3a3\u03a3 that appears as a symbol on the stack, add a move that reads tt from input and pops tt from the stack.

  5. 5
    Step 5

    If multiple productions apply to the same nonterminal (e.g., SS has two), the NPDA will have multiple alternative 0˘3b5\u03b5-moves for that same stack top.

  6. 6
    Step 6

    Accept when the input is fully consumed and the stack has no non-bottom symbols left (empty stack above Z0Z_0). Concretely: when stack top is Z0Z_0 and no input remains, transition to accept state or halt with acceptance.

NPDA definition (formal transitions)

We must implement these productions:

  • SαABBαAAS \to \alpha ABB \mid \alpha AA
  • AαBBαA \to \alpha BB \mid \alpha
  • BbBBAB \to bBB \mid A

Assumptions about alphabets

Let 0˘3a3\u03a3 contain at least 0˘07bb0˘07d2˘22a\u007b b \u007d \u222a symbols appearing inside α\alpha. Terminals in α\alpha are treated as literal input symbols the NPDA will consume in order.

Define the stack alphabet:

Γ=0˘07bZ0,S,A,B2˘22aΣ2˘22a(terminals inside α)0˘07d\Gamma=\u007b Z_0, S, A, B \u222a \Sigma \u222a \text{(terminals inside }\alpha\text{)} \u007d

(Practically, Σ\Sigma already includes those terminals.)

States

Let Q=0˘07bq0,qacc0˘07dQ=\u007b q_0, q_{acc} \u007d (single working state + accept).

Start move

On initialization, push SS above the bottom marker:

  • 0˘394(q0,0˘3b5,Z0)=0˘07b(q0,SZ0)0˘07d\u0394(q_0,\u03b5,Z_0)=\u007b (q_0,SZ_0)\u007d

(Interpretation: replace top Z0Z_0 by SZ0SZ_0.)

ϵ\epsilon-transitions for the grammar’s nonterminals

We now add ϵ\epsilon transitions that replace a stack-top nonterminal by the RHS string.

For SS

  1. For SαABBS \to \alpha ABB:
  • If top is SS, replace by the sequence αABB\alpha \, A \, B \, B. So the NPDA push is: push RHS symbols in reverse order:
  • push BB, then BB, then AA, then the symbols of α\alpha in reverse, and keep them so that the leftmost symbol of α\alpha is consumed first.
  1. For SαAAS \to \alpha AA similarly: replace SS by αAA\alpha \, A \, A.

For AA

  1. For AαBBA \to \alpha BB replace AA by αBB\alpha \, B \, B.
  2. For AαA \to \alpha replace AA by α\alpha.

For BB

  1. For BbBBB \to bBB replace BB by bBBb \, B \, B.
  2. For BAB \to A replace BB by AA.

To avoid ambiguity about the internal symbols of α\alpha, we represent α\alpha as a concrete terminal string:

α=a1a2ak\alpha = a_1 a_2 \cdots a_k

Then a push for α\alpha means pushing ak,,a2,a1a_k,\ldots,a_2,a_1 so that a1a_1 ends up on top.

Transitions written schematically

For clarity, we show the replacement rule in stack form.

SS-productions

  • If stack top is SS, push (αABB)(\alpha ABB):
Δ(q0,ϵ,S)(q0,  push(reverse(α)  B  B  A))\Delta(q_0,\epsilon,S)\ni (q_0,\; \text{push}(\text{reverse}(\alpha)\; B\; B\; A))
  • If stack top is SS, push (αAA)(\alpha AA):
Δ(q0,ϵ,S)(q0,  push(reverse(α)  A  A))\Delta(q_0,\epsilon,S)\ni (q_0,\; \text{push}(\text{reverse}(\alpha)\; A\; A))

AA-productions

  • AαBBA \to \alpha BB:
Δ(q0,ϵ,A)(q0,  push(reverse(α)  B  B))\Delta(q_0,\epsilon,A)\ni (q_0,\; \text{push}(\text{reverse}(\alpha)\; B\; B))
  • AαA \to \alpha:
Δ(q0,ϵ,A)(q0,  push(reverse(α)))\Delta(q_0,\epsilon,A)\ni (q_0,\; \text{push}(\text{reverse}(\alpha)))

BB-productions

  • BbBBB \to bBB:
Δ(q0,ϵ,B)(q0,  push(B  B  b))\Delta(q_0,\epsilon,B)\ni (q_0,\; \text{push}(B\; B\; b))

because RHS is bBBbBB; reversed push order puts bb first to be matched by input.

  • BAB \to A:
Δ(q0,ϵ,B)(q0,  A)\Delta(q_0,\epsilon,B)\ni (q_0,\; A)

Terminal matching transitions (consume input when terminal is on top)

For each terminal symbol tt (including bb and all aia_i from α\alpha):

  • If the stack top is tt, consume tt and pop it:
Δ(q0,t,t)(q0,ϵ)\Delta(q_0,t,t)\ni (q_0,\epsilon)

Concretely, for the symbol bb:

  • Δ(q0,b,b)={(q0,ϵ)}\Delta(q_0,b,b)=\{(q_0,\epsilon)\}

And similarly for each aia_i in α\alpha:

  • Δ(q0,ai,ai)={(q0,ϵ)}\Delta(q_0,a_i,a_i)=\{(q_0,\epsilon)\}

Acceptance transition

Use acceptance by empty stack above bottom marker:

  • If no input remains and top is Z0Z_0, move to accept:
Δ(q0,ϵ,Z0)(qacc,Z0)\Delta(q_0,\epsilon,Z_0)\ni (q_{acc},Z_0)

Alternatively, you can define F=0˘07bqacc0˘07dF=\u007b q_{acc}\u007d.

Production-to-transition mapping summary

How each CFG production is realized in the NPDA

Common pitfalls and how to avoid them

Knowledge Check

Question 1 of 4
Q1Single choice

In the standard CFG-to-NPDA construction, what does the NPDA do when a nonterminal X is on top of the stack?