Anda di halaman 1dari 74

The Pumping Lemma

for
Context-Free Languages

1
Take an infinite context-free language

Generates an infinite number


of different strings

Example: S → AB
A → aBb
B → Sb
B→b
2
S → AB
A → aBb
B → Sb
B→b In a derivation of a long string,
variables are repeated

A derivation:
S ⇒ AB ⇒ aBbB ⇒ abbB
⇒ abbSb ⇒ abbABb ⇒ abbaBbBb ⇒
⇒ abbabbBb ⇒ abbabbbb
3
Derivation tree string abbabbbb
S

A B

a B b S b

b A B

a B b b

b
4
Derivation tree string abbabbbb
S

A B

a B b S b

b A B

a B b b

repeated b
5
B ⇒ Sb ⇒ ABb ⇒ B

⇒ aBbBb ⇒ aBbbb S b

A B

a B b b

b
*
B ⇒ aBbbb B⇒b
6
Repeated Part B

S b

A B

a B b b

*
B ⇒ aBbbb
7
B
Another possible
derivation from B S b

* A B

B ⇒ aBbbb a B b b

S b

A B

a B b b

* *
B ⇒ aBbbb ⇒ aaBbbbbbb
8
B

S b

* A B

B ⇒ aBbbb a B b b

S b

A B

a B b b

* *
2 2
B ⇒(a ) B (bbb) ⇒(a ) B (bbb)
9
A Derivation from S
S
* A B
S ⇒ abbBb
a B b
*
B ⇒ aBbbb b

B⇒b

*
S ⇒ abbBb
10
S
* A B
S ⇒ abbBb
a B b b
*
B ⇒ aBbbb b

B⇒b

*
0 0
S ⇒ abbBb ⇒ abbbb = abb(a ) b(bbb)

11
* *
S ⇒ abbBb B ⇒ aBbbb B⇒b

*
0 0
S ⇒ abb(a ) b(bbb)

0 0
abb(a ) b(bbb) ∈ L(G )
12
A Derivation from S
S
* A B
S ⇒ abbBb
a B b
*
B ⇒ aBbbb b

B⇒b

*
S ⇒ abbBb
13
S
*
S ⇒ abbBb A B

* a B b S b

B ⇒ aBbbb b A B

B⇒b a B b b

* *
S ⇒ abbBb ⇒ abbaBbbb
14
S
*
S ⇒ abbBb A B

a b b
* B S

B ⇒ aBbbb b A B

a B b b
B⇒b S b

A B

a B b b

* *
2 2
S ⇒ abb(a ) B (bbb) ⇒ abb(a ) B (bbb)
15
S
*
S ⇒ abbBb A B

a B b S b
*
B ⇒ aBbbb b A B

a B b b

B⇒b S b

A B

a B b b

* *
2 2 2 2
S ⇒ abb(a ) B (bbb) ⇒ abb(a ) b(bbb)
16
* *
S ⇒ abbBb B ⇒ aBbbb B⇒b

*
2 2
S ⇒ abb(a ) b(bbb)

2 2
abb(a ) b(bbb) ∈ L(G )
17
A Derivation from S S

A B
*
S ⇒ abbBb
a B b S b

b A B
*
B ⇒ aBbbb a B b b

S b
B⇒b A B

a B b b

*
2 2
S ⇒ abb(a ) B (bbb)
18
S

* A B
S ⇒ abbBb a B b S b

* b A B
B ⇒ aBbbb a B b b

B⇒b S b

A B

a B b b

S b

A B

a B b b

* *
2 2 3 3
S ⇒ abb ( a ) B (bbb ) ⇒ abb ( a ) B (bbb )
19
S

* A B

S ⇒ abbBb a B b S b

* b A B

B ⇒ aBbbb a B b b

S b
B⇒b A B

a B b b

S b

A B

a B b b

*
3 3 3 3
S ⇒ abb (a ) B (bbb ) ⇒ abb (a ) b(bbb )
20
* *
S ⇒ abbBb B ⇒ aBbbb B⇒b

*
3 3
S ⇒ abb(a ) b(bbb)

3 3
abb(a ) b(bbb) ∈ L(G )
21
In General:

