0% found this document useful (0 votes)
152 views

6 Pushdown Automata

This document discusses pushdown automata (PDAs). It defines a PDA as having a finite state control, input alphabet, stack alphabet, transition function, start state, initial stack symbol, and set of accepting states. PDAs operate like finite automata but with an additional stack. The document provides examples of PDAs and explains how they are executed through instantaneous descriptions. It also discusses how to define the language of a PDA and how to convert between PDAs that accept by empty stack versus final state.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
152 views

6 Pushdown Automata

This document discusses pushdown automata (PDAs). It defines a PDA as having a finite state control, input alphabet, stack alphabet, transition function, start state, initial stack symbol, and set of accepting states. PDAs operate like finite automata but with an additional stack. The document provides examples of PDAs and explains how they are executed through instantaneous descriptions. It also discusses how to define the language of a PDA and how to convert between PDAs that accept by empty stack versus final state.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 67

Pushdown Automata

COMP 455 – 002, Spring 2019

Jim Anderson (modified by Nathan Otterness) 1


Pushdown Automata (PDAs)
 A pushdown automaton (PDA) is essentially a finite
automaton with a stack.
 Example PDA accepting 𝐿 = 0𝑛 1𝑛 | 𝑛 ≥ 0 :

► Initially, the symbol 𝑍0 is on


the stack.
► Acceptance can be by final
state or empty stack.

Jim Anderson (modified by Nathan Otterness) 2


Example PDA Execution

𝑍1
𝑍1
𝑍0
Stack

Input string: 0011


Current input

Jim Anderson (modified by Nathan Otterness) 3


Formal Definition of a PDA
 A PDA can be defined by a 7-tuple 𝑄, Σ, Γ, 𝛿, 𝑞0 , 𝑍0 , 𝐹 .
❖ 𝑄: A finite set of states
Takes a state, an input symbol
❖ Σ: The input alphabet or 𝜀, and a stack symbol.

❖ Γ: The stack alphabet


