CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 9 Mälardalen University

92
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 9 Mälardalen University 2005

description

CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 9 Mälardalen University 2005. Content The Pumping Lemma for CFL Applications of the Pumping Lemma for CFL Midterm Exam 2: Context-Free Languages. Pumping Lemma for CFL’s. - PowerPoint PPT Presentation

Transcript of CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 9 Mälardalen University

1

CD5560

FABER

Formal Languages, Automata and Models of Computation

Lecture 9

Mälardalen University

2005

2

Content

The Pumping Lemma for CFLApplications of the Pumping Lemma for CFLMidterm Exam 2: Context-Free Languages

3

Pumping Lemma for CFL’s

4

Comparison to Regular Language Pumping Lemma/Condition

5

What’s Difference between CFL’s and Regular Languages?

• In regular languages, a single substring “pumps”– Consider the language of even length strings over {a,b}– We can identify a single substring which can be pumped

• In CFL’s, multiple substrings can “pump”– Consider the language {anbn | n > 0}– No single substring can be pumped and allow us to stay in the

language– However, there do exist pairs of substrings which can be

pumped resulting in strings which stay in the language• This results in a modified pumping condition

6

Modified Pumping Condition

• A language L satisfies the regular language pumping

condition if: – there exists an integer m >

0 such that

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

– there exist strings u, v, w such that

• x = uvw and• |uv| ≤ m and• |v| ≥ 1 and• For all i ≥ 0, uviw is in L

• A language L satisfies the

CFL pumping condition if: – there exists an integer m >

0 such that

– for all strings x in L of length at least n

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

• x = uvwyz and

• |vwy| ≤ m and

• |vy| ≥ 1 and

• For all i ≥ 0, uviwyiz is in L

7

Pumping Lemma• All CFL’s satisfy the CFL pumping condition

All languages over {a,b}

“Pumping Languages”

CFL’s

8

Implications

• We can use the pumping lemma to prove a language L is not a CFL– Show L does not satisfy the CFL pumping

condition• We cannot use the pumping lemma to prove a

language is context-free– Showing L satisfies the pumping condition does

not guarantee that L is context-free

All languages over {a,b}

“Pumping Languages”

CFL’s

9

Pumping Lemma

What does it mean?

10

Pumping Condition

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

• x = uvwyz and

• |vwy| ≤ m and

• |vy| ≥ 1 and

• For all i ≥ 0, uviwyiz is in L

11

v and y can be pumped

• Let x = abcdefg be in L• Then there exist 2 substrings v and y in x such that v and y can

be repeated (pumped) in place any number of times and the resulting string is still in L– uviwyiz is in L for all i ≥ 0

• For example– v = cd and y = f

• uv0wy0z = uwz = abeg is in L• uv1wy1z = uvwyz = abcdefg is in L• uv2wy2z = uvvwyyz = abcdcdeffg is in L• uv3wy3z = uvvvwyyyz = abcdcdcdefffg is in L • …

1) x in L2) x = uvwyz3) For all i ≥ 0, uviwyiz is in L

12

What the other parts mean• A language L satisfies the CFL pumping condition if:

– there exists an integer m > 0 such that– for all strings x in L of length at least m

• x must be in L and have sufficient length

– there exist strings u, v, w, y, z such that• x = uvwyz and• |vwy| ≤ m and

– v and y are contained within m characters of x– Note: these are NOT necessarily the first m characters of x

• |vy| ≥ 1 and– v and y cannot both be – One of them might be , but not both

• For all i ≥ 0, uviwyiz is in L

13

How we use the Pumping Lemma

• We choose a specific language L– For example, {anbncn | n > 0}

• We have shown that L does not satisfy the pumping condition and

• concluded that L is not context-free

14

Showing L “does not pump”

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

0 such that– for all strings x in L of

length at least m– there exist strings u, v, w,

y, z such that• x = uvwyz and• |vwy| ≤ m and• |vy| ≥ 1 and• For all i ≥ 0, uviwyiz is

in L

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

sufficient size– there exists a string x in L of

length at least m such that– for all strings u, v, w, y, z

such that• x = uvwyz and• |vwy| ≤ m and• |vy| ≥ 1

– There exists a i ≥ 0 such that uviwyiz is not in L

15

Two Rules of Thumb

• Try to use blocks of at least m characters in x– For TWOCOPIES, choose x = ambmambm rather

than ambamb• Guarantees v and y cannot be in more than 2 blocks of x

• Try i=0 or i=2– i=0

• This reduces number of occurrences of v and y

– i=2• This increases number of occurrences of v and y

16

Summary

• We use the Pumping Lemma to prove a language is not a CFL– Note, does not work for all non CFL languages– Can be strengthened to Ogden’s Lemma

• Choosing a good string x is first key step• Choosing a good i is second key step• Typically have several cases for v, w, y

17

More Applicationsof

The Pumping Lemma

18

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

19

The Pumping Lemma for CFL

There exists an integer such that m

