Constructing a PDA for a Given Grammar and Testing Acceptance of a String
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
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 PDA construction (high level)
Let the PDA have:
- one start state ,
- a set of states where the PDA “processes input”,
- a stack alphabet containing terminals and nonterminals,
- acceptance by empty stack.
The core mechanics are:
- Push start symbol onto the stack.
- While input remains:
- If stack top is a nonterminal , nondeterministically choose a production and replace with (push onto the stack in reverse order).
- If stack top is a terminal , and next input symbol is , 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
- 1Step 1
Use , input alphabet , stack alphabet , start state , and accept by empty stack.
- 2Step 2
On an epsilon move, replace the initial stack marker (or empty stack) by pushing .
- 3Step 3
For each production , add transitions that pop and push onto the stack (reverse order).
- 4Step 4
For each terminal , when the stack top is , only allow a transition that matches the current input symbol (consume input and pop).
- 5Step 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 with acceptance by empty stack.
- \\Sigma=\{0,1\\}
- \\Gamma=\{0,1,S,B\\} (stack holds terminals and nonterminals)
- Start state:
- Stack acceptance: empty stack
1) Initialization
We assume the stack is empty initially. Use an epsilon transition to push :
(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:
- (match/read 0, pop 0)
- (match/read 1, pop 1)
3) Nonterminal expansions
Now encode the grammar productions by stack replacement.
Production
Pop , push RHS symbols (in reverse push order so that the first symbol ends up on top):
- (read as “replace by stack string with on top”)
Production
Production
Production
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 , any accepted string must begin with , because the PDA must eventually expand and the resulting stack prefix forces the first terminal to be .
Then each expands using:
- (contributes a single ), or
- (contributes a then whatever generates), or
- (contributes a then whatever generates).
This recursive structure guarantees that strings have form:
- followed by two “-generated” segments.
What the PDA does while reading an input
Initialization
Step 0Push onto the stack."
Expand $S$
Step 1Use so stack begins with ."
Match leading terminal
Step 2If input starts with , pop the top ."
Expand $B$ as needed
Step 3For each encountered, choose among to align with remaining input."
Finish
Step 4After all input symbols are consumed, keep popping only if stack can become empty (accept by empty stack)."
Test whether is accepted
First, note the mismatch: the grammar terminals are only and , so is not in the input alphabet .
So the string “” cannot be accepted because the PDA has no transition that can read a symbol .
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 and . Therefore, when the PDA reaches the input symbol , there is no valid move to consume it, and the run blocks.
terminal alphabet
undefined input symbol
type="warning" title="Important: the symbol is not a terminal" content="Your PDA (and grammar) only uses terminals and . If the input contains , there is no transition that can read it, so the string cannot be accepted."
FAQ
Knowledge Check
In the constructed PDA for this grammar, which rule set handles expansions of nonterminals like ?