1 positive properties of context-free languages. 2 context-free languages are closed under: union is...

38
1 Positive Properties of Context-Free languages

Post on 19-Dec-2015

228 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Positive Properties of Context-Free languages. 2 Context-free languages are closed under: Union is context free is context-free

1

Positive Propertiesof

Context-Free languages

Page 2: 1 Positive Properties of Context-Free languages. 2 Context-free languages are closed under: Union is context free is context-free

2

Context-free languages are closed under: Union

1L is context free

2L is context free

21 LL

is context-free

Page 3: 1 Positive Properties of Context-Free languages. 2 Context-free languages are closed under: Union is context free is context-free

3

Union Example:

|11 baSS

|| 222 bbSaaSS

Union grammar:

}{1nnbaL

}{2RwwL

21 | SSS }{}{ Rnn wwbaL

Page 4: 1 Positive Properties of Context-Free languages. 2 Context-free languages are closed under: Union is context free is context-free

4

In general:

Take any two context-free grammarswith start variables 21, SS

The union grammar has start variable 21 | SSS

G

21,GG

and )()()( 21 GLGLGL

is context-free

Page 5: 1 Positive Properties of Context-Free languages. 2 Context-free languages are closed under: Union is context free is context-free

5

Context-free languages are closed under: Concatenation

1L is context free

2L is context free

21LL

is context-free

Page 6: 1 Positive Properties of Context-Free languages. 2 Context-free languages are closed under: Union is context free is context-free

6

Concatenation Example:

|11 baSS

|| 222 bbSaaSS

Concatenation grammar:

}{1nnbaL

}{2RwwL

21SSS }}{{ Rnn wwbaL

Page 7: 1 Positive Properties of Context-Free languages. 2 Context-free languages are closed under: Union is context free is context-free

7

In general:

Take any two context-free grammarswith start variables 21, SS

The concatenation grammar has start variable 21 | SSS

G

21,GG

and )()()( 21 GLGLGL is context-free

Page 8: 1 Positive Properties of Context-Free languages. 2 Context-free languages are closed under: Union is context free is context-free

8

Context-free languages are closed under: Star-operation

L is context free *L is context-free

Page 9: 1 Positive Properties of Context-Free languages. 2 Context-free languages are closed under: Union is context free is context-free

9

Star operation Example:

|aSbS

Star grammar:

}{ nnbaL

|11 SSS *}{ nnbaL

Page 10: 1 Positive Properties of Context-Free languages. 2 Context-free languages are closed under: Union is context free is context-free

10

In general:

Take any context-free grammarswith start variable S

The concatenation grammar has start variable

1G

G

and *)()( 1 GLGL is context-free

|11 SSS

Page 11: 1 Positive Properties of Context-Free languages. 2 Context-free languages are closed under: Union is context free is context-free

11

Negative Propertiesof

Context-Free Languages

Page 12: 1 Positive Properties of Context-Free languages. 2 Context-free languages are closed under: Union is context free is context-free

12

Context-free languagesare not closed under: intersection

1L is context free

2L is context free

21 LL

not necessarilycontext-free

Page 13: 1 Positive Properties of Context-Free languages. 2 Context-free languages are closed under: Union is context free is context-free

13

Counter Example:

}{1mnn cbaL

|

|

cCC

aAbA

ACS

Context-free:

}{2mmn cbaL

|

|

bBbB

aAA

ABS

Context-free:

}{21nnn cbaLL NOT context-free

Page 14: 1 Positive Properties of Context-Free languages. 2 Context-free languages are closed under: Union is context free is context-free

14

Context-free languagesare not closed under: complement

L is context free L not necessarilycontext-free

Page 15: 1 Positive Properties of Context-Free languages. 2 Context-free languages are closed under: Union is context free is context-free

15

Counter Example:

}{1mnn cbaL

|

|

cCC

aAbA

ACS

Context-free:

}{2mmn cbaL

|

|

bBbB

aAA

ABS

Context-free:

}{2121nnn cbaLLLL

NOT context-free

Page 16: 1 Positive Properties of Context-Free languages. 2 Context-free languages are closed under: Union is context free is context-free

16

Intersectionof

Context-free languagesand

Regular Languages

Page 17: 1 Positive Properties of Context-Free languages. 2 Context-free languages are closed under: Union is context free is context-free

17

The intersection of a context-free language and a regular languageis a context-free language

1L context free

2L regular

21 LL

context-free

Page 18: 1 Positive Properties of Context-Free languages. 2 Context-free languages are closed under: Union is context free is context-free

18

1Lfor for 2LNPDA

1M

DFA

2M

Construct a new NPDA machinethat accepts

Machine Machine

M

21 LL

context-free regular

M simulates in parallel and 1M 2M

Page 19: 1 Positive Properties of Context-Free languages. 2 Context-free languages are closed under: Union is context free is context-free

19

1M 2M

1q 2qcba ,

transition

1p 2pa

transition

NPDA DFA

11, pq cba ,

transition

MNPDA

22 , pq

Page 20: 1 Positive Properties of Context-Free languages. 2 Context-free languages are closed under: Union is context free is context-free

20

1M 2M

0q

initial state

0p

initial state

NPDA DFA

Initial state

MNPDA

00 , pq

Page 21: 1 Positive Properties of Context-Free languages. 2 Context-free languages are closed under: Union is context free is context-free

21

1M 2M

1q

final state

1p

final states

NPDA DFA

final states

MNPDA

11, pq

2p

21, pq

Page 22: 1 Positive Properties of Context-Free languages. 2 Context-free languages are closed under: Union is context free is context-free

22

M simulates in parallel and 1M 2M

M accepts string w if and only if

accepts string and w1M

accepts string w2M

)()()( 21 MLMLML

Page 23: 1 Positive Properties of Context-Free languages. 2 Context-free languages are closed under: Union is context free is context-free

23

Therefore: )()( 21 MLML

(since is NPDA) M

is context-free

21 LL is context-free

Page 24: 1 Positive Properties of Context-Free languages. 2 Context-free languages are closed under: Union is context free is context-free

24

Example Applications of Regular Closure

Prove that: }100:{ nbaL nn

is context-free

Page 25: 1 Positive Properties of Context-Free languages. 2 Context-free languages are closed under: Union is context free is context-free

25

}{ nnbaWe know:

is context-free

Page 26: 1 Positive Properties of Context-Free languages. 2 Context-free languages are closed under: Union is context free is context-free

26

}{ 1001001 baL is regular

}{}){( 100100*1 babaL is regular

We also know:

Page 27: 1 Positive Properties of Context-Free languages. 2 Context-free languages are closed under: Union is context free is context-free

27

}{}){( 100100*1 babaL

is regular

}{ nnba

is context-free

1}{ Lba nn

is context-free

1}{

}100:{

Lba

nbaLnn

nn

is context-free

Page 28: 1 Positive Properties of Context-Free languages. 2 Context-free languages are closed under: Union is context free is context-free

28

Another application

Prove that: }:{ cba nnnwL

is not context-free

Page 29: 1 Positive Properties of Context-Free languages. 2 Context-free languages are closed under: Union is context free is context-free

29

}:{ cba nnnwL

}{*}**{ nnn cbacbaL

context-free regular Is context-free

If is context-free

Then

Impossible!!!

Therefore, is not context free L

Page 30: 1 Positive Properties of Context-Free languages. 2 Context-free languages are closed under: Union is context free is context-free

30

Decidable Propertiesof

Context-Free Languages

Page 31: 1 Positive Properties of Context-Free languages. 2 Context-free languages are closed under: Union is context free is context-free

31

Membership Question:

for grammarfind if string

G)(GLw

Page 32: 1 Positive Properties of Context-Free languages. 2 Context-free languages are closed under: Union is context free is context-free

32

Membership Question:

for grammarfind if string

G)(GLw

Membership Algorithms:

Parsers

Exhaustive search parser

CYKparsing algorithm

Page 33: 1 Positive Properties of Context-Free languages. 2 Context-free languages are closed under: Union is context free is context-free

33

Empty Language Question:

for grammar find if

G)(GL

Page 34: 1 Positive Properties of Context-Free languages. 2 Context-free languages are closed under: Union is context free is context-free

34

Empty Language Question:

for grammar find if

G)(GL

Algorithm:

Check if start variable is useless S

Remove useless variables

Page 35: 1 Positive Properties of Context-Free languages. 2 Context-free languages are closed under: Union is context free is context-free

35

Infinite Language Question:

for grammar find if is infinite

G)(GL

Page 36: 1 Positive Properties of Context-Free languages. 2 Context-free languages are closed under: Union is context free is context-free

36

Infinite Language Question:

for grammar find if is infinite

G)(GL

Algorithm:

• Remove useless variables• Remove unit and lambda productions

• Create the dependency graph for variables

• If there is a loop in the dependency graph the language is infinite

Page 37: 1 Positive Properties of Context-Free languages. 2 Context-free languages are closed under: Union is context free is context-free

37

Example:

cBSC

bbbBB

aaCbA

ABS

|

|

S

A

B

C

Dependency graph infinite

Page 38: 1 Positive Properties of Context-Free languages. 2 Context-free languages are closed under: Union is context free is context-free

38

cBSC

bbbBB

aaCbA

ABS

|

|

acbbSbbbacBSbBaCbBABS

ii bbbSacbb

bbbSacbbacbbSbbbS

)()(

)()( 22