chapter 6 pushdown automata

47
1 Chapter 6 Chapter 6 Pushdown automata Pushdown automata Sagrada Familia Sagrada Familia ( 聖聖聖 聖聖聖 ), Barcelon ), Barcelon a, Spain a, Spain

Upload: nasnan

Post on 19-Jan-2016

124 views

Category:

Documents


5 download

DESCRIPTION

Chapter 6 Pushdown automata. Sagrada Familia ( 聖家堂 ), Barcelona, Spain. Outline. 6.0 Introduction 6.1 Definition of PDA 6.2 The Language of a PDA 6.3 Equivalence of PDA’s and CFG’s 6.4 Deterministic PDA’s. 6.0 Introduction. Basic concepts: - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter 6 Pushdown automata

11

Chapter 6Chapter 6Pushdown automataPushdown automata

Sagrada Familia (Sagrada Familia ( 聖家聖家堂堂 ), Barcelona, Spain), Barcelona, Spain

Page 2: Chapter 6 Pushdown automata

22

OutlineOutline

6.0 Introduction6.0 Introduction 6.1 Definition of PDA6.1 Definition of PDA 6.2 The Language of a PDA6.2 The Language of a PDA 6.3 Equivalence of PDA’s and CFG’s6.3 Equivalence of PDA’s and CFG’s 6.4 Deterministic PDA’s6.4 Deterministic PDA’s

Page 3: Chapter 6 Pushdown automata

33

6.0 Introduction6.0 Introduction

Basic concepts:Basic concepts:– CFL’s may be accepted by pushdown automata (PDCFL’s may be accepted by pushdown automata (PD

A’s)A’s)

– A PDA is an A PDA is an -NFA with a stack.-NFA with a stack.

– The stack can be read, pushed, and popped only at tThe stack can be read, pushed, and popped only at the top.he top.

– Two different versions of PDA’s ---Two different versions of PDA’s ---Accepting strings by “entering an accepting state”Accepting strings by “entering an accepting state”Accepting strings by “emptying the stack”Accepting strings by “emptying the stack”

Page 4: Chapter 6 Pushdown automata

44

6.0 Introduction6.0 Introduction

Basic concepts (cont’d)Basic concepts (cont’d)

– The original PDA is The original PDA is nondeterministic.nondeterministic.

– There is also a subclass of PDA’s which are There is also a subclass of PDA’s which are

deterministicdeterministic in nature. in nature.

– Deterministic PDA’s (DPDA’s) resembles parsers Deterministic PDA’s (DPDA’s) resembles parsers

for CFL’s in compilers.for CFL’s in compilers.

Page 5: Chapter 6 Pushdown automata

55

6.0 Introduction6.0 Introduction

Basic concepts (cont’d)Basic concepts (cont’d)

– It is interesting to know what “language constructsIt is interesting to know what “language constructs

” which a DPDA can accept.” which a DPDA can accept.

– The stack is The stack is infiniteinfinite in size, so can be used as a in size, so can be used as a “m“m

emory”emory” to eliminate the weakness of to eliminate the weakness of “finite states”“finite states”

of NFA’s, which cannot accept languages like of NFA’s, which cannot accept languages like LL = =

{{aannbbnn | | nn 1} 1}..

Page 6: Chapter 6 Pushdown automata

66

6.1 Definition of PDA6.1 Definition of PDA

6.1.1 Informal Introduction6.1.1 Informal Introduction

– Advantage of the stack --- the stack can “remembeAdvantage of the stack --- the stack can “remembe

r” an r” an infiniteinfinite amount of information. amount of information.

– Weakness of the stack --- the stack can only be reaWeakness of the stack --- the stack can only be rea

d in a d in a first-in-last-outfirst-in-last-out manner. manner.

– Therefore, it can accept languages like Therefore, it can accept languages like LLwwrwwr = { = {wwwwRR

| | ww is in ( is in (00 + + 11))**}}, but not languages like , but not languages like LL = { = {aann

bbnnccnn | | nn 1} 1}. .

Page 7: Chapter 6 Pushdown automata

77

6.1 Definition of PDA6.1 Definition of PDA 6.1.1 Informal Introduction6.1.1 Informal Introduction

– A graphic model of a PDAA graphic model of a PDA

有限

控制器

讀取頭

磁帶

0 11 0 1 0

B

A

B

Z0

讀寫頭堆疊之頂

堆疊之底

圖 13.1 一部下推式自動機的圖形模式。

tape

tape reader

finite-state control

A graph model of a PDA

stack reader & writer

Top of stack

Bottom of stack

Page 8: Chapter 6 Pushdown automata

88

6.1 Definition of PDA6.1 Definition of PDA 6.1.1 Informal Introduction6.1.1 Informal Introduction

– The input string on the “tape” can only be The input string on the “tape” can only be readread..

– But operations applied to the stack is complicated; But operations applied to the stack is complicated;

we may we may replacereplace the top symbolthe top symbol by any by any stringstring --- ---

