Introduction
A pushdown automaton (PDA) is an NFA equipped with a stack. The stack provides unbounded memory that is accessible only at the top, which is exactly the power needed for context-free languages: every CFG has an equivalent PDA and every PDA has an equivalent CFG. The working skills are designing PDAs for matching languages such as , acceptance by empty stack versus final state, and the CFG PDA equivalence at a constructive level.
Prerequisites
- Context-Free Grammars, Derivations and Ambiguity: languages generated by CFGs.
- NFA, Epsilon-NFA and Subset Construction: nondeterministic control.
- Stacks: LIFO behaviour.
Core concepts
Formal definition
A PDA is where is the stack alphabet, is the start stack symbol, and
A transition means: in state , reading (or ), with on top of the stack, pop , push the string (rightmost symbol ends on top), and go to .
Instantaneous description
A configuration (ID) is with state , unread input , and stack string (left = bottom, right = top, or the opposite convention; state the convention). These notes write the top at the right end of .
Acceptance modes
- By final state. iff for some and any stack . The stack contents, including the bottom marker, do not matter.
- By empty stack. iff for any state . The stack must hold no symbols at all, so the bottom marker must be popped too; plays no role.
A stack holding only is not empty. A machine that ends with left accepts only if it is in a final state and the question uses final-state acceptance.
For nondeterministic PDAs the two modes are equivalent: every final-state PDA can be converted into an empty-stack PDA for the same language and back (Example 3). CFLs are exactly the languages of nondeterministic PDAs in either mode. For deterministic PDAs they are not equivalent: empty-stack DPDAs accept only languages with the prefix property, a proper subset of the final-state DPDA languages.
Deterministic PDA
If never has a choice (including no competing -move when a symbol move exists), the PDA is deterministic. DPDAs define a proper subclass of CFLs (next note).
How it works
PDA for
Informal design: on each , push ; on each , pop ; when only the bottom marker is left, pop it with an -move, leaving the stack empty, and accept by empty stack. A run dies if a finds instead of or if an follows a ; if 's remain, is never exposed and the stack never empties.
Two precise versions. Stack top at the right, and is the bottom marker ; a transition is written (read or , replace top by ).
| Move | Empty-stack version (main design) | Final-state version () |
|---|---|---|
| first | ||
| more | ||
| first | ||
| more | ||
| finish | , and for |
In short, the empty-stack machine uses a,Z/ZA; a,A/AA; b,A/; ,Z/, with state recording "a has been read" so that no is accepted after it. The run in the figure, written as IDs : . The last move pops ; the input is used up and the stack is empty, so aabb is accepted. The final-state version instead ends with and accepts because , with still on the stack; its start state is final, so is accepted without a move.
Determinism. The empty-stack version is not deterministic: in with on top it may pop (to accept ) or read . The final-state version is deterministic: no state has an -move and a symbol move on the same top symbol. This is unavoidable. A DPDA accepting by empty stack halts with an empty stack after any accepted string, so it cannot then accept a longer one; since and ab are both in , no DPDA accepts this language by empty stack, although one accepts it by final state.
Traces of rejected strings (final-state version): aabbab reaches and then reads a, for which has no move; aaabb ends in with on the stack, so the finishing move does not apply; abab fails like aabbab.

