CSC262 · 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
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]
Quick answer
This pushes the start symbol $S$ onto the stack and moves to the working state $q_1$.
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)}$$
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.
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.