22 Pushdown Automata Equivalence
22 Pushdown Automata Equivalence
Equivalence
Athanasios Aris Panagopoulos
Section Summary
• Equivalence of PDAs and CFGs
• CGF to PDA conversion
• PDA to CFG conversion
CFLs and PDAs
Theorem: The class of languages accepted by PDA is exactly the class
of CFL
Proof:
1. Lemma I: {CFL} ⊆ {PDA}
2. Lemma II: {CFL} ⊇ {PDA}
Lemma I: {CFL} ⊆ {PDA}
Lemma I: {CFL} ⊆ {PDA}: Each CFL is accepted by (at least one) PDA
Proof: CFG → PDA (Show that we can always construct a
corresponding PDA for a CFG)
CFG → PDA
Let G = (V, S, R, S) be a context-free grammar; we can construct a pushdown
automaton M such that L(M) = L(G):
M = ({p, q}, Σ, V, ∆N ∪ ∆P ∪ ∆I, p, {q})
1. ∆N = {(p, e, e), (q, S))} CFG PDA
2. ∆P = {(q, e, A), (q, x)) : (A → x) ∈ R}
3. ∆I = {((q, α, α), (q, e)) : α ∈ Σ}
• The machine we construct has only two states, p and q, and remains
permanently in state q after its first move.
• Also, M uses V, the set of terminals and nonterminal, as its stack alphabet.
➢ We have three rule types.
CFG → PDA
We have three rule types:
1. ∆N = {(p, e, e), (q, S))}
Begin by pushing S to the stack.
2. ∆P = {(q, e, A), (q, x)) : (A → x) ∈ R}
Replace the topmost symbol A by x.
3. ∆I = {((q, α, α), (q, e)) : α ∈ Σ}
Compare the topmost symbol from the stack with the input symbol
simulation of a leftmost derivation.
Example: CFG → PDA
Create a PDA for L(G) = {wcwR: w ∈ {α, b}∗}:
αbααbbααbα
Example: CFG → PDA
Solution: State Transition Unread Input Stack
p - αbααbbααbα e
q ∆1 αbααbbααbα S
q ∆2 αbααbbααbα S1αS1αS1
q ∆3 αbααbbααbα αS1bαS1αS1
q ∆7 bααbbααbα S1bαS1αS1
q ∆6 bααbbααbα bαS1αS1
q ∆8 ααbbααbα αS1αS1
∆7 αbbααbα α1αS1
q ∆5 αbbααbα S1S1αS1
q ∆3 αbbααbα αS1bS1αS1
q ∆7 bbααbα S1bS1αS1
q ∆6 bbααbα bS1αS1
…
…
…
Example: CFG → PDA
Solution:
…
…
…
State Transition Unread Input Stack
q ∆8 bααbα S1αS1
q ∆4 bααbα bS1ααS1
q ∆8 ααbα S1ααS1
q ∆6 ααbα ααS1
q ∆7 αbα αS1
q ∆7 bα S1
q ∆4 bα bS1α
q ∆8 α S1 α
q ∆6 α α
q ∆7 e e
Example: CFG → PDA
Construct a PDA for L = {wcu: w, u ∈ {α, b}* and u contains wR as a substring} and
the G = (V, Σ, R, S), where: V = {S, W, X, α, b, c}, Σ = {α, b, c} and
R={
• S → WX
• W → αWα
• W → bWb
• W → cX
• X → αX | bX | ε
}
and show the recognition of αbcαbαb (transition table) step by step.
Example: CFG → PDA State
p
Transition
-
Unread Input
αbcαbαb
Stack
e
q ∆1 αbcαbαb S
q ∆2 αbcαbαb WX
Solution:
q ∆3 αbcαbαb αWαX
M = (K, Σ, Γ, ∆, p, F) where:
q ∆9 bcαbαb WαX
K = {p, q}, Σ = {α, b, c}, Γ = {S, W, α, b, c}, F = {q}
q ∆4 bcαbαb bWbαX
Δ={ q ∆10 cαbαb WbαX
∆1:( (p, e, e), (q, S) ) ∆N q ∆5 cαbαb cXbαX
∆1:( (q, e, S), (q, WX) )
∆1:( (q, e, W), (q, αWα) ) q ∆11 αbαb XbαX
∆1:( (q, e, W), (q, bWb) ) q ∆6 αbαb αXbαX
∆1:( (q, e, W), (q, cX) ) ∆P q ∆9 bαb XbαX
∆1:( (q, e, X), (q, αX) ) q ∆8 bαb bαX
∆1:( (q, e, X), (q, bX) )
q ∆10 αb αX
∆1:( (q, e, X), (q, e)
∆1:( (q, α, α), (q, e) q ∆9 b X
∆I
∆1:( (q, b, b), (q, e) q ∆7 b bX
∆1:( (q, c, c), (q, e) q ∆10 e X
} q ∆8 e e
CFG → PDA
Claim: L(M) = L(G)
• Consider w ∈ Σ∗ and α ∈ (V − S)V∗ ∪ {e}
• then S ⇒∗𝐺 wα (q, w, S) ⊢𝑀 ∗
(q, e, α)
Proof
• ⇒ : induction on the length of the leftmost derivation
• ⇐ : induction on the number of transitions of type ∆P
➢ Beyond the scope of this class ☺
CFLs and PDAs
Theorem: The class of languages accepted by PDA is exactly the class
of CFL
Proof:
1. Lemma I: {CFL} ⊆ {PDA}
2. Lemma II: {CFL} ⊇ {PDA}
Lemma II: {CFL} ⊇ {PDA}
Lemma II: {CFL} ⊇ {PDA}: The language accepted by a PDA is a CFL
Proof: CFG PDA
(Show that we can always construct a corresponding CFG for a PDA)
CFG PDA
It is difficult to go directly so we will use a simplified PDA
CFG PDA
Simplified
PDA
Simplified PDA
A simplified PDA:
1. Has a single accepting state
2. Must empty its stack before accepting
3. Each transition of the pushdown automaton must either push to or
pop from the stack, but not both simultaneously
e, $ → e
q0 → q0 q0’
α, bb → c α, bb → e e, e → c
q0 q1 → q0 q01' q1
α, e → e α, e → b e, b → e
q0 q1 → q0 q01' q1
If we have an epsilon transition that neither pushes nor pops, then we replace it with two “dummy”
transitions that push and then immediately pop the same stack symbol.
Simplified PDA
A simplified PDA:
1. Has a single accepting state
2. Must empty its stack before accepting
3. Each transition of the pushdown automaton must either push to or
pop from the stack, but not both simultaneously
4. We push or pop only one symbol at a time
α, bbαS → e e, b → e e, b → e e, α → e e, S → e
q0 q1 → q0 r1 r2 r3 q1
e, e → b e, e → b b, e → b
b, e→ bbb
q0 q1 → q0 r1 r2 q1
Simplified PDA → CFG
Given a simplified PDA we can get a corresponding CFG using the
following rules:
PDA CFG
p … q S → Apq
q Αqq → e
α, e → X
p r
Αpq → αΑrsb
Note that X is pushed and popped
s q (α=e or b=e is also allowed)
b, X → e
p r q Αpq → ΑprΑrq
Example: PDA → CFG
Identify the corresponding CFG from the following PDA:
α, e → X
α, Y → e
b, e → Y
b, X → e
e, Z → e
e, e → Z
q1 q2
Example: PDA → CFG
Solution: Step I
First, we convert the PDA to a simplified one:
α, e → X
b, e → Y
α, Υ → e
b, Χ → e
e, Z → e
e, e → $ e, e → Z e, $ → e
q0 q1 q2 q3
Example: PDA → CFG
S: Αpq → αΑrsb: Aij → AikAjk:
• S → A03 • A03 → A12 A00 → A00A00 | A01A10 | A02A20 | A03A30
• A12 → A22 A01 → A00A01 | A01A11 | A02A21 | A03A31
Aii → e: A02 → A00A02 | A01A12 | A02A22 | A03A32
• A22 → αA22b
A03 → A00A03 | A01A13 | A02A23 | A03A33
• A00 → e • A22 → bA22α ...
• A11 → e A30 → A30A00 | A31A10 | A32A20 | A33A30
• A22 → e A31 → A30A01 | A31A11 | A32A21 | A33A31
• A33 → e A32 → A30A02 | A31A12 | A32A22 | A33A32
A33 → A30A03 | A31A13 | A32A23 | A33A33
43=64 rules (…)
Example: PDA → CFG
Extract the corresponding CFG for the following PDA:
0, e → 0
0, 1 → e
1, e → 1
1, 0 → e
q1
Example: PDA → CFG
Solution:
0, e → 0 S: Αpq → αΑrsb:
0, 1 → e
1, e → 1 • S → A02 • A11 → 0A111
1, 0 → e • A11 → 1A110
e, e → $ e, $ → e App → e: • A02 → A11
q0 q1 q2
• A00 → e
Aij → AikA jk :
• A11 → e
• A02 → A01A12
• A22 → e
• A01 → A01A11
• A12 → A11A12
• A11 → A11A11
Example: PDA → CFG
Solution: The produced CFG (after eliminating unreachable non-terminals) will be:
G = (V, Σ ,R, S), where V = {S, A02, A11}, Σ = {0, 1},
R ={ • S →A
02
• A02 → A11
• A11 → A11A11
• A11 → 0A111
• A11 → 1A110
• A11 → e
}