applications of regular closure

77
1 Applications of Regular Closure

Upload: mckile

Post on 06-Jan-2016

25 views

Category:

Documents


2 download

DESCRIPTION

Applications of Regular Closure. The intersection of a context-free language and a regular language is a context-free language. Regular Closure. context free. regular. context-free. An Application of Regular Closure. Prove that:. is context-free. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Applications  of  Regular Closure

1

Applications of

Regular Closure

Page 2: Applications  of  Regular Closure

2

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

1L context free

2L regular

21 LL

context-free

Regular Closure

Page 3: Applications  of  Regular Closure

3

An Application of Regular Closure

Prove that: }100:{ nbaL nn

is context-free

Page 4: Applications  of  Regular Closure

4

}{ nnbaWe know:

is context-free

Page 5: Applications  of  Regular Closure

5

}{ 1001001 baL is regular

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

We also know:

Page 6: Applications  of  Regular Closure

6

}{}){( 100100*1 babaL

regular

}{ nnbacontext-free

1}{ Lba nn is context-free

Lnba

Lbann

nn

}100:{

}{ 1

context-free

(regular closure)

Page 7: Applications  of  Regular Closure

7

Another Application of Regular Closure

Prove that: }:{ cba nnnwL

is not context-free

Page 8: Applications  of  Regular Closure

8

}:{ cba nnnwL

}{*}**{ nnn cbacbaL

context-free regular context-free

If is context-free

Then

Impossible!!!

Therefore, is not context free L

(regular closure)

Page 9: Applications  of  Regular Closure

9

Decidable Propertiesof

Context-Free Languages

Page 10: Applications  of  Regular Closure

10

Membership Question:

for context-free grammarfind if string

G)(GLw

Membership Algorithms: Parsers

• Exhaustive search parser

• CYK parsing algorithm

Page 11: Applications  of  Regular Closure

11

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 12: Applications  of  Regular Closure

12

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 graph then the language is infinite

Page 13: Applications  of  Regular Closure

13

Example:

cBSC

bbbBB

aaCbA

ABS

|

|

S

A

B

C

Dependency graph Infinite language

Page 14: Applications  of  Regular Closure

14

cBSC

bbbBB

aaCbA

ABS

|

|

acbbSbbbacBSbBaCbBABS

ii bbbSacbb

bbbSacbbacbbSbbbS

)()(

)()( 22

Page 15: Applications  of  Regular Closure

15

The Pumping Lemmafor

Context-Free Languages

Page 16: Applications  of  Regular Closure

16

Take an infinite context-free language

Example:

bB

SbB

aBbA

ABS

Generates an infinite numberof different strings

Page 17: Applications  of  Regular Closure

17abbabbbbabbabbBb

abbaBbBbabbABbabbSb

abbBaBbBABS

bB

SbB

aBbA

ABS

A derivation:

Page 18: Applications  of  Regular Closure

18

S

A B

bBa

b

bS

A B

bBa

b

b

Derivation tree string abbabbbb

Page 19: Applications  of  Regular Closure

19

S

A B

bBa

b

bS

A B

bBa

b

b

repeated

Derivation tree string abbabbbb

Page 20: Applications  of  Regular Closure

20

B

bS

A B

bBa

b

b

aBbbbaBbBb

ABbSbB

bB

Page 21: Applications  of  Regular Closure

21

B

bS

A B

bBa b

aBbbbB

Repeated Part

Page 22: Applications  of  Regular Closure

22

B

bS

A B

ba bB

bS

A B

bBa b

aaBbbbbbbaBbbbB

Another possible derivation

aBbbbB

Page 23: Applications  of  Regular Closure

23

S

A B

bBa

b

bS

A B

ba bB

abbaBbbbS

aBbbbB

Page 24: Applications  of  Regular Closure

24bbabbaaBbbbbabbaBbbbS

S

A B

bBa

b

bS

A B

ba bB

bS

A B

bBa b

aBbbbB

Page 25: Applications  of  Regular Closure

25bbabbaabbbbbbbabbaaBbbbbS

S

A B

bBa

b

bS

A B

ba bB

bS

A B

bBa b bBb

Page 26: Applications  of  Regular Closure

26

bbabbaabbbbbS

Therefore, the string

bbabbaabbbbb

is also generated by the grammar

Page 27: Applications  of  Regular Closure

27

abbaBbbbS

aBbbbB bBWe know:

We also know this string is generated:

abbaabbbb

abbaBbbbS

Page 28: Applications  of  Regular Closure

28

abbaBbbbS

aBbbbB bBWe know:

Therefore, this string is also generated:

bbabbaabbbbb

bbabbaaBbbbb

abbaBbbbS

Page 29: Applications  of  Regular Closure

29

abbaBbbbS

aBbbbB bBWe know:

Therefore, this string is also generated:

bbbbbbbaabba

bbbbbbBaabba

bbbbbbBaabba

abbaBbbbS

22

22

)()(

)()(

)()(

Page 30: Applications  of  Regular Closure

30

Therefore, this string is also generated:

bbbbbbbaabba

bbbbbbBaabba

abbaBbbbS

ii

ii

)()(

)()(

abbaBbbbS

aBbbbB bBWe know:

Page 31: Applications  of  Regular Closure

31

Therefore, knowing that

abbabbbb

is generated by grammar , we also know that

bbbbbbbaabba ii )()(

is generated by

G

G

Page 32: Applications  of  Regular Closure

32

In general:

We are given an infinite context-free grammar G

Assume has no unit-productions no -productions

G

Page 33: Applications  of  Regular Closure

33

Take a string with length bigger than

Some variable must be repeatedin the derivation of

)(GLw

(Number of productions) x(Largest right side of a production)

m >

w

Consequence:

Page 34: Applications  of  Regular Closure

34

S

A

A

x

Last repeated variable

u z

v y

uvxyzw

terminalsof strings :,,,, zyxvu

String

repeated

Page 35: Applications  of  Regular Closure

35

S

A

A

x

u z

v y

uAzS

vAyA

xA

Possiblederivations:

Page 36: Applications  of  Regular Closure

36

uAzS vAyA

xA

We know:

This string is also generated:

uxzuAzS*

zxyuv 00

Page 37: Applications  of  Regular Closure

37

This string is also generated:

uvxyzuvAyzuAzS**

The original zxyuvw 11

uAzS vAyA

xA

We know:

Page 38: Applications  of  Regular Closure

38

This string is also generated:

uvvxyyzuvvAyyzuvAyzuAzS***

zxyuv 22

uAzS vAyA

xA

We know:

Page 39: Applications  of  Regular Closure

39

This string is also generated:

uvvvxyyyzuvvvAyyyz

uvvAyyzuvAyzuAzS**

**

zxyuv 33

uAzS vAyA

xA

We know:

Page 40: Applications  of  Regular Closure

40

This string is also generated:

yyyzvxyuvvv

yyyzvAyuvvv

uvvvAyyyz

uvvAyyzuvAyzuAzS

zxyuv ii

uAzS vAyA

xA

We know:

*

***

*****

Page 41: Applications  of  Regular Closure

41

Therefore, any string of the form

zxyuv ii

is generated by the grammar G

0i

Page 42: Applications  of  Regular Closure

42

knowing that )(GLuvxyz

we also know that )(GLzxyuv ii

Therefore,

Page 43: Applications  of  Regular Closure

43

S

A

A

x

u z

v y

mvxy ||Observation:

Since is the last repeated variableA

Page 44: Applications  of  Regular Closure

44

S

A

A

x

u z

v y

1|| vyObservation:

Since there are no unit or productions

Page 45: Applications  of  Regular Closure

45

The Pumping Lemma:

there exists an integer such that m

for any string mwLw || ,

we can write

For infinite context-free language L

uvxyzw

with lengths 1||and || vymvxy

and it must be:

0 allfor , iLzxyuv ii

Page 46: Applications  of  Regular Closure

46

Applicationsof

The Pumping Lemma

Page 47: Applications  of  Regular Closure

47

Context-free languages

}0:{ nba nn

Non-context free languages

}0:{ ncba nnn

Page 48: Applications  of  Regular Closure

48

Theorem:The language

}0:{ ncbaL nnn

is not context free

Proof: Use the Pumping Lemmafor context-free languages

Page 49: Applications  of  Regular Closure

49

}0:{ ncbaL nnn

Assume for contradiction that

is context-free

Since is context-free and infinitewe can apply the pumping lemma

L

L

Page 50: Applications  of  Regular Closure

50

Pumping Lemma gives a magic numbersuch that:

m

Pick any string with length Lw mw ||

We pick:mmm cbaw

}0:{ ncbaL nnn

Page 51: Applications  of  Regular Closure

51

}0:{ ncbaL nnn

mmm cbaw

We can write: uvxyzw

with lengths andmvxy || 1|| vy

Page 52: Applications  of  Regular Closure

52

}0:{ ncbaL nnn

Pumping Lemma says:

Lzxyuv ii for all 0i

mmm cbawuvxyzw mvxy || 1|| vy

Page 53: Applications  of  Regular Closure

53

}0:{ ncbaL nnn

We examine all the possible locationsof string in vxy w

mmm cbawuvxyzw mvxy || 1|| vy

Page 54: Applications  of  Regular Closure

54

}0:{ ncbaL nnn

Case 1: vxy is withinma

ccccccbbbbbbaaaaaa .........

vxy

m m m

u z

mmm cbawuvxyzw mvxy || 1|| vy

Page 55: Applications  of  Regular Closure

55

}0:{ ncbaL nnn

Case 1: and consist from only v y a

ccccccbbbbbbaaaaaa .........

vxy

m m m

u z

mmm cbawuvxyzw mvxy || 1|| vy

Page 56: Applications  of  Regular Closure

56

}0:{ ncbaL nnn

Case 1:

ccccccbbbbbbaaaaaaaaaaaa .........22xyv

km m m

u z

Repeating andv y

mmm cbawuvxyzw mvxy || 1|| vy

1k

Page 57: Applications  of  Regular Closure

57

}0:{ ncbaL nnn

Case 1:

ccccccbbbbbbaaaaaaaaaaaa .........22xyv

km m m

u z

From Pumping Lemma: Lzxyuv 22

mmm cbawuvxyzw mvxy || 1|| vy

1k

Page 58: Applications  of  Regular Closure

58

}0:{ ncbaL nnn

Case 1:

Lcbazxyuv mmkm 22

From Pumping Lemma: Lzxyuv 22

However:

Contradiction!!!

mmm cbawuvxyzw mvxy || 1|| vy

1k

Page 59: Applications  of  Regular Closure

59

}0:{ ncbaL nnn

Case 2: vxy is withinmb

ccccccbbbbbbaaaaaa .........

vxy

m m m

u z

mmm cbawuvxyzw mvxy || 1|| vy

Page 60: Applications  of  Regular Closure

60

}0:{ ncbaL nnn

Case 2:

ccccccbbbbbbaaaaaa .........

vxy

m m m

u z

Similar analysis with case 1

mmm cbawuvxyzw mvxy || 1|| vy

Page 61: Applications  of  Regular Closure

61

}0:{ ncbaL nnn

Case 3: vxy is withinmc

ccccccbbbbbbaaaaaa .........

vxy

m m m

u z

mmm cbawuvxyzw mvxy || 1|| vy

Page 62: Applications  of  Regular Closure

62

}0:{ ncbaL nnn

Case 3:

ccccccbbbbbbaaaaaa .........

vxy

m m m

u z

Similar analysis with case 1

mmm cbawuvxyzw mvxy || 1|| vy

Page 63: Applications  of  Regular Closure

63

}0:{ ncbaL nnn

Case 4: vxy overlaps and ma

ccccccbbbbbbaaaaaa .........

vxy

m m m

u z

mb

mmm cbawuvxyzw mvxy || 1|| vy

Page 64: Applications  of  Regular Closure

64

}0:{ ncbaL nnn

Case 4:

ccccccbbbbbbaaaaaa .........

vxy

m m m

u z

Possibility 1: contains onlycontains onlyvy

ab

mmm cbawuvxyzw mvxy || 1|| vy

Page 65: Applications  of  Regular Closure

65

}0:{ ncbaL nnn

ccccccbbbbbbbbbbaaaaaaaaaa .........22xyv

1km 2km m

u z

mmm cbawuvxyzw mvxy || 1|| vy

Case 4:Possibility 1: contains onlycontains onlyvy

ab121 kk

Page 66: Applications  of  Regular Closure

66

}0:{ ncbaL nnn

Case 4:

ccccccbbbbbbbbbbaaaaaaaaaa .........22xyv

1km 2km m

u z

From Pumping Lemma: Lzxyuv 22

mmm cbawuvxyzw mvxy || 1|| vy

121 kk

Page 67: Applications  of  Regular Closure

67

}0:{ ncbaL nnn

Case 4:From Pumping Lemma: Lzxyuv 22

Lcbazxyuv mkmkm 2122However:

Contradiction!!!

mmm cbawuvxyzw mvxy || 1|| vy

121 kk

Page 68: Applications  of  Regular Closure

68

}0:{ ncbaL nnn

Case 4:

ccccccbbbbbbaaaaaa .........

vxy

m m m

u z

Possibility 2: contains andcontains onlyvy

ab

mmm cbawuvxyzw mvxy || 1|| vy

b

Page 69: Applications  of  Regular Closure

69

}0:{ ncbaL nnn

Case 4:

ccccccbbbbbbbbbbbaaaaabbaabaaa .........

22xyv

m km m

u z

Possibility 2: contains andcontains onlyvy

ab

mmm cbawuvxyzw mvxy || 1|| vy

b

1k 2k

121 kkk

Page 70: Applications  of  Regular Closure

70

}0:{ ncbaL nnn

mmm cbawuvxyzw mvxy || 1|| vy

Case 4:From Pumping Lemma: Lzxyuv 22

ccccccbbbbbbbbbbbaaaaabbaabaaa .........

22xyv

m km m

u z

1k 2k

121 kkk

Page 71: Applications  of  Regular Closure

71

}0:{ ncbaL nnn

mmm cbawuvxyzw mvxy || 1|| vy

Case 4:From Pumping Lemma: Lzxyuv 22

Lcbabazxyuv mkmkkm 2122

However:

Contradiction!!!

121 kkk

Page 72: Applications  of  Regular Closure

72

}0:{ ncbaL nnn

Case 4:

ccccccbbbbbbaaaaaa .........

vxy

m m m

u z

Possibility 3: contains onlycontains andvy

ab

mmm cbawuvxyzw mvxy || 1|| vy

a

Page 73: Applications  of  Regular Closure

73

}0:{ ncbaL nnn

Case 4:Possibility 3: contains onlycontains andvy

ab

mmm cbawuvxyzw mvxy || 1|| vy

a

Similar analysis with Possibility 2

Page 74: Applications  of  Regular Closure

74

}0:{ ncbaL nnn

Case 5: vxy overlaps and mb

ccccccbbbbbbaaaaaa .........

vxy

m m m

u z

mc

mmm cbawuvxyzw mvxy || 1|| vy

Page 75: Applications  of  Regular Closure

75

}0:{ ncbaL nnn

Case 5:

mmm cbawuvxyzw mvxy || 1|| vy

Similar analysis with case 4

ccccccbbbbbbaaaaaa .........

vxy

m m m

u z

Page 76: Applications  of  Regular Closure

76

There are no other cases to consider

(since , string cannot overlap , and at the same time)

vxymvxy ||

ma mb mc

Page 77: Applications  of  Regular Closure

77

In all cases we obtained a contradiction

Therefore: The original assumption that

}0:{ ncbaL nnn

is context-free must be wrong

Conclusion: is not context-freeL