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 {anbn}\{a^n b^n\}, acceptance by empty stack versus final state, and the CFG ⇔\Leftrightarrow PDA equivalence at a constructive level.

Prerequisites

Core concepts

Formal definition

A PDA is P=(Q,Σ,Γ,δ,q0,Z0,F)P = (Q, \Sigma, \Gamma, \delta, q_0, Z_0, F) where Γ\Gamma is the stack alphabet, Z0∈ΓZ_0 \in \Gamma is the start stack symbol, and

δ:Q×(Σ∪{ε})×Γ→finite subsets of Q×Γ∗.\delta: Q \times (\Sigma \cup \{\varepsilon\}) \times \Gamma \to \text{finite subsets of } Q \times \Gamma^*.

A transition δ(q,a,X)∋(p,γ)\delta(q, a, X) \ni (p, \gamma) means: in state qq, reading aa (or ε\varepsilon), with XX on top of the stack, pop XX, push the string γ\gamma (rightmost symbol ends on top), and go to pp.

Instantaneous description

A configuration (ID) is (q,w,α)(q, w, \alpha) with state qq, unread input ww, and stack string α\alpha (left = bottom, right = top, or the opposite convention; state the convention). These notes write the top at the right end of α\alpha.

Acceptance modes

  1. By final state. w∈L(P)w \in L(P) iff (q0,w,Z0)⊢∗(q,ε,γ)(q_0, w, Z_0) \vdash^* (q, \varepsilon, \gamma) for some q∈Fq \in F and any stack γ\gamma. The stack contents, including the bottom marker, do not matter.
  2. By empty stack. w∈N(P)w \in N(P) iff (q0,w,Z0)⊢∗(q,ε,ε)(q_0, w, Z_0) \vdash^* (q, \varepsilon, \varepsilon) for any state qq. The stack must hold no symbols at all, so the bottom marker Z0Z_0 must be popped too; FF plays no role.

A stack holding only Z0Z_0 is not empty. A machine that ends with Z0Z_0 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 δ\delta never has a choice (including no competing ε\varepsilon-move when a symbol move exists), the PDA is deterministic. DPDAs define a proper subclass of CFLs (next note).

How it works

PDA for {anbn∣n≥0}\{a^n b^n \mid n \ge 0\}

Informal design: on each aa, push AA; on each bb, pop AA; when only the bottom marker ZZ is left, pop it with an ε\varepsilon-move, leaving the stack empty, and accept by empty stack. A run dies if a bb finds ZZ instead of AA or if an aa follows a bb; if AA's remain, ZZ is never exposed and the stack never empties.

Two precise versions. Stack top at the right, and ZZ is the bottom marker Z0Z_0; a transition is written q→a,  X→γpq \xrightarrow{a,\;X \to \gamma} p (read aa or ε\varepsilon, replace top XX by γ\gamma).

MoveEmpty-stack version (main design)Final-state version (F={q0,qf}F = \{q_0, q_f\})
first aaq0→a,  Z→ZAq0q_0 \xrightarrow{a,\;Z \to ZA} q_0q0→a,  Z→ZAq1q_0 \xrightarrow{a,\;Z \to ZA} q_1
more aaq0→a,  A→AAq0q_0 \xrightarrow{a,\;A \to AA} q_0q1→a,  A→AAq1q_1 \xrightarrow{a,\;A \to AA} q_1
first bbq0→b,  A→εq1q_0 \xrightarrow{b,\;A \to \varepsilon} q_1q1→b,  A→εq2q_1 \xrightarrow{b,\;A \to \varepsilon} q_2
more bbq1→b,  A→εq1q_1 \xrightarrow{b,\;A \to \varepsilon} q_1q2→b,  A→εq2q_2 \xrightarrow{b,\;A \to \varepsilon} q_2
finishq1→ε,  Z→εq1q_1 \xrightarrow{\varepsilon,\;Z \to \varepsilon} q_1, and q0→ε,  Z→εq0q_0 \xrightarrow{\varepsilon,\;Z \to \varepsilon} q_0 for n=0n = 0q2→ε,  Z→Zqfq_2 \xrightarrow{\varepsilon,\;Z \to Z} q_f

In short, the empty-stack machine uses a,Z/ZA; a,A/AA; b,A/ε\varepsilon; ε\varepsilon,Z/ε\varepsilon, with state q1q_1 recording "a bb has been read" so that no aa is accepted after it. The run in the figure, written as IDs (q,unread input,stack)(q, \text{unread input}, \text{stack}): (q0,aabb,Z)⊢(q0,abb,ZA)⊢(q0,bb,ZAA)⊢(q1,b,ZA)⊢(q1,ε,Z)⊢(q1,ε,ε)(q_0, aabb, Z) \vdash (q_0, abb, ZA) \vdash (q_0, bb, ZAA) \vdash (q_1, b, ZA) \vdash (q_1, \varepsilon, Z) \vdash (q_1, \varepsilon, \varepsilon). The last move pops ZZ; the input is used up and the stack is empty, so aabb is accepted. The final-state version instead ends with (qf,ε,Z)(q_f, \varepsilon, Z) and accepts because qf∈Fq_f \in F, with ZZ still on the stack; its start state is final, so ε\varepsilon is accepted without a move.

