G27
Self-checkedTier 1 11 min readtoc.pda

Pushdown automata

Pushdown automata as NFAs with a stack: the formal definition, instantaneous descriptions and moves, acceptance by final state and by empty stack (and the conversions between them), nondeterminism and epsilon-moves, designing PDAs for a^n b^n, ww^R, equal counts, balanced parentheses and i = j or j = k, tracing a computation, and what a PDA cannot do (compare more than one count).

Why it exists

A finite automaton has only a finite memory, so it cannot match anbna^n b^n. Add an unbounded stack and the machine can count: push one symbol for every aa, pop one for every bb. The resulting machine, the pushdown automaton (PDA), is exactly as powerful as a context-free grammar (next page). GATE asks: design a PDA, say which language a given PDA accepts, trace a computation, count stack operations, decide which languages a PDA can accept.

Prerequisites

A PDA is an NFA plus a stack (NFA; the stack operations are as in the stack ADT).

Precise definitions

A pushdown automaton is a 7-tuple P=(Q,Σ,Γ,δ,q0,Z0,F)P = (Q, \Sigma, \Gamma, \delta, q_0, Z_0, F):

  • QQ finite set of states, Σ\Sigma input alphabet, Γ\Gamma stack alphabet;
  • δ:Q×(Σ∪{ε})×Γ→\delta : Q \times (\Sigma \cup \{\varepsilon\}) \times \Gamma \to finite subsets of Q×Γ∗Q \times \Gamma^* (the transition function);
  • q0q_0 start state, Z0∈ΓZ_0 \in \Gamma initial stack symbol (the bottom marker), F⊆QF \subseteq Q accepting states.

A move: if (p,γ)∈δ(q,a,X)(p, \gamma) \in \delta(q, a, X) then in state qq with input symbol aa next (or none read if a=εa = \varepsilon) and XX on top of the stack, the machine pops XX, pushes the string γ\gamma (written so that its first symbol ends up on top) and enters state pp. Every move pops exactly one symbol and pushes zero or more.

An instantaneous description (configuration) is (q,w,α)(q, w, \alpha): current state, unread input, stack contents (top first). (q,aw,Xβ)⊢(p,w,γβ)(q, aw, X\beta) \vdash (p, w, \gamma\beta) if (p,γ)∈δ(q,a,X)(p, \gamma) \in \delta(q, a, X); with ε\varepsilon in place of aa the input is unchanged.

Two standard acceptance conditions:

  • by final state: L(P)={w:(q0,w,Z0)⊢∗(q,ε,α) for some q∈F,α∈Γ∗}L(P) = \{w : (q_0, w, Z_0) \vdash^* (q, \varepsilon, \alpha) \text{ for some } q \in F, \alpha \in \Gamma^*\} (the stack is ignored);
  • by empty stack: N(P)={w:(q0,w,Z0)⊢∗(q,ε,ε)}N(P) = \{w : (q_0, w, Z_0) \vdash^* (q, \varepsilon, \varepsilon)\} (the final states are ignored).

Theorem. The two conditions define the same class of languages: from a PDA accepting by final state one builds one accepting by empty stack with the same language, and conversely (constructions below; checked on named and random PDAs).

A PDA is deterministic (DPDA) if for every state, input symbol and stack top at most one move applies, counting ε\varepsilon-moves: if δ(q,ε,X)\delta(q, \varepsilon, X) is non-empty then δ(q,a,X)\delta(q, a, X) is empty for every aa. Otherwise it is nondeterministic; a string is accepted if some computation accepts.

How it works

Reading a computation