By a single symbolBy a single symbol

By a string of symbolsBy a string of symbols

By the empty string By the empty string which means the top which means the top

stack symbol is “popped up (eliminated).”stack symbol is “popped up (eliminated).”

Page 9: Chapter 6 Pushdown automata

99

6.1 Definition of PDA6.1 Definition of PDA

6.1.1 Informal Introduction6.1.1 Informal Introduction– Example 6.1Example 6.1 - Design a PDA to accept the lan - Design a PDA to accept the lan

guage guage LLwwrwwr = { = {wwwwRR | | ww is in ( is in (00 + + 11))**}}. . In start state In start state qq00, copy input symbols onto the stack., copy input symbols onto the stack.At any time, At any time, nondeterministicallynondeterministically guess guess whetherwhether t t

he middle of he middle of wwwwRR is reached and enter is reached and enter qq11, , oror contin continue copying input symbols.ue copying input symbols.

In In qq11, compare remaining input symbols with those , compare remaining input symbols with those on the stack one by one.on the stack one by one.

If the stack can be so emptied, then the matching of If the stack can be so emptied, then the matching of ww with with wwRR succeeds. succeeds.

Page 10: Chapter 6 Pushdown automata

1010

6.1 Definition of PDA6.1 Definition of PDA

6.1.2 Formal Definition6.1.2 Formal DefinitionA PDA is a 7-tuple A PDA is a 7-tuple PP = ( = (QQ, , , , , , , , qq00, , ZZ00, , FF) where) where

– QQ: a finite set of states: a finite set of states : a finite set of input symbols: a finite set of input symbols : a finite stack alphabet: a finite stack alphabet : a transition function such that : a transition function such that ((qq, , aa, , XX) ) is is a seta set

of pairsof pairs ( (pp, , ) where) whereqqQQ (the current state) (the current state)aa or or aa = = (an input symbol or an empty string) (an input symbol or an empty string)XXppQQ (the next state) (the next state) (cont’d in the next page)(cont’d in the next page)

Page 11: Chapter 6 Pushdown automata

1111

6.1 Definition of PDA6.1 Definition of PDA

6.1.2 Formal Definition6.1.2 Formal Definition(continued from last page)(continued from last page)** which replaces which replaces XX on the top of the stack: on the top of the stack:

when when = = , the top stack symbol is , the top stack symbol is popped uppopped up

when when = = XX, the stack is unchanged, the stack is unchanged

when when = = YYZZ, , XX is replaced by is replaced by ZZ, and , and YY is pushed to the top is pushed to the top

when when = = ZZ, , XX is replaced by is replaced by ZZ and string and string is pushed to th is pushed to the tope top

qq00: the start state: the start state

ZZ00: the start symbol of the stack: the start symbol of the stack

FF: the set of accepting or final states: the set of accepting or final states

Page 12: Chapter 6 Pushdown automata

1212

6.1 Definition of PDA6.1 Definition of PDA 6.1.2 Formal Definition6.1.2 Formal Definition

– Example 6.2 (cExample 6.2 (cont’d from Example 6.1) - Designing a PDA to aont’d from Example 6.1) - Designing a PDA to accept the languageccept the language LLwwwwRR..

Need a start symbol Z of the stack and a 3rd state Need a start symbol Z of the stack and a 3rd state qq22 as the acc as the acc

epting state.epting state.

PP = ({ = ({qq00, , qq11, , qq22}, {0, 1}, {0, 1, }, {0, 1}, {0, 1, ZZ00}, }, , , qq00, , ZZ00, {, {qq22}) such that}) such that

((qq00, 0, , 0, ZZ00) = ) = {({(qq00, 0, 0ZZ00)},)}, ((qq00, 1, , 1, ZZ00) = {() = {(qq00, 1, 1ZZ00)})}

(initial pushing steps with (initial pushing steps with ZZ00 to mark stack bottom) to mark stack bottom)

((qq00, 0, 0) = {(, 0, 0) = {(qq00, 00)}, , 00)}, ((qq00, 0, 1) = {(, 0, 1) = {(qq00, 01)}, , 01)}, ((qq00, 1, 0) , 1, 0)

= {(= {(qq00, 10)}, , 10)}, ((qq00, 1, 1) = {(, 1, 1) = {(qq00, 11)}, 11)}

(continuing pushing)(continuing pushing)

Page 13: Chapter 6 Pushdown automata

1313

6.1 Definition of PDA6.1 Definition of PDA 6.1.2 Formal Definition6.1.2 Formal Definition

– Example 6.2 (cExample 6.2 (cont’d) ont’d)

((qq00, , , , ZZ00) = {() = {(qq11, , ZZ00)})}

(check if input is (check if input is which is in which is in LLwwwwRR))

((qq00, , , 0) = {(, 0) = {(qq11, 0)}, , 0)}, ((qq00,, , 1) = {(, 1) = {(qq11, 1)}, 1)}

(check the string’s middle)(check the string’s middle)

