Top Banner
1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 10 Mälardalen University 2012
154

CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 10

Jan 01, 2016

Download

Documents

CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 10 Mälardalen University 2012. Content The Pumping Lemma for CFL Applications of the Pumping Lemma for CFL Example of Midterm Exam 2 (CFL). The Pumping Lemma for Context-Free Languages. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

1

CDT314

FABER

Formal Languages, Automata and Models of Computation

Lecture 10

Mälardalen University

2012

Page 2: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

2

Content

The Pumping Lemma for CFLApplications of the Pumping Lemma for CFL

Example of Midterm Exam 2 (CFL)

Page 3: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

3

The Pumping Lemmafor

Context-Free Languages

Based on C Busch, RPI, Models of Computation

Page 4: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

4

Take an infinite context-free language.

Example:

bB

SbB

aBbA

ABS

It generates an infinite number of different strings:

Page 5: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

5

abbabbbbabbabbBb

abbaBbBbabbABbabbSb

abbBaBbBABS

A derivation

bB

SbB

aBbA

ABS

Page 6: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

6

S

A B

bBa

b

bS

A B

bBa

b

b

Derivation tree string abbabbbb

bB

SbB

aBbA

ABS

Page 7: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

7

S

A B

bBa

b

bS

A B

bBa

b

b

Derivation tree string abbabbbb

repeated

Page 8: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

8

B

bS

A B

bBa

b

b

aBbbbaBbBb

ABbSbB

bB

Page 9: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

9

Repeated part

aBbbbB

B

bS

A B

bBa b

Page 10: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

10aaBbbbbbbaBbbbB

Another possible derivation

aBbbbB

B

bS

A B

ba bB

bS

A B

bBa b

Page 11: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

11

aBbbbB

abbaBbbbS

S

A B

bBa

b

bS

A B

ba bB

derivation

Page 12: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

12

aBbbbB

S

A B

bBa

b

bS

A B

ba bB

bS

A B

bBa b

bbabbaaBbbbbabbaBbbbS

derivation

Page 13: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

13

aBbbbB

S

A B

bBa

b

bS

A B

ba bB

bS

A B

bBa b bbabbaaBbbbb

abbaBbbbS

b bB bbabbaabbbbbbbabbaaBbbbbS

derivation

Page 14: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

14

bbabbaabbbbbS

Therefore, the string

bbabbaabbbbb

is also generated by the grammar

Page 15: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

15

abbaBbbbS

aBbbbB

bB We know:

We also know the following string is generated:

abbaabbbb

abbaBbbbS

Page 16: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

16

We know:

Therefore, the following string is also generated:

bbabbaabbbbb

bbabbaaBbbbb

abbaBbbbS

abbaBbbbS

aBbbbB

bB

Page 17: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

17

abbaBbbbS

aBbbbB bB We know:

Therefore, the following string is also generated:

bbbbbbbaabba

bbbbbbBaabba

bbbbbbBaabba

abbaBbbbS

22

