CSC 473 Automata, Grammars & Languages 10/20/10 · CSC 473 Automata, Grammars & Languages 10/20/10...

5
CSC 473 Automata, Grammars & Languages 10/20/10 1 C SC 473 Automata, Grammars & Languages 41 PDA CFG Lemma 2.27: There is an algorithm for constructing, from any PDA P, a CFG G such that L(G) = L(P). Pf: Given a PDA we can convert it into a PDA with the following simplified structure: it has only one accept state: add ε-transitions from multiple accept states it empties its stack just before entering the accept state: Loop on a state that just pops: each PDA transition is either a “pure push” or a “pure pop - introduce new intermediate states { } accept F q = , accept f q !! ! " " , , pop pop X q q X ! ! " #$ " , a X ! " , aX ! " 0 (,,,, , ) P Q q F ! = "# C SC 473 Automata, Grammars & Languages 42 PDA CFG (contʼd) becomes becomes Idea of proof: construct G with variables for each p and q in the set of states Q. Arrange that if generates terminal string x, then PDA P started in state p with an empty stack on input string x has a computation that reaches state q with an empty stack. And conversely, if P started in state p with an empty stack has a computation on input string x that reaches state q with an empty stack, then How does P, when started on an empty stack in state p, operate on an input string x, ending with an empty stack in state q ? First move must be a push Last move must be a pop , aX Y ! , , aX Y ! ! ! " " , a ! ! " , , a X X ! ! ! " " pq A pq A . pq G A x ! , a X ! " , bX ! " C SC 473 Automata, Grammars & Languages 43 PDA CFG (contʼd) Trace computation of P on x starting in state p with empty stack, and ending in state q with empty stack: (1) stack never empties pq G rs A aA b ! p q a b rs G A y ! !"""""#"""""$ input Stack height r s pq G A x ! !"""""#"""""$ push X ! pop X ! Fig. 1

Transcript of CSC 473 Automata, Grammars & Languages 10/20/10 · CSC 473 Automata, Grammars & Languages 10/20/10...

CSC 473 Automata, Grammars & Languages 10/20/10

1

C SC 473 Automata, Grammars & Languages 41

PDA CFGLemma 2.27: There is an algorithm for constructing, fromany PDA P, a CFG G such that L(G) = L(P).

Pf: Given a PDA we canconvert it into a PDA with the following simplifiedstructure:• it has only one accept state:

• add ε-transitions from multiple accept states• it empties its stack just before entering the accept state:

•Loop on a state that just pops:

• each PDA transition is either a “pure push” or a “pure pop

- introduce new intermediate states

{ }accept

F q=,

acceptf q

! ! !"

"

,

,pop pop

X

q q X! !"

# $"

,a X! "

,a X !"

0( , , , , , )P Q q F!= " #

C SC 473 Automata, Grammars & Languages 42

PDA CFG (contʼd) becomes becomes

• Idea of proof: construct G with variables foreach p and q in the set of states Q. Arrange that if

generates terminal string x, then PDA P started in state pwith an empty stack on input string x has a computationthat reaches state q with an empty stack. And conversely,if P started in state p with an empty stack has acomputation on input string x that reaches state q with anempty stack, then

How does P, when started on an empty stack in state p,operate on an input string x, ending with an empty stack instate q ? First move must be a push Last move must be a pop

,a X Y! , ,a X Y! ! !" "

,a ! !" , ,a X X! ! !" "

pqA

pqA

.pq G

A x!

,a X! "

,b X !"

C SC 473 Automata, Grammars & Languages 43

PDA CFG (contʼd) Trace computation of P on x starting in state p with

empty stack, and ending in state q with empty stack:(1) stack never empties

pq G rsA aA b!

p qa b

rs GA y!

!"""""#"""""$

input

Stac

k he

ight

r s

pq GA x!

!"""""#"""""$

push X! pop X !

Fig. 1

CSC 473 Automata, Grammars & Languages 10/20/10

2

C SC 473 Automata, Grammars & Languages 44

PDA CFG (contʼd) Trace computation of P on x starting in state p with

empty stack, and ending in state q with empty stack:(2) stack empties somewhere

pq G pr rqA A A!

p q

r q GA z!

!"""""#"""""$

input

Stac

k he

ight

r

pr GA y!

!"""""#"""""$

Fig. 2

C SC 473 Automata, Grammars & Languages 45

PDA CFG (contʼd)Construction. Given PDA

construct with the followingrules in R:

◆ If

◆ then

( , )( )pq p q

p q Q Q A A A! " ! " #r r

r

&

pq rsA aA b!

0( , , , , ,{ })

acceptP Q q q!= " #

0,( , , , )

acceptq qG V R A= !

( )pp

p Q A !" # $

( , , , )( )( , )p q r s Q X a b!

" # " # $ " # %

,a X! "p r

,b X !"s q

C SC 473 Automata, Grammars & Languages 46

PDA CFG (contʼd)Claim 2.30: If thenPf: by induction on a derivation in G length k.Base: k=1. The only derivations of length 1 are and we haveStep: Assume (IH) true for derivations of ≤ k steps. WantClaim true for derivations of k+1 steps. Suppose that . The first derivation step is either ofthe form orCase . Then with So IH ⇒By construction, since is a rule of G,

pq G p qA A A!

r r

pq G rsA aA b!

pq GA x!

(p,x,!) !

P

"(q,!,!)

k

pq GA x!

1

pp GA !"

(p,!,!) !

P

"(p,!,!)

1k

pq GA x

+!

x ayb=

.k

rs GA y!

(r,y,!) !

P

"(s,!,!).

pq rsA aA b!

pq G rsA aA b!

CSC 473 Automata, Grammars & Languages 10/20/10

3

C SC 473 Automata, Grammars & Languages 47

