1. Home
  2. Theory of Computation
  3. Pushdown Automata (PDA)

Pushdown Automata (PDA)

A finite automaton plus a stack. Watch it count a's and b's and check balanced brackets — something no DFA can do — with a 3D stack that grows and shrinks.

Interactive 3DIntermediate12 min readTOCUpdated

Drag to rotate · Right-drag to pan · Click, then scroll to zoom · Space play · Left arrowRight arrow step

What's happening

A pushdown automaton is a finite automaton plus a stack — unlimited memory, but you can only touch the top. That is enough to recognise languages like aⁿbⁿ that need counting. Press Run, then try "aaabb".

Pseudocode

  1. q0 —(a, Z → AZ)→ q0
  2. q0 —(a, A → AA)→ q0
  3. q0 —(b, A → ε)→ q1
  4. q1 —(b, A → ε)→ q1
  5. q1 —(ε, Z → Z)→ q2
  6. q0 —(ε, Z → Z)→ q2
Read this model as text · 10 steps

One run of the model above, written out step by step. Useful with a screen reader, on a slow connection, or for quick revision. The values may differ from the run on your screen.

  1. A pushdown automaton is a finite automaton plus a stack — unlimited memory, but you can only touch the top. That is enough to recognise languages like aⁿbⁿ that need counting. Press Run, then try "aaabb".
  2. Input "aaabbb". The stack starts with the bottom marker Z. Push an A for every a. Pop one A for every b. If the stack is back to Z exactly when the input ends, the counts matched.
  3. Rule q0 → q0: read 'a', pop Z, push AZ.
  4. Rule q0 → q0: read 'a', pop A, push AA.
  5. Rule q0 → q0: read 'a', pop A, push AA.
  6. Rule q0 → q1: read 'b', pop A.
  7. Rule q1 → q1: read 'b', pop A.
  8. Rule q1 → q1: read 'b', pop A.
  9. Rule q1 → q2: read nothing (ε), pop Z, push Z.
  10. Input finished in an accepting state → "aaabbb" is ACCEPTED. A finite automaton could never do this — it has no memory to count with.

Try this in the 3D model

  • Run aaabbb. Watch the stack grow during the a's and shrink during the b's.
  • Try aaabb and abab. At which step does each get stuck?
  • Switch to balanced parentheses and test (()()) and then (()))(.

Why a stack?

A finite automaton has a fixed number of states, so it can’t count without limit. It can’t check that a string has as many b’s as a’s, or that brackets are balanced.

A pushdown automaton (PDA) adds one thing: a stack — unlimited memory where you can only push to and pop from the top. That’s enough to count and to match nested structures.

How a PDA moves

Each transition is written

read, pop → push
  • read — the next input symbol (or ε to read nothing),
  • pop — the symbol that must be on top of the stack (it gets removed),
  • push — the symbols to put back (top first; ε = push nothing).

The stack starts with a bottom marker Z. The 3D model shows the stack as a tower on the right.

Example 1: aⁿbⁿ

From Rule To Meaning
q0 a, Z → AZ q0 first a: push an A
q0 a, A → AA q0 each further a: push an A
q0 b, A → ε q1 first b: pop an A
q1 b, A → ε q1 each further b: pop an A
q1 ε, Z → Z q2 ✓ back to Z: counts matched
q0 ε, Z → Z q2 ✓ n = 0: the empty string is accepted too

For aaabbb the stack goes Z → AZ → AAZ → AAAZ → AAZ → AZ → Z, then the machine accepts. For aaabb the input runs out with A still on the stack → reject.

Example 2: balanced parentheses

Push an X for every (, pop one for every ). A ) with nothing to pop, or leftover X’s at the end, means unbalanced. This is exactly how compilers check brackets — and why you can’t do it with a regex.

PDAs and context-free grammars

PDAs recognise exactly the context-free languages — the languages generated by context-free grammars (CFGs), such as

S → a S b | ε          generates aⁿbⁿ
E → E + E | E * E | ( E ) | id     arithmetic expressions

Every programming language’s syntax is (mostly) described by a CFG, and parsers are essentially PDAs.

The Chomsky hierarchy

Machine Language class Example
Finite automaton Regular strings ending in 01
Pushdown automaton Context-free aⁿbⁿ, balanced brackets
Linear-bounded automaton Context-sensitive aⁿbⁿcⁿ
Turing machine Recursively enumerable anything computable

Even a PDA can’t do aⁿbⁿcⁿ — one stack can only count one thing at a time.

Code

def balanced(s):
    stack = ["Z"]
    for ch in s:
        if ch == "(":
            stack.append("X")            # push
        elif ch == ")":
            if stack[-1] != "X":
                return False             # nothing to pop → stuck
            stack.pop()                  # pop
    return stack == ["Z"]                # accept if only Z is left

print(balanced("(()())"), balanced("(()))("))   # True False

Common mistakes

  • Pushing symbols in the wrong order (the first symbol written is the new top).
  • Forgetting the bottom marker Z — it’s how the PDA knows the stack is “empty”.
  • Expecting deterministic PDAs to handle every context-free language (e.g. even-length palindromes need nondeterminism).

Complexity at a glance

Case / operationTimeWhy
Deterministic PDA run (input length n)O(n)
General CFG parsing (CYK)O(n³)
Extra spaceO(n) stack

Quick check

Test yourself — pick an answer to see if you got it.

1. What extra memory does a PDA have compared with a finite automaton?

2. Which language can a PDA recognise but a DFA cannot?

3. PDAs recognise exactly which class of languages?

4. In the transition "a, Z → AZ", what happens?

Saved in this browser.
Spotted a mistake or a bug in the 3D model?

Report a mistake

in Pushdown Automata (PDA). Thank you — every report makes the lesson better for the next reader.

We'll also include a link to the step of the 3D model you're on and your browser type, so we can reproduce it.