Pushdown Automata
A pushdown automaton (PDA) is essentially an
Formal Definition
A PDA is a 7-tuple
: finite set of states : input alphabet : stack alphabet : transition function finite subsets of : start state : initial stack symbol : accepting states
Transition Meaning
In state
, reading input , with on top of stack, go to state and replace by on the stack.
- If
: move allowed without consuming input (spontaneous) - If
: pop (push nothing) - If
: push , then (convention: leftmost symbol goes on top)
Instantaneous Descriptions (ID)
- State
- Remaining input
- Stack contents
(top at left)
Moves
Two Notions of Acceptance
Acceptance by Final State
The PDA accepts if, after reading all input, it reaches a final state (stack contents irrelevant).
Acceptance by Empty Stack
The PDA accepts if, after reading all input, the stack is empty (final state irrelevant).
Equivalence
These two notions are equivalent. Given a PDA accepting by final state, we can construct one accepting by empty stack (and vice versa).
Equivalence of PDA and CFG
Theorem: A language is context-free iff it is accepted by some PDA.
Direction 1: CFG PDA
Let
Intuition: At each step,
Transition function:
- Type 1:
for - Match input symbol with terminal on top of stack
- Type 2: For each production
: - Expand variable by its production body (replace
with on stack)
- Expand variable by its production body (replace
Proof of correctness: We prove
This ensures
Direction 2: PDA CFG
Given PDA
Key idea: Variables of
Starting in state
with on top of stack, eventually pop and reach state (with empty stack relative to ).
Productions:
- Basis: If
, then (pop in one move) - One intermediate: If
, then for all states - Two intermediate: If
, then for all states - General: If
, we need variables for each intermediate state
Start symbol:
Proof:
Properties of Context-Free Languages
Decision Properties
Given a CFG (or PDA), we can decide:
- Emptiness: Is
? — Check if start symbol is a "useful" variable (derives some terminal string). Solved during useless-variable elimination. - Membership: Is
? — Use the CYK algorithm, where . - Infiniteness: Is
infinite? — Similar to regular languages: find if there is a cycle in the variable dependency graph that generates non-empty strings.
Non-Decision Properties (Undecidable)
Questions that cannot be decided for CFLs (but can for regular languages):
- Are two CFLs equal? (
) - Are two CFLs disjoint? (
) - Is a CFL equal to
? - Is the complement of a CFL also context-free?
These require Turing machine theory to prove.
CYK Algorithm (Cocke-Younger-Kasami)
Input: A CFG
Dynamic Programming approach: Let
- Basis (
): - Induction (
): - Accept:
iff
The algorithm fills an
Closure Properties
CFLs are closed under:
- Union (
): Add to combined grammar - Concatenation (
): Add to combined grammar - Kleene Star (
): Add - Reversal (
): Reverse all right sides in productions - Homomorphism (
): Replace each terminal by - Substitution: Replace each terminal
by (a CFL) - Inverse Homomorphism (
): Use PDA construction — buffer in finite control
CFLs are NOT closed under:
- Intersection:
is intersection of two CFLs ( and ), but is not context-free - Complement: If closed, intersection would be closed (
) - Difference: Similar argument
However, the intersection of a CFL with a regular language IS a CFL (simulate DFA and PDA in parallel — product construction with stack from PDA).
Deterministic PDA (DPDA)
A PDA is deterministic if for every configuration, at most one move is possible:
- For each
, at most one choice (including -moves) - If
, then for all
DPDAs accept a proper subclass of CFLs called deterministic CFLs (DCFLs). DCFLs are important because they can be parsed efficiently (LR parsers). DCFLs are closed under complement.