* *
S ⇒ abbBb B ⇒ aBbbb B⇒b

*
i i
S ⇒ abb(a ) b(bbb)

i i
abb(a ) b(bbb) ∈ L(G ) i≥0
22
Consider now an infinite
context-free language L

Let G be the grammar of L − {λ}

Take G so that I has no unit-productions


no λ -productions
23
Let p = (Number of productions) x
(Largest right side of a production)

Let m = p +1
Example G : S → AB
p = 4 × 3 = 12
A → aBb
B → Sb m = p + 1 = 13
B→b
24
Take a string w∈ L (G )
with length | w |≥ m

We will show:
in the derivation of w
a variable of G is repeated

25
*
S⇒w

v1 ⇒ v2 ⇒  ⇒ vk ⇒ w

S = v1

26
v1 ⇒ v2 ⇒  ⇒ vk ⇒ w

| vi |<| vi +1 | + f maximum right hand side


of any production

| w |< k ⋅ f

m ≤| w |≤ k ⋅ f p<k⋅ f
27
v1 ⇒ v2 ⇒  ⇒ vk ⇒ w

p<k⋅ f

p
k> Number of productions
f in grammar

28
v1 ⇒ v2 ⇒  ⇒ vk ⇒ w
k> Number of productions
in grammar

Some production must be repeated


v1 ⇒  ⇒ a1 Aa2 ⇒  ⇒ a3 Aa4 ⇒  ⇒ w
S →r1
Repeated A→r2
B→r2
variable

29
w∈ L(G ) | w |≥ m
Derivation of string w
S ⇒  ⇒ a1 Aa2 ⇒  ⇒ a3 Aa4 ⇒  ⇒ w

Some variable is repeated

30
Derivation tree of string w
S

u z
Last repeated variable A

v y
w = uvxyz
repeated A
u , v, x, y , z :
Strings of terminals
x 31
S
Possible
derivations: u z
∗ A
S ⇒ uAz
∗ v y
A ⇒ vAy A

A⇒ x
x 32
We know:
∗ ∗ ∗
S ⇒ uAz A ⇒ vAy A⇒ x

This string is also generated:

∗ *
S ⇒ uAz ⇒ uxz

0 0
uv xy z
33
We know:
∗ ∗ ∗
S ⇒ uAz A ⇒ vAy A⇒ x

This string is also generated:

∗ * *
S ⇒ uAz ⇒ uvAyz ⇒ uvxyz

1 1
The original w = uv xy z
34
We know:
∗ ∗ ∗
S ⇒ uAz A ⇒ vAy A⇒ x

This string is also generated:

∗ * * *
S ⇒ uAz ⇒ uvAyz ⇒ uvvAyyz ⇒ uvvxyyz

2 2
uv xy z
35
We know:
∗ ∗ ∗
S ⇒ uAz A ⇒ vAy A⇒ x

This string is also generated:


∗ * *
S ⇒ uAz ⇒ uvAyz ⇒ uvvAyyz ⇒
* *
⇒ uvvvAyyyz ⇒ uvvvxyyyz
3 3
uv xy z
36
We know:
∗ ∗ ∗
S ⇒ uAz A ⇒ vAy A⇒ x
This string is also generated:
S⇒* uAz ⇒
* uvAyz ⇒
* uvvAyyz *⇒
*⇒ uvvvAyyyz ⇒
* 
* uvvvvAy  yyyz *⇒

* uvvvvxy  yyyz

i i
uv xy z
37
Therefore, any string of the form

i i
uv xy z i≥0

is generated by the grammar G

38
Therefore,

knowing that uvxyz ∈ L(G )

i i
we also know that uv xy z ∈ L(G )

L(G ) = L − {λ}

i i
uv xy z ∈ L
39
S

u z
A

v y
A

x
Observation: | vxy | ≤ m
Since A is the last repeated variable
40
S

u z
A

v y
A

x
Observation: | vy | ≥ 1
Since there are no unit or λ-productions 41
The Pumping Lemma:
For infinite context-free language L
there exists an integer m such that

for any string w ∈ L, | w |≥ m


we can write w = uvxyz
with lengths | vxy |≤ m and | vy |≥ 1
and it must be:
i i
uv xy z ∈ L, for all i ≥ 0
42
Applications
of
The Pumping Lemma

