Exercises — Pumping lemma for CFLs
Before we begin, one reminder of the tool we lean on constantly. The lemma says: if is context-free, there is a number (the pumping length) so that every string with can be cut into five pieces with (1) , (2) , and (3) for all . We prove a language is not a CFL by showing some long string has no valid split satisfying all three. See Parse Trees and Derivations and Chomsky Normal Form for the machinery behind it.

The figure above is the mental picture for the whole page: a string is a row of letters, and means the shaded pumping window is a short slider of width at most — it cannot reach across a gap bigger than .
Level 1 — Recognition
These check that you know the shape of the lemma before you use it.
L1.1
State the split the CFL pumping lemma produces, and list its three conditions. How many pumped pieces are there, and are they pumped with the same exponent or independent ones?
Recall Solution
The lemma splits a long string as — five pieces, with two pumped pieces and .
- (the two pumped pieces are not both empty).
- (the middle window is bounded by the pumping length).
- for all .
The two pieces and are pumped with the same exponent (not independent). Contrast Pumping lemma for regular languages which has one piece in .
L1.2
In the derivation of the lemma, we convert the grammar to Chomsky Normal Form with variables and set from . (a) Why CNF? (b) Which principle forces a variable to repeat on a root-to-leaf path?
Recall Solution
(a) In CNF every rule is or , so the parse tree is binary. A binary tree of height has at most leaves, which lets us convert "long string ⇒ many leaves" into "tall tree ⇒ long path" cleanly. (b) The Pigeonhole Principle: a path with more than variable nodes but only distinct variables must reuse one variable .
L1.3
For we chose . Explain in one sentence why the condition matters here.
Recall Solution
Because the window has width , and the three blocks each have length , the window cannot stretch from inside the -block to inside the -block (they are apart). So touches at most two of the three letter types — the geometric fact that makes the whole proof work.
Level 2 — Application
Now use the lemma to prove concrete languages are not CFLs.
L2.1
Prove is not context-free.
Recall Solution
Assume is a CFL; let be its pumping length. Choose with .
Take any split with and . Since , the window spans at most two adjacent blocks, so contains at most two of the three letter types (it can never contain both and ).
Pump to : . Since , at least one letter type strictly increases, but the untouched type (there is always at least one) keeps count . So the three counts are no longer all equal ⇒ . Contradiction. ∴ is not a CFL. ∎
L2.2
Prove is not context-free. (Watch: inside the language names a count, not the pump exponent.)
Recall Solution
Assume CFL with pumping length . Choose ; since , . Write the pump exponent as to avoid clashing with the language's .
Any split has , so spans at most two adjacent letter types. Two cases cover every split:
- omits (contains only 's and/or 's): pump up, . Some - or -count rises above while -count stays . Then or — the ordering breaks.
- omits (contains only 's and/or 's): pump down, . Some - or -count drops below while -count stays . Then that smaller count — the ordering breaks.
Every split falls into (at least) one case (if spans – it omits ; if – it omits ; if a single type, both cases apply). Contradiction. ∴ not a CFL. ∎
L2.3
Prove (strings of 's whose length is a perfect square) is not context-free.
Recall Solution
Assume CFL, pumping length . Choose ().
Any split has . Let , so . Pump to : the new length is Since , we have So the new length lies strictly between consecutive squares and — it is not a perfect square. Hence . Contradiction. ∴ not a CFL. ∎
Level 3 — Analysis
Here the split itself needs careful casework or a subtle window argument.
L3.1
Prove is not context-free. Explain why the naive string fails and why works.
Recall Solution
Why fails: it equals with ? No — actually is pumpable while staying in -like form for a naive attempt, and worse, a clever adversary split near the middle can keep it a valid . The point: a witness with only one -block and one -block gives the adversary room to pump symmetrically. We need a string where the two halves are far apart.
The real proof. Assume CFL, pumping length . Choose . This is with , so , and .
Take any split with , . Label the four blocks . Because , the window sits within a stretch of length , so it overlaps at most two adjacent blocks. Pump to (add symbols) — casework on where the window sits:
- Window in – region: the first half grows but the second half () is unchanged, so the two halves have different lengths ⇒ result cannot be (both halves of have equal length ).
- Window in – region: symmetric — second half grows, first half fixed.
- Window straddling – (the exact middle): even then it lies wholly in the "first-half's -region + second-half's -region," and adding symbols there shifts the midpoint, so the halves no longer split as identical copies.
In every case the halves become unequal in length or content, so . Contradiction. ∴ not a CFL. ∎
L3.2
Prove is not context-free.
Recall Solution
Assume CFL, pumping length . Choose ().
Any split has . Because the -block has length and the -block length , the window sits in one of three positions. Let number of 's in and number of 's in , with .
Pump to : new counts are , . For membership we need , i.e.
- If : need , but then , contradicting . So membership fails.
- If : then . But . Impossible.
Either way . Contradiction. ∴ not a CFL. ∎
Level 4 — Synthesis
Combine the lemma with Closure properties of CFLs or with intersections — sometimes the direct pump is awkward and closure is the cleaner tool.
L4.1
Prove is not context-free — first directly, then note a slicker route.
Recall Solution
Direct. Assume CFL, pumping length . Choose . Any split has , so touches at most two of the four blocks. Pump : at least one touched count rises but at least two untouched blocks keep count , so the four counts are no longer all equal ⇒ . Contradiction. ∎
Slicker (closure). Suppose were a CFL. CFLs are closed under intersection with regular languages. Intersect with the regular language (no change), then project... actually the clean statement: if were context-free, so would be a language we already know isn't. The direct pump above is the safest here — closure shines more in L4.2.
L4.2
Prove is not context-free using closure, given that CFLs are not closed under intersection but are closed under intersection with regular languages. (Contrast with the direct L2.1.)
Recall Solution
This one is a lesson in what closure can and cannot do.
CFLs are closed under intersection with a regular language, but is not itself an intersection of a CFL with a regular language in an obviously helpful way. However, here is the classic closure argument for a related fact:
Let and . Both are CFLs (each matches only one pair of blocks — build a grammar per Context-Free Grammars). Their intersection is If CFLs were closed under intersection, this would be a CFL. But by L2.1 it is not a CFL. Therefore CFLs are not closed under intersection — and conversely this exhibits as an intersection of two CFLs that is not a CFL. ∎
(Moral: closure gives a proof-by-outsourcing, but you still need the pumping lemma somewhere to seed the contradiction — here in L2.1.)
L4.3
Prove is not context-free — or show it IS. Decide which, then prove it.
Recall Solution
This one IS context-free — a synthesis test of whether you reach for the right tool. is a union of two CFLs, and CFLs are closed under union (Closure properties of CFLs). Grammar: Here generates and generates . Since a grammar exists, is context-free. Pumping lemma cannot prove this (it only ever proves not-CFL); you must exhibit a grammar or PDA — see Pushdown Automata. ∎
Level 5 — Mastery
Here the standard witness fails and you must engineer a smarter one, or count leaves yourself.
L5.1
Prove (unary strings of prime length) is not context-free.
Recall Solution
Assume CFL with pumping length (call it to avoid clash with "prime "). Pick a prime and take , .
Split with . Then for each . Its length is ; we need this to be prime for all . Choose : Since , we have and , so is a product of two integers each — a composite number, not prime. Hence . Contradiction. ∴ not a CFL. ∎
L5.2
Prove (lengths that are powers of two) is not context-free.
Recall Solution
Assume CFL, pumping length . Pick large enough that and take .
Let with . Pump to : new length . We need to be a power of two. But Since the length is strictly greater than , and since it is at most ; it equals only if , but forces only when and the window fills the whole string — impossible since when we take large enough that . So : strictly between consecutive powers of two ⇒ not a power of two. Hence . Contradiction. ∴ not a CFL. ∎
(Cleanest: choose with ; then gives with no equality edge case.)
L5.3
Bound problem: a CFG in CNF has variables. (a) Using the parent's convention , what is ? (b) A binary parse tree of height has at most how many leaves? (c) If a string has leaves, give a lower bound on the tree height and explain via Pigeonhole Principle why a variable must repeat.
Recall Solution
(a) . (b) At most leaves (binary tree of height ). (c) We need . Since , the height must satisfy . So the longest root-to-leaf path has variable nodes (plus the terminal leaf). With only distinct variables available, a path carrying variable nodes must reuse one — Pigeonhole Principle: 5 nodes into 4 pigeonholes forces a repeat. That repeated variable is the pumpable loop. ∎