Sub topic 8
Pushdown Automata: States, Stack, and Transitions
Prerequisites and working model
Assume you know finite automata, alphabets, strings, and context-free grammars. A pushdown automaton (PDA) is a finite automaton with an additional stack. The stack gives the machine unbounded memory, but it can access only the top symbol.
A PDA is commonly defined as
\[ (Q,\Sigma,\Gamma,\delta,q0,Z0,F) \]
where:
- \(Q\) is a finite set of states.
- \(\Sigma\) is the input alphabet.
- \(\Gamma\) is the stack alphabet.
- \(\delta\) is the transition function.
- \(q_0\) is the initial state.
- \(Z_0\) is the initial stack symbol.
- \(F\) is the set of final states.
A transition can be written as
\[ \delta(q,a,X)\ni(p,\alpha). \]
The PDA is in state \(q\), reads input symbol \(a\), and sees \(X\) at the top of the stack. It moves to state \(p\), consumes \(a\), removes \(X\), and pushes the string \(\alpha\). If \(a=\varepsilon\), no input symbol is consumed.
We write the top of the replacement string on the left. Thus, replacing \(X\) with \(AB\) puts \(A\) on top.
Example: recognizing \(a^n b^n\)
The language
\[ L=\{a^n b^n\mid n\geq 1\} \]
requires matching the number of a symbols with the number of b symbols. A PDA can push one marker for every a, then pop one marker for every b.
The machine has three logical phases:
- In the push state, read
aand pushA. - Change to the pop state when the first
bappears. - In the pop state, read
band remove oneA.
The input is accepted when all input is consumed and only the bottom stack marker remains.
flowchart TD
S([start]) --> P[push state]
P -- "a, Z0 / AZ0" --> P
P -- "a, A / AA" --> P
P -- "b, A / ε" --> R[pop state]
R -- "b, A / ε" --> R
R --> F([accept when input ends and stack has Z0])The notation a, A / AA means “read a, replace the top A with AA.” The new A is placed above the old marker.
def accepts_anbn(word):
stack = []
state = "push"
for symbol in word:
if state == "push" and symbol == "a":
stack.append("A")
elif symbol == "b" and stack:
state = "pop"
stack.pop()
else:
return False
return state == "pop" and not stack
print(accepts_anbn("aaabbb")) # True
print(accepts_anbn("aab")) # False
print(accepts_anbn("abab")) # FalseNondeterministic transitions
For a nondeterministic PDA, one configuration may have several possible next moves. The input is accepted if at least one computation branch accepts. A PDA may also use an \(\varepsilon\)-transition to change state or modify its stack without reading input.
This nondeterminism is useful when the machine must guess where one part of an input ends and another begins.
Exercise
What does a transition δ(q, b, A) ∋ (p, ε) do?
Show answer
It moves from q to p, consumes b, removes A from the stack, and pushes nothing in its place.