15
1 CS 301 - Lecture 17 Pumping Lemma for Context Free Grammars Fall 2008 Review Languages and Grammars Alphabets, strings, languages Regular Languages Deterministic Finite and Nondeterministic Automata Equivalence of NFA and DFA Regular Expressions Regular Grammars Properties of Regular Languages Languages that are not regular and the pumping lemma Context Free Languages Context Free Grammars – Derivations: leftmost, rightmost and derivation trees Parsing and ambiguity Simplifications and Normal Forms Nondeterministic Pushdown Automata Pushdown Automata and Context Free Grammars Deterministic Pushdown Automata Today: Pumping Lemma for context free grammars More Applications of The Pumping Lemma The Pumping Lemma: there exists an integer such that m for any string m w L w | | , we can write For infinite context-free language L uvxyz w = with lengths 1 | | and | | vy m vxy and it must be: 0 all for , i L z xy uv i i

More Applications - CSUmassey/Teaching/cs301/Restricted...– Nondeterministic Pushdown Automata – Pushdown Automata and Context Free Grammars – Deterministic Pushdown Automata

  • Upload
    others

  • View
    13

  • Download
    0

Embed Size (px)

Citation preview

Page 1: More Applications - CSUmassey/Teaching/cs301/Restricted...– Nondeterministic Pushdown Automata – Pushdown Automata and Context Free Grammars – Deterministic Pushdown Automata

1

CS 301 - Lecture 17 Pumping Lemma for

Context Free Grammars Fall 2008

Review •  Languages and Grammars

–  Alphabets, strings, languages •  Regular Languages

–  Deterministic Finite and Nondeterministic Automata –  Equivalence of NFA and DFA –  Regular Expressions –  Regular Grammars –  Properties of Regular Languages –  Languages that are not regular and the pumping lemma

•  Context Free Languages –  Context Free Grammars –  Derivations: leftmost, rightmost and derivation trees –  Parsing and ambiguity –  Simplifications and Normal Forms –  Nondeterministic Pushdown Automata –  Pushdown Automata and Context Free Grammars –  Deterministic Pushdown Automata

•  Today: –  Pumping Lemma for context free grammars

More Applications of

The Pumping Lemma

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 2: More Applications - CSUmassey/Teaching/cs301/Restricted...– Nondeterministic Pushdown Automata – Pushdown Automata and Context Free Grammars – Deterministic Pushdown Automata

2