((qqqq11, , )}, )}, qq11, 1, 1) = {(, 1, 1) = {(qq11, , )})}

(matching pairs)(matching pairs)

((qq11, , , , ZZ00) = {() = {(qq22, , ZZ00)})}

(entering final state)(entering final state)

Page 14: Chapter 6 Pushdown automata

1414

6.1 Definition of PDA6.1 Definition of PDA

6.1.3 A Graphic Notation for PDA’s6.1.3 A Graphic Notation for PDA’s– The transition diagram of a PDA is easier to follow.The transition diagram of a PDA is easier to follow.– We use “We use “aa, , XX//”” on an arc from state on an arc from state pp to to qq to to

represent that represent that “transition “transition ((qq, , aa, , XX) contains () contains (pp, , )”)”

– Example 6.3Example 6.3 The transition diagram of the PDA of The transition diagram of the PDA of Example 6.2 is as shown in Fig. 6.2 (see next page) Example 6.2 is as shown in Fig. 6.2 (see next page) (in p. 230 of the textbook).(in p. 230 of the textbook).

q pa, X/

Page 15: Chapter 6 Pushdown automata

1515

6.1 Definition of PDA6.1 Definition of PDA

6.1.3 A Graphic Notation for PDA’s6.1.3 A Graphic Notation for PDA’s

– Where is the nondeterminism? Where is the nondeterminism?

q0 q1, Z/

, 0/, 1/

q2, Z/

start

, Z/0

, Z/1

, 0/00, 1/01, 0/10, 1/11

, 0/, 1/

Fig. 6.2Fig. 6.2

(push 0 on top of Z0)

Page 16: Chapter 6 Pushdown automata

1616

6.1 Definition of PDA6.1 Definition of PDA

6.1.4 Instantaneous Descriptions of a PDA6.1.4 Instantaneous Descriptions of a PDA

– The The configurationconfiguration of a PDA is represented by a 3- of a PDA is represented by a 3-

tuple (tuple (qq, , ww, , ) where) where

qq is the state; is the state;

ww is the remaining input; and is the remaining input; and

is the stack content. is the stack content.

– Such a 3-tuple is called an Such a 3-tuple is called an instantaneous instantaneous

descriptiondescription ( (IDID) of the PDA.) of the PDA.

Page 17: Chapter 6 Pushdown automata

1717

6.1 Definition of PDA6.1 Definition of PDA

6.1.4 Instantaneous Descriptions of a PDA6.1.4 Instantaneous Descriptions of a PDA

– The change of an ID into another is called a The change of an ID into another is called a movemove, ,

denoted by the symbol , or when denoted by the symbol , or when PP is understo is understo

od.od.

– So, if So, if ((qq,, aa, , XX) contains () contains (pp, , ), then the following ), then the following

is a corresponding move:is a corresponding move:

((qq, , awaw, , XX) () (pp, , ww, , ))

– We use or to indicate zero or more moves. We use or to indicate zero or more moves.

_|P

_|

_|

*_|P

*_|

_|

Page 18: Chapter 6 Pushdown automata

1818

6.1 Definition of PDA6.1 Definition of PDA

6.1.4 Instantaneous Descriptions of a PDA6.1.4 Instantaneous Descriptions of a PDA– Example 6.4Example 6.4 (cont’d from Example 6.2) - (cont’d from Example 6.2) -

See Fig. 6.3See Fig. 6.3 Moves for the PDA to accept input Moves for the PDA to accept input ww = 1111: = 1111:

((qq00, 1111, , 1111, ZZ00) () (qq00, 111, 1, 111, 1ZZ00) )

((qq00, 11, 11, 11, 11ZZ00) () (qq11, 11, 11, 11, 11ZZ00) )

((qq11, 1, 1, 1, 1ZZ00) )

((qq11, , , , ZZ00) ) ( (qq22, , , , ZZ00))

There are other paths entering dead ends (not shown).There are other paths entering dead ends (not shown).

_|_|

_|

_|_|

_|

Page 19: Chapter 6 Pushdown automata

1919

6.1 Definition of PDA6.1 Definition of PDA

6.1.4 Instantaneous Descriptions of a PDA6.1.4 Instantaneous Descriptions of a PDA– Theorem 6.5Theorem 6.5

If If PP = ( = (QQ, , , , , , , , qq00, , ZZ00, , FF) is a PDA, and ) is a PDA, and

((qq, , xx, , ) () (pp, , yy, , ), ),

then for any string then for any string ww in in ** and and in in **, it is also true th, it is also true th

at at

((qq, , xxww, , ) () (pp, , yyww, , ).).

(The reverse is (The reverse is notnot true; but if true; but if is taken away, the reverse is true, as shown by the next theorem) is taken away, the reverse is true, as shown by the next theorem)

*_|P

*_|P

Page 20: Chapter 6 Pushdown automata

2020

6.1 Definition of PDA6.1 Definition of PDA

6.1.4 Instantaneous Descriptions of a PDA6.1.4 Instantaneous Descriptions of a PDA– Theorem 6.6Theorem 6.6

If If PP = ( = (QQ, , , , , , , , qq00, , ZZ00, , FF) is a PDA, an) is a PDA, an

d d

((qq, , xxww, , ) () (pp, , yyww, , ),),

then it is also true that then it is also true that

((qq, , xx, , ) () (pp, , yy, , ).).

*_|P

*_|P

Page 21: Chapter 6 Pushdown automata

2121

6.2 The Language of a PDA6.2 The Language of a PDA

Some important facts:Some important facts:

– Two ways to define languages of PDA’s: by final Two ways to define languages of PDA’s: by final state and by empty stack, as mentioned before.state and by empty stack, as mentioned before.

– It can be proved that a language It can be proved that a language LL has a PDA that has a PDA that accepts it by final state accepts it by final state if and only ifif and only if LL has a PDA has a PDA that accepts it by empty stack.that accepts it by empty stack.

– For a given PDA For a given PDA PP, the language that , the language that PP accepts by accepts by final state and by empty stack are usually final state and by empty stack are usually differentdifferent..

– In this section, we show In this section, we show conversionsconversions between the between the two ways of language acceptances.two ways of language acceptances.

Page 22: Chapter 6 Pushdown automata

2222

6.2 The Language of a PDA6.2 The Language of a PDA

6.2.1 Acceptance by Final State6.2.1 Acceptance by Final State– Definition: Definition:

If If PP = ( = (QQ, , , , , , , , qq00, , ZZ00, , FF) is a PDA. Th) is a PDA. Th

en en LL((PP), the language accepted by), the language accepted by P by final state P by final state, is , is

{{ww | ( | (qq00, , ww, , ZZ00) ) ( (qq, , , , ), ), qqFF}}

for any for any ..

– Example 6.7Example 6.7 - Proving the PDA shown in Example 6. - Proving the PDA shown in Example 6.

2 indeed accepts the language 2 indeed accepts the language LLwwwwRR (see the detail in t(see the detail in t

he textbook by yourself).he textbook by yourself).

*_|P

Page 23: Chapter 6 Pushdown automata

2323

6.2 The Language of a PDA6.2 The Language of a PDA

6.2.2 Acceptance by Empty Stack6.2.2 Acceptance by Empty Stack– Definition:Definition:

If If PP = ( = (QQ, , , , , , , , qq00, , ZZ00, , FF) is a PDA. ) is a PDA.

Then Then NN((PP), the language accepted by), the language accepted by P by empty P by empty stackstack, is , is

{{ww | ( | (qq00, , ww, , ZZ00) ) ( (qq, , , , )})}

for any for any qq..

– The set of final states The set of final states F F may be dropped to form a may be dropped to form a 6-6-

tupletuple, instead of a 7-tuple, for a PDA., instead of a 7-tuple, for a PDA.

*_|P

Page 24: Chapter 6 Pushdown automata

2424

6.2 The Language of a PDA6.2 The Language of a PDA

6.2.2 Acceptance by Empty Stack6.2.2 Acceptance by Empty Stack– Example 6.8Example 6.8

The PDA of Example 6.2 may be modified tThe PDA of Example 6.2 may be modified t

o accept o accept LLwwwwRR by empty stack: by empty stack:

simply change the original transitionsimply change the original transition

((qq11, , , , ZZ00) = {() = {(qq22, , ZZ00)} )}

to be to be

((qq11, , , , ZZ00) = {() = {(qq22, , )}.)}.

(just eliminate (just eliminate ZZ00))

Page 25: Chapter 6 Pushdown automata

2525

6.2 The Language of a PDA6.2 The Language of a PDA 6.2.3 From Empty Stack to Final State6.2.3 From Empty Stack to Final State

– Theorem 6.9 Theorem 6.9 (1/3)(1/3)

If If LL = = NN((PPNN) for some PDA ) for some PDA PPNN = ( = (QQ, , , , , , NN,,

qq00, , ZZ00), then there is a PDA ), then there is a PDA PPFF = such that = such that LL = = LL((PPFF).).

Proof. Proof. The idea is to use Fig. 6.4 below.The idea is to use Fig. 6.4 below.

pfq0

PN

, X, X00//, X, X00//

, X, X00//

p0

,, XX00//ZZ00XX00start

PF

Fig. 6.4 PF simulating PN and accepts if PN empties its stack

Page 26: Chapter 6 Pushdown automata

2626

6.2 The Language of a PDA6.2 The Language of a PDA 6.2.3 From Empty Stack to Final State6.2.3 From Empty Stack to Final State

– Theorem 6.9 Theorem 6.9 (2/3)(2/3)

If If LL = = NN((PPNN) for some PDA ) for some PDA PPNN = ( = (QQ, , , , , , NN, ,

qq00, , ZZ00), then there is a PDA ), then there is a PDA PPFF = such that = such that LL = = LL((PPFF).).

Proof. Proof. (cont’d)(cont’d)

Define Define PPFF = ( = (QQ∪∪{{pp00, , ppff}, }, , , ∪∪{{XX00}, }, FF, , pp00, , XX00, {, {ppff}) wh}) wh

ere ere FF is such that is such that

FF((pp00, , , , XX00) = {() = {(qq00, , ZZ00XX00)}. )}. ((用用 XX00墊底墊底 ).).

– For all For all qqQQ, , aa or or aa = = , and , and YY, , FF((qq, , aa, , YY) contai) contai

ns all the pairs in ns all the pairs in NN((qq, , aa, , YY).).

FF((qq, , , , XX00) contains () contains (ppff, , ) for ) for everyevery state state qq in in QQ..

Page 27: Chapter 6 Pushdown automata

2727

6.2 The Language of a PDA6.2 The Language of a PDA

6.2.3 From Empty Stack to Final State6.2.3 From Empty Stack to Final State– Theorem 6.9 Theorem 6.9 (3/3)(3/3)

If If LL = = NN((PPNN) for some PDA ) for some PDA PPNN = ( = (QQ, , , , , , NN,,

qq00, , ZZ00), then there is a PDA ), then there is a PDA PPFF = such that = such that LL = = LL((PPFF).).

Proof. Proof. (cont’d)(cont’d)

It can be proved that It can be proved that WW is in is in LL((PPFF) if and only if ) if and only if ww is in is in

NN((PPNN) (see the textbook).) (see the textbook).

Page 28: Chapter 6 Pushdown automata

2828

6.2 The Language of a PDA6.2 The Language of a PDA

6.2.3 From Empty Stack to Final State6.2.3 From Empty Stack to Final State

– Example 6.10 - Example 6.10 - Design a PDA which accepts theDesign a PDA which accepts the if/else if/else

errorserrors by empty stack. by empty stack.

Let Let ii represents represents ifif; ; ee represents represents elseelse..

The PDA is designed in such a way that The PDA is designed in such a way that

if the number ofif the number of elseelse (# (#elseelse) > ) > the number ofthe number of ifif (# (#ifif), ),

then the stack will be emptied.then the stack will be emptied.

Page 29: Chapter 6 Pushdown automata

2929

6.2 The Language of a PDA6.2 The Language of a PDA

6.2.3 From Empty Stack to Final State6.2.3 From Empty Stack to Final State– Example 6.10 (cont’d)Example 6.10 (cont’d)

A PDA A PDA by empty stackby empty stack for this is as follows: for this is as follows:

PPNN = ({ = ({qq}, {}, {ii, , ee}, {}, {ZZ}, }, NN, , qq, , ZZ))

when an “when an “ifif” is seen, push a “” is seen, push a “ZZ”;”;

when an “when an “elseelse” is seen, pop a “” is seen, pop a “ZZ”;”;

when (#when (#elseelse) > (#) > (#ifif + 1), the stack is emptied and the inp + 1), the stack is emptied and the input sting is accepted.ut sting is accepted.

For example, for input string For example, for input string ww = = ieeiee, the moves are:, the moves are:

((qq, , ieeiee, , ZZ) () (qq, , eeee, , ZZZZ) () (qq, , ee, , ZZ) () (qq, , , , ) accept) accept!! (how about (how about ww = = eeieei?)?)

start

i, Z/ZZe, Z/

qFig. 6.5

_| _| _|

Page 30: Chapter 6 Pushdown automata

3030

6.2 The Language of a PDA6.2 The Language of a PDA

6.2.3 From Empty Stack to Final State6.2.3 From Empty Stack to Final State– Example 6.10 (cont’d)Example 6.10 (cont’d)

A PDA A PDA by final stateby final state as follows: as follows:

PPFF = ({ = ({pp, , qq, , rr}, {}, {ii, , ee}, {}, {ZZ, , XX00}, }, FF, , pp, , XX00, {, {rr})})

For input For input ww = = ieeiee, the moves are:, the moves are:((pp, , ieeiee, , XX00) () (qq, , ieeiee, , ZXZX00) () (qq, , eeee, , ZZXZZX00) () (qq, , ee, , ZXZX00) () (qq, , , , XX00) () (rr, , , , ) accept) accept !!

Fig. 6.6

e, Z/e, Z/ii, , Z/ZZZ/ZZ

qp r, X, X00/ / , , XX00/Z/ZXX00start

_| _| _| _| _|

Page 31: Chapter 6 Pushdown automata

3131

6.2 The Language of a PDA6.2 The Language of a PDA 6.2.4 From Final State to Empty Stack6.2.4 From Final State to Empty Stack

– Theorem 6.11Theorem 6.11

Let Let LL be be LL((PPFF) for some PDA ) for some PDA PPFF = ( = (QQ, , , , , ,

FF, , qq00, , ZZ00, , FF). Then there is a PDA ). Then there is a PDA PPNN such that such that LL = = NN((PPNN). ).

Proof. Proof. The idea is to use Fig. 6.7 below (The idea is to use Fig. 6.7 below (in final states of in final states of PP

FF, pop up the remaining symbols in the stack, pop up the remaining symbols in the stack).).

Fig. 6.7 PN simulating PF and empties its stack when and only when PN enters an accepting state.

, , anyany//p0 pq0

PF , , anyany//, , XX00/Z/Z00XX00start

PN

, , anyany//

Page 32: Chapter 6 Pushdown automata

3232

6.3 Equivalence of PDA’s and CFG’s6.3 Equivalence of PDA’s and CFG’s

Equivalences to be proved:Equivalences to be proved:1)1) CFL’s defined by CFG’sCFL’s defined by CFG’s

2)2) Languages accepted by final state by some PDALanguages accepted by final state by some PDA

3)3) Languages accepted by empty stack by some PDLanguages accepted by empty stack by some PDAA

– Equivalence of 2) and 3) above have been proved.Equivalence of 2) and 3) above have been proved.

Page 33: Chapter 6 Pushdown automata

3333

6.3 Equivalence of PDA’s and CFG’s6.3 Equivalence of PDA’s and CFG’s

6.3.1 From Grammars to PDA’s6.3.1 From Grammars to PDA’s– Given a CFG Given a CFG GG = ( = (VV, , TT, , QQ, , SS), construct a PDA ), construct a PDA PP t t

hat accepts hat accepts LL((GG) by empty stack in the following w) by empty stack in the following way:ay:

– PP = ({ = ({qq}, }, TT, , VV∪∪TT, , , , qq, , SS) where the transition fun) where the transition function ction is defined by: is defined by:for each nonterminal for each nonterminal AA, ,

((qq, , , , AA) = {() = {(qq, , ) | ) | is a production of is a production of GG};};

for each terminal for each terminal aa, , ((qq, , aa, , aa) = {() = {(qq, , )}.)}.

Page 34: Chapter 6 Pushdown automata

3434

6.3 Equivalence of PDA’s and CFG’s6.3 Equivalence of PDA’s and CFG’s

6.3.1 From Grammars to PDA’s6.3.1 From Grammars to PDA’s

– Theorem 6.13 Theorem 6.13

If PDA If PDA PP is constructed from CFG is constructed from CFG GG by the by the

construction above, then construction above, then NN((PP) = ) = LL((GG).).

ProofProof. See the textbook.. See the textbook.

Page 35: Chapter 6 Pushdown automata

3535

6.3 Equivalence of PDA’s and CFG’s6.3 Equivalence of PDA’s and CFG’s

6.3.1 From Grammars to PDA’s6.3.1 From Grammars to PDA’s– Example 6.12Example 6.12 - Construct a PDA from the express - Construct a PDA from the express

ion grammar of Fig. 5.2:ion grammar of Fig. 5.2:

II aa | | bb | | IaIa | | IbIb | | II0 | 0 | II1;1;EE II | | EE**EE | | EE++EE | ( | (EE).).

The transition function for the PDA is as follows:The transition function for the PDA is as follows:

a) a) ((qq, , , , II) = {() = {(qq, , aa), (), (qq, , bb), (), (qq, , IaIa), (), (qq, , IbIb), (), (qq, , II0), (0), (qq, , II1)}1)}

b) b) ((qq, , , , EE) = {() = {(qq, I), (, I), (qq, , EE++EE), (), (qq, , EE**EE), (), (qq, (, (EE))}))}

c) c) ((qq, , dd, , dd) = {() = {(qq, , )} where )} where dd may any of the terminals may any of the terminals

aa, , bb, 0, 1, (, ), +, , 0, 1, (, ), +, **..

Page 36: Chapter 6 Pushdown automata

3636

6.3 Equivalence of PDA’s and CFG’s6.3 Equivalence of PDA’s and CFG’s

6.3.2 From PDA’s to Grammars6.3.2 From PDA’s to Grammars– Theorem 6.14Theorem 6.14

Let Let PP = ( = (QQ, , , , , , , , qq00, , ZZ00) be a PDA. Then t) be a PDA. Then t

here is a context-free grammar here is a context-free grammar GG such that such that LL((GG) = ) = NN((PP).).

Proof.Proof. Construct Construct GG = ( = (VV, , TT, , PP, , SS) where the set of nonterminal) where the set of nonterminal

s consists of:s consists of:

the special symbol the special symbol SS as the start symbol; as the start symbol;

all symbols of the form [all symbols of the form [pXqpXq] where ] where pp and and qq are states i are states i

n n QQ and and XX is a stack symbol in is a stack symbol in ..

Page 37: Chapter 6 Pushdown automata

3737

6.3 Equivalence of PDA’s and CFG’s6.3 Equivalence of PDA’s and CFG’s

6.3.2 From PDA’s to Grammars6.3.2 From PDA’s to Grammars– Theorem 6.14Theorem 6.14

Proof.Proof. (cont’d) (cont’d)The productions of The productions of GG are as follows. are as follows.

(a) For all states (a) For all states pp, , GG has the production has the production SS [ [qq00ZZ00pp].].

(b) Let (b) Let ((qq, , aa, , XX) contain the pair () contain the pair (rr, , YY11YY22 … … YYkk), where), where

– aa is either a symbol in is either a symbol in or or aa = = – kk can be any number, including 0, in which case the pai can be any number, including 0, in which case the pai

r is (r is (rr, , ).).

Then for all lists of states Then for all lists of states rr11, , rr22, …, , …, rrkk, , GG has the production has the production

[[qXrqXrkk] ] aa[[rYrY11rr11][][rr11YY22rr22]…[]…[rrkk11YYkkrrkk].].

Page 38: Chapter 6 Pushdown automata

3838

6.3 Equivalence of PDA’s and CFG’s6.3 Equivalence of PDA’s and CFG’s

6.3.2 From PDA’s to Grammars6.3.2 From PDA’s to Grammars– Example 6.15 --- Example 6.15 --- Convert the PDA of Example 6.10 (beloConvert the PDA of Example 6.10 (belo

w) to a grammar.w) to a grammar.

Nonterminals include only two symbols, Nonterminals include only two symbols, SS and [ and [qZqqZq].].

Productions:Productions:

1. 1. SS [ [qZqqZq]] (for the start symbol (for the start symbol SS););

2. [2. [qZqqZq] ] ii[[qZqqZq][][qZqqZq]] (from ((from (qq, , ZZZZ))NN((qq, , ii, ,

ZZ))))

3. [3. [qZqqZq] ] ee (from ((from (qq, , ))NN((qq, , ee, , ZZ))))

start

e, Z/i, Z/ZZ

qFig. 6.5

Page 39: Chapter 6 Pushdown automata

3939

6.3 Equivalence of PDA’s and CFG’s6.3 Equivalence of PDA’s and CFG’s

6.3.2 From PDA’s to Grammars6.3.2 From PDA’s to Grammars– Example 6.15 --- (Example 6.15 --- (cont’d)cont’d)

If we replace [If we replace [qZqqZq] by a simple symbol ] by a simple symbol AA, then the product, then the productions becomeions become

1. 1. SS AA 2. 2. AA iAAiAA 3. 3. AA ee Obviously, these productions can be simplified to beObviously, these productions can be simplified to be

1. 1. SS iSSiSS 2. 2. SS ee And the grammar may be written simply as And the grammar may be written simply as

GG = ({ = ({SS}, {}, {ii, , ee}, {}, {SS iSSiSS | | ee}, }, SS) )

Page 40: Chapter 6 Pushdown automata

4040

6.4 Deterministic PDA’s6.4 Deterministic PDA’s 6.4.1 Definition of a Deterministic PDA6.4.1 Definition of a Deterministic PDA