Determinism. The empty-stack version is not deterministic: in q0q_0 with ZZ on top it may pop ZZ (to accept ε\varepsilon) or read aa. The final-state version is deterministic: no state has an ε\varepsilon-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 ε\varepsilon and ab are both in {anbn}\{a^n b^n\}, no DPDA accepts this language by empty stack, although one accepts it by final state.

Traces of rejected strings (final-state version): aabbab reaches q2q_2 and then reads a, for which q2q_2 has no move; aaabb ends in q2q_2 with ZAZA on the stack, so the finishing move does not apply; abab fails like aabbab.

PDA for a^n b^n with stack contents along a run
Figure 1. PDA sketch for a^n b^n: push A on a, pop A on b, accept by empty stack by finally popping the bottom marker Z; sample run on aabb shows stack Z, ZA, ZAA, ZA, Z, then empty.

From CFG to PDA

Standard construction: start by pushing SS; while top is a variable AA, replace it by the reversed RHS αR\alpha^R, so δ(q,ε,A)\delta(q,\varepsilon,A) contains (q,αR)(q,\alpha^R) (nondeterministic choice of production); while top is a terminal aa, read aa from input and pop. Accept by empty stack. This PDA may use ε\varepsilon-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 S→aSb∣εS \to aSb \mid \varepsilon (top written first in this paragraph, to read like the sentential form): on ab the stack goes S→aSbS \to aSb (expand), read a leaving SbSb, expand S→εS \to \varepsilon leaving bb, read b, stack empty: accept. A wrong guess (expanding S→aSbS \to aSb a second time) simply dies.

From PDA to CFG

More technical: variables remember triples [p,X,q][p,X,q] meaning "pop XX going from pp to qq". The full construction is long; know that it exists and that the grammar has ∣Q∣2∣Γ∣|Q|^2 |\Gamma| variables [p,X,q][p,X,q], plus the start symbol.

Algorithms and pseudocode

Text
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 exhausted

FIFO 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 {anbncn}\{a^n b^n c^n\}.

Equivalence theorem

LL is context-free iff some PDA accepts LL by empty stack iff some PDA accepts LL by final state.

Worked examples

Example 1: anbna^n b^n

Run of the empty-stack PDA on aaabbb: push three AA's, pop three, leaving ZZ; the ε\varepsilon-move pops ZZ and the empty stack accepts. On aabbb: after two pushes and two pops, ZZ is on top with one bb unread; no move reads bb on ZZ, and popping ZZ leaves input unread, so every run fails. On aab: one AA remains above ZZ when the input ends, the stack cannot empty, reject.

Example 2: {wwR∣w∈{0,1}∗}\{ ww^R \mid w \in \{0,1\}^* \}

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 ε\varepsilon-pops.

Example 4: language {aibjck∣i=j or j=k}\{a^i b^j c^k \mid i=j \text{ or } j=k\}

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

LanguageIdea
{anb2n}\{a^n b^{2n}\}push two AAs per aa, pop one per bb
{a2nbn}\{a^{2n} b^n\}push one AA per aa, pop two per bb (two moves, the second an ε\varepsilon-move that pops again)
{aibj∣i≥j}\{a^i b^j \mid i \ge j\}push on aa, pop on bb, accept by final state with AAs possibly left over
{w∣#a(w)=#b(w)}\{w \mid \#_a(w) = \#_b(w)\}stack holds the surplus: push the symbol if the top is the same symbol or ZZ, pop if the top is the other symbol; accept when the top is ZZ
{wcwR}\{w c w^R\}push until c, then pop matching symbols: deterministic because c marks the middle
{wwR}\{w w^R\}same, but the middle must be guessed: nondeterministic, not a DCFL

The surplus PDA on abba: push AA (stack ZAZA), pop on bb (ZZ), push BB on bb (ZBZB), pop on aa (ZZ): accept. On aab the stack ends as ZAZA: reject.

Beyond one stack

ModelPower
PDA with a bounded stackfinite automaton (regular)
One stack (PDA)context-free languages
One counter (stack alphabet {Z,A}\{Z, A\} only)a proper subclass of CFLs, still contains {anbn}\{a^n b^n\}
Two stacks, or a queueTuring machines (recursively enumerable)
Two countersTuring machines

So {anbncn}\{a^n b^n c^n\} 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.
  • ε\varepsilon-input moves and ε\varepsilon-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 anbncna^n b^n c^n." 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 LL 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 wwRww^R 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 {aibjcidj}\{a^i b^j c^i d^j\} or copies {ww}\{ww\} are out of reach.
  • BOTTOM MARKER. "Accept by empty stack" means the marker is popped too; a machine that stops with ZZ on the stack accepts only in final-state mode.
  • MARKED VERSUS UNMARKED MIDDLE. {wcwR}\{wcw^R\} is deterministic; {wwR}\{ww^R\} 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

  1. 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).
  2. M. Sipser, Introduction to the Theory of Computation, 3rd ed., Cengage Learning: Section 2.2 (pushdown automata, equivalence with context-free grammars).
  3. 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).

Next steps