Sub topic 8
Pushdown automata: states, stack, and transitions
This chapter assumes you can read a DFA or NFA transition diagram and that you know what a context-free grammar is. You should also be comfortable with strings, languages, and the notation Σ* and ε. By the end you should be able to read a PDA's transition function, trace its moves on an input, and write a simple PDA yourself.
Why finite automata need a stack
A finite automaton remembers only its current state, which is a fixed amount of memory. To accept { aⁿbⁿ : n ≥ 1 } it would have to count the a's, and n has no upper bound. No fixed number of states is enough.
A pushdown automaton (PDA) is a finite automaton with an added stack. The stack is unbounded and last-in, first-out. The machine can only look at the top symbol, and it changes the stack by popping that symbol and pushing new ones. That is enough to match nested structure such as aⁿbⁿ, balanced parentheses, and wwᴿ. It is not enough to compare three independent counts, which is why aⁿbⁿcⁿ is out of reach.
The formal definition
A PDA is a 7-tuple M = (Q, Σ, Γ, δ, q₀, Z₀, F):
- Q is a finite set of states.
- Σ is the input alphabet.
- Γ is the stack alphabet. It may contain symbols that are not in Σ.
- δ is the transition function.
- q₀ ∈ Q is the start state.
- Z₀ ∈ Γ is the initial stack symbol. The stack holds only Z₀ at the start.
- F ⊆ Q is the set of final states.
The transition function has this type:
δ : Q × (Σ ∪ {ε}) × Γ → finite subsets of Q × Γ*
A single rule δ(p, a, X) ∋ (q, γ) reads: in state p, with input symbol a next, and X on top of the stack, go to state q. Consume a, pop X, and push the string γ in its place. The leftmost symbol of γ becomes the new top.
- If a = ε, the move consumes no input. These are called ε-moves.
- If γ = ε, the move is a pure pop.
- If γ = X, the stack is unchanged.
- If γ = YX, the move pushes Y.
Each move pops exactly one symbol and pushes zero or more. A move that "just peeks" must push the symbol back.
Configurations and the move relation
A configuration, also called an instantaneous description (ID), is a triple (state, remaining input, stack contents). The stack is written with the top on the left. The symbol ⊢ means "moves to in one step".
(p, a w, X β) ⊢ (q, w, γ β) if (q, γ) ∈ δ(p, a, X)
(p, w, X β) ⊢ (q, w, γ β) if (q, γ) ∈ δ(p, ε, X)The symbol ⊢* means zero or more steps. If the stack is empty, no move is possible, because every rule needs a top symbol.
Worked example: aⁿbⁿ
We use Z₀ as a bottom marker so that the machine can notice when all the a's have been matched. The states are q₀, q₁, and q₂, and the only final state is q₂.
| Rule | Meaning |
|---|---|
| δ(q₀, a, Z₀) = {(q₀, AZ₀)} | first a: push A above the marker |
| δ(q₀, a, A) = {(q₀, AA)} | more a's: push another A |
| δ(q₀, b, A) = {(q₁, ε)} | first b: pop an A, switch to matching mode |
| δ(q₁, b, A) = {(q₁, ε)} | each later b: pop an A |
| δ(q₁, ε, Z₀) = {(q₂, Z₀)} | only the marker is left: accept |
Trace on aabb:
(q0, aabb, Z0) ⊢ (q0, abb, AZ0) ⊢ (q0, bb, AAZ0)
⊢ (q1, b, AZ0) ⊢ (q1, ε, Z0)
⊢ (q2, ε, Z0)The input is fully consumed and the state is final, so aabb is accepted. On aab the machine ends in (q₁, ε, AZ₀). There is no applicable move and q₁ is not final, so it rejects. On abab the machine reaches (q₁, ab, Z₀) and then has no rule for reading a with Z₀ on top, so it dies.
Nondeterminism
The codomain of δ is a set, so a PDA may have several choices in one configuration, or none. An input is accepted if at least one sequence of choices ends in acceptance. The usual example is the language of even-length palindromes wwᴿ over {a, b}. The machine pushes each symbol it reads, and at some point it must guess that the middle has been reached. The guess is an ε-move into a popping state, and the popping state then matches each remaining input symbol against the top of the stack. No deterministic strategy can find the middle, which is the key fact in the later chapter on DPDA versus NPDA.
Simulating a PDA in code
The following program explores all configurations breadth-first. It stores the stack as a string with the top at index 0. The seen set stops it from revisiting configurations, and max_configs is a safety cap against ε-loops that grow the stack forever.
from collections import deque
def run_pda(delta, start, bottom, finals, w, max_configs=10000):
"""Accept by final state. delta maps (state, symbol, top) to a set of
(new_state, push_string). Use "" for epsilon. Top of stack is stack[0]."""
seen = set()
queue = deque([(start, 0, bottom)])
while queue:
config = queue.popleft()
if config in seen:
continue
seen.add(config)
if len(seen) > max_configs:
return False
state, i, stack = config
if i == len(w) and state in finals:
return True
if not stack:
continue
top, rest = stack[0], stack[1:]
moves = [("", i)]
if i < len(w):
moves.append((w[i], i + 1))
for sym, j in moves:
for new_state, push in delta.get((state, sym, top), ()):
queue.append((new_state, j, push + rest))
return False
delta = {
("q0", "a", "Z"): {("q0", "AZ")},
("q0", "a", "A"): {("q0", "AA")},
("q0", "b", "A"): {("q1", "")},
("q1", "b", "A"): {("q1", "")},
("q1", "", "Z"): {("q2", "Z")},
}
for s in ["aabb", "aaabbb", "aab", "abab", ""]:
print(repr(s), run_pda(delta, "q0", "Z", {"q2"}, s))
# 'aabb' True, 'aaabbb' True, 'aab' False, 'abab' False, '' FalseThe letter Z stands for Z₀ in the code.
Common errors
- Forgetting that a move pops. A rule with γ = ε removes the top symbol. To leave the stack alone, push the same symbol back.
- Writing the push string in the wrong order. In δ(q, a, A) ∋ (q, BC), B ends up on top. Reversing the string gives a different machine.
- Treating ε-moves as free. An ε-move still needs a matching top-of-stack symbol. With an empty stack the machine halts.
- Mixing up acceptance conditions. This chapter accepts by final state, and the stack may hold anything at that point. Acceptance by empty stack is a different mode, covered in the next chapter.
- Forgetting dead paths. A computation path that gets stuck simply stops. It does not reject the whole input, because only the absence of any accepting path rejects.
Exercise
Using the PDA above, give the final configuration reached on input aab, and say whether the input is accepted.
Show answer
The run is (q0, aab, Z0) ⊢ (q0, ab, AZ0) ⊢ (q0, b, AAZ0) ⊢ (q1, ε, AZ0). No rule applies in q1 with top A and no input, and q1 is not final. The input is rejected.
Exercise
Modify the PDA so that it accepts { aⁿb²ⁿ : n ≥ 1 } by final state.
Show answer
Push two A's for every a: δ(q0, a, Z0) = {(q0, AAZ0)} and δ(q0, a, A) = {(q0, AAA)}. Keep the remaining three rules unchanged. Each b pops one A, so 2n b's empty the A's exactly, and the ε-move on Z0 then leads to the final state q2.