Theory of Computation · Unit 5 · 7 hrs
Push Down Automata
Exam-focused notes for Push Down Automata (Theory of Computation, CSC262): what the TU syllabus asks and how it has actually been tested, with 9 solved past questions from this unit.
Push Down Automata in short
This unit adds a stack to the finite automaton, giving the pushdown automaton, the exact machine model that accepts precisely the context free languages, and it establishes the two-way translation between a PDA and the context free grammar that defines the same language.
What this unit covers
- Introduction to Push Down Automata (PDA)
- Representation of PDA
- Operations of PDA
- Move of a PDA
- Instantaneous Description for PDA
- Deterministic PDA
- Non Deterministic PDA
- Acceptance of strings by PDA
- Language of PDA
- Construction of PDA by Final State
- Construction of PDA by Empty Stack
- Conversion of PDA by Final State to PDA accepting by Empty Stack and vice-versa
- Conversion of CFG to PDA
- Conversion of PDA to CFG
Conversion of CFG to PDA
2081 · 10 marks
Mention the transition function of PDA. List the two ways that PDA accepts the string. Convert the following CFG to PDA.S→AS∣εS \rightarrow AS \mid \varepsilonS→AS∣εA→Ab∣Bb∣abA \rightarrow Ab \mid Bb \mid abA→Ab∣Bb∣ab[10]
Show solved answerHide answer
Pushdown Automata (PDA) - Transition Function, Acceptance, and CFG Conversion
1. Transition Function of PDA
A Pushdown Automaton (PDA) is defined as a 7-tuple:
$$PDA = (Q, \Sigma, \Gamma, \delta, q_0, Z_0, F)$$
Where:
- $Q$ = finite set of states
- $\Sigma$ = input alphabet
- $\Gamma$ = stack alphabet
- $\delta$ = transition function
- $q_0$ = initial state
- $Z_0$ = initial stack symbol
- $F$ = set of final/accepting states
Transition Function $\delta$:
$$\delta : Q \times (\Sigma \cup {\varepsilon}) \times \Gamma \rightarrow \mathcal{P}(Q \times \Gamma^*)$$
Interpretation: Given a current state $q \in Q$, a current input symbol $a \in \Sigma$ (or $\varepsilon$), and the top of stack symbol $X \in \Gamma$, the transition function returns a set of pairs $(p, \gamma)$ where:
- $p$ is the new state
- $\gamma \in \Gamma^*$ is the string that replaces $X$ on the stack
Notation: $\delta(q, a, X) = {(p, \gamma), \ldots}$
2. Two Ways PDA Accepts a String
A PDA can accept a string in two ways:
Way 1: Acceptance by Final State
- A string $w$ is accepted if, after reading the entire input $w$, the PDA is in a final (accepting) state $q \in F$.
- The stack content does not matter at the time of acceptance.
- Formally: $(q_0, w, Z_0) \vdash^* (q_f, \varepsilon, \gamma)$ for some $q_f \in F$ and $\gamma \in \Gamma^*$
Way 2: Acceptance by Empty Stack
- A string $w$ is accepted if, after reading the entire input $w$, the stack becomes empty.
- The state does not need to be a final state.
- Formally: $(q_0, w, Z_0) \vdash^* (q, \varepsilon, \varepsilon)$ for any state $q \in Q$
Note: Both methods are equivalent in power; any language accepted by one method can be accepted by the other.
3. Converting the Given CFG to PDA
Given Grammar:
$$S \rightarrow AS \mid \varepsilon$$ $$A \rightarrow Ab \mid Bb \mid ab$$ $$B \rightarrow \text{(not explicitly given, assumed terminal or derived)}$$
Note: Variable $B$ appears in $A \rightarrow Bb$ but no production for $B$ is given. We include it as-is and handle it in the PDA transitions. It is likely a terminal or a typo for a variable; we treat $B$ as a variable with no further productions (or it may be a terminal symbol in some interpretations). We proceed with the given grammar.
Method: CFG to PDA (Acceptance by Empty Stack)
Standard Construction:
For any CFG, we construct a PDA with:
- States: ${q_0, q_1, q_2}$ (typically three states: $q_0$ to push start symbol, $q_1$ as the main loop state, $q_2$ as accepting state)
- Or more commonly, a single-state PDA ${q}$ is used for simplicity.
Rules for conversion:
| CFG Rule | PDA Transition |
|---|---|
| For each production $A \rightarrow \alpha$ | $\delta(q, \varepsilon, A) = {(q, \alpha)}$ (pop $A$, push $\alpha$ reversed) |
| For each terminal $a \in \Sigma$ | $\delta(q, a, a) = {(q, \varepsilon)}$ (match and pop) |
Step-by-Step PDA Construction
PDA: $M = ({q_0, q_1}, {a, b}, {S, A, B, a, b, Z_0}, \delta, q_0, Z_0, {q_1})$
Initialization transitions:
$$\delta(q_0, \varepsilon, Z_0) = {(q_1, SZ_0)}$$
This pushes the start symbol $S$ onto the stack and moves to the working state $q_1$.
Production Rules $\rightarrow$ PDA Transitions
For variable $S$:
| Production | PDA Transition |
|---|---|
| $S \rightarrow AS$ | $\delta(q_1, \varepsilon, S) \ni (q_1, AS)$ |
| $S \rightarrow \varepsilon$ | $\delta(q_1, \varepsilon, S) \ni (q_1, \varepsilon)$ |
So: $\delta(q_1, \varepsilon, S) = {(q_1, AS),\ (q_1, \varepsilon)}$
For variable $A$:
| Production | PDA Transition |
|---|---|
| $A \rightarrow Ab$ | $\delta(q_1, \varepsilon, A) \ni (q_1, Ab)$ |
| $A \rightarrow Bb$ | $\delta(q_1, \varepsilon, A) \ni (q_1, Bb)$ |
| $A \rightarrow ab$ | $\delta(q_1, \varepsilon, A) \ni (q_1, ab)$ |
So: $\delta(q_1, \varepsilon, A) = {(q_1, Ab),\ (q_1, Bb),\ (q_1, ab)}$
For variable $B$: the grammar gives no production for $B$, so there is no transition $\delta(q_1, \varepsilon, B)$. Any derivation that uses $A \to Bb$ can therefore never remove $B$ from the stack and never finishes, so that alternative contributes no strings to the language; in effect only $A \to Ab \mid ab$ matter for what the PDA actually accepts.
Terminal matching transitions (match input symbol against stack top, then pop both):
$$\delta(q_1, a, a) = {(q_1, \varepsilon)}$$ $$\delta(q_1, b, b) = {(q_1, \varepsilon)}$$
Acceptance by empty stack:
$$\delta(q_1, \varepsilon, Z_0) = {(q_1, \varepsilon)}$$
Once $Z_0$ itself is popped with no more input to read, the stack is empty and the string is accepted.
Complete PDA
$$M = ({q_0, q_1}, {a, b}, {S, A, B, a, b, Z_0}, \delta, q_0, Z_0, {q_1})$$
with transition function
$$\delta(q_0, \varepsilon, Z_0) = {(q_1, SZ_0)}$$ $$\delta(q_1, \varepsilon, S) = {(q_1, AS),\ (q_1, \varepsilon)}$$ $$\delta(q_1, \varepsilon, A) = {(q_1, Ab),\ (q_1, ab)}$$ $$\delta(q_1, a, a) = {(q_1, \varepsilon)}$$ $$\delta(q_1, b, b) = {(q_1, \varepsilon)}$$ $$\delta(q_1, \varepsilon, Z_0) = {(q_1, \varepsilon)}$$
($B$ is kept in the stack alphabet only because it appears in the given grammar; since it has no production, it never actually gets pushed by any transition that leads to acceptance.)
Sample Acceptance Trace for $w = ab$
$$(q_0,\ ab,\ Z_0) \vdash (q_1,\ ab,\ SZ_0) \vdash (q_1,\ ab,\ ASZ_0) \vdash (q_1,\ ab,\ abSZ_0)$$ $$\vdash (q_1,\ b,\ bSZ_0) \vdash (q_1,\ \varepsilon,\ SZ_0) \vdash (q_1,\ \varepsilon,\ Z_0) \vdash (q_1,\ \varepsilon,\ \varepsilon)$$
The input is fully consumed and the stack is empty, so $w = ab$ is accepted, both by the empty-stack criterion and by the final-state criterion (since $q_1 \in F$). This completes the construction of a PDA equivalent to the given CFG.
Construction of PDA by Final State
2080 · 5 marks
Design a PDA over {x,y} which accepts strings defined by the language Show acceptance of xxyy. $L = {x^n y^n xy \mid n \geq 0}$ [5]
2078 · 5 marks
Define a Push Down Automata. Construct a PDA that accepts $L = {a^n b^n \mid n > 0}$. [5]
2080.1 · 5 marks
Construct a PDA that accepts string over Σ={a,b}\Sigma = { a, b }Σ={a,b} that contains equal number of a's followed by equal number of b's. Show acceptance of aabb and aab. [5]
Show solved answerHide answer
PDA for $L = {a^n b^n \mid n \geq 1}$
Given
- Alphabet: $\Sigma = {a, b}$
- Language: strings with equal number of $a$'s followed by equal number of $b$'s
- Strings to test: $aabb$ and $aab$
$$L = {a^n b^n \mid n \geq 1} = {ab, aabb, aaabbb, \ldots}$$
STEP 2 - Construction and Solution
Strategy
- Push each $a$ onto the stack.
- On the first $b$, start popping $a$'s.
- On subsequent $b$'s, keep popping $a$'s.
- Accept when only $Z_0$ remains after all input consumed (empty count).
Formal Definition
$$M = (Q, \Sigma, \Gamma, \delta, q_0, Z_0, F)$$
- $Q = {q_0, q_1, q_2}$
- $\Sigma = {a, b}$
- $\Gamma = {a, Z_0}$
- Start state $q_0$, initial stack symbol $Z_0$
- $F = {q_2}$
Transition Table
| State | Input | Stack Top | Push | Next State |
|---|---|---|---|---|
| $q_0$ | $a$ | $Z_0$ | $aZ_0$ | $q_0$ |
| $q_0$ | $a$ | $a$ | $aa$ | $q_0$ |
| $q_0$ | $b$ | $a$ | $\varepsilon$ | $q_1$ |
| $q_1$ | $b$ | $a$ | $\varepsilon$ | $q_1$ |
| $q_1$ | $\varepsilon$ | $Z_0$ | $Z_0$ | $q_2$ |
Notation: input, top / pushed-string.
Acceptance of $aabb$
| Step | State | Remaining Input | Stack (top→bottom) | Transition |
|---|---|---|---|---|
| 1 | $q_0$ | $aabb$ | $Z_0$ | $a,Z_0/aZ_0$ |
| 2 | $q_0$ | $abb$ | $aZ_0$ | $a,a/aa$ |
| 3 | $q_0$ | $bb$ | $aaZ_0$ | $b,a/\varepsilon$ |
| 4 | $q_1$ | $b$ | $aZ_0$ | $b,a/\varepsilon$ |
| 5 | $q_1$ | $\varepsilon$ | $Z_0$ | $\varepsilon,Z_0/Z_0$ |
| 6 | $q_2$ | $\varepsilon$ | $Z_0$ | halt in final state |
Input consumed, final state $q_2$ reached. $aabb$ is ACCEPTED.
Acceptance of $aab$
| Step | State | Remaining Input | Stack (top→bottom) | Transition |
|---|---|---|---|---|
| 1 | $q_0$ | $aab$ | $Z_0$ | $a,Z_0/aZ_0$ |
| 2 | $q_0$ | $ab$ | $aZ_0$ | $a,a/aa$ |
| 3 | $q_0$ | $b$ | $aaZ_0$ | $b,a/\varepsilon$ |
| 4 | $q_1$ | $\varepsilon$ | $aZ_0$ | stack top is $a$, but no $\varepsilon$-move on $a$ in $q_1$ |
Input exhausted while stack still holds an unmatched $a$ (top $\neq Z_0$), so $q_2$ cannot be reached. $aab$ is REJECTED (2 a's, 1 b: unequal).
Summary
| String | a's | b's | Result |
|---|---|---|---|
| $aabb$ | 2 | 2 | Accepted ($n=2$) |
| $aab$ | 2 | 1 | Rejected |
The PDA correctly accepts $a^n b^n$ and rejects strings with unequal counts.
Keep the transition table precise: write the $q_2$ transitions so that the empty-stack case is distinguished rather than folded into a,ε/a, and avoid an extra initialization state. The verdicts (accept $aabb$, reject $aab$) are unaffected.
Conversion of PDA to CFG
2080 · 5 marks
How is PDA to CFG conversion done? Consider a PDA that accepts by empty stack, Now construct an equivalent CFG. $P = ((p,q);{0,1},{Z},\delta,p,Z);$ $\delta(p,0,Z)=(p,0z), \delta(p,0,0)=(p,00), \delta(p,1,0)=(p,\varepsilon), \delta(p,\varepsilon,z)=(q,\varepsilon)$ [5]
2080.1 · 5 marks
How is conversion of PDA to CFG done? Illustrate with example. [5]
Representation of PDA
2079 · 5 marks
Give the formal definition of Push Down Automata. How CFG can be converted into equivalent PDA. Explain with an example. [5]
Conversion of PDA by Final State to PDA accepting by Empty Stack and vice-versa
2076 · 10 marks
How can you define the language accepted by a PDA? Explain how a PDA accepting language by empty stack is converted into an equivalent PDA accepting by final state and vice-versa.[10]
Acceptance of strings by PDA
2076 · 5 marks
Construct a PDA accepting language over {0, 1} representing strings with equal no of 0s and 1s. Show by sequence of IDs that 0101 is accepted by this PDA. [5]
Make Unit 5 stick
Practice CSC262 with flashcards & quizzes