5 Push Down Automata

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.

Push Down Automata: notes

Introduction to Push Down Automata (PDA)

A pushdown automaton (PDA) can be thought of as an $\epsilon$-NFA with one extra piece of memory attached: an unbounded stack. The stack lets a PDA remember an unbounded amount of information, unlike the fixed, finite memory of a plain finite automaton, but only in a restricted, last-in-first-out way: at each step the machine may only read (and typically pop) the symbol currently on top of the stack, and may only push new symbols onto that same top. This single restriction is exactly what makes a PDA powerful enough to recognize every context free language (matching nested structures such as balanced parentheses) while still falling short of a Turing machine's unrestricted memory access.

Representation of PDA

Formally, a PDA is a 7-tuple $$P = (Q, \Sigma, \Gamma, \delta, q_0, Z_0, F),$$ where $Q$ is a finite set of states, $\Sigma$ is the input alphabet, $\Gamma$ is the stack alphabet, $q_0 \in Q$ is the start state, $Z_0 \in \Gamma$ is the symbol initially on the stack, $F \subseteq Q$ is the set of final states, and $\delta$ is the transition function $$\delta: Q \times (\Sigma \cup {\epsilon}) \times \Gamma \to \text{finite subsets of } Q \times \Gamma^*.$$ Each argument to $\delta$ names a state, an input symbol (or $\epsilon$, for a move that consumes no input), and the stack symbol currently on top; each value returned is a set of pairs, a next state and a string of stack symbols that replaces the popped symbol.

Operations of PDA

Every move of a PDA is expressed uniformly as "pop the top stack symbol $X$, then push a string $\alpha$" even when the intended operation is simpler. Popping the top symbol with nothing pushed back is written by choosing $\alpha = \epsilon$: replacing $X$ by $\epsilon$ removes it entirely. Pushing a new symbol $Y$ on top of the current top $X$ (leaving $X$ in place) is written by choosing $\alpha = YX$: popping $X$ and pushing back $YX$ nets one new symbol $Y$ above the unchanged $X$. Replacing the top symbol by something else entirely, without growing or shrinking the stack, is written by choosing $\alpha$ to be a single symbol different from $X$. Every stack manipulation a PDA design needs, growing, shrinking, or relabelling the stack, reduces to one instance of this single pop-then-push rule.

Move of a PDA

A single move applies $\delta(q, a, X)$, where $q$ is the current state, $a$ is either the next input symbol or $\epsilon$, and $X$ is the current stack top. If $(p, \alpha) \in \delta(q, a, X)$, the machine may consume $a$ from the input (or consume nothing, if $a = \epsilon$), replace $X$ on the stack by the string $\alpha$ (with the leftmost symbol of $\alpha$ becoming the new top), and move to state $p$. Because $\delta$ returns a set of pairs, a PDA is non-deterministic by default: several different moves may be available from the same configuration, and, as with an NFA, the machine accepts if some sequence of choices leads to acceptance.

Instantaneous Description for PDA

The full configuration of a PDA at any moment during a computation, its current state, the input remaining to be read, and the current stack contents, is captured by an instantaneous description (ID), a triple $(q, w, \gamma)$ where $q \in Q$, $w \in \Sigma^$ is the unread suffix of the input, and $\gamma \in \Gamma^$ is the stack contents (written top symbol first). A single move is written using the yields relation $\vdash$: $(q, aw, X\gamma) \vdash (p, w, \alpha\gamma)$ exactly when $(p,\alpha) \in \delta(q,a,X)$, meaning consuming $a$ (or $a=\epsilon$) takes the machine from state $q$ with $X$ on top of the stack to state $p$ with $X$ replaced by $\alpha$, the rest of the stack, $\gamma$, unaffected. The reflexive, transitive closure $\vdash^*$ describes an entire run as a chain of such single moves.

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 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 RulePDA 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$:

ProductionPDA 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$:

ProductionPDA 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.

Open this answer on its own page →

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]

  • Language: $L = {x^n y^n xy \mid n \geq 0}$ - Alphabet: $\Sigma = {x, y}$ - String to trace for acceptance: xxyy The problem asks to show acceptance of xxyy. Let us check whether "xxyy" is in $L$. A string in $L$ has the form $x^n y^n xy$, i.e. it always ends with the literal suffix "xy". - ...
Open this answer on its own page →

2078 · 5 marks

Define a Push Down Automata. Construct a PDA that accepts $L = {a^n b^n \mid n > 0}$. [5]

A Push Down Automata (PDA) is a finite automaton equipped with an additional stack memory. It is a 7-tuple defined as: $$M = (Q, \Sigma, \Gamma, \delta, q0, Z0, F)$$ Where: Component Description ------------------------ $Q$ Finite set of states $\Sigma$ Finite set of input symbols (input alphabet...

Open this answer on its own page →

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 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

  1. Push each $a$ onto the stack.
  2. On the first $b$, start popping $a$'s.
  3. On subsequent $b$'s, keep popping $a$'s.
  4. 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

StateInputStack TopPushNext 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$

StepStateRemaining InputStack (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$

StepStateRemaining InputStack (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

Stringa'sb'sResult
$aabb$22Accepted ($n=2$)
$aab$21Rejected

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.

Open this answer on its own page →

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]

PDA $P = ({p, q}, {0,1}, {Z, 0}, \delta, p, Z)$ accepting by empty stack. Transitions: - $\delta(p, 0, Z) = (p, 0Z)$ - $\delta(p, 0, 0) = (p, 00)$ - $\delta(p, 1, 0) = (p, \varepsilon)$ - $\delta(p, \varepsilon, Z) = (q, \varepsilon)$ (Stack alphabet includes both $Z$ and $0$ since $0$ is p...

Open this answer on its own page →

2080.1 · 5 marks

How is conversion of PDA to CFG done? Illustrate with example. [5]

Given a Pushdown Automaton (PDA) P = (Q, Σ, Γ, δ, q₀, Z₀, F), we can construct an equivalent Context-Free Grammar (CFG) G = (V, T, R, S). --- For each pair of states (p, q) in the PDA, we create a non-terminal symbol A[p,q], which represents: "All strings that can take the PDA from state p with s...

Open this answer on its own page →

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]

--- A Pushdown Automaton (PDA) is a 7-tuple: $$M = (Q, \Sigma, \Gamma, \delta, q0, Z0, F)$$ Where: Component Description ------------------------ $Q$ Finite set of states $\Sigma$ Finite set of input symbols (input alphabet) $\Gamma$ Finite set of stack symbols (stack alphabet) $\delta$ Transitio...

Open this answer on its own page →

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]

A Pushdown Automaton (PDA) is a 7-tuple: $$P = (Q, \Sigma, \Gamma, \delta, q0, Z0, F)$$ Where: - $Q$ = finite set of states - $\Sigma$ = input alphabet - $\Gamma$ = stack alphabet - $\delta$ = transition function:

Open this answer on its own page →

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]

A Pushdown Automaton (PDA) is a 7-tuple: M = (Q, Σ, Γ, δ, q₀, Z₀, F) --- Language: L = { w ∈ {0,1} 0(w) = 1(w) } Idea: Use the stack to track the difference between counts of 0s and 1s. - Push a symbol when one character is in excess. - Pop when the opposite character is read. - Accept when the s...

Open this answer on its own page →