43
Non-context free languages
n n n
{a b c : n ≥ 0}

Context-free languages

n n
{a b : n ≥ 0}

44
Theorem: The language
n n n
L = {a b c : n ≥ 0}
is not context free

Proof: Use the Pumping Lemma


for context-free languages

45
n n n
L = {a b c : n ≥ 0}

Assume for contradiction that L


is context-free

Since L is context-free and infinite


we can apply the pumping lemma

46
n n n
L = {a b c : n ≥ 0}

Pumping Lemma gives a magic number m


such that:

Pick any string w∈ L with length | w |≥ m

m m m
We pick: w=a b c

47
n n n
L = {a b c : n ≥ 0}

m m m
w=a b c

We can write: w = uvxyz

with lengths | vxy |≤ m and | vy |≥ 1


48
n n n
L = {a b c : n ≥ 0}
m m m
w=a b c
w = uvxyz | vxy |≤ m | vy |≥ 1

Pumping Lemma says:

i i
uv xy z ∈ L for all i≥0
49
n n n
L = {a b c : n ≥ 0}
m m m
w=a b c
w = uvxyz | vxy |≤ m | vy |≥ 1

We examine all the possible locations


of string vxy in w

50
n n n
L = {a b c : n ≥ 0}
m m m
w=a b c
w = uvxyz | vxy |≤ m | vy |≥ 1

m
Case 1: vxy is within a

m m m
aaa...aaa bbb...bbb ccc...ccc
u vxy z
51
n n n
L = {a b c : n ≥ 0}
m m m
w=a b c
w = uvxyz | vxy |≤ m | vy |≥ 1

Case 1: v and y consist from only a

m m m
aaa...aaa bbb...bbb ccc...ccc
u vxy z
52
n n n
L = {a b c : n ≥ 0}
m m m
w=a b c
w = uvxyz | vxy |≤ m | vy |≥ 1

Case 1: Repeating v and y


k ≥1
m+k m m
aaaaaa...aaaaaa bbb...bbb ccc...ccc
u 2 2 z
v xy
53
n n n
L = {a b c : n ≥ 0}
m m m
w=a b c
w = uvxyz | vxy |≤ m | vy |≥ 1

2 2
Case 1: From Pumping Lemma: uv xy z ∈ L
k ≥1
m+k m m
aaaaaa...aaaaaa bbb...bbb ccc...ccc
u 2 2 z
v xy
54
n n n
L = {a b c : n ≥ 0}
m m m
w=a b c
w = uvxyz | vxy |≤ m | vy |≥ 1

2 2
Case 1: From Pumping Lemma: uv xy z ∈ L
k ≥1
2 2 m+ k m m
However: uv xy z = a b c ∉L
Contradiction!!!
55
n n n
L = {a b c : n ≥ 0}
m m m
w=a b c
w = uvxyz | vxy |≤ m | vy |≥ 1

m
Case 2: vxy is within b

m m m
aaa...aaa bbb...bbb ccc...ccc
u vxy z
56
n n n
L = {a b c : n ≥ 0}
m m m
w=a b c
w = uvxyz | vxy |≤ m | vy |≥ 1

Case 2: Similar analysis with case 1

m m m
aaa...aaa bbb...bbb ccc...ccc
u vxy z
57
n n n
L = {a b c : n ≥ 0}
m m m
w=a b c
w = uvxyz | vxy |≤ m | vy |≥ 1

m
Case 3: vxy is within c

m m m
aaa...aaa bbb...bbb ccc...ccc
u vxy z
58
n n n
L = {a b c : n ≥ 0}
m m m
w=a b c
w = uvxyz | vxy |≤ m | vy |≥ 1

Case 3: Similar analysis with case 1

m m m
aaa...aaa bbb...bbb ccc...ccc
u vxy z
59
n n n
L = {a b c : n ≥ 0}
m m m
w=a b c
w = uvxyz | vxy |≤ m | vy |≥ 1

m m
Case 4: vxy overlaps a and b

m m m
aaa...aaa bbb...bbb ccc...ccc
u vxy z
60
n n n
L = {a b c : n ≥ 0}
m m m
w=a b c
w = uvxyz | vxy |≤ m | vy |≥ 1

