0% found this document useful (0 votes)
29 views22 pages

Lec 17 Pda CFG

The document discusses converting context-free grammars (CFGs) to pushdown automata (PDAs). It states that PDAs and CFGs recognize the same class of languages. It then proves this by showing that for any CFG G, there exists a PDA A that recognizes the language of G by empty stack. The proof constructs a PDA with a single state that simulates the leftmost derivations of G by pushing anticipated productions onto the stack and popping them to verify matches with the input. An example CFG is also converted to an equivalent PDA.

Uploaded by

pratyaysarkar.cs
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)
29 views22 pages

Lec 17 Pda CFG

The document discusses converting context-free grammars (CFGs) to pushdown automata (PDAs). It states that PDAs and CFGs recognize the same class of languages. It then proves this by showing that for any CFG G, there exists a PDA A that recognizes the language of G by empty stack. The proof constructs a PDA with a single state that simulates the leftmost derivations of G by pushing anticipated productions onto the stack and popping them to verify matches with the input. An example CFG is also converted to an equivalent PDA.

Uploaded by

pratyaysarkar.cs
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/ 22

CS310 : Automata Theory 2019

Lecture 17: PDA == CFG

Instructor: Ashutosh Gupta

IITB, India

Compile date: 2019-02-11

cbna CS310 : Automata Theory 2019 Instructor: Ashutosh Gupta IITB, India 1
Recognizing “by empty stack” to “by final state”
Theorem 17.1
Let P = (Q, Σ, Γ, δ, q0 , Z0 , F ) be a PDA. There is a PDA P 0 such that
L(P 0 ) = L (P).
Proof. Let us construct the following PDA
P 0 = (Q ∪ {p0 , pf }, Σ, Γ ∪ {X0 }, δ 0 ∪ δ, p0 , X0 , {pf })
I new initial state p0 , accepting state pf , and initial stack symbol X0
I Extra transitions δ 0 are defined as follows
(
0 {(q0 , Z0 X0 )} q = p0
δ (q, , X0 ) ,
{(pf , )} q∈Q

, X0 /
, X0 /Z0 X0
start p0 q0 P , X0 / pf

cbna CS310 : Automata Theory 2019 Instructor: Ashutosh Gupta IITB, India 2
Recognizing “by empty stack” to “by final state” II
Proof(contd.).
claim: L (P) ⊆ L(P 0 )
Let us assume w ∈ L (P), i.e., w is accepting by P by empty stack.

Therefore (q0 , w , Z0 ) `∗ (q, , ).


P

Since all transitions of P are in P 0 , (q0 , w , Z0 ) `∗0 (q, , ).


P

Due to properties of `∗ , (q0 , w , Z0 X0 ) `∗0 (q, , X0 ).


P

Due to the definition of δ 0 ,

(p0 , w , X0 ) `0 (q0 , w , Z0 X0 ) `∗0 (q, , X0 ) `0 (pf , , )


P P P

Therefore, w ∈ L(P 0 ). ...


cbna CS310 : Automata Theory 2019 Instructor: Ashutosh Gupta IITB, India 3
Recognizing “by empty stack” to “by final state” III
Proof(contd.).
claim: L(P 0 ) ⊆ L (P)
Let us assume w ∈ L(P 0 ), i.e., w is accepting by P by final state.

Therefore, (p0 , w , X0 ) `∗0 (pf , , γ).


P

Due to the definition of P 0 , the first transition jumps to q0 and pushes Z0 .


Let q ∈ Q be the last state from where P 0 jumped to pf .(why exists?)

Only the last transition can pop X0 and no old transitions of P can push or
pop X0 .
(p0 , w , X0 ) `0 (q0 , w , Z0 X0 ) `∗0 (q, , X0 ) `0 (pf , , )
P P P
| {z }
X0 is always on stack

Therefore, (q0 , w , Z0 ) `∗ (q, , ). Therefore, w ∈ L (P).


P
cbna CS310 : Automata Theory 2019 Instructor: Ashutosh Gupta IITB, India 4
Example: recognizing “by empty stack” to “by final state”
Example 17.1
Consider the following PDA.

P = ({q0 }, {0 (0 ,0 )0 }, {Z }, δ, q0 , Z , {})

0 (0 , Z /ZZ

, X0 /Z X0 , X0 /
start p0 q0 pf

0 )0 , Z /

P 0 = ({q0 , p0 , pf }, {0 (0 ,0 )0 }, {Z , X0 }, δ 0 ∪ δ, p0 , X0 , {})

By construction, L (P) = L(P 0 ).


cbna CS310 : Automata Theory 2019 Instructor: Ashutosh Gupta IITB, India 5
Recognizing “by final state” to “by empty stack”
Theorem 17.2
Let P = (Q, Σ, Γ, δ, q0 , Z0 , F ) be a PDA. There is a PDA P 0 such that
L (P 0 ) = L(P).
Proof. Let us construct the following PDA
P 0 = (Q ∪ {p0 , p}, Σ, Γ ∪ {X0 }, δ 0 ∪ δ, p0 , X0 , {})
I new initial state p0 , state p, and initial stack symbol X0
I Extra transitions δ 0 are defined as follows
(
0 {(q0 , Z0 X0 )} q = p0 ∧ Z = X0
δ (q, , Z ) ,
{(p, )} q ∈ F ∪ {p} ∧ Z = Γ ∪ {X0 }

, any /
, X0 /Z0 X0
start p0 q0 P , any / p
, any /

cbna CS310 : Automata Theory 2019 Instructor: Ashutosh Gupta IITB, India 6
Recognizing “by final state” to “by empty stack” II
Proof(contd.).
claim: L(P) ⊆ L (P 0 )
Let us assume w ∈ L(P), i.e., w is accepting by P by final state.

Therefore (q0 , w , Z0 ) `∗ (q, , γ), where q ∈ F .


P

Since all transitions of P are in P 0 , (q0 , w , Z0 ) `∗0 (q, , γ).


P

Due to properties of `∗ , (q0 , w , Z0 X0 ) `∗0 (q, , γX0 ).


P

Due to the definition of δ 0 ,

(p0 , w , X0 ) `0 (q0 , w , Z0 X0 ) `∗0 (q, , γX0 ) `∗0 (p, , )


P P P
| {z }
loop at p will flush the stack
Therefore, w ∈ L(P 0 ). ...
cbna CS310 : Automata Theory 2019 Instructor: Ashutosh Gupta IITB, India 7
Recognizing “by final state” to “by empty stack” III

Proof(contd.).
claim: L (P 0 ) ⊆ L(P)
Let us assume w ∈ L (P 0 ), i.e., w is accepting by P by empty stack.

Since the P transitions cannot pop X0 , the empty stack is reached only at p.
Therefore, (p0 , w , X0 ) `∗0 (p, , ).
P

Due to the definition of P 0 , the first transition jumps to q0 and pushes Z0 .


Let qf ∈ F be the last state from where P 0 jumped to p.(why exists?)

(p0 , w , X0 ) `0 (q0 , w , Z0 X0 ) `∗0 (qf , , X0 γ) `∗0 (p, , )


P P P
| {z }
X0 is always on stack

Therefore, (q0 , w , Z0 ) `∗ (q, , γ). Therefore, w ∈ L(P).


P

cbna CS310 : Automata Theory 2019 Instructor: Ashutosh Gupta IITB, India 8
Topic 17.1

CFGs and PDAs

cbna CS310 : Automata Theory 2019 Instructor: Ashutosh Gupta IITB, India 9
CFGs and PDAs

We have defined
context-free grammars (CFGs) and pushdown automata (PDAs).

It is no coincidence that

PDAs and CFGs recognize same class of languages.

Let us see the proof.

cbna CS310 : Automata Theory 2019 Instructor: Ashutosh Gupta IITB, India 10
Topic 17.2

CFGs to PDAs

cbna CS310 : Automata Theory 2019 Instructor: Ashutosh Gupta IITB, India 11
CFGs to PDAs

Theorem 17.3
Let G = (N, T , P, S) be a CFG grammar. The PDA
Initial No final
Input symbols state state(why?)

A = ({q}, T , N ∪ T , δ, q, S, {})
Single state Stack symbols Start Stack
symbols
recognizes L(G ) by empty stack, where δ is defined as follows.

δ(q, , B) , {(q, β)|B → β ∈ P}


δ(q, a, a) , {(q, )}. Push anticipated
Verify the anticipated productions
guess while popping
cbna CS310 : Automata Theory 2019 Instructor: Ashutosh Gupta IITB, India 12
Example: CFGs to PDAs
Example 17.2
Consider CFG

1. P → 0P0 3. P → 
2. P → 1P1 4. P → 1

A = ({q}, {0, 1}, {0, 1, P}, δ, q, P, {})

, P/1P1
, P/0P0

, P/1
start q
, P/

1, 1/
0, 0/
cbna CS310 : Automata Theory 2019 Instructor: Ashutosh Gupta IITB, India 13
Proof : CFGs to PDAs
Proof.
Let w ∈ L(G ).

Consider the following left most derivation for w ,


lm lm lm
⇒ γ1 =
S= ⇒ ... =
⇒ γn = w .

We will show by induction that

(q, w , S) `∗ (q, yi , αi )
A
xi has been matched and
such that yi is to be matched by αi
I there is an xi such that w = xi yi and γi = xi αi and
I if αi 6= , then αi begins with nonterminal .

xi yi
αi
cbna CS310 : Automata Theory 2019 Instructor: Ashutosh Gupta IITB, India 14
Proof : CFGs to PDAs II

Proof(contd.)
base case:
For i = 1, γ1 = S. We have

(q, w , S) `∗ (q, w , S),


A

i.e. nothing is processed. Therefore, αi = S, y1 = w , and x1 = .

induction step:
By induction hypothesis, (q, w , S) `∗ (q, yi , αi ) such that w = xi yi and
A
γi = x i αi .

case αi = : we are done.(why?)


...

cbna CS310 : Automata Theory 2019 Instructor: Ashutosh Gupta IITB, India 15
Proof : CFGs to PDAs III

Proof(contd.)
case αi = Aαi0 , where A ∈ N:

Therefore, γi = xi Aαi0 and γi+1 = xi βαi0 for some rule A → β ∈ P.

PDA matches the move.

xi yi xi yi
A β
αi0 αi0

...

cbna CS310 : Automata Theory 2019 Instructor: Ashutosh Gupta IITB, India 16
Proof : CFGs to PDAs IV

Proof(contd.)
Two sub-cases

case βαi0 has no nonterminal. case Let βαi0 = xi0 A0 αi00 .


Therefore, i + 1 = n. (why?) Therefore, xi0 is prefix of yi .(why?)
Therefore, βαi0 = yi . Let yi = xi0 yi0 .
δ matches and pops all terminals. δ will match and pop top terminals.

xi yi xi yi xi xi0 yi0
xi yi 
β ∗ β ∗ A0

αi0 αi0 αi00

αi+1 = yi+1 =  αi+1 = A0 αi00 and yi+1 = yi0 ...

cbna CS310 : Automata Theory 2019 Instructor: Ashutosh Gupta IITB, India 17
Proof : CFGs to PDAs V

Proof(contd.)
Proving the other direction

Assume w ∈ L (A). So we have (q, w , S) `∗ (q, , )


A

For any B ∈ N and x ∈ T ∗ , we will prove



if (q, x, B) `∗ (q, , ) then B =⇒x
A
also says that any derivation of B
does not look below B in the stack

We prove by induction on the length of derivation n. ...

cbna CS310 : Automata Theory 2019 Instructor: Ashutosh Gupta IITB, India 18
Proof : CFGs to PDAs VI
Proof(contd.)
base case:
n = 1.
We can finish the run in a single transition only if B →  ∈ P. Therefore

if (q, , B) ` (q, , ) then B =


⇒
A

induction step:
n>1
Let B → Y1 ...Yk ∈ P is used in the first transition of A, which pops B and
stack is filled with Y1 , ..., Yk without consuming anything.
 x  Y x
1

B ..
.
Yk

cbna CS310 : Automata Theory 2019 Instructor: Ashutosh Gupta IITB, India ...19
Proof : CFGs to PDAs VII
Proof(contd.)

 x  Y x x 
1
.. ∗
B . 
Yk

Yi has to be eventually popped.(why?) Let us divide x = x1 x2 ...xk such that


x1 ..xi−1 xi xi+1 ..xk x1 ..xi−1 xi xi+1 ..xk
Yi
Yi+1
Yi+1 ∗
Yi is about .. Yi+1 is about
.. Yi+1 is not popped .
to be popped . to be popped
first time Yk first time
Yk
...
cbna CS310 : Automata Theory 2019 Instructor: Ashutosh Gupta IITB, India 20
Proof : CFGs to PDAs VII

Proof(contd.)
Therefore, we have (q, xi , Yi ) `∗ (q, , ) in less than n steps.
A


⇒ xi .
Due to induction hypothesis, Yi =

Due to the properties of derivations,



⇒ x1 ..xi−1 xi Yi+1 ..Yk
x1 ..xi−1 Yi Yi+1 ..Yk =

By stitching the derivations for each i, we will obtain



⇒ Y1 ...Yk =
B= ⇒ x1 ...xk .

Since we have (q, w , S) `∗ (q, , ), S =
⇒ w.
A

cbna CS310 : Automata Theory 2019 Instructor: Ashutosh Gupta IITB, India 21
End of Lecture 17

cbna CS310 : Automata Theory 2019 Instructor: Ashutosh Gupta IITB, India 22

You might also like