From CFG to PDA
Standard construction: start by pushing ; while top is a variable , replace it by the reversed RHS , so contains (nondeterministic choice of production); while top is a terminal , read from input and pop. Accept by empty stack. This PDA may use -moves heavily, and it needs only one state: every CFL is accepted by empty stack by a single-state PDA. Read from top to bottom, the stack holds the unexpanded part of a leftmost sentential form, so the run simulates a leftmost derivation.
For (top written first in this paragraph, to read like the sentential form): on ab the stack goes (expand), read a leaving , expand leaving , read b, stack empty: accept. A wrong guess (expanding a second time) simply dies.
From PDA to CFG
More technical: variables remember triples meaning "pop going from to ". The full construction is long; know that it exists and that the grammar has variables , plus the start symbol.
Algorithms and pseudocode
PDA-EMPTY-STACK-ACCEPT(P, w) // nondeterministic search
1 configs <- FIFO queue containing (q0, w, Z0); seen <- {(q0, w, Z0)}
2 while configs not empty
3 (q, x, alpha) <- DEQUEUE(configs)
4 if x = eps and alpha = eps: return YES
5 for each applicable transition on (q, first(x) or eps, top(alpha))
6 if the resulting configuration is not in seen: add it to seen and ENQUEUE it
7 return NO // only if the entire queue is exhaustedFIFO extraction is fair for this finitely branching transition system: each enqueued configuration is eventually processed. Exact duplicate configurations are suppressed by seen. Arbitrary epsilon-looping PDAs can generate infinitely many distinct stacks, so this search need not terminate on rejection.
One successful run proves acceptance; one failed run does not prove rejection. Rejection requires ruling out every branch or using a complete membership algorithm (for example, via a CFG).
Mathematical explanation
Why a stack matches CFGs
Parse trees expand the leftmost variable; the PDA stack stores the fringe of unfinished siblings. That is why one stack equals one "context-free" matching discipline, not .
Equivalence theorem
is context-free iff some PDA accepts by empty stack iff some PDA accepts by final state.
Worked examples
Example 1:
Run of the empty-stack PDA on aaabbb: push three 's, pop three, leaving ; the -move pops and the empty stack accepts. On aabbb: after two pushes and two pops, is on top with one unread; no move reads on , and popping leaves input unread, so every run fails. On aab: one remains above when the input ends, the stack cannot empty, reject.
Example 2:
Nondeterministically guess the middle: push while reading the first half, then pop matching symbols while reading the reverse half. Deterministic PDAs cannot find the middle of even-length palindromes over a 2-letter alphabet in general.
Example 3: final state versus empty stack
Convert empty-stack PDA to final-state: add a bottom marker, and when original stack would empty, move to a new accept state. Conversely, push a new bottom, simulate, and on original accept drain the stack with -pops.
Example 4: language
Nondeterministically choose which equality to check; push on the first letter run, pop on the second. Cannot check both equalities at once with one stack.
Example 5: PDAs for counting languages
| Language | Idea |
|---|---|
| push two s per , pop one per | |
| push one per , pop two per (two moves, the second an -move that pops again) | |
| push on , pop on , accept by final state with s possibly left over | |
| stack holds the surplus: push the symbol if the top is the same symbol or , pop if the top is the other symbol; accept when the top is | |
push until c, then pop matching symbols: deterministic because c marks the middle | |
| same, but the middle must be guessed: nondeterministic, not a DCFL |
The surplus PDA on abba: push (stack ), pop on (), push on (), pop on (): accept. On aab the stack ends as : reject.
Beyond one stack
| Model | Power |
|---|---|
| PDA with a bounded stack | finite automaton (regular) |
| One stack (PDA) | context-free languages |
| One counter (stack alphabet only) | a proper subclass of CFLs, still contains |
| Two stacks, or a queue | Turing machines (recursively enumerable) |
| Two counters | Turing machines |
So needs either more structure than one stack (a linear-bounded automaton suffices) or a second stack.
Important properties and observations
- Nondeterminism is essential for the full CFL class.
- -input moves and -stack-like choices are allowed.
- PDAs are not closed under complement (because CFLs are not).
- Finite automata are PDAs that ignore the stack.
Common misconceptions.
- "PDA can recognize ." Not with one stack.
- "Empty stack and final state define different classes." They do not for nondeterministic PDAs.
- "DPDA equals PDA." False; next note.
- "Empty stack and final state are interchangeable for DPDAs too." Not quite: a DPDA accepting by empty stack can accept only languages with the prefix property (no string in is a proper prefix of another), so final state is the standard mode for DPDAs.
- "The stack must be empty to accept by final state." No; the stack contents are irrelevant in that mode.
GATE examination perspective
- STACK TRACE. Write top-of-stack after each step.
- GUESS THE MIDDLE. Palindromes and need nondeterministic midpoint.
- MODE OF ACCEPTANCE. Read whether the question says empty stack or final state.
- ONE MATCHING. One stack enforces one dependency (nested, last-in first-out). Crossing dependencies such as or copies are out of reach.
- BOTTOM MARKER. "Accept by empty stack" means the marker is popped too; a machine that stops with on the stack accepts only in final-state mode.
- MARKED VERSUS UNMARKED MIDDLE. is deterministic; is not.
- TWO STACKS. A PDA with two stacks is as powerful as a Turing machine; a PDA with a finite stack is a finite automaton.
- STATE COUNT. Every CFL has a one-state PDA accepting by empty stack.
Summary
Sources
- J. E. Hopcroft, R. Motwani, J. D. Ullman, Introduction to Automata Theory, Languages, and Computation, 3rd ed., Pearson Addison-Wesley, Sections 6.1 (definition of the PDA), 6.2 (acceptance by final state and by empty stack, and the conversions between them), 6.3 (equivalence of PDAs and CFGs), 8.5.2 and 8.5.3 (multistack machines, counter machines).
- M. Sipser, Introduction to the Theory of Computation, 3rd ed., Cengage Learning: Section 2.2 (pushdown automata, equivalence with context-free grammars).
- P. Linz, An Introduction to Formal Languages and Automata, 6th ed., Jones & Bartlett Learning: Sections 7.1 and 7.2 (nondeterministic pushdown automata and context-free languages).