Case 4: Possibility 1: v contains only a


y contains only b
m m m
aaa...aaa bbb...bbb ccc...ccc
u vxy z
61
n n n
L = {a b c : n ≥ 0}
m m m
w=a b c
w = uvxyz | vxy |≤ m | vy |≥ 1

Case 4: Possibility 1: v contains only a


k1 + k 2 ≥ 1 y contains only b
m + k1 m + k2 m
aaa...aaaaaaa bbbbbbb...bbb ccc...ccc
u 2 2 z
v xy
62
n n n
L = {a b c : n ≥ 0}
m m m
w=a b c
w = uvxyz | vxy |≤ m | vy |≥ 1

2 2
Case 4: From Pumping Lemma: uv xy z ∈ L
k1 + k 2 ≥ 1
m + k1 m + k2 m
aaa...aaaaaaa bbbbbbb...bbb ccc...ccc
u 2 2 z
v xy
63
n n n
L = {a b c : n ≥ 0}
m m m
w=a b c
w = uvxyz | vxy |≤ m | vy |≥ 1

2 2
Case 4: From Pumping Lemma: uv xy z ∈ L
k1 + k 2 ≥ 1

2 2 m + k1 m + k2 m
However: uv xy z = a b c ∉L
Contradiction!!!
64
n n n
L = {a b c : n ≥ 0}
m m m
w=a b c
w = uvxyz | vxy |≤ m | vy |≥ 1

Case 4: Possibility 2: v contains a and b


y contains only b
m m m
aaa...aaa bbb...bbb ccc...ccc
u vxy z
65
n n n
L = {a b c : n ≥ 0}
m m m
w=a b c
w = uvxyz | vxy |≤ m | vy |≥ 1

Case 4: Possibility 2: v contains a and b


k1 + k 2 + k ≥ 1 y contains only b
m k1 k 2 m+k m
aaa...aaaaabbaabb bbbbbbb...bbb ccc...ccc
u 2
v xy 2 z
66
n n n
L = {a b c : n ≥ 0}
m m m
w=a b c
w = uvxyz | vxy |≤ m | vy |≥ 1

2 2
Case 4: From Pumping Lemma: uv xy z ∈ L
k1 + k 2 + k ≥ 1
m k1 k 2 m+k m
aaa...aaaaabbaabb bbbbbbb...bbb ccc...ccc
u 2
v xy 2 z
67
n n n
L = {a b c : n ≥ 0}
m m m
w=a b c
w = uvxyz | vxy |≤ m | vy |≥ 1

2 2
Case 4: From Pumping Lemma: uv xy z ∈ L

However: k1 + k 2 + k ≥ 1
2 2 m k1 k 2 m + k m
uv xy z =a b a b c ∉L
Contradiction!!!
68
n n n
L = {a b c : n ≥ 0}
m m m
w=a b c
w = uvxyz | vxy |≤ m | vy |≥ 1

Case 4: Possibility 3: v contains only a


y contains a and b
m m m
aaa...aaa bbb...bbb ccc...ccc
u vxy z
69
n n n
L = {a b c : n ≥ 0}
m m m
w=a b c
w = uvxyz | vxy |≤ m | vy |≥ 1

Case 4: Possibility 3: v contains only a


y contains a and b

Similar analysis with Possibility 2

70
n n n
L = {a b c : n ≥ 0}
m m m
w=a b c
w = uvxyz | vxy |≤ m | vy |≥ 1

m m
Case 5: vxy overlaps b and c

m m m
aaa...aaa bbb...bbb ccc...ccc
u vxy z
71
n n n
L = {a b c : n ≥ 0}
m m m
w=a b c
w = uvxyz | vxy |≤ m | vy |≥ 1

Case 5: Similar analysis with case 4

m m m
aaa...aaa bbb...bbb ccc...ccc
u vxy z
72
There are no other cases to consider

(since | vxy |≤ m , string vxy cannot

m m m
overlap a , b and c at the same time)

73
In all cases we obtained a contradiction

Therefore: The original assumption that


n n n
L = {a b c : n ≥ 0}
is context-free must be wrong

Conclusion: L is not context-free

74