Context-free languages

}0:{ ≥nba nn

Non-context free languages

}0:{ ≥ncba nnn

}*},{:{ bawwwR ∈

}},{:{ bavvv ∈

Theorem: The language

}*},{:{ bavvvL ∈=

is not context free

Proof: Use the Pumping Lemma for context-free languages

Assume for contradiction that is context-free

Since is context-free and infinite we can apply the pumping lemma

L

L

}*},{:{ bavvvL ∈=

Pumping Lemma gives a magic number such that:

m

Pick any string of with length at least m

we pick: Lbaba mmmm ∈

L

}*},{:{ bavvvL ∈=

Page 3: More Applications - CSUmassey/Teaching/cs301/Restricted...– Nondeterministic Pushdown Automata – Pushdown Automata and Context Free Grammars – Deterministic Pushdown Automata

3

We can write:

with lengths and mvxy ≤|| 1|| ≥vy

uvxyzbaba mmmm =

Pumping Lemma says:

Lzxyuv ii ∈ for all 0≥i

}*},{:{ bavvvL ∈=

We examine all the possible locations of string in vxy

mvxy ≤|| 1|| ≥vyuvxyzbaba mmmm =

mmmm baba

}*},{:{ bavvvL ∈=

Case 1: vxy is within the first ma

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

m m m

u z

mvxy ≤|| 1|| ≥vyuvxyzbaba mmmm =

x y

m

1kav = 2kay = 121 ≥+ kk

}*},{:{ bavvvL ∈=

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

21 kkm ++ m m

u z

mvxy ≤|| 1|| ≥vyuvxyzbaba mmmm =

x 2y

m

Case 1: vxy is within the first ma

1kav = 2kay = 121 ≥+ kk

}*},{:{ bavvvL ∈=

Page 4: More Applications - CSUmassey/Teaching/cs301/Restricted...– Nondeterministic Pushdown Automata – Pushdown Automata and Context Free Grammars – Deterministic Pushdown Automata

4

mvxy ≤|| 1|| ≥vyuvxyzbaba mmmm =

Case 1: vxy is within the first ma

121 ≥+ kk

Lzxyuvbaba mmmkkm ∉=++ 2221

}*},{:{ bavvvL ∈=

mvxy ≤|| 1|| ≥vyuvxyzbaba mmmm =

Case 1: vxy is within the first ma

Lzxyuv ∈22

Contradiction!!!

Lzxyuvbaba mmmkkm ∉=++ 2221

However, from Pumping Lemma:

}*},{:{ bavvvL ∈=

is in the first is in the first Case 2:

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

m m m

u z

mvxy ≤|| 1|| ≥vyuvxyzbaba mmmm =

x y

m

mamb

vy

1kav = 2kby = 121 ≥+ kk

}*},{:{ bavvvL ∈=

is in the first is in the first Case 2:

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

1km + 2km + m

u z

mvxy ≤|| 1|| ≥vyuvxyzbaba mmmm =

x 2y

m

mamb

vy

1kav = 2kby = 121 ≥+ kk

}*},{:{ bavvvL ∈=

Page 5: More Applications - CSUmassey/Teaching/cs301/Restricted...– Nondeterministic Pushdown Automata – Pushdown Automata and Context Free Grammars – Deterministic Pushdown Automata

5

is in the first is in the first Case 2:

mvxy ≤|| 1|| ≥vyuvxyzbaba mmmm =

mamb

vy

121 ≥+ kk

Lzxyuvbaba mmkmkm ∉=++ 2221

}*},{:{ bavvvL ∈=

is in the first is in the first Case 2:

mvxy ≤|| 1|| ≥vyuvxyzbaba mmmm =

mamb

vy

Lzxyuvbaba mmkmkm ∉=++ 2221

Lzxyuv ∈22

Contradiction!!!

However, from Pumping Lemma:

}*},{:{ bavvvL ∈=

is in the first overlaps the first Case 3:

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

m m m

u z

mvxy ≤|| 1|| ≥vyuvxyzbaba mmmm =

x y

m

mmbamb

vy

21 kk bav = 3kby = 1, 21 ≥kk

}*},{:{ bavvvL ∈=

is in the first overlaps the first Case 3:

bbaabbaabbaa .................................2v

m 2k 3km +

u z

mvxy ≤|| 1|| ≥vyuvxyzbaba mmmm =

x 2y

m

mmbamb

vy

21 kk bav = 3kby = 1, 21 ≥kk

1k m

}*},{:{ bavvvL ∈=

Page 6: More Applications - CSUmassey/Teaching/cs301/Restricted...– Nondeterministic Pushdown Automata – Pushdown Automata and Context Free Grammars – Deterministic Pushdown Automata

6

is in the first overlaps the first Case 3:

mvxy ≤|| 1|| ≥vyuvxyzbaba mmmm =

mmbamb

vy

Lzxyuvbababa mmkmkkm ∉=+ 22312

1, 21 ≥kk

}*},{:{ bavvvL ∈=

is in the first overlaps the first Case 3:

mvxy ≤|| 1|| ≥vyuvxyzbaba mmmm =

mmbamb

vy

Lzxyuvbababa mmkkkm ∉= 22312

Lzxyuv ∈22

Contradiction!!!

However, from Pumping Lemma:

}*},{:{ bavvvL ∈=

Overlaps the first in the first Case 4:

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

m m m

u z

mvxy ≤|| 1|| ≥vyuvxyzbaba mmmm =

x y

m

mammba

vy

Analysis is similar to case 3

}*},{:{ bavvvL ∈= Other cases: vxy is within mmmm baba

or

or

mmmm baba

mmmm baba

Analysis is similar to case 1:

mmmm baba

Page 7: More Applications - CSUmassey/Teaching/cs301/Restricted...– Nondeterministic Pushdown Automata – Pushdown Automata and Context Free Grammars – Deterministic Pushdown Automata

7

More cases: vxy overlaps mmmm baba

or

mmmm baba

Analysis is similar to cases 2,3,4:

mmmm baba

Since , it is impossible to overlap:

There are no other cases to consider

mvxy ≤||vxy mmmm baba

nor

nor

mmmm baba

mmmm baba

In all cases we obtained a contradiction

Therefore: The original assumption that

is context-free must be wrong

Conclusion: is not context-free L

}*},{:{ bavvvL ∈=Context-free languages

}0:{ ≥nba nn

Non-context free languages

}0:{ ≥ncba nnn

}*},{:{ bawwwR ∈

}},{:{ bawww ∈

}0:{ ! ≥nan

Page 8: More Applications - CSUmassey/Teaching/cs301/Restricted...– Nondeterministic Pushdown Automata – Pushdown Automata and Context Free Grammars – Deterministic Pushdown Automata

8

Theorem: The language

is not context free

Proof: Use the Pumping Lemma for context-free languages

}0:{ ! ≥= naL nAssume for contradiction that is context-free

Since is context-free and infinite we can apply the pumping lemma

L

L

}0:{ ! ≥= naL n

Pumping Lemma gives a magic number such that:

m

Pick any string of with length at least m

we pick: Lam ∈!

L

}0:{ ! ≥= naL n

We can write:

with lengths and mvxy ≤|| 1|| ≥vy

uvxyzam =!

Pumping Lemma says:

Lzxyuv ii ∈ for all 0≥i

}0:{ ! ≥= naL n

Page 9: More Applications - CSUmassey/Teaching/cs301/Restricted...– Nondeterministic Pushdown Automata – Pushdown Automata and Context Free Grammars – Deterministic Pushdown Automata

9

We examine all the possible locations of string in vxy

mvxy ≤|| 1|| ≥vy

!ma

uvxyzam =!

There is only one case to consider

}0:{ ! ≥= naL n

v

!m

u zx y

1kav = 2kay = mkk ≤+≤ 211

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

mvxy ≤|| 1|| ≥vyuvxyzam =!

}0:{ ! ≥= naL n

2v

21! kkm ++

u zx 2y

1kav = 2kay =

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

mvxy ≤|| 1|| ≥vyuvxyzam =!

}0:{ ! ≥= naL n

mkk ≤+≤ 211

2v

km +!

u zx 2y

1kav = 2kay =

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

mvxy ≤|| 1|| ≥vyuvxyzam =!

}0:{ ! ≥= naL n

mk ≤≤1

21 kkk +=

Page 10: More Applications - CSUmassey/Teaching/cs301/Restricted...– Nondeterministic Pushdown Automata – Pushdown Automata and Context Free Grammars – Deterministic Pushdown Automata

10

mvxy ≤|| 1|| ≥vyuvxyzam =!

}0:{ ! ≥= naL n

mk ≤≤1

zxyuva km 22! =+

)!1( )1(!

!! !!

+=

+=

+<

+≤+

mmmmmm

mmkm

mk ≤≤1Since , for we have: 2≥m

)!1(!! +<+< mkmm

mvxy ≤|| 1|| ≥vyuvxyzam =!

}0:{ ! ≥= naL n

Lzxyuva km ∉=+ 22!

)!1(!! +<+< mkmm

mvxy ≤|| 1|| ≥vyuvxyzam =!

}0:{ ! ≥= naL n

Lzxyuva km ∉=+ 22!

Lzxyuv ∈22

Contradiction!!!

However, from Pumping Lemma:

Page 11: More Applications - CSUmassey/Teaching/cs301/Restricted...– Nondeterministic Pushdown Automata – Pushdown Automata and Context Free Grammars – Deterministic Pushdown Automata

11

We obtained a contradiction

Therefore: The original assumption that

is context-free must be wrong

Conclusion: is not context-free L

}0:{ ! ≥= naL n

Context-free languages

}0:{ ≥nba nn

Non-context free languages

}0:{ ≥ncba nnn

}*},{:{ bawwwR ∈

}},{:{ bawww ∈

}0:{ ! ≥nan}0:{2

≥nba nn

Theorem: The language

is not context free

Proof: Use the Pumping Lemma for context-free languages

}0:{2

≥= nbaL nnAssume for contradiction that is context-free

Since is context-free and infinite we can apply the pumping lemma

L

L

}0:{2

≥= nbaL nn

Page 12: More Applications - CSUmassey/Teaching/cs301/Restricted...– Nondeterministic Pushdown Automata – Pushdown Automata and Context Free Grammars – Deterministic Pushdown Automata

12

Pumping Lemma gives a magic number such that:

m

Pick any string of with length at least m

we pick: Lba mm ∈2

L

}0:{2

≥= nbaL nn

We can write:

with lengths and mvxy ≤|| 1|| ≥vy

uvxyzba mm =2

Pumping Lemma says:

Lzxyuv ii ∈ for all 0≥i

}0:{2

≥= nbaL nn

We examine all the possible locations

of string in vxy

mvxy ≤|| 1|| ≥vyuvxyzba mm =2

mm ba2

}0:{2

≥= nbaL nn

Most complicated case:

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

2m m

u zx y

is in is in ma

mbvy

mvxy ≤|| 1|| ≥vyuvxyzba mm =2

}0:{2

≥= nbaL nn

Page 13: More Applications - CSUmassey/Teaching/cs301/Restricted...– Nondeterministic Pushdown Automata – Pushdown Automata and Context Free Grammars – Deterministic Pushdown Automata

13

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

2m m

u zx y

1kav = 2kby =

mvxy ≤|| 1|| ≥vyuvxyzba mm =2

}0:{2

≥= nbaL nn

mkk ≤+≤ 211

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

2m m

u zx y

1kav = 2kby =

Most complicated sub-case: 01 ≠k 02 ≠k

mvxy ≤|| 1|| ≥vyuvxyzba mm =2

}0:{2

≥= nbaL nn

mkk ≤+≤ 211

and

0v

12 km − 2km −

u zx 0y

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

}0:{2

≥= nbaL nn

mvxy ≤|| 1|| ≥vyuvxyzba mm =2

1kav = 2kby =

Most complicated sub-case: 01 ≠k 02 ≠k

mkk ≤+≤ 211

and

}0:{2

≥= nbaL nn

mvxy ≤|| 1|| ≥vyuvxyzba mm =2

1kav = 2kby =

Most complicated sub-case: 01 ≠k 02 ≠k

mkk ≤+≤ 211

and

zxyuvba kmkm 00212

=−−

Page 14: More Applications - CSUmassey/Teaching/cs301/Restricted...– Nondeterministic Pushdown Automata – Pushdown Automata and Context Free Grammars – Deterministic Pushdown Automata

14

12

2

222

12

)1()(

km

mm

mkm

−<

+−=

−≤−

01 ≠k 02 ≠k mkk ≤+≤ 211and

221

2 )( kmkm −≠−

}0:{2

≥= nbaL nn

mvxy ≤|| 1|| ≥vyuvxyzba mm =2

221

2 )( kmkm −≠−

Lzxyuvba kmkm ∉=−− 00212

}0:{2

≥= nbaL nn

mvxy ≤|| 1|| ≥vyuvxyzba mm =2

Lzxyuvba kmkm ∉=−− 00212

However, from Pumping Lemma: Lzxyuv ∈00

Contradiction!!!

When we examine the rest of the cases we also obtain a contradiction

Page 15: More Applications - CSUmassey/Teaching/cs301/Restricted...– Nondeterministic Pushdown Automata – Pushdown Automata and Context Free Grammars – Deterministic Pushdown Automata

15

In all cases we obtained a contradiction

Therefore: The original assumption that

is context-free must be wrong

Conclusion: is not context-free L

}0:{2

≥= nbaL nn

What’s Next •  Read

–  Linz Chapter 1,2.1, 2.2, 2.3, (skip 2.4), 3, 4, 5, 6.1, 6.2, (skip 6.3), 7.1, 7.2, 7.3, (skip 7.4), and 8

–  JFLAP Chapter 1, 2.1, (skip 2.2), 3, 4, 5, 6, 7 •  Next Lecture Topics From 8.2

–  Closure and Decidable Properties for Context Free Languages •  Quiz 3 in Recitation on Wednesday 11/12

–  Covers Linz 7.1, 7.2, 7.3, (skip 7.4), 8, and JFLAP 5,6,7 –  Closed book, but you may bring one sheet of 8.5 x 11 inch paper with any notes you

like. –  Quiz will take the full hour

•  Homework –  Homework Due Thursday