Pumping lemma for CFLs
The pumping lemma for context-free languages (two pumped parts, s = uvxyz with |vxy| at most p and vy not empty), its proof from a repeated variable on a long path of a Chomsky normal form parse tree, the game for proving that a language is not context-free (a^n b^n c^n, ww, a^i b^j c^k with i < j < k, squares, a^i b^j c^k d^l with i = k and j = l), the table of decompositions, and the usual mistakes.
Why it exists
Regular languages are limited because a finite state repeats; context-free languages are limited because a variable repeats in a long parse tree, and the part of the string derived between two occurrences of that variable can be repeated on both sides. The pumping lemma for CFLs turns this into a test: if a language has no such repeatable pair of parts, it is not context-free. GATE asks it for the standard languages (, , ...) and as true/false statements.
Prerequisites
You need closure properties of CFLs (closure properties) and Chomsky normal form (normal forms); the structure of the argument is that of the regular pumping lemma.
Precise definitions
Pumping lemma for context-free languages. If is context-free, there is an integer such that every string with can be written with
- (at least one of is not empty),
- ,
- for every .
The two pumped parts and are pumped together, the same number of times. As for regular languages the lemma is only a necessary condition: it can prove that a language is not context-free, never that it is.
Proof sketch (textbook). Take a grammar in Chomsky normal form with variables and . A parse tree is binary, so a string of length at least has a root-to-leaf path with more than variables on it, and some variable occurs twice on it; take the repetition nearest the leaf. The lower derives , the upper derives (the string between has two sides, because the tree is binary and the upper has another child on one side). The tree can be cut at the lower and replaced by the upper subtree, or the upper subtree repeated: , so is derivable. Choosing the lowest repetition keeps ; one side of the split is nonempty, which gives . The value was checked empirically (below).
How it works
The game
To show is not context-free:
- The adversary picks .
- You pick a string with .
- The adversary picks any decomposition with and .
- You pick with .
If you win for every and every decomposition, is not context-free. The table plays it with for six non-context-free languages and for the context-free language , listing the number of legal decompositions and how many survive :
The pumping lemma for CFLs with p = 4: start
Computed by a script, not typed| language | string s (|s| ≥ 4) | decompositions u v x y z with |vxy| ≤ 4, |vy| ≥ 1 | decompositions that survive i = 0 ... 9 | a surviving one |
|---|---|---|---|---|
| a^n b^n c^n | ||||
| w w | ||||
| a^i b^j c^k with i < j < k | ||||
| a^(n²) | ||||
| a^q, q prime | ||||
| a^i b^j c^k d^l with i = k and j = l | ||||
| a^n b^n (a context-free language: the lemma holds) |
Step 0
Every legal decomposition of the chosen string is tried with i = 0, 1, ..., 9. For the non-context-free languages none survives (that is the proof). For a^n b^n, which is context-free, some decompositions survive, as the lemma promises.
Every decomposition of the six strings fails; for (which is context-free) seven survive, for example , pumped together. The reasoning behind the failures:
. Take . Since , the window cannot touch all three letters: it misses 's or 's completely. If contain only symbols from at most two of the three blocks, pumping up (or down) changes the number of those letters but not the third, so the counts are no longer equal.
. Take with . A case analysis on where the window (length ) lies, in the first half, in the second half or straddling the middle, shows that pumping with or always produces a string that is not of the form : in the first two cases the midpoint of the pumped string moves into a block of the other symbol, in the third the two blocks next to the middle become unequal in size. The program tries every decomposition (415 of them for ) and none survives.
. Take . The window cannot contain both an and a (there are 's between them). If has no : pump up with , so the 's stay at . If contains an , the 's reach at least ; the order could then survive only if 's were added too, but then and fails (and if no was added, fails). If has only 's, then and fails. If contains a (and no ): pump down with ; if a is removed then while , and if only 's are removed then : again the order fails.
. Take . Pumping once gives length with , strictly between consecutive squares and .
. Take a prime and pump with : the length is , composite. (A unary context-free language is regular, a textbook fact, so every unary non-regular language such as this one is automatically not context-free.)
. Take . The window touches at most two neighbouring blocks, so pumping breaks one of the two crossing equalities.
Why matters
Without the bound the adversary could choose and far apart. Take and : with , , (a window of length ) pumping and together keeps and does not touch and , so this decomposition survives. The bound forbids it, and with the bound every decomposition fails.
Choosing the string
Make the language's equalities cross each other and put the blocks so that a window of length can touch only part of the structure (blocks of length separated by blocks of length ). For with an inequality use the string just on the boundary and pump up or down as needed.
Worked examples
EX 1Why a^n b^n satisfies the lemma
EasyFor with take , , , , . Then , and is in the language for every . The lemma holds, as it must for a context-free language. With no window can contain both an and a and no decomposition survives (checked), so a pumping length for must be at least .
EX 2Count the decompositions
EasyFor a string of length at least the number of choices of with and depends on the length. The table's counts: for a string of length with , for length , for length , for length (recounted with an independent enumeration).
EX 3a^n b^n c^n is not context-free
MediumSuppose it is, with pumping length . Let . Take any decomposition. has length at most , so it cannot contain both an and a (the 's between them number ). Case 1: has no . Then pumping up () increases the number of 's or 's (or both) but not the 's, so the counts differ. Case 2: has no : symmetric. So in every case. Contradiction.
EX 4Using closure first
Medium. Intersect with (regular) to get , which is not context-free; therefore is not. This avoids a direct pumping argument.
EX 5Typical questions
GATE level(a) "Which of the following is not context-free: , , , ?" . (b) " is context-free." False. is. (c) "The pumping lemma can prove a language is context-free." False. (d) " is context-free." True. (e) ":" not context-free (the quadratic count cannot be matched by a stack). (f) "A language that satisfies the pumping lemma may still be non-context-free." True (as for regular languages).
Cheat box
Cheat box
- CFL : , , for all .
- Necessary condition only. You choose and ; the adversary chooses and the decomposition.
- Non-CFL: , , , ( prime), with , , with .
- CFL: , , with or , .
- : the window touches at most two neighbouring blocks.
Traps and GATE patterns
Connections
Builds on
Across subjects
No cross-subject links yet.
It is the context-free counterpart of the regular pumping lemma (regular languages); it gives the witness for the closure counterexamples (closure of CFLs) and for the next level of the Chomsky hierarchy (hierarchy).
PYQs
11 previous year questions from your book. I re-solved each one myself (2 by running or simulating, 9 by reasoning only); the book's key is shown next to my answer. All PYQs
Let Σ = {a, b, c, d} and let L = { aibjckdl | i, j, k, l ≥ 0
}.
Which of the following constraints ensure(s) that the
language L is context-free?- (a)i + k = j + l
- (b)i = k and j = l
- (c)i = l and j = k
- (d)i + j = k + l
Consider the following two languages over the alphabet
{a, b, c}, where m and n are natural numbers.
L1 = {ambmcm+n | m, n ≥ 1}
L2 = {ambncm+n | m, n ≥ 1}
Which ONE of the following statements is CORRECT?
THEORY OF COMPUTATION- (a)A Both L1 and L2 are context-free languages.
- (b)L1 is a context-free language but L2 is not a context-free language.
- (c)L1 is not a context-free language but L2 is a context- free language..
- (d)Neither L1 nor L2 are context-free languages.
Consider the following languages:
L1 = {ww | w ∈ {a, b}* }
L2 = {anbncm | m, n≥ 0}
L3 = {ambncn | m, n≥ 0}
Which of the following statements is/are FALSE?- (a)L1 is not context-free but L2 and L3 are deterministic context-free
- (b)Neither L1 nor L2 is context-free.
- (c)L2, L3, and L2 ∩ L3 all are context-free.
- (d)Neither L1 nor its complement is context-free.
For a string w, we define wR to be the reverse of w. For example, if w= 01101 then wR= 10110. Which of the following languages is/are context-free?
- (a){wxxRwR | w,x ∈ {0,1}*}
- (b){wxwR | w,x ∈ {0,1}*}
- (c){wxwRxR | w,x ∈ {0,1}*}
- (d){wwRxxR | w,x ∈ {0,1}*}
Consider the following languages.
L1 = {wxyx | w, x, y ε (0 + 1)+}
L2 = {xy | x, y ε(a + b)*, |x| = |y|, x !=y}
Which one of the following is TRUE?- (a)L1 is context-free but not regular and L2 is context-free.
- (b)Neither L1 nor L2 is context-free.
- (c)L1 is regular and L2 is context-free.
- (d)L1 is context-free but L2 is not context-free.
Which one of the following languages over Σ = {a,b} is
NOT context-free?- (a){wwR | Wε {a, b}*}
- (b){wanwRbn | Wε {a,b}*, n >= 0}
- (c){anbi|i ε{n, 3n, 5n}, n >= 0}
- (d){wanbnwR |w ε {a, b}*, n >= 0}
Consider the following languages:
I.{ambncpdq |m + p = n + q,where m,n,p,q >= 0}
II.{ambncpdq |m=n and p=q,where m,n,p,q >= 0}
III.{ambncpdq |m=n and p!=q,where m,n,p,q>= 0}
IV.{ambncpdq |mn = p + q, where m, n, p, q>= 0}
Which of the above languages are context-free?- (a)I and IV only
- (b)I and II only
- (c)II and III only
- (d)II and IV only
Consider the following languages:
L1 = {ap│p is a prime number}
L2 = {anbmc2m | n ≥ 0, m ≥ 0}
L3 = {anbnc2n │ n ≥ 0}
L4 = {anbn │ n ≥ 1}
Which of the following are CORRECT?
I. L1 is context-free but not regular.
II. L2 is not context-free.
III. L3 is not context-free but recursive.
IV. L4 is deterministic context-free.- (a)I, II and IV only
- (b)II and III only
- (c)I and IV only
- (d)III and IV only
Consider the following languages:
L1 = {anbmcn+m: m, n > 1}
L2 = {anbnc2n: n > 1}
Which one of the following is TRUE?- (a)Both L1 and L2 are context-free.
- (b)L1 is context-free while L2 is not context-free.
- (c)L2 is context-free while L1 is not context-free.
- (d)Neither L1 nor L2 is context-free.
Consider the languages
L1 = {0 i1 j | i != j}.
L2 = {0 i1 j | i = j}.
L3 = {0 i1 j | i = 2j+1}.
L4 = {0 i1 j | i!= 2j}.
Which one of the following statements is true?- (a)Only L2 is context free.
- (b)Only L2 and L3 are context free.
- (c)Only L1 and L2 are context free.
- (d)All are context free.
Let
L1 = {0n+m1n0m | n, m >= 0},
L2 = {0n+m1n+m0m | n, m >= 0} and
L3 = {0n+m1n+m0n+m | n, m >= 0}.
Which of these languages are NOT context free?- (a)L1 only
- (b)L3 only
- (c)L1 and L2
- (d)L2 and L3
Self-check
Question 1
Why must the pumped parts and be pumped the same number of times?
Show answerHide
They come from the same repeated variable: , and each use of the loop adds one and one .
Question 2
What does prevent?
Show answerHide
It prevents the adversary from pumping parts that are far apart (in different blocks of the chosen string), which would let it keep all counts balanced.
Question 3
Is context-free? Why?
Show answerHide
Yes: with and ; only one comparison is needed.
Question 4
Which block structure of do you choose for ?
Show answerHide
: every window of length touches at most two neighbouring blocks.
Revision card
One-screen revision
- , , , .
- Proves non-CFL only; you choose and .
- Non-CFL: , , , primes, crossing equalities.
- Use intersection with a regular language first when counts are involved.
References
- ·Sipser, Introduction to the Theory of Computation (3rd edition), Section 2.3 (Theorem 2.34, the pumping lemma for context-free languages)
- ·Hopcroft, Motwani, Ullman, Introduction to Automata Theory, Languages, and Computation (3rd edition), Section 7.2 (the pumping lemma for context-free languages), Section 7.2.3 (Ogden's lemma is in the exercises)
Verification
scripts/enum-checks/toc_cfl.py: for 60 random grammars in Chomsky normal form with m variables (so p = 2^m is at most 8), every string of the language of length p to p + 3 has a decomposition uvxyz with |vxy| <= p, |vy| >= 1 that pumps for i = 0, 1, 2 (CYK membership); for the six non-context-free languages of the table (a^n b^n c^n, ww, i < j < k, a^(n^2), a^q with q prime, i = k and j = l) the chosen string has NO surviving decomposition for any p from 1 to 6 (pumped with i = 0..11); for a^n b^(n^2) (p = 1..4) no decomposition survives either; with the bound |vxy| <= p removed the language i = k, j = l would pass (v = a, x = b^p, y = c survives); a^n b^n has surviving decompositions for p = 2..7 and none for p = 1; the number of decompositions in the table was recounted independently. The proof sketch and the value p = 2^m (m the number of variables of a CNF grammar) follow the textbook and are only checked empirically (needs verification); Ogden's lemma and the statement that unary context-free languages are regular are textbook facts, quoted. Textbook chapter references are from memory. SECOND PASS (2026-11-02, done by the assistant, not a human check): every Self-check question was solved before the printed answer was read and all agreed; the definitions, worked examples, cheat box and traps were read claim by claim and the numbers re-derived by hand or by a small script (state counts, derivations, parse-tree and move counts, decomposition counts); tables and animations are machine output and were read, not re-run; The case analysis for i less than j less than k was made precise (the earlier wording skipped the case in which both an a and a b are pumped). Status raised to self-checked; verified needs a textbook or human check.
last verified: never
My notes (saved in this browser)