Pushdown Automata: COMP2600 - Formal Methods For Software Engineering
Pushdown Automata: COMP2600 - Formal Methods For Software Engineering
Pushdown Automata: COMP2600 - Formal Methods For Software Engineering
Katya Lebedeva
a0 a1 a2 ... .... an
Finite
State
zk
Control
stack
memory
z2
z1
A finite state control reads the string, one symbol at a time (the input symbol).
1. Consumes the input symbol from the input string. If ε is the input symbol,
then no input symbol is consumed.
3. Replaces the symbol at the top of the stack by any string (see next slide!!!)
γ Operation on stack
ε Popping: removal of the most recently added element (stack’s top symbol)
A Popping stack’s top symbol. Pushing A. Hence, no change is made.
B Popping stack’s top symbol. Pushing B.
w Popping stack’s top symbol. Pushing X1 X2 . . . Xn onto the stack (X1 is on the top).
{an bn | n ≥ 1}
Recall that this language cannot be recognised by a FSA.
Intuition:
• phase 1: (state q1 ) push a’s, one by one, from the input onto the stack
• phase 2: (state q2 ) when seeing b on the tape pop a from the stack
• finalise: if the stack is empty and the input is exhausted in the final state
(q3 ), accept the string.
a, X/α
q p
The only thing that the digram does not tell us is which stack symbol is the
start symbol. Conventionally, it is Z .
a, a/aa b, a/ε
δD : Q × (Σ ∪ {ε}) × Γ → Q × Γ∗
δD : (state, input symbol or ε, top-of-stack) → (new state, string of stack’s symbols)
• δ is a transition function
Q×Γ∗
δ : Q × (Σ ∪ {ε}) × Γ → 2
such that for all q ∈ Q and s ∈ Γ, δ(q, ε, s) is defined iff δ(q, a, s) is undefined
for all a ∈ Σ.
It is also useful to see the portion of the input string that remains.
• q is the state
Convention: show the top of the stack at the left end of γ and the bottom at
the right end
Consuming a (which may be ε) from the input and replacing X on top of the
stack by α, we can go from state q to state p.
Note that what remains on the input, i.e. w, and what is below the top of the
stack, i.e. β, do not influence the action of the PDA in this computation step.
They are merely carried along and can influence later steps.
The machine halts in the final state with input exhausted and an empty stack.
The string is accepted.
• PDA is in state q0
• the tape head is on the leftmost symbol of the input string
• the stack contains only Z
Computation and termination: Starting at the start configuration, the PDA
performs a sequence of computation steps (moves). It terminates when the
stack becomes empty.
Acceptance by final state: The PDA consumes the input and enters a final
state.
Acceptance by empty stack: The PDA consumes the input and empties the
stack.
• If the top of the stack has symbol Z , pop it and enter the final state via an
ε-transition. Hopefully our input has been used up too!
Full formal details are left as an exercise.
Deterministic PDAs recognise all the regular languages, but only a proper
subset of context free languages!
L(DPDA) ⊂ L(NPDA)
(r, Z) ∈ δ(q, ε, Z)
(r, a) ∈ δ(q, ε, a)
(r, b) ∈ δ(q, ε, b)
q is the ‘push’ state, and r the ‘match and pop’ state.
Theorem
We will only justify this result in one direction: for any CFG, there is a corre-
sponding PDA.
Given a CFG
G = (Σ, N, S, P)
We define a PDA
P = ({q0 , q1 , q2 }, q0 , {q2 }, Σ, N ∪ Σ, Z, δ)
Note that P has three states: q0 (initial), q1 (processing), and q2 (final). Its
alphabet the alphabet (i.e. the set of terminals) of G, and the set of its stack
symbols consists of G’s terminals and non-terminals.
(q1 , α) ∈ δ(q1 , ε, A)
3. For each terminal symbol t , pop the stack if it matches the input:
S → S+T | T
T → T ∗U | U
U → (S) | i
1. Initialise:
2. Expand non-terminals:
4. Terminate: