context-free languages yukita

52
Context-free Languages http://cis.k.hosei.ac.jp/ ~yukita/

Upload: kimberly-chapman

Post on 02-Jan-2016

218 views

Category:

Documents


0 download

TRANSCRIPT

Context-free Languages

http://cis.k.hosei.ac.jp/~yukita/

2

Context-free grammar G1

. are # and 1, 0,

. called is ,Especially

. are and

rules. calledoften arewhich

of consistsgrammar A

#

10

terminals

variablestart the

variables

production

rules onsubstituti

A

BA

B

BA

AA

3

Parse tree for 000#111 in grammar G1

A

A

A

A

B

0 0 0 # 1 1 1

4

The English Language

<SENTENSE> <NOUN-PHRASE> <VERB-PHRASE>

<NOUN-PHRASE> <CMPLX-NOUN>| <CMPLX-NOUN> <PREP-PHRASE>

<VERB-PHRASE> <CMPLX-VERB>| <CMPLX-VERB> <PREP-PHRASE>

<PREP-PHRASE> <PREP> <CMPLX-NOUN>

<CMPLX-NOUN> <ARTICLE> <NOUN>

<CMPLX-VERB> <VERB>|<VERB><NOUN-PHRASE>

<ARTICLE> a | the

<NOUN> boy | girl | flower

<VERB> touches | likes | sees

<PREP> with

5

