Sub topic 12
The Pumping Lemma for Context-Free Languages
This chapter assumes you can write a context-free grammar (CFG), read a parse tree, and convert a grammar to Chomsky normal form (CNF). It also assumes you know the pumping lemma for regular languages. The goal is to use the context-free version correctly in a proof, and to avoid the mistakes that cost marks in GATE.
The statement
If L is a context-free language (CFL), there is a constant p ≥ 1, the pumping length, with this property. Every string z in L with |z| ≥ p can be split as z = u v w x y so that:
- |vwx| ≤ p.
- |vx| ≥ 1, so v and x are not both empty.
- u vⁱ w xⁱ y is in L for every i ≥ 0.
The regular lemma pumps one substring, y in xyz. This lemma pumps two substrings, v and x, in step. Both are repeated the same number of times, and they sit on either side of w. That pairing is the reason it can show a^n b^n is context-free, because a pushdown stack matches two counts. It is also the reason it can show a^n b^n c^n is not, because two pumped pieces cannot keep three counts equal.
Why it holds
Take a grammar for L in CNF with k variables. Every rule is A → BC or A → a. Set p = 2ᵏ.
- A parse tree in CNF is a binary tree on its variable nodes. A path containing m variable nodes yields at most 2^(m−1) terminals.
- If |z| ≥ 2ᵏ, some root-to-leaf path therefore contains at least k+1 variable nodes.
- By the pigeonhole principle, one variable A appears twice on that path. Choose the repeat within the lowest k+1 variables of the path.
- Let the upper A's subtree yield vwx and the lower A's subtree yield w. Because only k+1 variables lie below the upper A, |vwx| ≤ 2ᵏ = p.
- The upper A has two children, and one of them contains the lower A. The other child yields at least one terminal, and that terminal belongs to v or x. So |vx| ≥ 1.
- Replacing the lower A's subtree with a copy of the upper A's subtree pumps up. Replacing the upper A's subtree with the lower one pumps down (i = 0).
The proof as a game
The lemma has the quantifier form ∃p ∀z ∃(split) ∀i. To prove L is not context-free, you assume it is, and then you play against an adversary:
| Step | Who moves | What they do | Column | Column |
|---|---|---|---|---|
| 1 | Adversary | Picks p. You know nothing about it except p ≥ 1. | ||
| 2 | You | Pick a string z in L with | z | ≥ p, written in terms of p. |
| 3 | Adversary | Picks any split z = uvwxy meeting conditions 1 and 2. | ||
| 4 | You | Pick an i, usually 0 or 2, and show u vⁱ w xⁱ y is not in L. |
You must defeat every legal split, so a proof needs a case analysis. The condition |vwx| ≤ p is your main tool. It limits the window that gets pumped.
Here is the standard proof for L = { aⁿbⁿcⁿ : n ≥ 1 }. Take z = aᵖbᵖcᵖ. The window vwx has length at most p, so it cannot touch both an a and a c, because the b block of length p lies between them. So vx touches at most two of the three letters, and it touches at least one. Pumping with i = 2 raises the count of the touched letters and leaves the untouched one unchanged. The counts are no longer equal, so the string is not in L. This contradicts the lemma.
Checking splits by brute force
The case analysis can be checked by machine for one fixed p. This program tries every legal split of a string and reports whether any split survives pumping.
def survives(z, p, in_lang, powers=(0, 2, 3)):
"""Return True if some split z = uvwxy with |vwx| <= p and |vx| >= 1
stays in the language for every pumping power in `powers`."""
n = len(z)
for i in range(n + 1):
for j in range(i, n + 1):
for k in range(j, n + 1):
for l in range(k, n + 1):
if l - i > p:
break
if (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_lang(u + v * t + w + x * t + y) for t in powers):
return True
return False
def anbn(s):
n = len(s) // 2
return n >= 1 and s == "a" * n + "b" * n
def anbncn(s):
n = len(s) // 3
return n >= 1 and s == "a" * n + "b" * n + "c" * n
p = 4
print(survives("a" * p + "b" * p, p, anbn)) # True
print(survives("a" * p + "b" * p + "c" * p, p, anbncn)) # FalseThe first call prints True. For aⁿbⁿ, the split v = a, w empty, x = b at the boundary of the two blocks pumps correctly, so the lemma gives no contradiction. The second call prints False: no split of a⁴b⁴c⁴ survives. This is evidence for p = 4 only. A real proof must work for an arbitrary p, which is what the window argument above provides.
Common errors
- Using the lemma to prove a language is context-free. The lemma is a necessary condition. A language can satisfy it and still not be context-free. A language that fails it is definitely not context-free. A language that passes tells you nothing.
- Fixing z before p. The string z must depend on p, such as aᵖbᵖcᵖ. A fixed string like a⁵b⁵c⁵ is not valid, because the adversary can choose p larger than 5.
- Choosing a z shorter than p. The lemma applies only when |z| ≥ p.
- Forgetting |vwx| ≤ p. Without this bound you cannot rule out a window that spans a whole block. Most case analyses depend on it.
- Assuming v and x lie in the same block. They can be in different blocks, and one of them can be empty. Cover every case: both inside one block, straddling a boundary between two blocks, or one piece empty.
- Testing only i = 1. Pumping with i = 1 returns the original string. Use i = 0 or i = 2.
- Choosing a convenient split. The adversary picks the split, not you. You must show the contradiction for all legal splits.
Which languages are context-free
A pumping argument is useful mainly for languages that need to compare three or more counts, or to compare two unrelated strings.
| Language | Context-free? | Reason | Column |
|---|---|---|---|
| aⁿbⁿ | Yes | S → aSb \ | ab |
| aⁿbⁿcⁿ | No | Pumping contradiction above | |
| aⁿbᵐcⁿ with n, m ≥ 1 | Yes | The stack matches a with c, and b is free | |
| aⁿbⁿcᵐ with n, m ≥ 1 | Yes | Concatenation of aⁿbⁿ and c⁺ | |
| ww over {a,b} | No | Pump z = aᵖbᵖaᵖbᵖ | |
| wwᴿ | Yes | The stack stores w and reads it back reversed | |
| a^(n²) | No | A CFL over a one-letter alphabet is regular, and the squares are not regular | |
| aⁱbʲcᵏ with i < j < k | No | Pump up or down to break the ordering |
A shortcut for unary languages: over a one-letter alphabet, every CFL is regular. So if a unary language fails the regular pumping lemma, it is not context-free either.
Exercise
Why does the string z = aᵖbᵖcᵖ work for the pumping proof, but z = a^(p/2) b^(p/2) c^(p/2) would not?
Show answer
The proof needs |z| ≥ p, and a^(p/2) b^(p/2) c^(p/2) has length 3p/2, which is fine for that. The real problem is that the window bound |vwx| ≤ p no longer separates a from c, because the b block has length only p/2. A window of length p can then contain both an a and a c, and the argument that vx touches at most two letters fails.
Exercise
A student shows that every string z in L of length at least p has a split satisfying all three pumping conditions. Can the student conclude L is context-free? Give an example.
Show answer
No. The lemma is only a necessary condition. For example, a language can satisfy the pumping property for suitable p and still not be context-free. The lemma can prove a language is not context-free, but never that it is.