22

)()(

)()(

)()(

Page 18: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

18

Therefore, the following string is also generated:

bbbbbbbaabba

bbbbbbBaabba

abbaBbbbS

ii

ii

)()(

)()(

abbaBbbbS

aBbbbB bB We know:

Page 19: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

19

Therefore, knowing that

abbabbbb

is generated by grammar

bbbbbbbaabba ii )()(

is generated by G

We also know thatbB

SbB

aBbA

ABS

G

Page 20: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

20

In general

We are given an infinite

context-free grammar . G

Assume has no unit-productions

and no -productions.

G)( BA

Page 21: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

21

Take a string

with length bigger than )(GLw

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

m >

Some variable must be repeated

in the derivation of .w

Consequence:

Page 22: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

22

S

A

A

x

Last repeated variable

u z

v y

uvxyzw string

repeated

:,,,, zyxvu strings of terminals

Page 23: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

23

uAzS

vAyA

xA

Possible

derivationsS

A

A

x

u z

v y

Page 24: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

24

uAzS vAyA

xA

We know:

Following string is also generated:

uxzuAzS*

zxyuv 00

Page 25: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

25

This string is also generated:

uvxyzuvAyzuAzS**

The original zxyuvw 11

uAzS vAyA

xA

We know:

Page 26: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

26

This string is also generated:

uvvxyyzuvvAyyzuvAyzuAzS***

zxyuv 22

uAzS vAyA

xA

We know:

Page 27: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

27

This string is also generated:

uvvvxyyyzuvvvAyyyz

uvvAyyzuvAyzuAzS**

**

zxyuv 33

uAzS vAyA

xA

We know:

Page 28: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

28

This string is also generated:

yyyzvxyuvvv

yyyzvAyuvvv

uvvvAyyyz

uvvAyyzuvAyzuAzS

zxyuv ii

uAzS vAyA

xA

We know:

*

***

*****

Page 29: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

29

Therefore, any string of the form

zxyuv ii

is generated by the grammar G

0i

Page 30: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

30

knowing that )(GLuvxyz

we also know that )(GLzxyuv ii

Therefore

Page 31: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

31

mvxy ||Observation:

Since is the last repeated variableA

S

A

A

x

u z

v y

A string has length bigger than )(GLw

(Number of productions) x

(Largest right side of a production)m >

m

Page 32: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

32

1|| vyObservation

Since there are no unit or productions

S

A

A

x

u z

v y

Page 33: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

33

The Pumping Lemma for CFL

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 0 allfor , iLzxyuv ii

Page 34: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

34

Applicationsof

The Pumping Lemma for CFL

Page 35: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

35

Regular Languages

}{ nnba }{ RwwContext-Free Languages

Non-regular languages

Unrestricted grammar languages

}0:{ ncba nnn

**ba

Page 36: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

36

Theorem The language

}0:{ ncbaL nnn

is not context free.

Proof Use the Pumping Lemma

for context-free languages.

Example

Page 37: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

37

}0:{ ncbaL nnn

Assume the contrary, that

is context-free.

Since is context-free and infinite

we can apply the pumping lemma.

L

L

Page 38: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

38

Pumping Lemma gives a number

such that:

m

for any string with length Lw mw ||

We can choose e.g.mmm cbaw

}0:{ ncbaL nnn

Page 39: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

39

}0:{ ncbaL nnn

mmm cbaw

We can write: uvxyzw

with lengths andmvxy || 1|| vy

Page 40: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

40

}0:{ ncbaL nnn

Pumping Lemma says:

Lzxyuv ii for all 0i

mmm cbaw uvxyzw mvxy || 1|| vy

Page 41: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

41

}0:{ ncbaL nnn

We examine all the possible locations

of string in vxy w

mmm cbaw uvxyzw mvxy || 1|| vy

Page 42: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

42

}0:{ ncbaL nnn

Case 1: vxy is withinma

ccccccbbbbbbaaaaaa .........

vxy

m m m

u z

mmm cbaw uvxyzw mvxy || 1|| vy

Page 43: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

43

Case 1: and consist from only v y a

ccccccbbbbbbaaaaaa .........

vxy

m m m

u z

}0:{ ncbaL nnn

mmm cbaw uvxyzw mvxy || 1|| vy

Page 44: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

44

}0:{ ncbaL nnn

Case 1:

22xyv

ccccccbbbbbbaaaaaaaaaaaa .........km m m

u z

Repeating andv y

mmm cbaw uvxyzw mvxy || 1|| vy

1k

Page 45: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

45

}0:{ ncbaL nnn

mmm cbaw uvxyzw mvxy || 1|| vy

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

km m m

u z

1k

Case 1: From Pumping Lemma: Lzxyuv 22

Page 46: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

46

}0:{ ncbaL nnn

Case 1:

Lcbazxyuv mmkm 22

From Pumping Lemma: Lzxyuv 22

However:

Contradiction!

mmm cbaw uvxyzw mvxy || 1|| vy

1k

Page 47: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

47

}0:{ ncbaL nnn

ccccccbbbbbbaaaaaa .........

vxy

m m m

u z

Case 2: vxy is withinmb

mmm cbaw uvxyzw mvxy || 1|| vy

Page 48: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

48

}0:{ ncbaL nnn

Case 2: Similar analysis to case 1

ccccccbbbbbbaaaaaa .........

vxy

m m m

u z

mmm cbaw uvxyzw mvxy || 1|| vy

Page 49: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

49

}0:{ ncbaL nnn

Case 3: vxy is withinmc

ccccccbbbbbbaaaaaa .........

vxy

m m m

u z

mmm cbaw uvxyzw mvxy || 1|| vy

Page 50: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

50

}0:{ ncbaL nnn

Case 3: Similar analysis to case 1

ccccccbbbbbbaaaaaa .........

vxy

m m m

u z

mmm cbaw uvxyzw mvxy || 1|| vy

Page 51: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

51

}0:{ ncbaL nnn

ccccccbbbbbbaaaaaa .........

vxy

m m m

u z

Case 4: vxy overlaps and ma mb

mmm cbaw uvxyzw mvxy || 1|| vy

Page 52: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

52

}0:{ ncbaL nnn

Case 4:

ccccccbbbbbbaaaaaa .........

vxy

m m m

u z

Possibility 1: contains onlycontains only

vy

ab

mmm cbaw uvxyzw mvxy || 1|| vy

Page 53: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

53

}0:{ ncbaL nnn

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

1km 2km m

u z

mmm cbaw uvxyzw mvxy || 1|| vy

Case 4: Possibility 1: contains onlycontains only

vy

ab

121 kk

Page 54: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

54

}0:{ ncbaL nnn

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

1km 2km m

u z

Case 4: From Pumping Lemma: Lzxyuv 22

mmm cbaw uvxyzw mvxy || 1|| vy

121 kk

Page 55: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

55

}0:{ ncbaL nnn

Case 4: From Pumping Lemma: Lzxyuv 22

Lcbazxyuv mkmkm 2122However:

Contradiction!

mmm cbaw uvxyzw mvxy || 1|| vy

121 kk

Page 56: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

56

}0:{ ncbaL nnn

Case 4:

ccccccbbbbbbaaaaaa .........

vxy

m m m

u z

Possibility 2: contains andcontains only

vy

ab

mmm cbaw uvxyzw mvxy || 1|| vy

b

Page 57: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

57

}0:{ ncbaL nnn

Case 4:

ccccccbbbbbbbbbbbaaaaaaabbbaaa .........22xyv

m km m

u z

Possibility 2: contains andcontains only

vy

ab

mmm cbaw uvxyzw mvxy || 1|| vy

b

1k 2k

121 kkk

Page 58: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

58

}0:{ ncbaL nnn

mmm cbaw uvxyzw mvxy || 1|| vy

Case 4:From Pumping Lemma: Lzxyuv 22

121 kkk

ccccccbbbbbbbbbbbaaaaaaabbbaaa .........22xyv

m km m

u z

1k 2k

Page 59: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

59

}0:{ ncbaL nnn

mmm cbaw uvxyzw mvxy || 1|| vy

Case 4: From Pumping Lemma: Lzxyuv 22

Lcbbaazxyuv mkmkkm 2122

However:

Contradiction!

121 kkk

Page 60: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

60

}0:{ ncbaL nnn

Case 4:

ccccccbbbbbbaaaaaa .........

vxy

m m m

u z

Possibility 3: contains onlycontains andy

ab

mmm cbaw uvxyzw mvxy || 1|| vy

av

Page 61: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

61

}0:{ ncbaL nnn

Case 4: Possibility 3: contains only

contains andya

b

mmm cbaw uvxyzw mvxy || 1|| vy

a

Similar analysis with Possibility 2

v

Page 62: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

62

}0:{ ncbaL nnn

Case 5: vxy overlaps and mb

ccccccbbbbbbaaaaaa .........

vxy

m m m

u z

mc

mmm cbaw uvxyzw mvxy || 1|| vy

Page 63: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

63

}0:{ ncbaL nnn

Case 5: Similar analysis to case 4

mmm cbaw uvxyzw mvxy || 1|| vy

ccccccbbbbbbaaaaaa .........

vxy

m m m

u z

Page 64: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

64

There are no other cases to consider.

(Since , string cannot

overlap , and at the same time)

vxymvxy ||

ma mb mc

Page 65: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

65

In all cases we obtain a contradiction.

Therefore: The original assumption that

}0:{ ncbaL nnn

is context-free must be wrong.

Conclusion: is not context-free.L

END OF PROOF

Page 66: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

66

Comparison between the Pumping Lemma for Context

Free and Regular Languages

Page 67: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

67

What is the difference between Context Free Languages and Regular Languages?

In regular languages a single symbol/substring in the string w can be “pumped”.

...... ......q

Page 68: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

68

The difference between Context Free Languages and Regular Languages

In CFL’s multiple symbols/substrings in the string w can be “pumped”.

Consider the language {an bn | n > 0}

No single symbol can be pumped and allow us to stay in the language.

However, there do exist pairs of symbols which can be pumped resulting in strings which stay in the language.

Thus a CFL pumping lemma applies.

Page 69: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

69

aBbbbB

S

A B

bBa

b

bS

A B

ba bB

bS

A B

bBa b bbabbaaBbbbb

abbaBbbbS

b bB bbabbaabbbbbbbabbaaBbbbbS

CFG productions

Page 70: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

70

S

A

A

x

Last repeated variable

u z

v y

uvxyzw String

repeated

:,,,, zyxvu strings of terminals

Page 71: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

71

Pumping Conditions for RL and CFL

A language L satisfies the RL pumping condition if: – there exists an integer m > 0 such

that– for all strings w in L of length at

least m– there exist strings x, y, z such that

• w = xyz and• |xy| ≤ m and• |y| ≥ 1 and• for all i ≥ 0, xyiz is in L

|xy| ≤ m is in the beginning of the w and can be pumped within m symbols.

A language L satisfies the CFL pumping condition if:

– there exists an integer m > 0 such that

– for all strings w in L of length at least m

– there exist strings u, v, x, y, z such that

• w = uvxyz and• |vxy| ≤ m and• |vy| ≥ 1 and

• for all i ≥ 0, uvixyiz is in L

|vxy| ≤ m and u comes first in the w and it can be arbitrarily long.

Page 72: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

72

Pumping Lemma

All CFL’s satisfy the CFL pumping condition

But some languages that satisfy CFL pumping condition are not CFL!

All languages over {a,b}

“Pumping Languages”

CFL’s

Page 73: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

73

Implications

We can use the pumping lemma to prove a language L is not a CFL.

Show that L does not satisfy the CFL pumping condition.

We cannot use the pumping lemma to prove a language is CFL.

Showing L satisfies the pumping condition does not guarantee that L is context-free.

All languages over {a,b}

“Pumping Languages”

CFL’s

Page 74: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

74

Pumping Lemma

What does it mean?

Page 75: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

75

Pumping Condition

• A language L satisfies the CFL pumping condition if: – there exists an integer m > 0 such that– for all strings w in L of length at least m– there exist strings u, v, x, y, z such that

• w = uvxyz and• |vxy| ≤ m and• |vy| ≥ 1 and

Then for all i ≥ 0, uviwyiz is in L

Page 76: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

76

v and y can be pumped

• Let w = abcdefg be in L• Then there exist substrings v and y in w such

that v and y can be repeated (pumped) and the resulting string is still in L:

uvixyiz is in L for all i ≥ 0

1) w in L2) w = uvxyz3) for all i ≥ 0, uvixyiz is in L