The first machine accepts anbna^n b^n (n≥0n \ge 0) and is deterministic. States q0q_0 (start, accepting: the empty string), q1q_1 (reading aa's), q2q_2 (reading bb's), q3q_3 (accepting). Moves:

  • δ(q0,a,Z)={(q1,AZ)}\delta(q_0, a, Z) = \{(q_1, AZ)\}: first aa: push AA above the bottom marker;
  • δ(q1,a,A)={(q1,AA)}\delta(q_1, a, A) = \{(q_1, AA)\}: every further aa pushes another AA;
  • δ(q1,b,A)={(q2,ε)}\delta(q_1, b, A) = \{(q_2, \varepsilon)\} and δ(q2,b,A)={(q2,ε)}\delta(q_2, b, A) = \{(q_2, \varepsilon)\}: every bb pops an AA;
  • δ(q2,ε,Z)={(q3,Z)}\delta(q_2, \varepsilon, Z) = \{(q_3, Z)\}: when only the marker is left, accept.

Computation on aaabbbaaabbb (the stack is shown with its top at the left; the table is the transition function, the outlined cell is the move used):

A PDA for a^n b^n reading aaabbb: start

Parser run by a script, not typed

Pushdown automaton, one accepting computation (a shortest one found by breadth-first search over configurations). The stack is on the left with its top first; the unread input is on the right.

    stack (top first) (top at the left)
    Z
    unread input
    aaabbb
    action start in state q0
    transition table: (state, stack top) and input symbol give new state and the symbols pushed (top first)
    state, topabε
    q0, Zq1,AZ
    q1, Aq1,AAq2,ε
    q2, Aq2,ε
    q2, Zq3,Z

    Step 0

    The machine starts in state q0 with the stack Z (only the bottom marker) and the whole input unread. The stack is shown with its top at the left.

    0 / 7
    computed by script: the machine is simulated or the sets are constructed by scripts/toc_lib.py, one step at a time. python3 scripts/gen-toc3-traces.py

    The stack grows to n+1n + 1 symbols (the nn markers AA and ZZ) and the machine accepts when the stack is back to ZZ with all input read. It rejects aabaab (it ends with an AA left, no move to q3q_3), aabbbaabbb (the third bb finds ZZ on top and no move) and baba (no move from q0q_0 on bb).

    Nondeterminism: even palindromes

    A PDA cannot know where the middle of wwRw w^R is. It guesses: while reading the first half it pushes each symbol; at some moment it takes an ε\varepsilon-move into the second half, where it pops a symbol for every matching input symbol.

    • δ(q0,c,Z)={(q0,cZ)}\delta(q_0, c, Z) = \{(q_0, cZ)\} and δ(q0,c,t)={(q0,ct)}\delta(q_0, c, t) = \{(q_0, ct)\} for c,t∈{a,b}c, t \in \{a, b\}: push the symbol read;
    • δ(q0,ε,X)={(q1,X)}\delta(q_0, \varepsilon, X) = \{(q_1, X)\} for X∈{a,b,Z}X \in \{a, b, Z\}: guess the middle;
    • δ(q1,c,c)={(q1,ε)}\delta(q_1, c, c) = \{(q_1, \varepsilon)\}: match and pop; δ(q1,ε,Z)={(q2,Z)}\delta(q_1, \varepsilon, Z) = \{(q_2, Z)\}, q2q_2 accepting.

    On abbaabba one computation accepts (guessing the middle after abab):

    A PDA for w w^R reading abba (it guesses the middle): start

    Parser run by a script, not typed

    Pushdown automaton, one accepting computation (a shortest one found by breadth-first search over configurations). The stack is on the left with its top first; the unread input is on the right.

      stack (top first) (top at the left)
      Z
      unread input
      abba
      action start in state q0
      transition table: (state, stack top) and input symbol give new state and the symbols pushed (top first)
      state, topabε
      q0, aq0,aaq0,baq1,a
      q0, bq0,abq0,bbq1,b
      q0, Zq0,aZq0,bZq1,Z
      q1, aq1,ε
      q1, bq1,ε
      q1, Zq2,Z

      Step 0

      The machine starts in state q0 with the stack Z (only the bottom marker) and the whole input unread. The stack is shown with its top at the left.

      0 / 6
      computed by script: the machine is simulated or the sets are constructed by scripts/toc_lib.py, one step at a time. python3 scripts/gen-toc3-traces.py

      All computations are followed at once in the table of configurations. After the whole input there are 44 of them; one is in the accepting state q2q_2 with the stack ZZ:

      All computations of the w w^R automaton on abba at once: start

      Computed by a script, not typed
      input readconfigurations (state, stack with the top first)numberaccepting?
      nothing read
      after a
      after ab
      after abb
      after abba

      Step 0

      Nondeterminism means many computations in parallel. After each input symbol the table lists every configuration that some computation can be in (ε-moves included). The string is accepted if some computation ends in an accepting configuration.

      0 / 5
      computed by script: the machine is simulated or the sets are constructed by scripts/toc_lib.py, one step at a time. python3 scripts/gen-toc3-traces.py

      This machine is nondeterministic (δ(q0,ε,X)\delta(q_0, \varepsilon, X) and δ(q0,c,X)\delta(q_0, c, X) both apply). That is not an accident: no DPDA accepts the set of even palindromes (textbook theorem, proved on the next batch's page).

      More machines (each verified on all strings up to a length bound)

      • Equal numbers of aa and bb (any order): one state; the stack holds the surplus. On aa: pop a BB if one is on top (cancel), otherwise push AA; on bb: pop an AA if one is on top, otherwise push BB. Accept when only ZZ is left (and the input is used up).
      • Balanced parentheses: push a marker on ((, pop on )); accept with only ZZ left.
      • aibjcka^i b^j c^k with i=ji = j or j=kj = k: at the start guess which of the two comparisons to make (an ε\varepsilon-move into one of two branches): branch one pushes for aa, pops for bb, ignores cc; branch two ignores aa, pushes for bb, pops for cc.

      From final state to empty stack, and back

      • Final state →\to empty stack. Put a new bottom marker X0X_0 under Z0Z_0 and a new start state that pushes Z0Z_0 above it. From every accepting state add an ε\varepsilon-move to a new state pep_e that pops everything (including X0X_0). The new marker stops the machine from emptying its stack by accident.
      • Empty stack →\to final state. Add a new marker X0X_0 under Z0Z_0; whenever X0X_0 becomes the top (the old stack is empty), move to a new accepting state.

      What a PDA cannot do

      One stack gives one unbounded counter that can be compared once. {anbncn}\{a^n b^n c^n\} and {ww}\{ww\} (no reversal) are not accepted by any PDA (the pumping lemma for CFLs, later; quoted here). A PDA with two stacks is as powerful as a Turing machine (textbook theorem, quoted). A PDA that does not use its stack is just an NFA, so every regular language is accepted by a PDA.

      Worked examples

      EX 1Which language

      Easy

      A PDA has states q0,q1,q2q_0, q_1, q_2 with q2q_2 accepting. It pushes XX on every 00 read in q0q_0, on the first 11 it pops an XX and goes to q1q_1, on every further 11 it pops an XX, and from q1q_1 with only ZZ left it moves by ε\varepsilon to q2q_2. It accepts {0n1n:n≥1}\{0^n 1^n : n \ge 1\} (checked on all strings up to length 1010); the empty string is rejected because q0q_0 is not accepting. Always check whether the start state is accepting and whether the machine can go back to pushing after it started popping.

      EX 2Trace a computation

      Easy

      For the anbna^n b^n machine on aabbaabb: (q0,aabb,Z)⊢(q1,abb,AZ)⊢(q1,bb,AAZ)⊢(q2,b,AZ)⊢(q2,ε,Z)⊢(q3,ε,Z)(q_0, aabb, Z) \vdash (q_1, abb, AZ) \vdash (q_1, bb, AAZ) \vdash (q_2, b, AZ) \vdash (q_2, \varepsilon, Z) \vdash (q_3, \varepsilon, Z): five moves, accepted because q3∈Fq_3 \in F and the input is used up.

      EX 3Design: a^n b^(2n)

      Medium

      Push two symbols for every aa (or one symbol for each aa and then pop one symbol for every two bb's using an extra state). With the first method: δ(q0,a,Z)={(q0,AAZ)}\delta(q_0, a, Z) = \{(q_0, AAZ)\}, δ(q0,a,A)={(q0,AAA)}\delta(q_0, a, A) = \{(q_0, AAA)\}, δ(q0,b,A)={(q1,ε)}\delta(q_0, b, A) = \{(q_1, \varepsilon)\}, δ(q1,b,A)={(q1,ε)}\delta(q_1, b, A) = \{(q_1, \varepsilon)\}, then an ε\varepsilon-move on ZZ to an accepting state. The stack reaches height 2n+12n + 1 (and the machine needs the extra ε\varepsilon-moves on ZZ, from q0q_0 for n=0n = 0 and from q1q_1 at the end; checked on all strings up to length 1010).

      EX 4Final state versus empty stack

      Medium

      The language {anbn}\{a^n b^n\} by empty stack with two states: δ(q0,a,Z)={(q0,AZ)}\delta(q_0, a, Z) = \{(q_0, AZ)\}, δ(q0,a,A)={(q0,AA)}\delta(q_0, a, A) = \{(q_0, AA)\}, δ(q0,b,A)={(q1,ε)}\delta(q_0, b, A) = \{(q_1, \varepsilon)\}, δ(q1,b,A)={(q1,ε)}\delta(q_1, b, A) = \{(q_1, \varepsilon)\}, δ(q0,ε,Z)={(q1,ε)}\delta(q_0, \varepsilon, Z) = \{(q_1, \varepsilon)\} (for n=0n = 0) and δ(q1,ε,Z)={(q1,ε)}\delta(q_1, \varepsilon, Z) = \{(q_1, \varepsilon)\} (for the end): the last move pops ZZ and the stack is empty. No final states are needed.

      EX 5Typical questions

      GATE level

      (a) "Which is accepted by a PDA: {anbn}\{a^n b^n\}, {anbncn}\{a^n b^n c^n\}, {wwR}\{ww^R\}?" The first and third. (b) "Is every regular language accepted by a PDA?" Yes. (c) "Acceptance by final state versus empty stack:" equivalent in power for nondeterministic PDAs. (d) "A PDA accepts {aibjck:i=j or j=k}\{a^i b^j c^k : i = j \text{ or } j = k\}:" yes (a nondeterministic choice); it does not need to count both at once. (e) "Is {w∈{a,b}∗:#a=#b}\{w \in \{a, b\}^* : \#a = \#b\} accepted by a DPDA?" Yes. The deterministic machine uses two states: q0q_0 (accepting) means "only the bottom marker is on the stack", q1q_1 means "surplus present"; the first surplus symbol is a special bottom-level symbol, so popping it returns to q0q_0 (verified on all strings up to length 1010). (f) "Maximum stack height of the anbna^n b^n machine on anbna^n b^n:" n+1n + 1.

      Cheat box

      Cheat box

      • PDA =(Q,Σ,Γ,δ,q0,Z0,F)= (Q, \Sigma, \Gamma, \delta, q_0, Z_0, F); a move pops one symbol, pushes a string, may read or not read input.
      • Accept by final state (stack ignored) or by empty stack (states ignored); equivalent.
      • anbna^n b^n: push for aa, pop for bb (deterministic). wwRww^R: push, guess the middle (ε\varepsilon-move), pop (nondeterministic). Counts compared once only.
      • anbncna^n b^n c^n and wwww are not PDA languages. Regular ⊂\subset DPDA languages ⊂\subset PDA languages.
      • Deterministic: no ε\varepsilon-move together with an input move on the same state and stack top.

      Traps and GATE patterns

      Connections

      The machines of this page are the machines of the next one: every grammar has a PDA and every PDA a grammar (PDA and CFG); the deterministic subclass matters for parsing (deterministic versus nondeterministic PDA); the NFA is a PDA that ignores its stack (NFA).

      PYQs

      6 previous year questions from your book. I re-solved each one myself (5 by running or simulating, 1 by reasoning only); the book's key is shown next to my answer. All PYQs

      GATE 2023 · MCQ · 2 marksQ10 · page 226 of the book
      Consider the pushdown automaton (PDA) P below,
      which runs on the input alphabet {a, b}, has stack
      alphabet {⊥, A}, and has three states {s, p, q}, with s
      being the start state. A transition from state u to state v,
      labelled c/X/γ, where c is an input symbol or , X is a
      stack symbol, and γ is a string of stack symbols,
      represents the fact that in state u, the PDA can read c
      from the input, with X on the top of its stack, pop X from
      the stack, push in the string γ on the stack, and go to state
      v. In the initial configuration, the stack has only the
      symbol ⊥ in it. The PDA accepts by empty stack.
      
      Which one of the following options correctly describes the
      language accepted by P?
      • (a){ambn | 1<=m and n<m}
      • (b){ambn | 0<=n<=m}
      • (c){ambn | 0<=n and 0<=m}
      • (d){am| 0<=m} U {bn | 0<=n}
      GATE 2021 · Set 1 · NATQ16 · page 227 of the book
      In a pushdown automaton P=(Q,Σ,Γ,δ,q0,F), a
      transition of the form,
      
       where p, q∈Q, a ∈ Σ ∪ {ε}, X , Y ∈ Γ∪ {ε},
      represents:(q,Y) ∈ δ(p,a,X)
      Consider the following p ushdown automaton over the
      input alphabet Σ={a,b} and stack alphabet Γ={#,A}.
      
       The number of strings of length 100 accepted by the above
      pushdown automaton is ___________
      GATE 2016 · Set 1 · MCQQ27 · page 228 of the book
      Consider the transition diagram of a PDA given below
      with input alphabet Σ = {a,b} and stack alphabet Γ =
      {X,Z}. Z is the initial stack symbol. Let L denote the
      language accepted by the PDA.
      
      Which one of the following is TRUE?
      • (a)L = {anbn | n>= 0} and is not accepted by any finite automata
      • (b)L = {an| n >= 0} {anbn ∪ | n > =0} and is not accepted by any deterministic PDA.
      • (c)L is not accepted by any Turning machine that halts on every input.
      • (d)L = {an| n >= 0} {anbn ∪ | n > =0} and is deterministic context-free. THEORY OF COMPUTATION
      year not read yetQ29 · page 229 of the book
      Consider the NPDA 〈Q = {q0, q1, q2}, Σ = {0, 1}, Γ =
      {0, 1, ⊥}, δ, q0, ⊥, F = {q2}〉, where (as per usual
      convention) Q is the set of states, Σ is the input alphabet,
      Γ is stack alphabet, δ is the state transition function, q0
      is the initial state, ⊥ is the initial stack symbol, and F is
      the set of accepting states, The state transition is as
      follows:
      
       Which one of the following sequences must follow the
      string 101100 so that the overall string is accepted by
      the automaton?
      • (a)10110
      • (b)10010
      • (c)01010
      • (d)01001
      GATE 2006 · MCQQ46 · page 231 of the book
      Consider the pushdown automaton (PDA) below which
      runs over the input alphabet (a,b,c). It has the stack
      alphabet { Z0,X } where Z0 is the bottom-of-stack marker.
      The set of states of the PDA is (s,t,u,f) where s is the start
      state and f is the final state. The PDA accepts by final state.
      The transitions of the PDA given below are depicted in a
      standard manner. For example, the transition (s,b,X)-
      (t,XZ0) means that if the PDA is in state s and the symbol
      on the top of the stack is X, then it can read b from the
      input and move to state t after popping the top of stack and
      pushing the symbols Z0 and X (in that order) on the stack.
      (s,a,Z0) → (s,XXZ0)
      (s,∈,Z0) → (f,∈)
      (s,a,X) → (s,XXX)
      (s,b,X) → (t,∈)
      (t,b,X) → (t,∈)
      (t,c,X) → (u,∈)
      (u,c,X) → (u,∈)
      (u,∈,Z0) → (f,∈)
      The Language accepeted by the PDA is
      • (a). {albmcn | l=m=n }
      • (b). {albmcn | l=m}
      • (c). {albmcn | 2l=m+n}
      • (d). {albmcn | m=n}
      GATE 2005 · MCQ · 2 marksQ51 · page 232 of the book
      Let P be a non-deterministic push-down automaton
      (NPDA) with exactly one state, q, and exactly one symbol
      Z, in its stack alphabet. State q is both the starting as well
      as the accepting state of the PDA. The stack is initialized
      with one Z before the start of the operation of the PDA.
      Let the input alphabet of the PDA be Σ. Let L(P) be the
      language accepted by the PDA by reading a string and
      reaching its accepting state. Let N{P} be the language
      accepted by the PDA by reading a string and emptying its
      stack. Which of the following statements is TRUE?
      • (a)L(P) is necessarily Σ* but N{P} is not necessarily Σ*.
      • (b)N{P} is necessarily Σ* but L(P) is not necessarily Σ*.
      • (c)Both L(P) and N(P) are necessarily Σ*.
      • (d)Neither L(P) nor N(P) are necessarily Σ*

      Self-check

      Question 1

      Why can a PDA accept {anbn}\{a^n b^n\} but a DFA cannot?

      Show answer

      The stack remembers nn, which needs unbounded memory; a DFA has finitely many states.

      Question 2

      What is the maximum stack height of the anbna^n b^n machine above on a5b5a^5 b^5?

      Show answer

      66 (five markers AA and the bottom marker ZZ).

      Question 3

      Where does the PDA for wwRww^R use nondeterminism?

      Show answer

      In the ε\varepsilon-move that guesses the middle of the string.

      Question 4

      How do you turn a PDA that accepts by final state into one that accepts by empty stack?

      Show answer

      Add a new bottom marker, a new start move that pushes Z0Z_0 above it, and ε\varepsilon-moves from every accepting state to a state that pops everything.

      Revision card

      One-screen revision

      • PDA = NFA + stack; move pops one symbol and pushes a string; configurations (q,w,α)(q, w, \alpha).
      • Final state or empty stack accept the same languages.
      • DPDA: no conflict between ε\varepsilon and input moves; wwRww^R needs guessing.
      • Cannot do anbncna^nb^nc^n or wwww; regular ⊂\subset PDA.

      References

      • ·Sipser, Introduction to the Theory of Computation (3rd edition), Section 2.2 (pushdown automata)
      • ·Hopcroft, Motwani, Ullman, Introduction to Automata Theory, Languages, and Computation (3rd edition), Chapter 6 (pushdown automata), Sections 6.1 to 6.3

      Verification

      scripts/enum-checks/toc_cfg.py: the six PDAs on this page (a^n b^n deterministic by final state and by empty stack, even palindromes, equal numbers of a and b, balanced parentheses, i = j or j = k) accept exactly the strings of independent predicates on all strings up to length 10 (12 for parentheses, 9 for the three-symbol language); the search for an accepting computation is a breadth-first search over configurations (state, unread input, stack) with a bound on the stack height; the deterministic one has no state and stack top with both an epsilon move and an input move; the stack of the a^n b^n machine reaches height n + 1 for n = 0..7; final-state PDAs were converted to empty-stack PDAs and back (named and 100 random PDAs) with the same language on all strings up to length 5 to 8; the computation traces were replayed against the transition tables. Facts about PDAs with two stacks and about the limits of PDAs are textbook statements, quoted. Textbook chapter references are from memory. SECOND PASS (2026-11-02, done by the assistant, not a human check): every Self-check question was solved before the printed answer was read and all agreed; the definitions, worked examples, cheat box and traps were read claim by claim and the numbers re-derived by hand or by a small script (state counts, derivations, parse-tree and move counts, decomposition counts); tables and animations are machine output and were read, not re-run; One unclear sentence (the one-state machine for equal numbers of a and b) and the wording of the empty-stack example were rewritten. Status raised to self-checked; verified needs a textbook or human check.

      last verified: never