41
Summer 2004 COMP 335 1 Closure Properties of Context-Free languages

Closure Properties Context-Free languages

  • Upload
    others

  • View
    9

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Closure Properties Context-Free languages

Summer 2004 COMP 335 1

Closure Propertiesof

Context-Free languages

Page 2: Closure Properties Context-Free languages

Summer 2004 COMP 335 2

Context-free languages are closed under: Union

1L is context free

2L is context free21 LL ∪

is context-free

Union

Page 3: Closure Properties Context-Free languages

Summer 2004 COMP 335 3

Example

λ|11 baSS →

λ|| 222 bbSaaSS →

Union

}{1nnbaL =

}{2RwwL =

21 | SSS →}{}{ Rnn wwbaL ∪=

Language Grammar

Page 4: Closure Properties Context-Free languages

Summer 2004 COMP 335 4

In general:

The grammar of the unionhas new start variableand additional production 21 | SSS →

For context-free languageswith context-free grammarsand start variables

21, LL

21, GG21, SS

21 LL ∪S

Page 5: Closure Properties Context-Free languages

Summer 2004 COMP 335 5

Context-free languages are closed under: Concatenation

1L is context free

2L is context free21LL

is context-free

Concatenation

Page 6: Closure Properties Context-Free languages

Summer 2004 COMP 335 6

Example

λ|11 baSS →

λ|| 222 bbSaaSS →

Concatenation

}{1nnbaL =

}{2RwwL =

21SSS →}}{{ Rnn wwbaL =

Language Grammar

Page 7: Closure Properties Context-Free languages

Summer 2004 COMP 335 7

In general:

The grammar of the concatenationhas new start variableand additional production 21SSS →

For context-free languageswith context-free grammarsand start variables

21, LL

21, GG21, SS

21LLS

Page 8: Closure Properties Context-Free languages

Summer 2004 COMP 335 8

Context-free languages are closed under: Star-operation

L is context free *L is context-free

Star Operation

Page 9: Closure Properties Context-Free languages

Summer 2004 COMP 335 9

λ|aSbS →}{ nnbaL =

λ|11 SSS →*}{ nnbaL =

Example

Language Grammar

Star Operation

Page 10: Closure Properties Context-Free languages

Summer 2004 COMP 335 10

In general:

The grammar of the star operationhas new start variableand additional production

For context-free languagewith context-free grammarand start variable

LGS

*L1S

λ|11 SSS →

Page 11: Closure Properties Context-Free languages

Summer 2004 COMP 335 11

Negative Propertiesof

Context-Free Languages

Page 12: Closure Properties Context-Free languages

Summer 2004 COMP 335 12

Context-free languagesare not closed under: intersection

1L is context free

2L is context free

21 LL ∩

not necessarilycontext-free

Intersection

Page 13: Closure Properties Context-Free languages

Summer 2004 COMP 335 13

Example

}{1mnn cbaL =

λλ

||

cCCaAbAACS

→→→

Context-free:

}{2mmn cbaL =

λλ|

|bBcBaAAABS

→→→

Context-free:

}{21nnn cbaLL =∩ NOT context-free

Intersection

Page 14: Closure Properties Context-Free languages

Summer 2004 COMP 335 14

Context-free languagesare not closed under: complement

L is context free L not necessarilycontext-free

Complement

Page 15: Closure Properties Context-Free languages

Summer 2004 COMP 335 15

}{2121nnn cbaLLLL =∩=∪

NOT context-free

Example

}{1mnn cbaL =

λλ

||

cCCaAbAACS

→→→

Context-free:

}{2mmn cbaL =

λλ|

|bBcBaAAABS

→→→

Context-free:

Complement

Page 16: Closure Properties Context-Free languages

Summer 2004 COMP 335 16

Intersectionof

Context-free languagesand

Regular Languages

Page 17: Closure Properties Context-Free languages

Summer 2004 COMP 335 17

The intersection ofa context-free language anda regular language

is a context-free language

1L context free

2L regular21 LL ∩

context-free

Page 18: Closure Properties Context-Free languages

Summer 2004 COMP 335 18

1Lfor for 2LNPDA

1MDFA

2M

Construct a new NPDA machinethat accepts

Machine Machine

M21 LL ∩

context-free regular

M simulates in parallel and 1M 2M

Page 19: Closure Properties Context-Free languages

Summer 2004 COMP 335 19

1M 2M

1q 2qcba →,

transition1p 2pa

transition

NPDA DFA

11, pq cba →,

transition

MNPDA

22, pq

Page 20: Closure Properties Context-Free languages

Summer 2004 COMP 335 20

1M 2M

1q 2qcb →,λ

transition1p

NPDA DFA

11, pq cb →,λ

transition

MNPDA

12, pq

Page 21: Closure Properties Context-Free languages

Summer 2004 COMP 335 21

1M 2M

0q

initial state0p

initial state

NPDA DFA

Initial state

MNPDA

00, pq

Page 22: Closure Properties Context-Free languages

Summer 2004 COMP 335 22

1M 2M

1q

final state

1p

final states

NPDA DFA

final states

MNPDA

11, pq

2p

21, pq

Page 23: Closure Properties Context-Free languages

Summer 2004 COMP 335 23

Example:

λλλ →, λλλ →, λλ →,$

1, →λb1, →λa

λ→1,dλ→1,c

0q 1q 2q 3q

1M

}},{,},{|,|||:{ *2

*121211 dcwbawwwwwL ∈∈==

NPDA

context-free

Page 24: Closure Properties Context-Free languages

Summer 2004 COMP 335 24

0p

2M

*2 },{ caL =

ca,

DFA

regular

Page 25: Closure Properties Context-Free languages

Summer 2004 COMP 335 25

Automaton for: }0:{21 ≥=∩ ncaLL nn

λλλ →, λλλ →,

1, →λa λ→1,c

00, pq 01, pq 02, pq 03, pq

NPDA M

context-free

λλ →,$

Page 26: Closure Properties Context-Free languages

Summer 2004 COMP 335 26

M simulates in parallel and 1M 2M

M accepts string w if and only if

accepts string and w1Maccepts string w2M

)()()( 21 MLMLML ∩=

In General:

Page 27: Closure Properties Context-Free languages

Summer 2004 COMP 335 27

Therefore:

)()( 21 MLML ∩

is NPDAM

is context-free

21 LL ∩ is context-free

Page 28: Closure Properties Context-Free languages

Summer 2004 COMP 335 28

Applications of

Regular Closure

Page 29: Closure Properties Context-Free languages

Summer 2004 COMP 335 29

The intersection ofa context-free language anda regular language

is a context-free language

1L context free

2L regular21 LL ∩

context-free

Regular Closure

Page 30: Closure Properties Context-Free languages

Summer 2004 COMP 335 30

An Application of Regular Closure

Prove that: }0,100:{ ≥≠= nnbaL nn

is context-free

Page 31: Closure Properties Context-Free languages

Summer 2004 COMP 335 31

}0:{ ≥nba nn

We know:

is context-free

Page 32: Closure Properties Context-Free languages

Summer 2004 COMP 335 32

}{ 1001001 baL = is regular

}{}){( 100100*1 babaL −+= is regular

We also know:

Page 33: Closure Properties Context-Free languages

Summer 2004 COMP 335 33

}{}){( 100100*1 babaL −+=

regular}{ nnba

context-free

1}{ Lba nn ∩ context-free

LnnbaLba nnnn =≥≠=∩ }0,100:{}{ 1is context-free

(regular closure)

Page 34: Closure Properties Context-Free languages

Summer 2004 COMP 335 34

Another Application of Regular Closure

Prove that: }:{ cba nnnwL ===

is not context-free

Page 35: Closure Properties Context-Free languages

Summer 2004 COMP 335 35

}:{ cba nnnwL ===

}{*}**{ nnn cbacbaL =∩

context-free regular context-free

If is context-free

Then

Impossible!!!

Therefore, is not context free L

(regular closure)

Page 36: Closure Properties Context-Free languages

Summer 2004 COMP 335 36

Decidable Propertiesof

Context-Free Languages

Page 37: Closure Properties Context-Free languages

Summer 2004 COMP 335 37

Membership Question:

for context-free grammarfind if string

G)(GLw∈

Membership Algorithms: Parsers

• Exhaustive search parser

• CYK parsing algorithm

Page 38: Closure Properties Context-Free languages

Summer 2004 COMP 335 38

Empty Language Question:

for context-free grammar find if

G∅=)(GL

Algorithm:

S

1. Remove useless variables

2. Check if start variable is useless

Page 39: Closure Properties Context-Free languages

Summer 2004 COMP 335 39

Infinite Language Question:for context-free grammar find if is infinite

G)(GL

Algorithm:

1. Remove useless variables

2. Remove unit and productions

3. Create dependency graph for variables

4. If there is a loop in the dependency graphthen the language is infinite

λ

Page 40: Closure Properties Context-Free languages

Summer 2004 COMP 335 40

Example:

cBSCbbbBB

aaCbAABS

→→→→

||

S

A

B

C

Dependency graph Infinite language

Page 41: Closure Properties Context-Free languages

Summer 2004 COMP 335 41

cBSCbbbBB

aaCbAABS

→→→→

||

acbbSbbbacBSbBaCbBABS ⇒⇒⇒⇒

ii bbbSacbb

bbbSacbbacbbSbbbS

)()(

)()( 22

∗∗

⇒⇒