6 Pushdown Automata
6 Pushdown Automata
𝑍1
𝑍1
𝑍0
Stack
𝑞0 , 0011, 𝑍0 ├ 𝑞0 , 011, 𝑍1 𝑍0
├ 𝑞0 , 11, 𝑍1 𝑍1 𝑍0
├ 𝑞1 , 11, 𝑍1 𝑍1 𝑍0
├ 𝑞1 , 1, 𝑍1 𝑍0
├ 𝑞1 , 𝜀, 𝑍0
├ 𝑞2 , 𝜀, 𝜀
Proof:
Accepting states are unnecessary
Let 𝑃𝑁 = 𝑄, Σ, Γ, 𝛿, 𝑞0 , 𝑍0 , ∅ when accepting by empty stack.
Then, 𝑃𝐹 = 𝑄 ∪ 𝑝0 , 𝑝𝑓 , Σ, Γ ∪ Χ0 , 𝛿𝐹 , 𝑝0 , 𝑋0 , 𝑝𝑓 ,
where…
Z0 Z0 Z0 Z0 Z0
… … … …
X0 X0 X0 X0 X0 X0 X0 X0
Simulate 𝑃𝑁
Push 𝑃𝑁 ’s
start symbol If 𝑃𝑁 empties its stack,
move to the final state
Jim Anderson (modified by Nathan Otterness) 17
Converting Final State → Empty Stack
Proof:
Let 𝑃𝐹 = 𝑄, Σ, Γ, 𝛿, 𝑞0 , 𝑍0 , 𝐹
Then, 𝑃𝑁 = 𝑄 ∪ 𝑝0 , 𝑝 , Σ, Γ ∪ Χ0 , 𝛿𝑁 , 𝑝0 , 𝑋0 , ∅ ,
where…
Z0 Z0 Z0 Z0 Z0
X0 X0 X0 … X0 … X0 … X0 … X0
c
a
consume c, a, a
a (one at a time)
(Empty)
Jim Anderson (modified by Nathan Otterness) 23
Formal Definition of a PDA for a CFG
∗
Inductive step: Assume 𝑞, 𝑤, 𝑆 ├ 𝑞, 𝑦𝑖 , 𝛼𝑖 by the
inductive hypothesis. We need to show that
∗ Reminder: After simulating
𝑞, 𝑤, 𝑆 ├ 𝑞, 𝑦𝑖+1 , 𝛼𝑖+1 . the derivation up to 𝛾𝑖 ,
𝑦𝑖 is the unconsumed input
and 𝛼𝑖 is the stack contents.
Let 𝑃 = 𝑄, Σ, Γ, 𝛿, 𝑞0 , 𝑍0 , ∅ .
Define 𝐺 = 𝑉, Σ, 𝑅, 𝑆 such that 𝑉 contains 𝑞𝑋𝑝 for
all 𝑞, 𝑝 ∈ 𝑄, and all 𝑋 ∈ Γ.
𝑅 is defined on the next slide…
❖ 𝑞0 𝑍0 𝑞1 → 0 𝑞0 𝑋𝑞0 𝑞0 𝑍0 𝑞1
❑ 𝑟1 = 𝑞0 and 𝑟2 = 𝑞1
❖ 𝑞0 𝑍0 𝑞0 → 0 𝑞0 𝑋𝑞1 𝑞1 𝑍0 𝑞0
❑ 𝑟1 = 𝑞1 and 𝑟2 = 𝑞0
❖ 𝑞0 𝑍0 𝑞1 → 0 𝑞0 𝑋𝑞1 𝑞1 𝑍0 𝑞1
❑ 𝑟1 = 𝑞1 and 𝑟2 = 𝑞1
Jim Anderson (modified by Nathan Otterness) 42
Converting PDA→CFG: Example
𝛿 𝑞0 , 0, 𝑍0 = 𝑞0 , 𝑋𝑍0
𝛿 𝑞0 , 0, 𝑋 = 𝑞0 , 𝑋𝑋
Production rules from 𝛿 𝑞0 , 0, 𝑍0 = 𝑞0 , 𝑋𝑍0 : 𝛿 𝑞0 , 1, 𝑋 = 𝑞1 , 𝜀
𝛿 𝑞1 , 1, 𝑋 = 𝑞1 , 𝜀
❖ 𝑞0 𝑍0 𝑞0 → 0 𝑞0 𝑋𝑞0 𝑞0 𝑍0 𝑞0 𝛿 𝑞1 , 𝜀, 𝑋 = 𝑞1 , 𝜀
❑ 𝑟1 = 𝑞0 and 𝑟2 = 𝑞0 𝛿 𝑞1 , 𝜀, 𝑍0 = 𝑞1 , 𝜀
❖ 𝑞0 𝑍0 𝑞1 → 0 𝑞0 𝑋𝑞1 𝑞1 𝑍0 𝑞1
❑ 𝑟1 = 𝑞1 and 𝑟2 = 𝑞1
Jim Anderson (modified by Nathan Otterness) 43
Converting PDA→CFG: Example
𝛿 𝑞0 , 0, 𝑍0 = 𝑞0 , 𝑋𝑍0
𝛿 𝑞0 , 0, 𝑋 = 𝑞0 , 𝑋𝑋
Production rules from 𝛿 𝑞0 , 0, 𝑋 = 𝑞0 , 𝑋𝑋 : 𝛿 𝑞0 , 1, 𝑋 = 𝑞1 , 𝜀
𝛿 𝑞1 , 1, 𝑋 = 𝑞1 , 𝜀
❖ 𝑞0 𝑋𝑞0 → 0 𝑞0 𝑋𝑞0 𝑞0 𝑋𝑞0 𝛿 𝑞1 , 𝜀, 𝑋 = 𝑞1 , 𝜀
❑ 𝑟1 = 𝑞0 and 𝑟2 = 𝑞0 𝛿 𝑞1 , 𝜀, 𝑍0 = 𝑞1 , 𝜀
Proof sketch:
A CFG is ambiguous if and only if multiple leftmost
derivations are possible.
If we apply the previous PDA→CFG construction to a
DPDA, we will end up with production rules where
multiple derivations of the same string aren’t possible.
Jim Anderson (modified by Nathan Otterness) 65
DPDAs and Ambiguous Grammars
Theorem 6.21: If 𝐿 = 𝐿 𝑃 for some DPDA 𝑃, then 𝐿
has an unambiguous CFG. If a string 𝑤 contained a
prefix in 𝐿$, 𝑤 and the prefix
would both need to end
with $, which is not possible
Proof sketch: under our definition of 𝐿$.