Constructing a PDA for a Given Grammar and Testing Acceptance of a String

Constructing a PDA for a Given Grammar and Testing Acceptance of a String

Verified Sources
Sep 12, 2026

We construct a PDA (pushdown automaton) from a context-free grammar (CFG) using the standard “simulate leftmost derivations with a stack” method. For CFG production rules, the PDA replaces a nonterminal on top of the stack with the RHS of a production, while terminal symbols on top must match the current input symbol.

Grammar

  • S0BBS \to 0BB
  • B0S1S0B \to 0S \mid 1S \mid 0

We will build a PDA that accepts exactly the language generated by this CFG (with acceptance by empty stack).

Mermaid view of the derivation simulation:

transition function
nonterminal expansion
leftmost derivation simulation
acceptance by empty stack
stack top

CFG to PDA (standard construction idea)

Standard CFG \to PDA construction (high level)

Let the PDA have:

  • one start state qstartq_{\text{start}},
  • a set of states where the PDA “processes input”,
  • a stack alphabet containing terminals and nonterminals,
  • acceptance by empty stack.

The core mechanics are:

  1. Push start symbol SS onto the stack.
  2. While input remains:
    • If stack top is a nonterminal XX, nondeterministically choose a production XαX \to \alpha and replace XX with α\alpha (push α\alpha onto the stack in reverse order).
    • If stack top is a terminal aa, and next input symbol is aa, pop it and consume the input symbol.

This is the generic method for constructing a PDA equivalent to a CFG.

production rule
nondeterminism
stack replacement
consumption step

Construct the PDA for the given grammar

  1. 1
    Step 1

    Use Q={qstart,q}Q=\{q_{start},q\}, input alphabet Σ={0,1}\Sigma=\{0,1\}, stack alphabet Γ={0,1,S,B}\Gamma=\{0,1,S,B\}, start state qstartq_{start}, and accept by empty stack.

  2. 2
    Step 2

    On an epsilon move, replace the initial stack marker (or empty stack) by pushing SS.

  3. 3
    Step 3

    For each production XαX\to \alpha, add transitions that pop XX and push α\alpha onto the stack (reverse order).

  4. 4
    Step 4

    For each terminal a{0,1}a\in\{0,1\}, when the stack top is aa, only allow a transition that matches the current input symbol aa (consume input and pop).

  5. 5
    Step 5

    Once all input symbols are read, the PDA can only proceed if the stack can be popped to empty via terminal matches; then accept by empty stack.

The PDA definition (explicit transitions)

We describe the PDA M=(Q,Sigma,Gamma,delta,qstart)M=(Q,\\Sigma,\\Gamma,\\delta,q_{start}) with acceptance by empty stack.

  • Q={qstart,q}Q=\{q_{start},q\}
  • \\Sigma=\{0,1\\}
  • \\Gamma=\{0,1,S,B\\} (stack holds terminals and nonterminals)
  • Start state: qstartq_{start}
  • Stack acceptance: empty stack

1) Initialization

We assume the stack is empty initially. Use an epsilon transition to push SS:

  • δ(qstart,varepsilon,varepsilon)ni(q,S)\delta(q_{start},\\varepsilon,\\varepsilon)\\ni(q,S)

(If you prefer a bottom marker, the same idea applies; the construction is equivalent.)

2) Terminal matching

For any time the top of stack is a terminal, it must match the input symbol:

  • δ(q,0,0)ni(q,varepsilon)\delta(q,0,0)\\ni(q,\\varepsilon) (match/read 0, pop 0)
  • δ(q,1,1)ni(q,varepsilon)\delta(q,1,1)\\ni(q,\\varepsilon) (match/read 1, pop 1)

3) Nonterminal expansions

Now encode the grammar productions by stack replacement.

Production Sto0BBS \\to 0BB
Pop SS, push RHS symbols 0,B,B0,B,B (in reverse push order so that the first symbol 00 ends up on top):

  • δ(q,varepsilon,S)ni(q,0BB)\delta(q,\\varepsilon,S)\\ni(q,0BB) (read as “replace SS by stack string 0BB0BB with 00 on top”)

Production Bto0SB \\to 0S

  • δ(q,varepsilon,B)ni(q,0S)\delta(q,\\varepsilon,B)\\ni(q,0S)

Production Bto1SB \\to 1S

  • δ(q,varepsilon,B)ni(q,1S)\delta(q,\\varepsilon,B)\\ni(q,1S)

Production Bto0B \\to 0

  • δ(q,varepsilon,B)ni(q,0)\delta(q,\\varepsilon,B)\\ni(q,0)

That fully specifies the PDA behavior.

stack string
epsilon transition
match transition
replacement

type="tip" title="Pro tip: Reverse-order pushing" content="When you replace a stack-top nonterminal X by a RHS α, you must push the symbols so the leftmost terminal/nonterminal of α becomes the new stack-top. This avoids mistakes when simulating derivations."

PDA sanity check on structure

From Sto0BBS\\to 0BB, any accepted string must begin with 00, because the PDA must eventually expand SS and the resulting stack prefix forces the first terminal to be 00.

Then each BB expands using:

  • Bto0B\\to 0 (contributes a single 00), or
  • Bto0SB\\to 0S (contributes a 00 then whatever SS generates), or
  • Bto1SB\\to 1S (contributes a 11 then whatever SS generates).

This recursive structure guarantees that strings have form:

  • 00 followed by two “BB-generated” segments.

What the PDA does while reading an input

Initialization

Step 0

Push SS onto the stack."

Expand $S$

Step 1

Use S0BBS\to 0BB so stack begins with 00."

Match leading terminal

Step 2

If input starts with 00, pop the top 00."

Expand $B$ as needed

Step 3

For each BB encountered, choose among 0S,1S,00S,1S,0 to align with remaining input."

Finish

Step 4

After all input symbols are consumed, keep popping only if stack can become empty (accept by empty stack)."

Test whether 01040104 is accepted

First, note the mismatch: the grammar terminals are only 00 and 11, so 44 is not in the input alphabet Σ={0,1}\Sigma=\{0,1\}.

So the string “01040104” cannot be accepted because the PDA has no transition that can read a symbol 44.

Formally, during matching transitions, the PDA can only consume an input symbol when it matches a terminal on the top of stack, and terminals available are only 00 and 11. Therefore, when the PDA reaches the input symbol 44, there is no valid move to consume it, and the run blocks.

terminal alphabet
undefined input symbol

type="warning" title="Important: the symbol 44 is not a terminal" content="Your PDA (and grammar) only uses terminals 00 and 11. If the input contains 44, there is no transition that can read it, so the string cannot be accepted."

FAQ

Knowledge Check

Question 1 of 3
Q1Single choice

In the constructed PDA for this grammar, which rule set handles expansions of nonterminals like BB?