𝑄×Γ∗
❖ 𝛿: The transition function: 𝑄 × Σ ∪ 𝜀 ×Γ→ 2
❖ 𝑞0 : The start state
Returns a set of (state,
❖ 𝑍0 ∈ Γ: The initial stack symbol stack symbol string) pairs.
❖ 𝐹: The set of accepting states
Jim Anderson (modified by Nathan Otterness) 4
Moves in a PDA
𝛿 𝑞, 𝑎, 𝑍 = { 𝑝1 , 𝛾1 , … , 𝑝𝑚 , 𝛾𝑚 ≡
If:
 The current state is 𝑞, and
 The current input symbol is 𝑎, and
 The symbol 𝑍 is on the top of the stack

Jim Anderson (modified by Nathan Otterness) 5


Moves in a PDA
𝛿 𝑞, 𝑎, 𝑍 = { 𝑝1 , 𝛾1 ), … , 𝑝𝑚 , 𝛾𝑚 ≡
If the current state is 𝑞, the current input symbol is 𝑎,
and the symbol 𝑍 is on the top of the stack,
Then choose a value 𝑖, (nondeterministic!)
 Enter state 𝑝𝑖 ,
 Replace the symbol on top of the stack with 𝛾𝑖 , and
 Advance to the next input symbol.

Jim Anderson (modified by Nathan Otterness) 6


𝜀-Transitions in a PDA
𝛿 𝑞, 𝜀, 𝑍 = { 𝑝1 , 𝛾1 , … , 𝑝𝑚 , 𝛾𝑚 ≡
 The rules here are identical for the states and the
stack, but no input is consumed.
❖ This adds further nondeterminism.

Jim Anderson (modified by Nathan Otterness) 7


Example: PDA Move Notation
 𝑀= 𝑞0 , 𝑞1 , 𝑞2 , 0, 1 , 𝑍0 , 𝑍1 , 𝛿, 𝑞0 , 𝑍0 , 𝑞2
 𝛿 𝑞0 , 0, 𝑍0 = 𝑞0 , 𝑍1 𝑍0
 𝛿 𝑞0 , 0, 𝑍1 = 𝑞0 , 𝑍1 𝑍1
 𝛿 𝑞0 , 𝜀, 𝑍0 = 𝑞2 , 𝜀 , 𝑞1 , 𝑍0
 𝛿 𝑞0 , 𝜀, 𝑍1 = 𝑞1 , 𝑍1
 𝛿 𝑞1 , 1, 𝑍1 = 𝑞1 , 𝜀
 𝛿 𝑞1 , 𝜀, 𝑍0 = 𝑞2 , 𝜀

Jim Anderson (modified by Nathan Otterness) 8


Instantaneous Description
 An instantaneous description (ID) describes the
“global state” of a PDA.
 An instantaneous description is given by 𝑞, 𝑤, 𝛾 :
❖ 𝑞: The current state
❖ 𝑤: The remaining input
❖ 𝛾: The current contents of the stack
 We write 𝑞, 𝑎𝑤, 𝑍𝛼 ├ 𝑝, 𝑤, 𝛽𝛼 if 𝛿 𝑞, 𝑎, 𝑍
𝑀
contains 𝑝, 𝛽 .
𝑖 ∗ 𝑖 ∗
 We can also write ├, ├, ├, ├, ├.
𝑀 𝑀
Jim Anderson (modified by Nathan Otterness) 9
Instantaneous Description Example

𝑞0 , 0011, 𝑍0 ├ 𝑞0 , 011, 𝑍1 𝑍0
├ 𝑞0 , 11, 𝑍1 𝑍1 𝑍0
├ 𝑞1 , 11, 𝑍1 𝑍1 𝑍0
├ 𝑞1 , 1, 𝑍1 𝑍0
├ 𝑞1 , 𝜀, 𝑍0
├ 𝑞2 , 𝜀, 𝜀

Jim Anderson (modified by Nathan Otterness) 10


Formalizing Graphical Notation

Current symbol on What to replace the


top of the stack. top symbol with.
Jim Anderson (modified by Nathan Otterness) 11
The Language of a PDA
Let 𝑀 be a PDA: 𝑀 = 𝑄, Σ, Γ, 𝛿, 𝑞0 , 𝑍0 , 𝐹
𝑀 defines two (possibly different) languages:
 The language accepted by final state:

❖𝐿 𝑀 ≡ 𝑤 | 𝑞0 , 𝑤, 𝑍0 ├ 𝑝, 𝜀, 𝛾 , 𝑝 ∈ 𝐹, 𝛾 ∈ Γ ∗
 The language accepted by empty stack:

❖𝑁 𝑀 ≡ 𝑤 | 𝑞0 , 𝑤, 𝑍0 ├ 𝑝, 𝜀, 𝜀 , 𝑝 ∈ 𝑄

Jim Anderson (modified by Nathan Otterness) 12


More PDA Examples
 𝐿 = 𝑥𝑥 𝑅 | 𝑥 ∈ 𝟎 + 𝟏 ∗

Jim Anderson (modified by Nathan Otterness) 13


More PDA Examples
 𝐿 is the language consisting of strings with an
equal number of 0s and 1s.

This accepts by empty stack. (You


could also convert it to accept by
final state, instead.)

Jim Anderson (modified by Nathan Otterness) 14


Converting Empty Stack → Final State

Theorem 6.9: If 𝐿 = 𝑁(𝑃𝑁 ) for some PDA 𝑃𝑁 , then


𝐿 = 𝐿(𝑃𝐹 ) for some PDA 𝑃𝐹 .
• 𝑃𝑁 : Accepts by empty stack
• 𝑃𝐹 : Accepts by final state

Proof:
Accepting states are unnecessary
 Let 𝑃𝑁 = 𝑄, Σ, Γ, 𝛿, 𝑞0 , 𝑍0 , ∅ when accepting by empty stack.

 Then, 𝑃𝐹 = 𝑄 ∪ 𝑝0 , 𝑝𝑓 , Σ, Γ ∪ Χ0 , 𝛿𝐹 , 𝑝0 , 𝑋0 , 𝑝𝑓 ,
where…

Jim Anderson (modified by Nathan Otterness) 15


Converting Empty Stack → Final State
𝑃𝑁 = 𝑄, Σ, Γ, 𝛿, 𝑞0 , 𝑍0 , ∅
 𝛿𝐹 𝑝0 , 𝜀, Χ0 = 𝑞0 , 𝑍0 𝑋0 𝑃𝐹 = 𝑄 ∪ 𝑝0 , 𝑝𝑓 , Σ, Γ ∪ Χ0 , 𝛿𝐹 , 𝑝0 , Χ0 , 𝑝𝑓

❖ 𝑃𝐹 starts by pushing 𝑃𝑁 ’s start symbol onto the


stack. 𝑃𝐹 will find its own start symbol (𝑋0 ) on
top of the stack only when 𝑃𝑁 ’s stack is empty.
 For all 𝑞 ∈ 𝑄, 𝑎 ∈ Σ ∪ 𝜀 , and Υ ∈ Γ, 𝛿𝐹 𝑞, 𝑎, Υ
contains all pairs in 𝛿𝑁 𝑞, 𝑎, Υ .
❖ 𝑃𝐹 just simulates 𝑃𝑁 after initializing the stack.
 For all 𝑞 ∈ 𝑄, 𝛿𝐹 𝑞, 𝜀, 𝑋0 contains 𝑝𝑓 , 𝜀 .
❖ 𝑃𝐹 can accept by final state whenever 𝑃𝑁 would
by empty stack.
Jim Anderson (modified by Nathan Otterness) 16
Converting Empty Stack → Final State

 The behavior of 𝑃𝐹 (just showing the stack):

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

Theorem 6.11: If 𝐿 = 𝐿 𝑃𝐹 for some PDA 𝑃𝐹 , then


𝐿 = 𝑁 𝑃𝑁 for some PDA 𝑃𝑁 . • 𝑃𝐹 : Accepts by final state
• 𝑃𝑁 : Accepts by empty stack

Proof:
 Let 𝑃𝐹 = 𝑄, Σ, Γ, 𝛿, 𝑞0 , 𝑍0 , 𝐹
 Then, 𝑃𝑁 = 𝑄 ∪ 𝑝0 , 𝑝 , Σ, Γ ∪ Χ0 , 𝛿𝑁 , 𝑝0 , 𝑋0 , ∅ ,
where…

Jim Anderson (modified by Nathan Otterness) 18


Converting Final State → Empty Stack
𝑃𝐹 = 𝑄, Σ, Γ, 𝛿, 𝑞0 , 𝑍0 , 𝐹
 𝛿𝑁 𝑝0 , 𝜀, 𝑋0 = 𝑞0 , 𝑍0 𝑋0 . 𝑃𝑁 = 𝑄 ∪ 𝑝0 , 𝑝 , Σ, Γ ∪ Χ0 , 𝛿𝑁 , 𝑝0 , Χ0 , ∅
❖ 𝑃𝑁 starts by pushing 𝑃𝐹 ’s start symbol onto the stack. 𝑋0
ensures that 𝑃𝑁 doesn’t prematurely clear the stack.
 For all 𝑞 ∈ 𝑄, 𝑎 ∈ Σ ∪ {𝜀}, and Υ ∈ Γ, 𝛿𝐹 𝑞, 𝑎, Υ contains all
pairs in 𝛿𝑁 𝑞, 𝑎, Υ .
❖ 𝑃𝑁 simulates 𝑃𝐹 after initializing the stack.
 For all 𝑞 ∈ 𝐹, Υ ∈ Γ ∪ 𝑋0 , 𝛿𝑁 𝑞, 𝜀, Υ contains 𝑝, 𝜀 .
❖ If 𝑃𝐹 can accept by final state, 𝑃𝑁 can begin emptying the
stack.
 For all Υ ∈ Γ ∪ 𝑋0 , 𝛿𝑁 𝑝, 𝜀, Υ = 𝑝, 𝜀 .
❖ 𝑃𝑁 continues to empty its stack until it is completely
empty.
Jim Anderson (modified by Nathan Otterness) 19
Converting Final State → Empty Stack

 The behavior of 𝑃𝑁 (just showing the stack):

Z0 Z0 Z0 Z0 Z0
X0 X0 X0 … X0 … X0 … X0 … X0

Push 𝑃𝐹 ’s start Simulate 𝑃𝐹


symbol on the If 𝑃𝐹 enters a final state,
stack. go to state 𝑝 and empty
the stack.
Jim Anderson (modified by Nathan Otterness) 20
Converting CFGs to PDAs
Given a CFG 𝐺, we will construct a PDA that simulates leftmost
derivations in 𝐺.
Main ideas:
 Each left-sentential form of 𝐺 is of the form 𝑥𝐴𝛼, where:
❖ 𝑥 is all terminals,
❖ 𝐴 is the variable that will be replaced in the next derivation
step, and
❖ 𝛼 is some string that can include both variables and
terminals.
 If you look at the “state” of the PDA at this point,
❖ 𝐴𝛼 will be on its stack, and
❖ input 𝑥 will have already been consumed.
Jim Anderson (modified by Nathan Otterness) 21
Converting CFGs to PDAs
Apply the production 𝐴 → 𝑦𝐶𝛽 like this:

y Pop off terminals in 𝑦


C while consuming C
corresponding input
A  symbols 
  

Replace 𝐴 (on top of the stack)


with all of the terminals and
variables it produces.

Jim Anderson (modified by Nathan Otterness) 22


Example PDA Execution for a CFG
Productions: PDA transition function:
• 𝑆 → 𝑆𝑆 𝜀 𝑎𝑆𝑎 | 𝑏𝐴 • 𝛿 𝑞, 𝜀, 𝑆 = 𝑞, 𝑆𝑆 , 𝑞, 𝜀 , 𝑞, 𝑎𝑆𝑎 , 𝑞, 𝑏𝐴
• 𝐴→𝑐 • 𝛿 𝑞, 𝜀, 𝐴 = 𝑞, 𝑐
• 𝛿 𝑞, 𝑑, 𝑑 = 𝑞, 𝜀 , where 𝑑 ∈ 𝑎, 𝑏, 𝑐
A LM derivation: 𝑆 ⇒ 𝑎𝑆𝑎 ⇒ 𝑎𝑎𝑆𝑎𝑎 ⇒ 𝑎𝑎𝑏𝐴𝑎𝑎 ⇒ 𝑎𝑎𝑏𝑐𝑎𝑎.
The PDA’s stack when processing the input 𝑎𝑎𝑏𝑐𝑎𝑎:
a b
a S S A A
S S a a a a
consume consume consume
S a a a a a a a b a

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

Define a PDA 𝑃 accepting the language defined by


CFG 𝐺:
 𝑃 = 𝑞 , 𝑇, 𝑉 ∪ 𝑇, 𝛿, 𝑞, 𝑆, ∅ Reminder:
• PDA definition: 𝑄, Σ, Γ, 𝛿, 𝑞0 , 𝑍0 , 𝐹
 𝛿 is defined by: • CFG definition: 𝑉, 𝑇, 𝑆, 𝑃
❖ Rule 1: For each variable 𝐴:
𝛿 𝑞, 𝜀, 𝐴 = 𝑞, 𝛽 | 𝐴 → 𝛽 is a production
❖ Rule 2: For each terminal 𝑎:
𝛿 𝑞, 𝑎, 𝑎 = 𝑞, 𝜀
 Note: this means that any CFG can be accepted by
a PDA (by empty stack) with only one state.
Jim Anderson (modified by Nathan Otterness) 24
Proving CFG → PDA Correctness
Theorem 6.13: 𝑁 𝑃 = 𝐿 𝐺 , where 𝑃 and 𝐺 are
defined as before. Shorthand for the language
of 𝑃 accepted by empty stack.

Proof: We will show that 𝑤 is in 𝑁 𝑃 if and only if it


is in 𝐿 𝐺 .
“If”: If 𝑤 is in 𝐿 𝐺 then it has a LM derivation
𝑆 = 𝛾1 𝛾2 … 𝛾𝑛 = 𝑤.
𝑙𝑚 𝑙𝑚 𝑙𝑚
We will show by induction on 𝑖 that

𝑞, 𝑤, 𝑆 ├ 𝑞, 𝑦𝑖 , 𝛼𝑖 , where 𝛾𝑖 = 𝑥𝑖 𝛼𝑖 , and 𝑥𝑖 𝑦𝑖 = 𝑤.

Jim Anderson (modified by Nathan Otterness) 25


Proving CFG → PDA Correctness
Base case: 𝑖 = 1, so 𝛾1 = 𝑆. This means 𝑥1 = 𝜀, 𝑦1 = 𝑤,

and 𝛼1 = 𝑆. So, 𝑞, 𝑤, 𝑆 ├ 𝑞1 , 𝑦1 , 𝛼1 .


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.

We need to show that 𝑃 can make moves that simulate


the next step.
Jim Anderson (modified by Nathan Otterness) 26
Proving CFG → PDA Correctness
This follows from the definition of 𝛿:
 𝛼𝑖 is of the form 𝐴 …, where 𝐴 is a variable.
 The derivation step 𝛾𝑖 ⇒ 𝛾𝑖+1 involves replacing 𝐴
by some string 𝛽.
 By Rule 1 in the definition of 𝛿, we can replace 𝐴
by 𝛽 on top of the stack.
 By Rule 2 in the definition of 𝛿, any leading
terminals in 𝛽 can be popped off the stack and the
corresponding input consumed.

Jim Anderson (modified by Nathan Otterness) 27


Proving CFG → PDA Correctness
“Only if”: We want to prove that if 𝑤 is in 𝑁 𝑀 , then 𝑤 is
in 𝐿 𝐺 . To do so, we prove something more general:
∗ ∗
Claim: If 𝑞, 𝑥, 𝐴 ├ 𝑞, 𝜀, 𝜀 , then 𝐴 ⇒ 𝑥.
𝐺
In words: Consider running 𝑃 when 𝐴 is on top of the stack,
and continue running until 𝐴 (and anything that replaced it)
has been popped off the stack. If 𝑥 is the string of input
symbols consumed while doing this, then 𝑥 is derivable
from 𝐴.

Example: From the example PDA, 𝑞, 𝑎𝑏𝑐𝑎, 𝑆 ├ 𝑞, 𝜀, 𝜀 and

𝑆 ⇒ 𝑎𝑏𝑐𝑎.
𝐺
Jim Anderson (modified by Nathan Otterness) 28
Proving CFG → PDA Correctness
Revisiting the example from before: Productions:
• 𝑆 → 𝑆𝑆 𝜀 𝑎𝑆𝑎 | 𝑏𝐴
∗ ∗ • 𝐴→𝑐
𝑞, 𝑎𝑏𝑐𝑎, 𝑆 ├ 𝑞, 𝜀, 𝜀 and 𝑆 ⇒ 𝑎𝑏𝑐𝑎
𝐺
We can see the derivation for

𝑆 ⇒ 𝑎𝑏𝑐𝑎 starting here, where
𝐺
𝑆 is on top of the stack. a b
a S S A A
consume consume
S S a a a a b a
consume
S a a a a a a a

c Here, 𝑆 and everything that


replaced it has been popped
a a off, and 𝑎𝑏𝑐𝑎 was consumed.
a consume 𝑐 a consume 𝑎 a …
Jim Anderson (modified by Nathan Otterness) 29
Proving CFG → PDA Correctness
∗ ∗
Claim: If 𝑞, 𝑥, 𝐴 ├ 𝑞, 𝜀, 𝜀 , then 𝐴 ⇒ 𝑥.
𝐺
We will prove this by induction on the number of
moves made by 𝑃.
Base case: 𝑃 makes one move. 𝐴 can be popped off
the stack directly only if 𝐴 → 𝜀 is a production (in

which case 𝑥 = 𝜀). In this case, the claim that 𝐴 ⇒ 𝜀
𝐺
follows.

Jim Anderson (modified by Nathan Otterness) 30


Proving CFG → PDA Correctness
Claim:
∗ ∗
∗ ∗ 𝑞, 𝑥, 𝐴 ├ 𝑞, 𝜀, 𝜀 , then 𝐴 ⇒ 𝑥
Claim: If 𝑞, 𝑥, 𝐴 ├ 𝑞, 𝜀, 𝜀 , then 𝐴 ⇒ 𝑥. 𝐺
𝐺
Inductive step: Consider 𝑛 moves, and assume that
the claim is true for fewer than 𝑛 moves.
The first move must be defined because of a
production of the form 𝐴 → 𝑌1 𝑌2 … 𝑌𝑘 , where each 𝑌𝑖
is either a variable or terminal.
If 𝒀𝟏 is a terminal symbol, then the only thing that 𝑃
can do is pop it off the stack and consume the
corresponding symbol in the input.
Jim Anderson (modified by Nathan Otterness) 31
Proving CFG → PDA Correctness
Claim:
∗ ∗
Inductive step, continued: 𝑞, 𝑥, 𝐴 ├ 𝑞, 𝜀, 𝜀 , then 𝐴 ⇒ 𝑥
𝐺

The first move must be defined because of a


production of the form 𝐴 → 𝑌1 𝑌2 … 𝑌𝑘 , where each 𝑌𝑖
is either a variable or terminal.
If 𝐘𝟏 is a variable, then consider the behavior of 𝑃
until 𝑌1 , or whatever ends up replacing it, is erased
from the stack. If 𝑦1 is the string of input symbols
consumed while doing this, then by the inductive

hypothesis 𝑌1 ⇒ 𝑦1 .
In turn, this argument can be applied to 𝑌2 … 𝑌𝑘 .
Jim Anderson (modified by Nathan Otterness) 32
Proving CFG → PDA Correctness
Claim:
∗ ∗
𝑞, 𝑥, 𝐴 ├ 𝑞, 𝜀, 𝜀 , then 𝐴 ⇒ 𝑥
𝐺

From the previous slide, it follows that 𝑥 = 𝑥1 𝑥2 … 𝑥𝑘 ,


where 𝑥𝑖 = 𝑌𝑖 if 𝑌𝑖 is a terminal and 𝑥𝑖 = 𝑦𝑖 if 𝑌𝑖 is a
variable. By concatenating these various derivations,

we have 𝐴 ⇒ 𝑥.

Jim Anderson (modified by Nathan Otterness) 33


Converting PDAs to CFGs
Theorem 6.14: If 𝐿 = 𝑁 𝑃 for some PDA 𝑃, then
𝐿 = 𝐿 𝐺 for some CFG 𝐺.
These symbols will end up generating all
strings that cause 𝑋 to be popped from
Proof: the stack while moving from state 𝑞 to 𝑝.

Let 𝑃 = 𝑄, Σ, Γ, 𝛿, 𝑞0 , 𝑍0 , ∅ .
Define 𝐺 = 𝑉, Σ, 𝑅, 𝑆 such that 𝑉 contains 𝑞𝑋𝑝 for
all 𝑞, 𝑝 ∈ 𝑄, and all 𝑋 ∈ Γ.
𝑅 is defined on the next slide…

Jim Anderson (modified by Nathan Otterness) 34


Converting PDAs to CFGs
𝑅 is defined as follows:
 𝑆 → 𝑞0 𝑍0 𝑝 , for all 𝑝 ∈ 𝑄.
 If 𝛿 𝑞, 𝑎, 𝑋 contains 𝑟, 𝑌1 𝑌2 … 𝑌𝑘 , then 𝑅 contains the
rule 𝑞𝑋𝑟𝑘 → 𝑎 𝑟𝑌1 𝑟1 𝑟1 𝑌2 𝑟2 … 𝑟𝑘−1 𝑌𝑘 𝑟𝑘 .
❖ 𝑎 is an input symbol or 𝜀
❖ 𝑋 and each 𝑌𝑖 are stack symbols
❖ 𝑟1 , 𝑟2 , … 𝑟𝑘 can be any list of states, so we need to
add a production rule for all possible lists of 𝑘 states.
 If 𝛿 𝑞, 𝑎, 𝑋 contains 𝑟, 𝜀 , then 𝑅 contains the
rule 𝑞𝑋𝑟 → 𝑎.
Jim Anderson (modified by Nathan Otterness) 35
Intuition Behind PDA→CFG Method
A leftmost derivation in 𝐺 of a string 𝑤 simulates 𝑃
with 𝑤 as an input.
∗ ∗
𝑆 ⇒ 00 𝑞0 𝑋𝑞1 𝑞1 𝑋𝑞1 [𝑞1 𝑍0 𝑞1 ] ⇒ 00011
𝑙𝑚 𝑙𝑚
(This comes from the example in the following slides).

Jim Anderson (modified by Nathan Otterness) 36


Intuition Behind PDA→CFG Method
A leftmost derivation in 𝐺 of a string 𝑤 simulates 𝑃
with 𝑤 as an input.
∗ ∗
𝑆 ⇒ 00 𝑞0 𝑋𝑞1 𝑞1 𝑋𝑞1 [𝑞1 𝑍0 𝑞1 ] ⇒ 00011
𝑙𝑚 𝑙𝑚

The input The state 𝑃 will be in after


𝑃’s current state 𝑋 or the symbols that
consumed by 𝑃
replace 𝑋 have been
erased from the stack.

Jim Anderson (modified by Nathan Otterness) 37


Intuition Behind PDA→CFG Method
A leftmost derivation in 𝐺 of a string 𝑤 simulates 𝑃
with 𝑤 as an input.
∗ ∗
𝑆 ⇒ 00 𝑞0 𝑋𝑞1 𝑞1 𝑋𝑞1 [𝑞1 𝑍0 𝑞1 ] ⇒ 00011
𝑙𝑚 𝑙𝑚

𝑃’s current stack:


𝑋𝑋𝑍0

Jim Anderson (modified by Nathan Otterness) 38


Converting PDA→CFG: Example
 We will convert the following PDA 𝑃 to a CFG.
 𝑁 𝑃 = 0𝑖 1𝑗 | 𝑖 ≥ 𝑗 ≥ 1 .
❖𝑃 = q0 , q1 , 0, 1 , 𝑋, 𝑍0 , 𝛿, 𝑞0 , 𝑍0 , ∅
❖𝛿 𝑞0 , 0, 𝑍0 = 𝑞0 , 𝑋𝑍0
❖𝛿 𝑞0 , 0, 𝑋 = 𝑞0 , 𝑋𝑋
❖𝛿 𝑞0 , 1, 𝑋 = 𝑞1 , 𝜀
❖𝛿 𝑞1 , 1, 𝑋 = 𝑞1 , 𝜀
❖𝛿 𝑞1 , 𝜀, 𝑋 = 𝑞1 , 𝜀
❖𝛿 𝑞1 , 𝜀, 𝑍0 = 𝑞1 , 𝜀
Jim Anderson (modified by Nathan Otterness) 39
Converting PDA→CFG: Example
The variables in the CFG created from this example
PDA will be:
𝑉 = { 𝑞0 𝑋𝑞0 , 𝑞0 𝑋𝑞1 , 𝑞1 𝑋𝑞0 , 𝑞1 𝑋𝑞1 ,
𝑞0 𝑍0 𝑞0 , 𝑞0 𝑍0 𝑞1 , 𝑞1 𝑍0 𝑞0 , 𝑞1 𝑍0 𝑞1 }

Jim Anderson (modified by Nathan Otterness) 40


Converting PDA→CFG: Example
𝛿 𝑞0 , 0, 𝑍0 = 𝑞0 , 𝑋𝑍0
𝛿 𝑞0 , 0, 𝑋 = 𝑞0 , 𝑋𝑋
 Production rules from the start state 𝑆: 𝛿 𝑞0 , 1, 𝑋 = 𝑞1 , 𝜀
𝛿 𝑞1 , 1, 𝑋 = 𝑞1 , 𝜀
❖𝑆 → 𝑞0 𝑍0 𝑞0 𝛿 𝑞1 , 𝜀, 𝑋 = 𝑞1 , 𝜀
𝛿 𝑞1 , 𝜀, 𝑍0 = 𝑞1 , 𝜀
❖𝑆 → 𝑞0 𝑍0 𝑞1

Jim Anderson (modified by Nathan Otterness) 41


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 𝑋𝑞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 𝑋𝑞0 𝑞0 𝑍0 𝑞1 These four rules are due to the


❑ 𝑟1 = 𝑞0 and 𝑟2 = 𝑞1 number of possible lists of 𝑟1 , … , 𝑟𝑘 .
In this case 𝑘 = 2, and we have
❖ 𝑞0 𝑍0 𝑞0 → 0 𝑞0 𝑋𝑞1 𝑞1 𝑍0 𝑞0 two states, so we end up with four
❑ 𝑟1 = 𝑞1 and 𝑟2 = 𝑞0 possible lists containing two states.

❖ 𝑞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 , 𝜀

❖ 𝑞0 𝑋𝑞1 → 0 𝑞0 𝑋𝑞0 𝑞0 𝑋𝑞1


❑ 𝑟1 = 𝑞0 and 𝑟2 = 𝑞1
❖ 𝑞0 𝑋𝑞0 → 0 𝑞0 𝑋𝑞1 𝑞1 𝑋𝑞0
❑ 𝑟1 = 𝑞1 and 𝑟2 = 𝑞0
❖ 𝑞0 𝑋𝑞1 → 0 𝑞0 𝑋𝑞1 𝑞1 𝑋𝑞1
❑ 𝑟1 = 𝑞1 and 𝑟2 = 𝑞1
Jim Anderson (modified by Nathan Otterness) 44
Converting PDA→CFG: Example
𝛿 𝑞0 , 0, 𝑍0 = 𝑞0 , 𝑋𝑍0
𝛿 𝑞0 , 0, 𝑋 = 𝑞0 , 𝑋𝑋
 Production rule from 𝛿 𝑞0 , 1, 𝑋 = 𝑞1 , 𝜀 : 𝛿 𝑞0 , 1, 𝑋 = 𝑞1 , 𝜀
𝛿 𝑞1 , 1, 𝑋 = 𝑞1 , 𝜀
❖ 𝑞0 𝑋𝑞1 → 1 𝛿 𝑞1 , 𝜀, 𝑋 = 𝑞1 , 𝜀
𝛿 𝑞1 , 𝜀, 𝑍0 = 𝑞1 , 𝜀
 Production rule from 𝛿 𝑞1 , 1, 𝑋 = 𝑞1 , 𝜀 :
❖ 𝑞1 𝑋𝑞1 → 1
 Production rule from 𝛿 𝑞1 , 𝜀, 𝑋 = 𝑞1 , 𝜀 :
❖ 𝑞1 𝑋𝑞1 → 𝜀
 Production rule from 𝛿 𝑞1 , 𝜀, 𝑍0 = 𝑞1 , 𝜀 :
❖ 𝑞1 𝑍0 𝑞1 → 𝜀
Jim Anderson (modified by Nathan Otterness) 45
Converting PDA→CFG: Example
 We now have production rules for every variable
except for 𝑞1 𝑍0 𝑞0 and [𝑞1 𝑋𝑞0 ].
❖ These variables have no production rules—if they
are ever produced we could never replace them
with terminals.
❖ Intuitively, this makes sense because the original
PDA can’t possibly transition from 𝑞1 to 𝑞0 .
❑ Remember that 𝑞1 𝑋𝑞0 corresponds to 𝑃 transitioning
from 𝑞1 to 𝑞0 while popping 𝑋 off the stack.

Jim Anderson (modified by Nathan Otterness) 46


Converting PDA→CFG: Example
 𝑆 → 𝑞0 𝑍0 𝑞0  𝑞0 𝑋𝑞1 → 0 𝑞0 𝑋𝑞1 𝑞1 𝑋𝑞1
 𝑆 → 𝑞0 𝑍0 𝑞1  𝑞0 𝑋𝑞1 → 1
 𝑞0 𝑍0 𝑞0 → 0 𝑞0 𝑋𝑞0 𝑞0 𝑍0 𝑞0  𝑞1 𝑋𝑞1 → 1
 𝑞0 𝑍0 𝑞1 → 0 𝑞0 𝑋𝑞0 𝑞0 𝑍0 𝑞1  𝑞1 𝑋𝑞1 → 𝜀
 𝑞0 𝑍0 𝑞0 → 0 𝑞0 𝑋𝑞1 𝑞1 𝑍0 𝑞0  𝑞1 𝑍0 𝑞1 → 𝜀
 𝑞0 𝑍0 𝑞1 → 0 𝑞0 𝑋𝑞1 𝑞1 𝑍0 𝑞1
 𝑞0 𝑋𝑞0 → 0 𝑞0 𝑋𝑞0 𝑞0 𝑋𝑞0
This technique can result in many useless
 𝑞0 𝑋𝑞1 → 0 𝑞0 𝑋𝑞0 𝑞0 𝑋𝑞1
productions, which are either unreachable
 𝑞0 𝑋𝑞0 → 0 𝑞0 𝑋𝑞1 𝑞1 𝑋𝑞0 or will never lead to a terminal string.

Jim Anderson (modified by Nathan Otterness) 47


Converting PDA→CFG: Example
 𝑆 → 𝑞0 𝑍0 𝑞0  𝑞0 𝑋𝑞1 → 0 𝑞0 𝑋𝑞1 𝑞1 𝑋𝑞1
 𝑆 → 𝑞0 𝑍0 𝑞1  𝑞0 𝑋𝑞1 → 1
 𝑞0 𝑍0 𝑞0 → 0 𝑞0 𝑋𝑞0 𝑞0 𝑍0 𝑞0  𝑞1 𝑋𝑞1 → 1
 𝑞0 𝑍0 𝑞1 → 0 𝑞0 𝑋𝑞0 𝑞0 𝑍0 𝑞1  𝑞1 𝑋𝑞1 → 𝜀
 𝑞0 𝑍0 𝑞0 → 0 𝑞0 𝑋𝑞1 𝑞1 𝑍0 𝑞0  𝑞1 𝑍0 𝑞1 → 𝜀
 𝑞0 𝑍0 𝑞1 → 0 𝑞0 𝑋𝑞1 𝑞1 𝑍0 𝑞1
 𝑞0 𝑋𝑞0 → 0 𝑞0 𝑋𝑞0 𝑞0 𝑋𝑞0
 𝑞0 𝑋𝑞1 → 0 𝑞0 𝑋𝑞0 𝑞0 𝑋𝑞1 We know that 𝑞1 𝑍0 𝑞0 and 𝑞1 𝑋𝑞0 can never
lead to terminals due to having no productions,
 𝑞0 𝑋𝑞0 → 0 𝑞0 𝑋𝑞1 𝑞1 𝑋𝑞0 so any production of these variables is useless.
Jim Anderson (modified by Nathan Otterness) 48
Converting PDA→CFG: Example
 𝑆 → 𝑞0 𝑍0 𝑞0  𝑞0 𝑋𝑞1 → 0 𝑞0 𝑋𝑞1 𝑞1 𝑋𝑞1
 𝑆 → 𝑞0 𝑍0 𝑞1  𝑞0 𝑋𝑞1 → 1
 𝑞0 𝑍0 𝑞0 → 0 𝑞0 𝑋𝑞0 𝑞0 𝑍0 𝑞0  𝑞1 𝑋𝑞1 → 1
 𝑞0 𝑍0 𝑞1 → 0 𝑞0 𝑋𝑞0 𝑞0 𝑍0 𝑞1  𝑞1 𝑋𝑞1 → 𝜀
 𝑞0 𝑍0 𝑞0 → 0 𝑞0 𝑋𝑞1 𝑞1 𝑍0 𝑞0  𝑞1 𝑍0 𝑞1 → 𝜀
 𝑞0 𝑍0 𝑞1 → 0 𝑞0 𝑋𝑞1 𝑞1 𝑍0 𝑞1
 𝑞0 𝑋𝑞0 → 0 𝑞0 𝑋𝑞0 𝑞0 𝑋𝑞0
 𝑞0 𝑋𝑞1 → 0 𝑞0 𝑋𝑞0 𝑞0 𝑋𝑞1 𝑞0 𝑍0 𝑞0 has only one other production, which
also produces itself, meaning that 𝑞0 𝑍0 𝑞0 can
 𝑞0 𝑋𝑞0 → 0 𝑞0 𝑋𝑞1 𝑞1 𝑋𝑞0 never be entirely replaced with terminals.
Jim Anderson (modified by Nathan Otterness) 49
Converting PDA→CFG: Example
 𝑆 → 𝑞0 𝑍0 𝑞0  𝑞0 𝑋𝑞1 → 0 𝑞0 𝑋𝑞1 𝑞1 𝑋𝑞1
 𝑆 → 𝑞0 𝑍0 𝑞1  𝑞0 𝑋𝑞1 → 1
 𝑞0 𝑍0 𝑞0 → 0 𝑞0 𝑋𝑞0 𝑞0 𝑍0 𝑞0  𝑞1 𝑋𝑞1 → 1
 𝑞0 𝑍0 𝑞1 → 0 𝑞0 𝑋𝑞0 𝑞0 𝑍0 𝑞1  𝑞1 𝑋𝑞1 → 𝜀
 𝑞0 𝑍0 𝑞0 → 0 𝑞0 𝑋𝑞1 𝑞1 𝑍0 𝑞0  𝑞1 𝑍0 𝑞1 → 𝜀
 𝑞0 𝑍0 𝑞1 → 0 𝑞0 𝑋𝑞1 𝑞1 𝑍0 𝑞1
 𝑞0 𝑋𝑞0 → 0 𝑞0 𝑋𝑞0 𝑞0 𝑋𝑞0
 𝑞0 𝑋𝑞1 → 0 𝑞0 𝑋𝑞0 𝑞0 𝑋𝑞1 𝑞0 𝑋𝑞0 has only one remaining production,
which only produces itself. So, 𝑞0 𝑋𝑞0 can never
 𝑞0 𝑋𝑞0 → 0 𝑞0 𝑋𝑞1 𝑞1 𝑋𝑞0 be replaced with terminals and is also useless.
Jim Anderson (modified by Nathan Otterness) 50
Converting PDA→CFG: Example
After removing all of the useless productions, we end up
with the following production rules in the grammar:
 𝑆 → 𝑞0 𝑍0 𝑞1
 𝑞0 𝑍0 𝑞1 → 0 𝑞0 𝑋𝑞1 𝑞1 𝑍0 𝑞1
 𝑞0 𝑋𝑞1 → 0 𝑞0 𝑋𝑞1 𝑞1 𝑋𝑞1
 𝑞0 𝑋𝑞1 → 1
 𝑞1 𝑋𝑞1 → 1
 𝑞1 𝑋𝑞1 → 𝜀
 𝑞1 𝑍0 𝑞1 → 𝜀
Jim Anderson (modified by Nathan Otterness) 51
Converting PDA→CFG: Example 𝑆 → 𝑞0 𝑍0 𝑞1
𝑞0 𝑍0 𝑞1 → 0 𝑞0 𝑋𝑞1 𝑞1 𝑍0 𝑞1
𝑞0 𝑋𝑞1 → 0 𝑞0 𝑋𝑞1 𝑞1 𝑋𝑞1
Remember that the language of the PDA and CFG is 𝑞0 𝑋𝑞1 → 1
0𝑖 1 𝑗 | 𝑖 ≥ 𝑗 ≥ 1 . 𝑞1 𝑋𝑞1 → 1
𝑞1 𝑋𝑞1 → 𝜀
Example string in the language: 001. 𝑞1 𝑍0 𝑞1 → 𝜀

Leftmost derivation in the Sequence of moves in the 𝛿 𝑞0 , 0, 𝑍0 = 𝑞0 , 𝑋𝑍0


constructed grammar: original PDA: 𝛿 𝑞0 , 0, 𝑋 = 𝑞0 , 𝑋𝑋
𝛿 𝑞0 , 1, 𝑋 = 𝑞1 , 𝜀
• 𝑆 ⇒ 𝑞0 𝑍0 𝑞1 • 𝑞0 , 001, 𝑍0 𝛿 𝑞1 , 1, 𝑋 = 𝑞1 , 𝜀
• ⇒ 0 𝑞0 𝑋𝑞1 𝑞1 𝑍0 𝑞1 • ├ 𝑞0 , 01, 𝑋𝑍0 𝛿 𝑞1 , 𝜀, 𝑋 = 𝑞1 , 𝜀
• ⇒ 00 𝑞0 𝑋𝑞1 𝑞0 𝑋𝑞1 𝑞1 𝑍0 𝑞1 • ├ 𝑞0 , 1, 𝑋𝑋𝑍0 𝛿 𝑞1 , 𝜀, 𝑍0 = 𝑞1 , 𝜀
• ⇒ 001 𝑞0 𝑋𝑞1 𝑞1 𝑍0 𝑞1 • ├ 𝑞1 , 𝜀, 𝑋𝑍0
• ⇒ 001 𝑞1 𝑍0 𝑞1 • ├ 𝑞1 , 𝜀, 𝑍0
• ⇒ 001 • ├ 𝑞1 , 𝜀, 𝜀
Jim Anderson (modified by Nathan Otterness) 52
Proving PDA→CFG Correctness
We will prove that our construction is correct by first proving
that the variables mean what we say they do. Remember that this is the
∗ ∗ condition for determining 𝑤’s
Claim: 𝑞𝑋𝑝 ⇒ 𝑤 if and only if 𝑞, 𝑤, 𝑋 ├ 𝑝, 𝜀, 𝜀 . membership in the language
𝐺 𝑃 of a PDA by empty stack.
 “We can derive 𝑤 from 𝑞𝑋𝑝 if and only if we can move
from state 𝑞 to 𝑝 while consuming 𝑤 and popping 𝑋 from
the stack.”
∗ ∗
 Special case: 𝑞0 𝑍0 𝑝 ⇒ 𝑤 if and only if 𝑞0 , 𝑤, 𝑍0 ├ 𝑝, 𝜀, 𝜀 .
 Because 𝑆 ⇒ 𝑞0 𝑍0 𝑝 (by our construction), this implies
𝑤 ∈ 𝐿 𝐺 if and only if 𝑤 ∈ 𝑁 𝑃 .
Jim Anderson (modified by Nathan Otterness) 53
Proving PDA→CFG Correctness
∗ ∗
Claim (again): 𝑞𝑋𝑝 ⇒ 𝑤 if and only if 𝑞, 𝑤, 𝑋 ├ 𝑝, 𝜀, 𝜀 .
𝐺 𝑃
“If”: We show by induction on the number of PDA
𝑖 ∗
moves that 𝑞, 𝑤, 𝑋 ├ 𝑝, 𝜀, 𝜀 implies 𝑞𝑋𝑝 ⇒ 𝑤.
The only way the PDA can
accept a string in one step is if
Base case: 𝑖 = 1. This means that 𝑤 ∈ Σ ∪ 𝜀 . the string is a single symbol or 𝜀.

𝛿 𝑞, 𝑤, 𝑋 must contain 𝑝, 𝜀 . Therefore, 𝑞𝑋𝑝 → 𝑤 is a


production due to our construction of the CFG.

Jim Anderson (modified by Nathan Otterness) 54


Proving PDA→CFG Correctness
∗ ∗
𝑞𝑋𝑝 ⇒ 𝑤 if and only if 𝑞, 𝑤, 𝑋 ├ 𝑝, 𝜀, 𝜀
𝐺 𝑃
“If”, Inductive step: 𝑖 steps, where 𝑖 > 1.

 𝑞, 𝑎𝑥, 𝑋 ├ 𝑟0 , 𝑥, 𝑌1 … 𝑌𝑘 ├ 𝑝, 𝜀, 𝜀
❖ Here, 𝑤 = 𝑎𝑥, where 𝑎 ∈ Σ ∪ 𝜀
❖ This means that 𝑟0 , 𝑌1 … 𝑌𝑘 ∈ 𝛿 𝑞, 𝑎, 𝑋 , so the CFG will
have a production 𝑞𝑋𝑟𝑘 → 𝑎 𝑟0 𝑌1 𝑟1 … 𝑟𝑘−1 𝑌𝑘 𝑟𝑘 , where
❑ 𝑟𝑘 = 𝑝, and
❑ 𝑟1 , 𝑟2 … 𝑟𝑘−1 are any states in 𝑄.

Jim Anderson (modified by Nathan Otterness) 55


Proving PDA→CFG Correctness
“If”, Inductive step, continued

 𝑞, 𝑎𝑥, 𝑋 ├ 𝑟0 , 𝑥, 𝑌1 … 𝑌𝑘 ├ 𝑝, 𝜀, 𝜀
❖ We have a production 𝑞𝑋𝑟𝑘 → 𝑎 𝑟0 𝑌1 𝑟1 … 𝑟𝑘−1 𝑌𝑘 𝑟𝑘 , where
❑ 𝑟𝑘 = 𝑝, and
❑ 𝑟1 , 𝑟2 … 𝑟𝑘−1 are any states in 𝑄.
 For each 𝑌𝑖 in 𝑌1 … 𝑌𝑘 :
❖ Let 𝑟𝑖 be the state of the PDA when 𝑌𝑖 is popped off the stack.
❖ Let 𝑤𝑖 be the input consumed when popping 𝑌𝑖 off the stack.
❑ In this example, 𝑥 = 𝑤1 … 𝑤𝑘

❖ Putting the above points another way, 𝑟𝑖−1 , 𝑤𝑖 , 𝑌𝑖 ├ 𝑟𝑖 , 𝜀, 𝜀 .
 Any set of moves going from 𝑟𝑖−1 to 𝑟𝑖 will take

fewer than 𝑛 moves, so the
inductive hypothesis tells us that 𝑟𝑖−1 𝑌𝑖 𝑟𝑖 ⇒ 𝑤𝑖 .

 Therefore, 𝑎 𝑟0 𝑌1 𝑟1 … 𝑟𝑘−1 𝑌𝑘 𝑟𝑘 ⇒ 𝑎𝑤1 … 𝑤𝑘 = 𝑎𝑥 = 𝑤.
Jim Anderson (modified by Nathan Otterness) 56
Proving PDA→CFG Correctness
∗ ∗
Claim (again): 𝑞𝑋𝑝 ⇒ 𝑤 if and only if 𝑞, 𝑤, 𝑋 ├ 𝑝, 𝜀, 𝜀 .
𝐺 𝑃
“Only if”: We show by induction on the number of
𝑖 ∗
derivation steps that 𝑞𝑋𝑝 ⇒ 𝑤 implies 𝑞, 𝑤, 𝑋 ├ 𝑝, 𝜀, 𝜀 .

Base case: 𝑖 = 1. 𝑞𝑋𝑝 → 𝑤, where 𝑤 ∈ Σ ∪ 𝜀 .


From the construction of the CFG, we know that 𝛿 𝑞, 𝑤, 𝑋
must contain 𝑝, 𝜀 .

Jim Anderson (modified by Nathan Otterness) 57


Proving PDA→CFG Correctness
𝑖 ∗
“Only if” claim: 𝑞𝑋𝑝 ⇒ 𝑤 implies 𝑞, 𝑤, 𝑋 ├ 𝑝, 𝜀, 𝜀
𝑟𝑘 = 𝑝
Inductive step: 𝑖 > 1.
𝑖−1
𝑞𝑋𝑟𝑘 ⇒ 𝑎 𝑟0 𝑌1 𝑟1 … 𝑟𝑘−1 𝑌𝑘 𝑟𝑘 𝑤.

Let 𝑤 = 𝑎𝑤1 𝑤2 … 𝑤𝑘 , where 𝑟𝑖−1 𝑌𝑖 𝑟𝑖 ⇒ 𝑤𝑗 and 1 ≤ 𝑖 ≤ 𝑘.

By the inductive hypothesis, 𝑟𝑖−1 , 𝑤𝑖 , 𝑌𝑖 ├ 𝑟𝑖 , 𝜀, 𝜀 ,
and 1 ≤ 𝑖 ≤ 𝑘.

Therefore, 𝑟𝑖−1 , 𝑤𝑖 , 𝑌𝑖 𝑌𝑖+1 … 𝑌𝑘 ├ 𝑟𝑖 , 𝜀, 𝑌𝑖+1 … 𝑌𝑘
From the first step of the derivation, ∗
𝑞, 𝑤, 𝑋 ├ 𝑟0 , 𝑤1 … 𝑤𝑘 , 𝑌1 … 𝑌𝑘 . So, 𝑞, 𝑤, 𝑋 ├ 𝑝, 𝜀, 𝜀 .
Jim Anderson (modified by Nathan Otterness) 58
Deterministic PDAs
A PDA 𝑃 = 𝑄, Σ, Γ, 𝛿, 𝑞0 , 𝑍0 , 𝐹 is deterministic if and
only if:
There is at most one
1. 𝛿 𝑞, 𝑎, 𝑋 has at most one member for any 𝑞 ∈ 𝑄, possible transition for
every combination of
𝑎 ∈ Σ ∪ 𝜀 , and 𝑋 ∈ Γ. state, input symbol,
and stack symbol.
2. If 𝛿 𝑞, 𝑎, 𝑋 is nonempty for some 𝑎 ∈ Σ, then
𝛿 𝑞, 𝜀, 𝑋 must be empty.
 Deterministic PDAs (DPDAs) are useful in parsing.
 Unlike DFAs, which are just as powerful as NFAs,
DPDAs are less powerful than nondeterministic PDAs.
Jim Anderson (modified by Nathan Otterness) 59
Regular Languages and DPDAs
Theorem 6.17: If 𝐿 is a regular language, then
𝐿 = 𝐿 𝑃 for some DPDA 𝑃.

Proof sketch: A DFA is a special case of a DPDA in


which the stack is not used, i.e., all moves “replace”
𝑍0 by 𝑍0 , where 𝑍0 is the start symbol.

Jim Anderson (modified by Nathan Otterness) 60


DPDAs Accepting by Empty Stack
The DPDA would need
to empty its stack while
It is impossible for a DPDA that accepts by empty reading 𝑥, so it would
fail when trying to
stack to accept both a string 𝑥 and a string 𝑥𝑦, where process more input on
𝑦 ≠ 𝜀. an empty stack.

A language 𝐿 has the prefix property if there are no


two different strings 𝑥 and 𝑦 in 𝐿 such that 𝑥 is a
prefix of 𝑦.
Theorem 6.19: A language 𝐿 is 𝑁 𝑃 for some DPDA
𝑃 if and only if 𝐿 has the prefix property and 𝐿 is
𝐿 𝑃′ for some DPDA 𝑃′ .

Jim Anderson (modified by Nathan Otterness) 61


Relationships Between DPDAs and CFLs “Proper” inclusion:
The superset must have
some elements not
present in the subset.
Theorem: The languages accepted by DPDAs by final
state properly include the regular languages, but are
properly included in the context-free languages.

Proof sketch: Regular language inclusion (not


necessarily proper inclusion) is implied by Theorem 6.17
(a regular language must be accepted by some DPDA).

Jim Anderson (modified by Nathan Otterness) 62


DPDA Inclusion Proof Sketch, Continued

The fact that regular languages are properly included


in the languages of DPDAs is because the language
𝑤2𝑤 𝑅 | 𝑤 ∈ 𝟎 + 𝟏 ∗ is accepted by a DPDA by final
state, but is not a regular language.
 The “2” tells the DPDA when to start looking for 𝑤 𝑅 ,
enabling it to be deterministic.

Jim Anderson (modified by Nathan Otterness) 63


DPDA Inclusion Proof Sketch, Continued

The fact that the language of DPDAs is properly


included by context-free languages is because the
language 𝑤𝑤 𝑅 | 𝑤 ∈ 𝟎 + 𝟏 ∗ is accepted by a PDA
but not by any DPDA.
 We will not prove in class that it’s impossible for a
DPDA to accept this, but the intuition is that the
DPDA can’t “know” when 𝑤 ends and 𝑤 𝑅 begins.

Jim Anderson (modified by Nathan Otterness) 64


DPDAs and Ambiguous Grammars
Theorem 6.20: If 𝐿 = 𝑁 𝑃 for some DPDA 𝑃, then 𝐿
has an unambiguous CFG.

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 𝐿$.

Define 𝐿$ = 𝑥$ | 𝑥 ∈ 𝐿 , and $ is a new symbol not in


𝐿’s alphabet. This means that 𝐿$ has the prefix property.
We can modify 𝑃 to accept 𝐿$ by final state. Therefore,
by Theorem 6.19, 𝐿$ = 𝑁 𝑃′ for some DPDA 𝑃′.

Jim Anderson (modified by Nathan Otterness) 66


DPDAs and Ambiguous Grammars
Theorem 6.21: If 𝐿 = 𝐿 𝑃 for some DPDA 𝑃, then 𝐿 has
an unambiguous CFG.
Proof sketch, continued:
We know by Theorem 6.20 that, 𝐿$ has an unambiguous
CFG 𝐺, because 𝐿$ = 𝑁 𝑃′ for some DPDA 𝑃′ .
However, in 𝐺, $ is a terminal symbol. To fix this,
define a grammar 𝐺 ′ that is the same as 𝐺 except $ is a
variable and the production $ → 𝜀 is included.
Now, 𝐺 ′ is unambiguous, and 𝐿 = 𝐿 𝐺 ′ .

Jim Anderson (modified by Nathan Otterness) 67

You might also like