Page 77: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

77

For example w = abcdefg

v = cd and y = f

uv0xy0z = uxz = abeg is in L

uv1xy1z = uvxyz = abcdefg is in L

uv2xy2z = uvvxyyz = abcdcdeffg is in L

uv3xy3z = uvvvxyyyz = abcdcdcdefffg is in L

Page 78: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

78

What the other parts mean

A language L satisfies the CFL pumping condition if: there exists an integer m > 0 such thatfor all strings w in L of length at least mw must be in L

Page 79: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

79

What the other parts mean

There exist strings u, v, x, y, z such that• w = uvxyz and• |vxy| ≤ m andv and y are contained within m characters of wNote: these are NOT necessarily the first m

characters of w|vy| ≥ 1(v and y cannot both be , one of them might

be , but not both)• For all i ≥ 0, uvixyiz is in L

Page 80: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

80

APPLYING CFL PUMPING LEMMA TO PROVE THAT A LANGUAGE L IS NOT CONTEXT-FREE

Page 81: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

81

How we use the Pumping Lemma

1. We choose a specific language L.

2. We show that L does not satisfy the pumping condition.

3. We conclude that L is not context-free.

Page 82: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

82

Showing that L “does not pump”

A language L satisfies the CFL pumping condition if: there exists an integer m > 0

