•If derivation is long enough, some variable mustappear twice on same path in parse tree
S
B
C
B
C
S
S
B
C
B
C
a
b
a
b
a
b
S BC CSCaSCaBCCabCCabaCabaSB
abaBCBababCBababaB
ababab
Pumping example
•Then we can “cut and paste” part of parse tree
S
B
C
B
C
S
S
B
C
B
C
a
b
a
b
a
b
B
S
B
C
a
b
b
B
C
B
C
S
S
B
C
B
a
b
a
b
b
C
S
ababab
ababbabb
✗
Pumping example
•We can repeat this many times
•Every sufficiently large derivation will have a middlepart that can be repeated indefinitely
ababab
✗
ababbabb
✗
ababbbabbb
ababnabnbb
Pumping in general
uvwxy
uv3wx3y
u
v
v
v
w
x
y
A
A
A
A
x
x
uv2wx2y
u
v
v
w
x
y
A
A
A
x
u
v
w
x
y
A
A
u
w
y
A
uwy
Example
•If L3 has a context-free grammar G, then
•What happens for anbncn?
•No matter how it is split, uv2wx2y∉L3!
If uvwxy is in G, so are uv2wx2y, uv3wx3y, uwy, ...
L3 = {anbncn: n ≥ 0}
w
u
y
x
v
aaa ... aabbb ... bbccc ... cc
Pumping lemma for context-free languages
•Pumping lemma: For every context-free language L
There exists a number nsuch that for everystring z in L, we can write z = uvwxy where|vwx| ≤ n|vx| ≥ 1For every i≥ 0, the string uviwxiy is in L.
w
u
y
x
v
Pumping lemma for context-free languages
•So to prove L is not context-free, it is enough that
For every nthere exists z in L, such that forevery way of writing z = uvwxy where|vwx| ≤ n and |vx| ≥ 1, the string uviwxiy isnot in L for some i≥ 0.
w
u
y
x
v
Proving language is not context-free
•Like for regular languages, you need a strategy thatalways wins you this game
Donald
choose nwrite z = uvwxy(|vwx| ≤ n,|vx| ≥ 1)
you
choose zLchoose iyou win if uviwxiyL
1
2
w
u
y
x
v
≤ n
At least oneis not empty
Example
Donald
choose nwrite z = uvwxy(|vwx| ≤ n,|vx| ≥ 1)
you
choose zLchoose iyou win if uviwxiyL
1
2
1
2
L3 = {anbncn: n ≥ 0}
w
u
y
x
v
aaa ... aabbb ... bbccc ... cc
choose n
write z = uvwxy
z= anbncn
i = ?
Example
•Case 1:v or x contains two kinds of symbolsThen uv2wx2y not in L3 because pattern is wrong
•Case 2: v and x both contain one kind of symbolThen uv2wx2y does not have same number of as, bs, cs
x
v
aaa ... aabbb ... bbccc ... cc
x
v
aaa ... aabbb ... bbccc ... cc
More examples
L1 = {anbn: n ≥ 0}
L2 = {s: s has same number of as and bs}
L3 = {anbncn: n ≥ 0}
L4 = {ssR: s∈ {a, b}*}
L5 = {ss: s∈ {a, b}*}
Which is context-free?
✘
✔
✔
✔
Example
1
2
L5 = {ss: s∈ {a, b}*}
w
u
y
x
v
a a a a a a a a a b a a a a a a a a a b
choose n
write z = uvwxy
z= anbanb
i = ?
w
x
v
u
y
a a a a a a a a a b a a a a a a a a a b
What if:
Example
1
2
L5 = {ss: s {a, b}*}
w
u
y
x
v
a a a a a a b b b b b b a a a a a a b b b b b b
choose n
write z = uvwxy
z= anbnanbn
i = ?
Recall that |vwx| ≤ n
Example
Case 1:
w
x
v
a a a a a a b b b b b b a a a a a a b b b b b b
Three cases
Case 2:
w
x
v
a a a a a a b b b b b b a a a a a a b b b b b b
Case 3:
w
x
v
a a a a a a b b b b b b a a a a a a b b b b b b
vwx is in the first half of anbnanbn
vwx is in the middle part of anbnanbn
vwx is in the second half of anbnanbn
Example
Case 1:
w
x
v
a a a a a a b b b b b b a a a a a a b b b b b b
Apply pumping with i = 0
Case 2:
w
x
v
a a a a a a b b b b b b a a a a a a b b b b b b
Case 3:
w
x
v
a a a a a a b b b b b b a a a a a a b b b b b b
uwy looks like ajbkanbn, where j < n or k < n
uwy looks like anbjakbn, where j < n or k < n
uwy looks like anbnajbk, where j < n or k < n
Example
Case 1:
Apply pumping with i = 0
Case 2:
Case 3:
uv0wx0y looks like ajbkanbn, where j < n or k < n
uv0wx0y looks like anbjakbn, where j < n or k < n
uv0wx0y looks like anbnajbk, where j < n or k < n
Not of the form ss
Not of the form ss
Not of the form ss
This covers all the cases, so L5 is not context-free