Definition 2.1

}.|{)( is of The

.

or if Write. written ,

yields say that wegrammar, theof rule a is

and , terminalsand variablesof strings are and , If

theis 4.

and },)(,|"{"

where ofset finite a is 3.

ofset finite a is 2.

ofset finite a is 1.

where,),,,( tuple-4 a is A

**

21

*

*

wSwGLGlanguage

vuuuu

vuvuuwvuAvuwv

uAvwA

wvu

symbol.startVS

VwVAwAR

rules,R

terminals,

V

SRVgrammar free context

k

variables,

6

Context-free Languages

state.accept an is if rule theAdd

.),( if rule theAdd

.DFA theof stateeach for variableaAssign

language. free-context a be out to turnslanguageregular Any

a is language that the

say wegrammar, free-context aby generated is language a If

ii

jiji

ii

RR

qaqaRR

qR

language. free-context

7

Context Dependency

. yield to

for context theconsidered is _ rule,latter In the

dependent.context

is whilet,independencontext is

.)( and , ,,Let *

w

BCA

AwCABCwB

VwVCBA

8

Example 2.2 G3

s.parenthese

nestedproperly of strings all of consists language the

ly,respective ,)"" and ("" as and regardyou If

).(in are and , ,

.||

rule oneonly of consists where,),},{},({

3

3

ba

GLaababbaaabbbabab

SSaSbS

RSRa,bSG

9

Example 2.3 G4

a

R

aV

RVG

|)EXPR(FACTOR

FACTOR|FACTORTERMTERM

TERM|TERMEXPREXPR

)}.(,,,,{ },FACTOR,TERM,EXPR{

).EXPR,,,(4

10

Parse tree for a+aXa

a + a X a

<EXPR>

<EXPR><TERM>

<TERM> <TERM>

<FACTOR> <FACTOR>

<FACTOR>

11

Parse tree for (a+a)Xa

( a + a ) X a

<EXPR>

<EXPR>

<TERM>

<TERM> <TERM>

<FACTOR> <FACTOR>

<FACTOR>

<EXPR>

<FACTOR>

<TERM>

12

Ambiguity in grammar G5

a |

EXPR|

EXPREXPR|

EXPREXPREXPR

13

A parse tree for a+aXa

a + a X a

<EXPR>

<EXPR>

<EXPR> <EXPR>

<EXPR>

14

Another parse tree for a+aXa

a + a X a

<EXPR>

<EXPR>

<EXPR> <EXPR>

<EXPR>

15

Different derivations for the same parse tree

a + a X a

<EXPR>

<EXPR>

<EXPR> <EXPR>

<EXPR>

aaa

aa

a

a

aaa

aa

a

}{

}E{

}EE{

E

EEE

}{

E}{

E }E{

E } EE{

EEE

16

Leftmost Derivation

• If a string has two different parse trees, we say that the grammar is ambiguous.

• A derivation of a string is a leftmost derivation if at every step the leftmost remaining variable is the one replaced.

• Every parse tree has unique leftmost derivation.

17

Definition 2.4 Ambiguity

. is language hesay that t wecasein which

language, ambiguous-nonany by generated benot can that

languages are There language. same thegeneratecan grammar

ambiguous-non and grammar ambiguousAn :Remark

y.ambiguousl string some generatesit if is

Grammar s.derivationleftmost different moreor twohasit if

grammar free-context aby is stringA

ambiguous inherently

G

G

ambiguous

G

Gyambiguousl derivedw

na

a

18

Definition 2.5 Chomsky normal form

.

and ), and , (where

}),{, and , (where

:forms following theof is rule

every if in isgrammar free-contextA

S

aVAaA

SVCBVABCA

form normalChomsky

19

Theorem 2.6

form. normalChomsky in grammar

free-context aby generated is language free-contextAny

20

Proof of Th. 2.6

rules. eliminate all weuntil process srepeat thi We

. removed previously

have weunless add we, rule thehave weIf

on. so And . and ,

, rules theadd , rule a is thereIf

. rule theadd , rule a is thereif , such For

. where rule a Remove 2.

sides. handright in theoccur never symbolstart new The

. rule theand statestart new a Add 1. 00

A

R

RAR

uvwRuAvwR

uvAwRuAvAwR

uvRuAvRA

SAA

SSS

21

Proof of Th. 2.6

. rule theadd and variablenew with the

rules preceeding in the alany termin replace we,2 If

. variablesnew are ,,, Here,

.,,,,

rules with the3 whereeach replace We4.

rules.unit all eliminate weuntil steps serepeat the We

removed. previously ruleunit a was thisunless add we

,any and , ,such For . ruleunit a remove We3.

221

1233222111

21

iii

i

k

kkk

k

uUU

uk

AAA

uuAAuAAuAAuA

kuuuA

uA

uBBABA

22

Example 2.7 G6

||

||

||

right. on the appears symbolstart new a make tostepfirst the

applying ofresult The left. on theshown is original The .1 6

bBbB

SBASBA

aBASASaBASAS

G

SS0

23

Example 2.7 Step 2

bBbB

SBASBA

SASSAaaBASASaaBASAS

SSSS

A

bBbB

εSBASBA

aaBASASaBASAS

SSSS

B

||

|||||||

it.for onscompensati introduce and , rules Remove

|||

|||

it.for onscompensati introduce and , rules Remove 2.

00

00

24

Example 2.7 Step 3

bBbB

ASSAaaBASAbASbA

ASSAaaBASASASSAaaBASAS

ASSAaaBASASASSAaaBASAS

SABA

bBbB

SBASBA

ASSAaaBASASASSAaaBASAS

ASSAaaBASASSS

SSSS

||||||

||||||||

|||| ||||

. Remove . rulesunit Remove

||

||||||||

||||

right. on the Remove left. on the rulesunit Remove 3.

00

00

0

25

Example 2.7 Step 4

bB

aU

SAA

ASSAaUBAAbA

ASSAaaBASAbA

ASSAaUBAAS

ASSAaaBASAS

ASSAaUBAAS

ASSAaaBASAS

|||||

|||||

||||

||||

||||

||||

1

1

1

10

0

26

Pushdown Automata

statecontrol

a a b b input

statecontrol

a a b b input

x

y

z

...

stack

pushdown automaton

finite automaton

27

Definition 2.8

states.accept ofset theis 6.

and state,start theis 5.

function,n transitio theis 2: 4.

alphabet,stack theis 3.

alphabet,input theis 2.

states, ofset theis 1.

and sets, finite all are and ,,, where

),,,,,,( tuple-6 a isautomaton pushdown A

0

0

QF

q

Q

Q

FQ

FqQ

Q

28

Computation

. 3.

.,,

somefor and where),,,(),( 2.

. and 1.

n.computatio theof

branch accepting on the has that contentsstack of sequence

therepresent strings The .conditions next three esatisfy th

,,, strings and ,,, states of sequences if

where,input accepts machine The

*

111

000

*1010

21

Fr

tba

btsatsawrbr

sqr

M

s

sssQrrr

wwwww

m

iiiii

i

mm

im

29

Theorem 2.12

• A language is context free if and only if some pushdown automaton recognizes it.

• Lemma 2.13– If a language is context free, then some

pushdown automaton recognizes it.

• Lemma 2.15– If a pushdown automaton recognizes some

language, then it is context free.

30

Proof of Lemma 2.13CFL Recognized by PDA

)}.,{(),,(

)},,{(),,(

)},,{(),,(

),,( contains ),,(

for notation shorthand be

, where, ),,(),(Let

).,,,,,(PDA construct We

11

232

121

1

1

urq

uqq

uqq

uqsaq

uuusaqur

FqQP

l

l

l

ll

l

31

Proof of Lemma 2.13

)},{(,$),(

)},{(),,(

}in rule a is where|),{(),,(

$)},{(),,(

follows. as define We

shorthand. theimplement toneeded

states ofset theis where,},,{put We

acceptloop

looploop

looploop

loopstart

acceptloopstart

qq

qaaq

RwAwqAq

Sqq

EEqqqQ

32

State Diagram of P

qstart

qloop

qaccept

S$

,$

,Aw for rule Aw

a,a for terminal a

33

Example 2.14

qstart

qloop

qaccept

,Sb

,T

a,a

b,b

,Sb ,T ,a

,Ta ,T,S$

,$

SaTb | b

TTa |

34

Proof of Lemma 2.15 Recognized by PDA CFL

time.same at the

both donot doesbut move), (astack theoff one popsor

move) (astack theonto symbol a pusheseither ition Each trans 3.

accepting. beforestack its emptiesIt 2.

. state,accept single a hasIt 1.

.conditions following the

satisfies machine that generarity losing without assumecan We

.grammar aconstruct We

accept

pop

push

q

P

G

35

Proof of Lemma 2.15

.in rule put the ,each For (3)

.in rule put the ,,,each For (2)

.in rule put the

),,,(),( and ),,(),( if

,, and ,,,,,each For (1)

:are rules theand , },,|{Put

.construct Wegiven. be }){,,,,,(Let

accept0 ,

accept0

GAQp

GAAAQrqp

GbaAA

tbsqaptr

batQsrqp

ASQqpAV

GqqQP

pp

rqprpq

rspq

qqpq

36

ApqAprArq

stack

height

input

string

generated

by Apq

p qr

generated

by Apr

generated

by Arq

37

ApqaArsb

stack

height

input

string

generated

by Apq

p q

r s

generated

by Ars

a b

38

Claim 2.16 If Apq generates x, then x can bring P from p with empty stack to q with empty stack.

.or either is

derivation in this stepfirst The steps. 1 with that Assume

)1 prove and (Assume

stack.empty with stack toempty with

from takesInput . issuch as in rulesonly The . variablesno

contains RHS whoserule a usemust step single a with derivationA

. from of derivation in the steps ofnumber on theInduction Proof.

*

rqprpqrspq

pq

pp

pq

AAAbaAA

kxA

kk

p

pPAG

Ax

:Step Induction

:Basis

39

Proof (continued)

.or either is

derivation in this stepfirst The steps. 1 with that Assume

)1 prove and (Assume

stack.empty with stack toempty with

from takesInput . issuch as in rulesonly The . variablesno

contains RHS whoserule a usemust step single a with derivationA

. from of derivation in the steps ofnumber on theInduction Proof.

*

rqprpqrspq

pq

pp

pq

AAAbaAA

kxA

kk

p

pPAG

Ax

:Step Induction

:Basis

40

Proof (continued)

stack.empty with stack toempty with from bringcan Therefore,

).,,(),( and ),,(),(

, of rule a is Because stack.empty on to

stackempty on from gocan that us tellshypothesisinduction The

. have Westeps. withingcomplete should which , generate Let

: Case

qpPx

tbsqaptr

GbaAAs

rP

aybxkyA

baAA

rspq

rs

rspq

41

Proof (continued)

end. and beginning at the stacksempty with to fromit bringcan

Hence,

end. and beginning at the stacksempty with , to from bringcan

and , to from bringcan

thatus tellshypothesisinduction The

steps. within complete should sderivationBoth . and ,Let

: Case**

qpx

qrPz

rpPy

kzAyAyzx

AAA

rqpr

rqprpq

42

Claim 2.17 If x can bring P from p with empty stack to q with empty stack, Apq generates x.

end. and beginning the

at stacksempty with steps 1in to from brings that Suppose

.1length of nscomputatiofor trueprove

and ,0most at length n computatiofor trueAssume

. rule thehas on,constructiBy

. read to timehasonly .say

state, same at the ends and startsIt steps. 0 hasn computatio The

.input on stacksempty with to from goes

that ofn computatio in the steps ofnumber on theInduction Proof.

kqpPx

k

k

AG

xPp

xqp

P

pp

:step Induction

:Basis

43

Proof (continued)

. Hence, . that us tellshypothesisinduction The

end. and beginning at the stacksempty

withsteps 1 within to from brings input Then, .Let

.in is So, . and

,, ,, somefor ),,(),( and ),,(),( Then,

:end and beginning at theonly empty isstack The

**

xAyA

k-srPyaybx

GbaAAt

baQqrtbsqaptr

pqrs

rspq

Case

44

Proof (continued)

. ,in is Because

. and that us tellshypothesisinduction The

steps. within complete nscomputatio twoThe

. to from brings and , to from brings where,Let

:endor beginning theother than stateat empty becomesstack The

*

**

xyzAGAAA

zAyA

k

qrPzrpPyyzx

r

pqrqprpq

rqpr

Case

45

Corollary 2.18 Every regular language is context free.

context-free languages

regular

languages

46

Theorem 2.19 [Pumping Lemma]

.|| 3.

and ,0|| 2.

, ,0each For 1.

:conditions following thesatisfying

into devided bemay then ,least at length of in stringany is if where,

length) pumping (the number a is e then therlanguage, free-context a is If

pvxy

vy

Azxyuvi

uvxyzs

spAs

pA

ii

47

Proof T

R

R

u v x y z

T

R

u v

T

R

u

R

R

v x y

y z

x

z

48

Proof

obvious. is 1Condition

path. in thelowest therepeats that one thebe let e,convenienclater For

repeats. ,say variable,some Thus

. terminalsofconsist leavesonly since variables1||least at havemust

which,2||least at length havemust path longest The

.2||least at height requires least at length of stringany for nodes)

ofnumber smallest the(having treeparse a Then, .set We

.most at is

generated string oflength the,most at is treeparse theofheight theIf

children. than more havecan node no tree,parseany In

rule. a of RHS in the symbols ofnumber maximum thebe 2Let

1||2||

R

R

V

V

Vps

bbp

b

h

b

b

VV

h

49

Proof

.most at length of string a generatecan

height thisof A tree high. 2||most at is generates wheresubtree theSo,

path. on the variables1|| bottom in the repeatsit that so chose We

:3Condition

slide. previous in the figure theSee broken. is of

minimality the, they wereIf .not are and both that requires 2Condition

2|| pb

VvxyR

VR

yv

V

50

Example 2.20 .Let }.0|{ uvxyzcbasncbaB pppnnn

ap bp cp

v y

v y

v

y

Case

v and y are homogeneous

v y

Case

v or y is heterogeneous

or

v y

v y

51

Example 2.21 .Let }.0|{ pppkji cbaskjicbaC

ap bp cp

v y

v y

v

y

Case v and y are homogeneous

v y

Case v or y is heterogeneous

or

See if uv0xy0z=uxz breaks the balance.

See if uv2xy2z breaks the balance.

See if uv2xy2z or uxz destroys the order.

v y

v y

52

Example 2.22 .1010Let }*}.1,0{|{ ppppswwwD

0p 0p1p 1p

vxy

0p 0p1p 1p

vxy

0p 0p1p 1p

vxy

See if the first half of uv2xy2z begins with 0 while the latter half begins with 1.

See if the first half of uv2xy2z ends with 0 while the latter half ends with 1.

See if uv0xy0z=uxz =0p1i0j1p, where i and j can not both be p.