PDA CFG (contʼd)

Case . Then with So IH ⇒

Putting these together:

pq G p qA A A!

r r

! (p,x,") !

P(r,yb,X)!

P

#(s,b,X)!

P(q,",")

x yz=

& .k k

pr G rq GA y A z

! !" "

(p,y,!) !

P

"(r,!,!) & (r,z,!) !

P

"(q,!,!)

(p,yz,!) !

P

"(r,z,!) = (r,z,!) !

P

"(q,!,!)!

( ) ( , ) ( , , )&( , ) ( , , ).X r X p a q s b X! " " !# $ $

C SC 473 Automata, Grammars & Languages 48

PDA CFG (contʼd)Claim 2.31: If thenPf: by induction on a computation in P of length k:

Base: k=0. The only computations of length 0 are where x = ε. By construction

Step: Assume (IH) true for computations of ≤ k steps. WantClaim true for computations of k+1 steps. Suppose that . Two cases: either the stackdoes not empty in midst of this computation (Fig. 1) or itBecomes empty during the computation (Fig. 2). Call theseCase 1 and Case 2.

.pq G

A x!

" (p,x,!) !

P

"(q,!,!)

1.

pp GA !" (p,x,!) !

P

0(p,!,!)

(p,x,!) !

P

k(q,!,!)

(p,x,!) !

P

k +1(q,!,!).

C SC 473 Automata, Grammars & Languages 49

PDA CFG (contʼd)Case 1: See Fig.1. The symbol X pushed in the 1st moveIs the same as that popped in the last move. Let the 1st

and last moves be governed by the push/pop transitions:

By construction, there is a rule in GLet x = ayb. Since thenwe must have By IHThen Using we conclude

.pq rs

A aA b!

.pq G

A ayb x!

" =

.rs GA y

!"

(r,y,X) !

P

k !1(s,",X)

( , ) ( , , )&( , ) ( , , ).r X p a q s b X! " " !# #

(r,y,!) !

P

k "1(s,!,!).

1

pq G rsA aA b!

CSC 473 Automata, Grammars & Languages 10/20/10

4

C SC 473 Automata, Grammars & Languages 50

PDA CFG (contʼd)Case 2: See Fig.2. Let r be the intermediate state where

the stack becomes empty. Then

By the IH, andSince by construction there is a rule in G of the form

thenpq pr rq

A A A!

pr GA y

!"

( , )y z x yz! =

(p,y,!) !

P

"k(r,!,!) & (r,z,!) !

P

"k(q,!,!)

.rq GA z

!"

.pq G pr rq G

A A A yz x!

" " = !

C SC 473 Automata, Grammars & Languages 51

PDA CFG (contʼd)Ex:

Rules of G:(1) push-pop pairs (1st kind):

# #sf qqA A!

(, A! "

s

#, $! "

f#,$ !"

),A !"

is a well-balanced string of parentheses{ {(, )}( ) }!*

L P = #w# | w

q

s#, $! "

q

q

(, A! "

f

q

#,$ !"

q

q),A !"

q

:

{ , , }

P

Q s q f=

{ ,$}

{#,(,)}

A! =

" =

( )qq qqA A!

C SC 473 Automata, Grammars & Languages 52

PDA CFG (contʼd)Note: If (p´ unreachable) then

(abbreviated ).Such variables are useless; all rules involving them on leftor right sides can be eliminated as useless productions. Forthis grammar(2) Rules of the 2nd Kind (with useless rules removed—only 10/27 survive) in the order s,q,f:

(p,!,!) !

P

"( #p ,!,!)

ss ss ss

sq ss sq

sq sq qq

sf ss sf

sf sq qf

sf sf ff

A A AA A A

A A A

A A AA A A

A A A

!

!

!

!

!

!

{ | }pp G

x A x!

" # = $ ppA ! = "

, ,fq qs fsA A A= ! = ! = !

qq qq qq

qf qq qf

qf qf ff

ff ff ff

A A A

A A A

A A A

A A A

!

!

!

!

CSC 473 Automata, Grammars & Languages 10/20/10

5

C SC 473 Automata, Grammars & Languages 53

PDA CFG (contʼd)(2) Rules of the 3rd Kind:

Combining all rules with same LHS:

ss

qq

ff

AA

A

!

!

!

"

"

"

|

|

# # | | |

( )| |

|

|

ss ss ss

sq ss sq sq qq

sf qq ss sf sq qf sf ff

qq qq qq qq

qf qq qf qf ff

ff ff ff

A A AA A A A A

A A A A A A A A

A A A A

A A A A A

A A A

!

!

!

"

"

"

"

"

"

C SC 473 Automata, Grammars & Languages 54

PDA CFG (contʼd)Simplify: easy to see that

Substituting this into rules:

Eliminate useless rules like

ss

ff

A

A

!

!

=

=

|

# # | | |

( )| |

|

sq sq sq qq

sf qq sf sq qf sf

qq qq qq qq

qf qq qf qf

A A A A

A A A A A A

A A A A

A A A A

!

"

"

"

"

X X!

# # |

( )| |

sq sq qq

sf qq sq qf

qq qq qq qq

qf qq qf

A A A

A A A A

A A A A

A A A

!

"

"

"

"

C SC 473 Automata, Grammars & Languages 55

PDA CFG (contʼd)Another kind of useless rule: generate no terminal strings. Eliminate these variables any and rules mentioning them. Final simplified grammar is:

Note: chose to use endmarkers # for clarity, but these couldhave been ε, (input symbols can be anything in )leading to the familiar grammar

# #

( )| |sf qq

qq qq qq qq

A A

A A A A !

"

"

,sq qfA A

( )| |sf qq

qq qq qq qq

A A

A A A A !

"

"

!"