mwGLw || ),(

can be written

Let be a context free grammar. G

uvxyzw

with lengths 1||and || vymvxy

and 0 allfor , iLzxyuv ii

20

Regular Languages

}{ nnba }{ RwwContext-Free Languages

Non-regular languages

Unrestricted grammar languages

}0:{ ncba nnn

**ba

21

Theorem The language

}*},{:{ bawwwL

is not context free

Proof Use the Pumping Lemma

for context-free languages

22

Assume the contrary - that

is context-free

Since is context-free and infinite

we can apply the pumping lemma

L

L

}*},{:{ bawwwL

23

Pumping Lemma gives a number

such that:

m

we pick: Lbaba mmmm

}*},{:{ bawwwL

Pick any string of

with length at least mL

24

We can write:

}*},{:{ bawwwL

uvxyzbaba mmmm

Pumping Lemma says:

Lzxyuv ii for all 0i

with lengths mvxy || 1|| vyand

25

We examine all the possible locations

of string in vxy

mvxy || 1|| vyuvxyzbaba mmmm

}*},{:{ bawwwL

mmmm baba

26

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

27

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

28

mvxy || 1|| vyuvxyzbaba mmmm

}*},{:{ bawwwL

Case 1: vxy is within the firstma

121 kk

Lzxyuvbaba mmmkkm 2221

29

mvxy || 1|| vyuvxyzbaba mmmm

}*},{:{ bawwwL

Case 1: vxy is within the firstma

Contradiction!

Lzxyuvbaba mmmkkm 2221

Lzxyuv 22However, from Pumping Lemma:

30

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

31

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

32

mvxy || 1|| vyuvxyzbaba mmmm

}*},{:{ bawwwL

is in the first

is in the firstCase 2: mamb

vy

121 kk

Lzxyuvbaba mmkmkm 2221

33

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:

34

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

35

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

36

mvxy || 1|| vyuvxyzbaba mmmm

}*},{:{ bawwwL

is in the first

overlaps the firstCase 3: mmbamb

vy

Lzxyuvbabbaa mmkmkkm 22321

1, 21 kk

37

mvxy || 1|| vyuvxyzbaba mmmm

}*},{:{ bawwwL

is in the first

overlaps the firstCase 3: mmbamb

vy

Lzxyuvbabbaa mmkkkm 22321

Contradiction!

Lzxyuv 22However, from Pumping Lemma:

38

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

39

Other cases: vxy is within

mmmm babaor

or

mmmm baba

mmmm baba

Analysis is similar to case 1: mmmm baba

40

More cases: vxy overlaps

mmmm baba ormmmm baba

Analysis is similar to cases 2,3,4:

mmmm baba

41

Since , it is impossible for

to overlap:

There are no other cases to consider

mvxy ||vxy

mmmm baba

nor normmmm baba mmmm baba

neither

42

In all cases we obtained a contradiction

Therefore: The original assumption that

is context-free must be wrong

Conclusion: is not context-freeL

}*},{:{ bawwwL

END OF PROOF

43

Regular Languages

}{ nnba }{ RwwContext-Free Languages

Non-regular languages

Unrestricted grammar languages

}0:{ ncba nnn

**ba

}{ww

44

Theorem The language

is not context free

Proof Use the Pumping Lemma

for context-free languages

}0:{ ! naL n

45

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

46

Pumping Lemma gives a magic number

such that:

m

we pick: Lam !

Pick any string of with length at least mL

}0:{ ! naL n

47

We can write: uvxyzam !

Pumping Lemma says:

Lzxyuv ii for all 0i

}0:{ ! naL n

with lengths mvxy || 1|| vy and

48

We examine all the possible locations

of string in vxy !ma

mvxy || 1|| vyuvxyzam !

There is only one case to consider

}0:{ ! naL n

49

v

!m

u zx y

1kav 2kay mkk 211

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

mvxy || 1|| vyuvxyzam !

}0:{ ! naL n

50

2v

21! kkm

u zx 2y

1kav 2kay

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

mvxy || 1|| vyuvxyzam !

}0:{ ! naL n

mkk 211

51

2v

km !

u zx 2y

1kav 2kay

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

mvxy || 1|| vyuvxyzam !

}0:{ ! naL n

mk 1

21 kkk

52

mvxy || 1|| vyuvxyzam !

}0:{ ! naL n

mk 1

zxyuva km 22!

53

)!1(

)1(!

!!

!!

m

mm

mmm

mmkm

)!1(!! mkmm

mk 1Since

2m we have:for

54

mvxy || 1|| vyuvxyzam !

}0:{ ! naL n

Lzxyuva km 22!

)!1(!! mkmm

55

mvxy || 1|| vyuvxyzam !

}0:{ ! naL n

Lzxyuva km 22!

Lzxyuv 22

Contradiction!

However, from Pumping Lemma:

56

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

57

Regular Languages

**ba

Context-free languages

Unrestricted grammar languages}0:{ ncba nnn

}0:{ nba nn

}},{:{ bawww

}*},{:{ bawwwR

}0:{ ! nan}0:{

2nba nn

58

Theorem The language

is not context free

Proof Use the Pumping Lemma

for context-free languages

}0:{2

nbaL nn

59

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

60

Pumping Lemma gives a number

such that:

m

we pick: Lba mm 2

Pick any string of with length at least mL

}0:{2

nbaL nn

61

We can write:

with lengths andmvxy || 1|| vy

uvxyzba mm 2

Pumping Lemma says:

Lzxyuv ii for all 0i

}0:{2

nbaL nn

62

We examine all the possible locations

of string in vxy

mvxy || 1|| vyuvxyzba mm 2

mm ba2

}0:{2

nbaL nn

63

Most complicated case:

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

2m m

u zx y

is in

is inmamb

vy

}0:{2

nbaL nn

mvxy || 1|| vyuvxyzba mm 2

64

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

2m m

u zx y

1kav 2kby

}0:{2

nbaL nn

mkk 211

mvxy || 1|| vyuvxyzba mm 2

65

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

2m m

u zx y

1kav 2kby

Most complicated sub-case: 01 k 02 k

}0:{2

nbaL nn

mkk 211

and

mvxy || 1|| vyuvxyzba mm 2

660v

12 km 2km

u zx 0y

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

}0:{2

nbaL nn

1kav 2kby

Most complicated sub-case: 01 k 02 k

mkk 211

and

mvxy || 1|| vyuvxyzba mm 2

67

}0:{2

nbaL nn

1kav 2kby

Most complicated sub-case: 01 k 02 k

mkk 211

and

zxyuvba kmkm 00212

mvxy || 1|| vyuvxyzba mm 2

68

12

2

222

12

)1()(

km

mm

mkm

01 k 02 k mkk 211and

221

2 )( kmkm

69

}0:{2

nbaL nn

221

2 )( kmkm

Lzxyuvba kmkm 00212

mvxy || 1|| vyuvxyzba mm 2

70

}0:{2

nbaL nn

Lzxyuvba kmkm 00212

However, from Pumping Lemma: Lzxyuv 00

Contradiction!

mvxy || 1|| vyuvxyzba mm 2

71

When we examine the rest of the cases

we also obtain a contradiction

72

In all cases we obtained a contradiction

Therefore: The original assumption that

is context-free must be wrong

Conclusion: is not context-freeL

}0:{2

nbaL nn

END OF PROOF

73

Midterm Exam 2Context-Free Languages

Place: Lambda Time: on Monday 20054-05-16, 13:15-15:00

It is OPEN BOOK. (This means you are allowed to bring in

one book of your choice.)It will cover Context-free Languages.

You will have the complete 2 hours to do the test.

74

Check your knowledge before midterm exam!

Selected Examplesof

CF Language Problems

75

Let G be the grammar with productions:

Claim: L(G) = L

||

bBcB

BaScS

}:{ mnkcbaL kmn Find a CFG for the following language

Example

76

Proof:Consider the following derivation:

S * anScn anBcn * anbmBcmcn anbmc(n + m)

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

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

}:{ mnkcbaL kmn

Find a CFG for the following language

||

bBcB

BaScS

77

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

78

}:{ 32 NnbaL nn Find a PDA and CFG for the following language

Example

Is the automaton deterministic? Yes. It acts in a unique way in each state.

fqiq

/, 23 ab22 /, aa

/, 23 ab

79

CFG : 32| SbaS

}:{ 32 NnbaL nn

fqiq

/, 23 ab22 /, aa

/, 23 ab

80

}2:},{{ ba nnbaxL Find a PDA and CFG for the following language

Example

bb

aa

/,

/,

/,

/,

/,

/,

aab

aba

aab

baa

PDA

81

CFG :

SSbSaSaaSaSbaSbSaS ||||

}2:},{{ ba nnbaxL bb

aa

/,

/,

/,

/,

/,

/,

aab

aba

aab

baa

82

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 = {anbn: n 0}

}5:{ ofmultiplenotisnbaL nn

Prove that the language L is context-free

Example

83

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

L2 = {anbn: n 0}

Let L1 denote the complement of L1. We have that L = L1 L2.

L1 is a regular language, since we can easily build a finite automaton with 10 states that accepts any string in this language.

L1 is regular too, since regular languages are closed under complement.

84

The language L2 is context-free.

The grammar is: S aSb |

Therefore, the language L = L1 L2 is also context-free,

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

END O PROOF

85

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

Example

CFG

babAaAA

AASaS

|||

||

ababaaabaAaaabAaa

AAaaAASaaASaS

Production ex.

86

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

PDA

fqiq

babAaAA

AASaS

|||/,

||/,

/,

/,

bb

aa

S/,

87

}different are symbols finishing theand starting the

,:},{{ ba nnbaxL

Find a PDA and CFG for the following language

Example

PDA /,a

/,

/,

/,

/,

ba

bb

ab

aa

bb /,

/, ab /,a

/, a

/,

/,

/,

/,

ba

bb

ab

aa

88

}different are symbols finishing theand starting the

,:},{{ ba nnbaxL

/,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)

89

Additional Excersize..

90

91

92