such thatfor all strings w in L of length

at least mthere exist strings u, v, x, y, z

such thatw = uvxyz and|vxy| ≤ m and|vy| ≥ 1 andfor all i ≥ 0, uvixyiz is in L

A language L does not satisfy the CFL pumping condition if: for all integers m of sufficient

sizethere exists a string w in L of

length at least m such thatfor all strings u, v, x, y, z where

w = uvxyz and|vxy| ≤ m and|vy| ≥ 1

there exists a i ≥ 0 such that uvixyiz is not in L

Page 83: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

83

Regular Languages

}{ nnba }{ RwwContext-Free Languages

Non-regular languages

Unrestricted grammar languages

}0:{ ncba nnn

**ba

Page 84: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

84

More Pumping Lemma Applications

Page 85: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

85

The Pumping Lemma for CFL

there exists an integer such that m

for any string mwLw || ,

we can write

For infinite context-free language L

uvxyzw

with lengths mvxy ||

then 0 allfor , iLzxyuv ii

1|| vyand

Page 86: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

86

Theorem The language

}*},{:{ bawwwL

is not context free.

Proof Use the Pumping Lemma

for context-free languages.

Example

Page 87: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

87

Assume the contrary - that

is context-free.

Since is context-free and infinite

we can apply the pumping lemma.

L

L

}*},{:{ bawwwL

Page 88: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

88

Pumping Lemma gives a number

such that we can

m

So we pick: Lbaba mmmm

}*},{:{ bawwwL

pick any string of

with length at least mL

Page 89: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

89

We can write:

}*},{:{ bawwwL

uvxyzbaba mmmm

Pumping Lemma says:

Lzxyuv ii for all 0i

with lengths mvxy || 1|| vyand

Page 90: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

90

We examine all the possible locations

of string in vxy

mvxy || 1|| vyuvxyzbaba mmmm

}*},{:{ bawwwL

mmmm baba

Page 91: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

91

Case 1: vxy is within the firstma

bbaabbaa ........................v

m m m

u z

mvxy || 1|| vyuvxyzbaba mmmm

}*},{:{ bawwwL

x y

m

1kav 2kay 121 kk

Page 92: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

92

bbaabbaa ..................................2v

21 kkm m m

u z

mvxy || 1|| vyuvxyzbaba mmmm

}*},{:{ bawwwL

x 2y

m

Case 1: vxy is within the firstma

1kav 2kay 121 kk

Page 93: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

93

mvxy || 1|| vyuvxyzbaba mmmm

}*},{:{ bawwwL

Case 1: vxy is within the firstma

121 kk

Lzxyuvbaba mmmkkm 2221

Page 94: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

94

mvxy || 1|| vyuvxyzbaba mmmm

}*},{:{ bawwwL

Case 1: vxy is within the first ma

Contradiction!

Lzxyuvbaba mmmkkm 2221

Lzxyuv 22However, from Pumping Lemma:

Page 95: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

95

mvxy || 1|| vyuvxyzbaba mmmm

}*},{:{ bawwwL

bbaabbaa ........................v

m m m

u zx y

m

is in the first

is in the firstCase 2: mamb

vy

1kav 2kby 121 kk

Page 96: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

96

mvxy || 1|| vyuvxyzbaba mmmm

}*},{:{ bawwwL

bbaabbaa ....................................2v

1km 2km m

u zx 2y

m

is in the first

is in the firstCase 2: mamb

vy

1kav 2kby 121 kk

Page 97: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

97

mvxy || 1|| vyuvxyzbaba mmmm

}*},{:{ bawwwL

is in the first

is in the firstCase 2: mamb

vy

121 kk

Lzxyuvbaba mmkmkm 2221

Page 98: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

98

mvxy || 1|| vyuvxyzbaba mmmm

}*},{:{ bawwwL

is in the first

is in the firstCase 2: mamb

vy

Lzxyuvbaba mmkmkm 2221

Contradiction!

Lzxyuv 22However, from Pumping Lemma:

Page 99: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

99

mvxy || 1|| vyuvxyzbaba mmmm

}*},{:{ bawwwL

bbaabbaa ........................v

m m m

u zx y

m

is in the first

overlaps the firstCase 3: mmbamb

vy

21 kk bav 3kby 1, 21 kk

Page 100: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

100

mvxy || 1|| vyuvxyzbaba mmmm

}*},{:{ bawwwL

is in the first

overlaps the firstCase 3: mmbamb

vy

21 kk bav 3kby 1, 21 kk

2v zx 2y

bbaabbbbaaaa .................................m 2k 3km

u

m1k m

Page 101: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

101

mvxy || 1|| vyuvxyzbaba mmmm

}*},{:{ bawwwL

is in the first

overlaps the firstCase 3: mmbamb

vy

Lzxyuvbabbaa mmkmkkm 22321

1, 21 kk

Page 102: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

102

mvxy || 1|| vyuvxyzbaba mmmm

}*},{:{ bawwwL

is in the first

overlaps the firstCase 3: mmbamb

vy

Lzxyuvbabbaa mmkkkm 22321

Contradiction!

Lzxyuv 22However, from Pumping Lemma:

Page 103: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

103

overlaps the first

in the firstCase 4:

bbaabbaa ........................v

m m m

u z

mvxy || 1|| vyuvxyzbaba mmmm

}*},{:{ bawwwL

x y

m

mammba

vy

Analysis is similar to case 3

Page 104: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

104

Other cases: vxy is within

mmmm babaor

or

mmmm baba

mmmm baba

Analysis is similar to case 1: mmmm baba

Page 105: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

105

More cases: vxy overlaps

mmmm baba ormmmm baba

Analysis is similar to cases 2,3,4:

mmmm baba

Page 106: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

106

Since , it is impossible for

to overlap:

There are no other cases to consider.

mvxy ||vxy

mmmm baba

nor normmmm baba mmmm baba

neither

Page 107: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

107

In all cases we obtain a contradiction.

Therefore: The original assumption that

is context-free must be wrong.

Conclusion: is not context-free.L

}*},{:{ bawwwL

END OF PROOF

Page 108: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

108

Regular Languages

}{ nnba }{ RwwContext-Free Languages

Non-regular languages

Unrestricted grammar languages

}0:{ ncba nnn

**ba

}{ww

Page 109: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

109

Theorem The language

is not context free.

Proof Use the Pumping Lemma

for context-free languages.

}0:{ ! naL n

Example

Page 110: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

110

Since is context-free and infinite

we can apply the pumping lemma.

L

Assume to the contrary that

is context-free.

L

}0:{ ! naL n

Page 111: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

111

Pumping Lemma gives a number

such that we can:

m

so we pick: Lam !

pick any string of with length at least mL

}0:{ ! naL n

Page 112: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

112

We can write: uvxyzam !

Pumping Lemma says:

Lzxyuv ii for all 0i

}0:{ ! naL n

with lengths mvxy || 1|| vy and

Page 113: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

113

We examine all the possible locations

of the string in vxy !ma

mvxy || 1|| vyuvxyzam !

There is only one case to consider.

}0:{ ! naL n

Page 114: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

114

v

!m

u zx y

1kav 2kay mkk 211

aa ...............

mvxy || 1|| vyuvxyzam !

}0:{ ! naL n

Page 115: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

115

2v

21! kkm

u zx 2y

1kav 2kay

aa ...........................

mvxy || 1|| vyuvxyzam !

}0:{ ! naL n

mkk 211

Page 116: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

116

2v

km !

u zx 2y

1kav 2kay

aa ...........................

mvxy || 1|| vyuvxyzam !

}0:{ ! naL n

mk 1

21 kkk

Page 117: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

117

mvxy || 1|| vyuvxyzam !

}0:{ ! naL n

mk 1

zxyuva km 22!

Page 118: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

118

)!1(

)1(!

!!

!!

m

mm

mmm

mmkm

)!1(!! mkmm

mk 1Since

2m we have:for

Page 119: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

119

mvxy || 1|| vyuvxyzam !

}0:{ ! naL n

Lzxyuva km 22!

)!1(!! mkmm

Page 120: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

120

mvxy || 1|| vyuvxyzam !

}0:{ ! naL n

Lzxyuva km 22!

Lzxyuv 22

Contradiction!

However, from Pumping Lemma:

Page 121: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

121

We obtained a contradiction

Therefore: The original assumption that

is context-free must be wrong

Conclusion: is not context-freeL

}0:{ ! naL n

END OF PROOF

Page 122: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

122

Regular Languages

**ba

Context-free languages

Unrestricted grammar languages}0:{ ncba nnn

}0:{ nba nn

}},{:{ bawww

}*},{:{ bawwwR

}0:{ ! nan

Page 123: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

123

Theorem The language

is not context free

Proof Use the Pumping Lemma

for Context-free languages

}0:{2

nbaL nn

Example

Page 124: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

124

Since is context-free and infinite

we can apply the pumping lemma.

L

Assume to the contrary that

is context-free.

L

}0:{2

nbaL nn

Page 125: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

125

Pumping Lemma gives a number

such that we can:

m

so we choose: Lba mm 2

pick any string of with length at least mL

}0:{2

nbaL nn

Page 126: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

126

We can write:

with lengths andmvxy || 1|| vy

uvxyzba mm 2

Pumping Lemma says:

Lzxyuv ii for all 0i

}0:{2

nbaL nn

Page 127: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

127

We examine all the possible locations

of string in vxy

mvxy || 1|| vyuvxyzba mm 2

mm ba2

}0:{2

nbaL nn

Page 128: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

128

The most complicated case:

bbaa ...........................v

2m m

u zx y

is in

is inmamb

vy

}0:{2

nbaL nn

mvxy || 1|| vyuvxyzba mm 2

Page 129: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

129

bbaa ...........................v

2m m

u zx y

1kav 2kby

}0:{2

nbaL nn

mkk 211

mvxy || 1|| vyuvxyzba mm 2

Page 130: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

130

bbaa ...........................v

2m m

u zx y

1kav 2kby

The most complicated sub-case: 01 k 02 k

}0:{2

nbaL nn

mkk 211

and

mvxy || 1|| vyuvxyzba mm 2

Page 131: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

1310v

12 km 2km

u zx 0y

bbaa ..................

}0:{2

nbaL nn

1kav 2kby

The most complicated sub-case: 01 k 02 k

mkk 211

and

mvxy || 1|| vyuvxyzba mm 2

Page 132: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

132

}0:{2

nbaL nn

1kav 2kby

01 k 02 k

mkk 211

and

zxyuvba kmkm 00212

mvxy || 1|| vyuvxyzba mm 2

The most complicated sub-case:

Page 133: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

133

12

2

222

12

)1()(

km

mm

mkm

01 k 02 k mkk 211and

221

2 )( kmkm

Page 134: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

134

}0:{2

nbaL nn

221

2 )( kmkm

Lzxyuvba kmkm 00212

mvxy || 1|| vyuvxyzba mm 2

Page 135: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

135

}0:{2

nbaL nn

Lzxyuvba kmkm 00212

However, from Pumping Lemma:

Lzxyuv 00

Contradiction!

mvxy || 1|| vyuvxyzba mm 2

Page 136: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

136

When we examine the rest of the cases

we also obtain a contradiction.

Page 137: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

137

In all cases we obtain a contradiction.

Therefore: The original assumption that

is context-free must be wrong.

Conclusion: is not context-free.L

}0:{2

nbaL nn

END OF PROOF

Page 138: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

138

Check your knowledge before the Midterm 2!

Selected Examplesof

Context Free Language Problems

Page 139: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

139

Solution 1.

Let G be the grammar with productions:

Claim: L(G) = L

||

bBcB

BaScS

}:{ mnkcbaL kmn

Problem 1. Find a CFG for the following language

Page 140: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

140

Proof:Consider the following derivation:

(the first * applies S aSc n times, the second * to B bBc m times)

Since all words in L(G) must follow this pattern in their derivations, it is clear that L(G) L

Find a CFG for the following language:

||

bBcB

BaScS

}:{ mnkcbaL kmn

)(

**

mncmbna

ncmcBmbnancBnancSnaS

Page 141: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

141

Consider w L, w = anbmc(n + m) for some n, m 0

The derivation

S * anScn anBcn * anbmBcmcn anbmc(n + m) clearly produces w for any n, m.

L L(G) L L(G)

G is a CFG for L

Find a CFG for the following language

}:{ mnkcbaL kmn

END OF PROOF

||

bBcB

BaScS

Page 142: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

142

}:{ 32 NnbaL nn

Find a PDA and CFG for the following language

Is the automaton deterministic? Yes. It acts in a unique way in each state, no -transitions.

fqiq

/, 23 ab22 /, aa

/, 23 ab

Problem 2.

Solution 2.

Page 143: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

143

CFG :|32SbaS

}:{ 32 NnbaL nn

fqiq

/, 23 ab22 /, aa

/, 23 ab

Page 144: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

144

}2:},{{ ba nnbaxL

Find a PDA and CFG for the following language

Problem 3.

bb

aa

/,

/,

/,

/,

/,

/,

aab

aba

aab

baa

PDA

Solution 3.

Page 145: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

145

CFG :

|||| SSbSaSaaSaSbaSbSaS

bb

aa

/,

/,

/,

/,

/,

/,

aab

aba

aab

baa

}2:},{{ ba nnbaxL

Page 146: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

146

Consider the following two languages:

L1 ={w : w is made from a’s and b’s

and the length of w is a multiple of 10}

L2 = {an bn: n 0}

}:{ tenofmultipleNOTisnnbnaL

Prove that the language L is context-free

Problem 4.

Solution 4.

Page 147: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

147

L1 ={w : w is made from a’s and b’s and the length of w is a multiple of ten}

L2 = {an bn: n 0}

Let L1c denote the complement of L1.

We have that: L = L1c L2.

L1 is a regular language, since we can easily build a

finite automaton with 10 states that accepts any string in this language.

L1c is regular too, since regular languages are closed

under complement.

Page 148: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

148

The language L2 is context-free.

The grammar is: S aSb |

Therefore, the language L = L1c L2 is also

context-free,

since context-free languages are closed under regular intersection (Regular Closure).

END O PROOF

Page 149: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

149

}||,},{,:{ nxbaxNnaxL n Find a PDA and CFG for the following language

Problem 5.

CFG

babAaAA

AASaS

|||

||

ababaaabaAaaabAaa

AAaaAASaaASaS

Production ex.

Solution 5.

Page 150: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

150

Grammar NPDA

/,

/,

/,

/,

T

TaT

bS

aSTbS

T

TaT

bS

aSTbS

/,

/,

bb

aa

q0 q1 2qS/, $/$,

Remember converting grammar to NPDA, example:

Page 151: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

151

}||,},{,:{ nxbaxNnaxL n

PDA

fqiq

babAaAA

AASaS

|||/,

||/,

/,

/,

bb

aa

S/,

babAaAA

AASaS

|||

||

Page 152: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

152

}

,:},{{

different are symbolsfinishing the and startingtheba nnbaxL

Find a PDA and CFG for the following language:

Problem 6.

PDA

/,a

/,

/,

/,

/,

ba

bb

ab

aa

bb /,

/, ab /,a

/, a

/,

/,

/,

/,

ba

bb

ab

aa

Solution 6.

Page 153: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

153

/,a

/,

/,

/,

/,

ba

bb

ab

aa

bb /,

/, ab /,a

/, a

/,

/,

/,

/,

ba

bb

ab

aa

CFG, direct construction

Strings start and finish with different symbols

Strings contain at least one more a than b

bAaaAbS |

bAAAbAAAbaAaA ||||(we must have AA here as only one A just balances b)

}

,:},{{

different are symbolsfinishing the and startingtheba nnbaxL

Page 154: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 10

154

http://www.liacs.nl/~hoogeboo/praatjes/tarragona/schoolpda-VIII.pdf

Famous Pushdown Automata Examples

http://www.nature.com/nature/journal/v417/n6889/full/nature00771.html

Computational and evolutionary aspects of language

Further Reading