Sub topic 13
Using the pumping lemma to show a language is not context-free
This chapter assumes you know the pumping lemma statement for context-free languages (CFLs) from the previous chapter, the pumping lemma for regular languages, and proof by contradiction. Here you learn to turn the lemma into a complete proof.
The statement you will use
If L is a CFL, there is a pumping length p such that every string z in L with |z| ≥ p can be split as z = uvwxy where:
- |vwx| ≤ p
- |vx| ≥ 1
- u vⁱ w xⁱ y is in L for every i ≥ 0
To show a language is not context-free, you prove that no such p exists. The lemma is a necessary condition for being a CFL. A language that fails it is not a CFL. A language that satisfies it may still not be a CFL, so the lemma can never prove that a language is context-free.
The proof as a game
The statement has a quantifier pattern: there exists p, for all z, there exists a split, for all i. Negating it gives a game with an adversary.
- The adversary picks p. You cannot choose it or assume anything about it except p ≥ 1.
- You pick a string z in L with |z| ≥ p, written in terms of p.
- The adversary picks any split satisfying conditions 1 and 2.
- You pick a pumping count i and show that u vⁱ w xⁱ y is not in L.
You win only if step 4 works for every split the adversary could choose in step 3. That means you must cover all cases.
Worked proof: aⁿbⁿcⁿ
Let L = { aⁿbⁿcⁿ | n ≥ 1 }. Suppose L is context-free with pumping length p.
Choose z = aᵖbᵖcᵖ. Then |z| = 3p ≥ p, so z splits as uvwxy with |vwx| ≤ p and |vx| ≥ 1.
The block bᵖ has length p. So vwx, which has length at most p, cannot touch both the first a and the last c. It misses the a's, the c's, or both. Two cases cover everything:
- vwx contains no c. Then v and x contain only a's and b's, and at least one of them is nonempty. Pump with i = 2. The number of a's or the number of b's goes above p, but the number of c's stays p. The counts are no longer equal.
- vwx contains no a. Then v and x contain only b's and c's. Pump with i = 2. The number of b's or c's goes above p, but the number of a's stays p.
In both cases u v² w x² y is not in L. This contradicts the lemma, so L is not context-free.
The proof depends on the choice of z. If you had chosen z = a^p b^p, the adversary could pick v = a and x = b and every pumped string would stay in the language. A good z forces the adversary's window of length p to be too short to cover everything that has to stay balanced.
Checking the case analysis by brute force
For a fixed small p, you can test every split by computer. This does not prove the claim for all p, but it catches a bad choice of z or a missing case.
def in_L(s):
n = len(s) // 3
return n >= 1 and s == "a" * n + "b" * n + "c" * n
def every_split_fails(p, pumps=(0, 2)):
z = "a" * p + "b" * p + "c" * p
m = len(z)
for i in range(m + 1):
for j in range(i, m + 1):
for k in range(j, m + 1):
for l in range(k, m + 1):
if l - i > p or (j - i) + (l - k) == 0:
continue
u, v, w, x, y = z[:i], z[i:j], z[j:k], z[k:l], z[l:]
if all(in_L(u + v * e + w + x * e + y) for e in pumps):
return False # this split survives pumping
return True
for p in range(1, 6):
print(p, every_split_fails(p)) # True for every pIf this printed False for some p, the adversary would have a split that survives, and you would need a different z.
Worked proof: a with square length
Let L = { a^(n²) | n ≥ 1 }. Suppose L is context-free with pumping length p.
Choose z = a^(p²). Every split has v and x made only of a's. Let k = |vx|, so 1 ≤ k ≤ |vwx| ≤ p. Pump with i = 2. The new string has length p² + k, and
p² < p² + k ≤ p² + p < p² + 2p + 1 = (p + 1)²
The length lies strictly between two consecutive squares, so it is not a perfect square. The pumped string is not in L, which is a contradiction.
Here there is only one case, because a unary alphabet leaves no choice about which letters v and x contain.
An alternative route: closure properties
CFLs are closed under intersection with a regular language. This sometimes avoids a messy case analysis.
Take L₂ = { w ∈ {a,b,c} | the number of a's, b's and c's are equal }. Then L₂ ∩ abc = { aⁿbⁿcⁿ | n ≥ 0 }. If L₂ were a CFL, this intersection would be one too. We showed it is not, so L₂ is not a CFL.
Use this technique when the language has no clean form for the pumping argument. Intersecting with a regular language can restrict it to the form you can pump. GATE questions often reward this approach.
Common errors
| Mistake | Why it fails | Column | Column |
|---|---|---|---|
| Choosing a specific p such as p = 3 | The adversary chooses p. Your z must be written in terms of p. | ||
| Choosing the split yourself | The adversary chooses the split. You must refute every legal split. | ||
| Using only one case | The proof must cover every position of vwx. | ||
| Ignoring | vwx | ≤ p | This condition is what limits vwx to at most two adjacent blocks. |
| Forgetting | vx | ≥ 1 | If vx may be empty, pumping changes nothing and no contradiction arises. |
| Concluding that a language is a CFL because pumping works | The lemma is one-directional. | ||
| Pumping with a fixed i for all cases | Choose i per case. Some cases need i = 0, others i = 2. |
Which languages are usually not context-free
The following are standard non-CFLs, and they are the ones GATE tends to use:
- { aⁿbⁿcⁿ | n ≥ 1 }
- { ww | w ∈ {a,b}* }, the copy language. The reversed version { wwᴿ } is a CFL.
- { aⁿ² | n ≥ 1 } and { aᵖ | p is prime }
- { aⁿbⁿcᵐ... } with three counts that must satisfy equality or ordering constraints, for example { aⁱbʲcᵏ | i < j < k }
The pattern is that one stack cannot compare three quantities, or compare a string with a copy of itself in the same order.
Exercise
Use the pumping lemma to show that L = { aⁿbⁿcⁿdⁿ | n ≥ 1 } is not context-free. Which string z do you choose, and what are the cases?
Show answer
Assume L is context-free with pumping length p and choose z = aᵖbᵖcᵖdᵖ. Since |vwx| ≤ p, vwx cannot contain both an a and a d, so it misses at least one of a or d. Then v and x touch at most three letters, and at least one letter is touched. Pump with i = 2: the touched letters increase in count while some untouched letter keeps count p. The four counts are no longer equal, so the pumped string is not in L. This contradicts the lemma.
Exercise
Why does choosing z = aᵖbᵖ fail when you try to prove that { aⁿbⁿcⁿ } is not context-free?
Show answer
The string aᵖbᵖ is not in L, because it has no c's. Even for a similar language that does contain it, the adversary can choose v = a and x = b, which keeps the counts of a and b equal after pumping. The chosen z must be in L, and it must be long enough that vwx cannot reach every letter whose count is constrained.