– Intuitively, a PDA is deterministic if there is never Intuitively, a PDA is deterministic if there is never a choice of moves (including a choice of moves (including movesmovesin any situin any situation.ation.

– Formally, a PDA Formally, a PDA PP = ( = (QQ, , , , , , , , qq00, , ZZ00, , FF) is said ) is said

to be deterministic (a DPDA) if and only if the follto be deterministic (a DPDA) if and only if the following two conditions are met:owing two conditions are met:

((qq, , aa, , XX) has at most one element for any ) has at most one element for any qqQQ, , aa or or aa = = , and , and XX. . (“(“一定要有”一定要有” ))

If If ((qq, , aa, , XX) is nonempty for some ) is nonempty for some aa, then , then ((qq, , , , XX) ) must be empty. must be empty. (“(“不能多於一個”不能多於一個” ))

Page 41: Chapter 6 Pushdown automata

4141

6.4 Deterministic PDA’s6.4 Deterministic PDA’s 6.4.1 Definition of a DPDA6.4.1 Definition of a DPDA

– Example 6.16Example 6.16 – – There is no DPDA for There is no DPDA for LLwwwwRR of Example 6.2. of Example 6.2.

But there is a DPDA for a modified version of But there is a DPDA for a modified version of LLwwwwRR as fo as fo

llows, llows, which is not an RL (proved later)which is not an RL (proved later)::

LLwcwwcwRR = { = {wcwwcwRR | | ww LL((((00 + + 11))**)})}..

To recognize To recognize wcwwcwRR, just store 0’s & 1’s in stack , just store 0’s & 1’s in stack until ceuntil center marker nter marker cc is seen is seen. Then, match the remaining input . Then, match the remaining input wwRR with the stack content ( with the stack content (ww).).

The PDA can so be designed to be deterministic by searThe PDA can so be designed to be deterministic by searching the center marker ching the center marker without trying matching all the without trying matching all the timetime nondeterministicallynondeterministically . .

Page 42: Chapter 6 Pushdown automata

4242

6.4 Deterministic PDA’s6.4 Deterministic PDA’s

6.4.1 Definition of a DPDA6.4.1 Definition of a DPDA– Example 6.16Example 6.16 (cont’d) A desired DPDA is as (cont’d) A desired DPDA is as

follows. follows. (The difference is just the blue (The difference is just the blue cc.).)

q0 q1c, Z/

c, 0/c, 1/

q2, Z/

start

, Z/0

, Z/1

, 0/00, 1/01, 0/10, 1/11

, 0/, 1/

Fig. 6.11Fig. 6.11

Page 43: Chapter 6 Pushdown automata

4343

6.4 Deterministic PDA’s6.4 Deterministic PDA’s

6.4.2 Regular Languages and DPDA’s6.4.2 Regular Languages and DPDA’s– The DPDA’s accepts a class of languages that is The DPDA’s accepts a class of languages that is betweenbetween the R the R

L’s and the CFL’s, as proved in the following.L’s and the CFL’s, as proved in the following.

– Theorem 6.17Theorem 6.17

If If LL is an RL, then is an RL, then LL = = LL((PP) for some DPDA ) for some DPDA PP (accepting by fi (accepting by final state).nal state).

Proof.Proof. Easy. Just use a DPDA to simulate a DFA as follows. Easy. Just use a DPDA to simulate a DFA as follows.

If DFA If DFA AA = ( = (QQ, , , , AA, , qq00, , FF) accepts ) accepts LL, then construct DPDA , then construct DPDA PP

= (= (QQ, , , {, {ZZ00}, }, PP, , qq00, , ZZ00, , FF) where ) where PP is such that is such that PP((qq, , aa, , ZZ00) = ) =

{({(pp, , ZZ00)} for all states )} for all states pp and and qq in in QQ such that such that AA((qq, , aa) = ) = pp..

Page 44: Chapter 6 Pushdown automata

4444

6.4 Deterministic PDA’s6.4 Deterministic PDA’s 6.4.2 Regular Languages and DPDA’s6.4.2 Regular Languages and DPDA’s

– The language-recognizing capability of the The language-recognizing capability of the DPDA by DPDA by empty stackempty stack is is rather limitedrather limited..

– Theorem 6.19Theorem 6.19

A language A language LL is is NN((PP) for some DPDA ) for some DPDA PP if and only i if and only if f LL has the has the prefix propertyprefix property and and LL is is LL((P'P') for some D) for some DPDA PDA P'P' (for proof, do exercise 6.4.3). (for proof, do exercise 6.4.3).

– A language A language LL is said to have the is said to have the prefix propertyprefix property if th if there are no two different strings ere are no two different strings xx and and yy in in LL such that such that xx is a prefix of is a prefix of yy. .

(For examples of such languages, see Example 6.18)(For examples of such languages, see Example 6.18)

Page 45: Chapter 6 Pushdown automata

4545

6.4 Deterministic PDA’s6.4 Deterministic PDA’s

6.4.3 DPDA’s and CFL’s6.4.3 DPDA’s and CFL’s– DPDA’s can be used to accept non-RL’s, for examplDPDA’s can be used to accept non-RL’s, for exampl

e, e, LLwcwwcwRR mentioned before. mentioned before.

It can be proved by the pumping lemma that It can be proved by the pumping lemma that LLwcwwcwR R is not an is not an

RL RL (see the textbook, pp. 254~255).(see the textbook, pp. 254~255).

– On the other hand, DPDA’s On the other hand, DPDA’s by final stateby final state cannotcannot accept certai accept certai

n CFL’s, for example, n CFL’s, for example, LLwwwwRR..

It can be proved that It can be proved that LLwwwwRR cannot be accepted by a DPDA cannot be accepted by a DPDA

by final state (see an informal proof in the textbook, p. 25by final state (see an informal proof in the textbook, p. 255).5).

Page 46: Chapter 6 Pushdown automata

4646

6.4 Deterministic PDA’s6.4 Deterministic PDA’s

6.4.3 DPDA’s and CFL’s6.4.3 DPDA’s and CFL’s– ConclusionConclusion: :

The languages accepted by DPDA’s by final state properly inThe languages accepted by DPDA’s by final state properly in

clude RL’s, but are properly included in CFL’s.clude RL’s, but are properly included in CFL’s.

Page 47: Chapter 6 Pushdown automata

4747

6.4 Deterministic PDA’s6.4 Deterministic PDA’s

6.4.4 DPDA’s and Ambiguous Grammars6.4.4 DPDA’s and Ambiguous Grammars– Theorem 6.20Theorem 6.20

If If LL = = NN((PP) () (accepting by empty stackaccepting by empty stack) for some ) for some DDPPDA DA PP, then , then LL has an unambiguous CFG. has an unambiguous CFG.

Proof. See the textbook.Proof. See the textbook.

– Theorem 6.21Theorem 6.21

If If LL = = LL((PP) for some ) for some DDPDA PDA P P ((accepting by final staaccepting by final statete), then ), then LL has an unambiguous CFG. has an unambiguous CFG.

Proof. See the textbook.Proof. See the textbook.