G27
Self-checkedTier 1 10 min readtoc.pumping-lemma-cfl

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 (anbncna^n b^n c^n, wwww, ...) 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 LL is context-free, there is an integer p≥1p \ge 1 such that every string s∈Ls \in L with ∣s∣≥p|s| \ge p can be written s=u v x y zs = u\,v\,x\,y\,z with

  1. ∣v y∣≥1|v\,y| \ge 1 (at least one of v,yv, y is not empty),
  2. ∣v x y∣≤p|v\,x\,y| \le p,
  3. u vi x yi z∈Lu\,v^i\,x\,y^i\,z \in L for every i≥0i \ge 0.

The two pumped parts vv and yy 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 mm variables and p=2mp = 2^m. A parse tree is binary, so a string of length at least 2m2^m has a root-to-leaf path with more than mm variables on it, and some variable AA occurs twice on it; take the repetition nearest the leaf. The lower AA derives xx, the upper AA derives v x yv\,x\,y (the string between has two sides, because the tree is binary and the upper AA has another child on one side). The tree can be cut at the lower AA and replaced by the upper subtree, or the upper subtree repeated: A⇒∗vAyA \Rightarrow^* v A y, so uvixyizu v^i x y^i z is derivable. Choosing the lowest repetition keeps ∣vxy∣≤2m|vxy| \le 2^m; one side of the split is nonempty, which gives ∣vy∣≥1|vy| \ge 1. The value p=2mp = 2^m was checked empirically (below).

How it works

The game

To show LL is not context-free:

  1. The adversary picks pp.
  2. You pick a string s∈Ls \in L with ∣s∣≥p|s| \ge p.
  3. The adversary picks any decomposition s=uvxyzs = uvxyz with ∣vxy∣≤p|vxy| \le p and ∣vy∣≥1|vy| \ge 1.
  4. You pick i≥0i \ge 0 with uvixyiz∉Lu v^i x y^i z \notin L.

If you win for every pp and every decomposition, LL is not context-free. The table plays it with p=4p = 4 for six non-context-free languages and for the context-free language anbna^n b^n, listing the number of legal decompositions and how many survive i=0,…,9i = 0, \ldots, 9:

The pumping lemma for CFLs with p = 4: start

Computed by a script, not typed
languagestring s (|s| ≥ 4)decompositions u v x y z with |vxy| ≤ 4, |vy| ≥ 1decompositions that survive i = 0 ... 9a 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.

0 / 7
computed by script: the machine is simulated or the sets are constructed by scripts/toc_lib.py, one step at a time. python3 scripts/gen-toc4-traces.py

Every decomposition of the six strings fails; for anbna^n b^n (which is context-free) seven survive, for example v=av = a, y=by = b pumped together. The reasoning behind the failures:

{anbncn}\{a^n b^n c^n\}. Take s=apbpcps = a^p b^p c^p. Since ∣vxy∣≤p|vxy| \le p, the window vxyvxy cannot touch all three letters: it misses aa's or cc's completely. If v,yv, y 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.

{ww}\{ww\}. Take s=apbpapbp=tts = a^p b^p a^p b^p = tt with t=apbpt = a^p b^p. A case analysis on where the window vxyvxy (length ≤p\le p) lies, in the first half, in the second half or straddling the middle, shows that pumping with i=0i = 0 or i=2i = 2 always produces a string that is not of the form t′t′t't': 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 p=4p = 4) and none survives.

{aibjck:i<j<k}\{a^i b^j c^k : i < j < k\}. Take s=apbp+1cp+2s = a^p b^{p+1} c^{p+2}. The window cannot contain both an aa and a cc (there are p+1p + 1 bb's between them). If vyvy has no cc: pump up with i=2i = 2, so the cc's stay at p+2p + 2. If vyvy contains an aa, the aa's reach at least p+1p + 1; the order i<ji < j could then survive only if bb's were added too, but then j≥p+2=kj \ge p + 2 = k and j<kj < k fails (and if no bb was added, j=p+1≤ij = p + 1 \le i fails). If vyvy has only bb's, then j≥p+2=kj \ge p + 2 = k and j<kj < k fails. If vyvy contains a cc (and no aa): pump down with i=0i = 0; if a bb is removed then j≤pj \le p while i=pi = p, and if only cc's are removed then k≤p+1=jk \le p + 1 = j: again the order fails.

{an2}\{a^{n^2}\}. Take s=ap2s = a^{p^2}. Pumping once gives length p2+kp^2 + k with 1≤k≤∣vy∣≤p1 \le k \le |vy| \le p, strictly between consecutive squares p2p^2 and (p+1)2(p + 1)^2.

{aq:q prime}\{a^q : q \text{ prime}\}. Take a prime q≥pq \ge p and pump with i=q+1i = q + 1: the length is q+q ∣vy∣=q(1+∣vy∣)q + q\,|vy| = q(1 + |vy|), 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.)

{aibjckdl:i=k and j=l}\{a^i b^j c^k d^l : i = k \text{ and } j = l\}. Take apbpcpdpa^p b^p c^p d^p. The window touches at most two neighbouring blocks, so pumping breaks one of the two crossing equalities.

Why ∣vxy∣≤p|vxy| \le p matters

Without the bound the adversary could choose vv and yy far apart. Take {aibjckdl:i=k and j=l}\{a^i b^j c^k d^l : i = k \text{ and } j = l\} and s=apbpcpdps = a^p b^p c^p d^p: with v=av = a, x=bpx = b^p, y=cy = c (a window of length p+2p + 2) pumping vv and yy together keeps i=ki = k and does not touch jj and ll, so this decomposition survives. The bound ∣vxy∣≤p|vxy| \le p 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 pp can touch only part of the structure (blocks of length pp separated by blocks of length pp). For aibjcka^i b^j c^k 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

Easy

For s=apbps = a^p b^p with p≥2p \ge 2 take u=ap−1u = a^{p-1}, v=av = a, x=εx = \varepsilon, y=by = b, z=bp−1z = b^{p-1}. Then ∣vxy∣=2≤p|vxy| = 2 \le p, ∣vy∣=2≥1|vy| = 2 \ge 1 and uvixyiz=ap−1+ibp−1+iu v^i x y^i z = a^{p-1+i} b^{p-1+i} is in the language for every i≥0i \ge 0. The lemma holds, as it must for a context-free language. With p=1p = 1 no window can contain both an aa and a bb and no decomposition survives (checked), so a pumping length for anbna^n b^n must be at least 22.

EX 2Count the decompositions

Easy

For a string of length at least pp the number of choices of (u,v,x,y,z)(u, v, x, y, z) with ∣vxy∣≤p|vxy| \le p and ∣vy∣≥1|vy| \ge 1 depends on the length. The table's counts: 295295 for a string of length 1212 with p=4p = 4, 415415 for length 1616, 8585 for length 55, 175175 for length 88 (recounted with an independent enumeration).

EX 3a^n b^n c^n is not context-free

Medium

Suppose it is, with pumping length pp. Let s=apbpcps = a^p b^p c^p. Take any decomposition. vxyvxy has length at most pp, so it cannot contain both an aa and a cc (the bb's between them number pp). Case 1: vyvy has no cc. Then pumping up (i=2i = 2) increases the number of aa's or bb's (or both) but not the cc's, so the counts differ. Case 2: vyvy has no aa: symmetric. So uv2xy2z∉Luv^2xy^2z \notin L in every case. Contradiction.

EX 4Using closure first

Medium

L={w∈{a,b,c}∗:#a=#b=#c}L = \{w \in \{a, b, c\}^* : \#a = \#b = \#c\}. Intersect with a∗b∗c∗a^* b^* c^* (regular) to get {anbncn}\{a^n b^n c^n\}, which is not context-free; therefore LL is not. This avoids a direct pumping argument.

EX 5Typical questions

GATE level

(a) "Which of the following is not context-free: {anbn}\{a^n b^n\}, {anbncn}\{a^n b^n c^n\}, {wwR}\{ww^R\}, {anbmcm}\{a^n b^m c^m\}?" {anbncn}\{a^n b^n c^n\}. (b) "{ww}\{ww\} is context-free." False. {wwR}\{ww^R\} is. (c) "The pumping lemma can prove a language is context-free." False. (d) "{aibjck:i=j or j=k}\{a^i b^j c^k : i = j \text{ or } j = k\} is context-free." True. (e) "{anbn2}\{a^n b^{n^2}\}:" 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 ⇒\Rightarrow ∃p ∀s∈L,∣s∣≥p ∃ s=uvxyz\exists p\ \forall s \in L, |s| \ge p\ \exists\, s = uvxyz: ∣vy∣≥1|vy| \ge 1, ∣vxy∣≤p|vxy| \le p, uvixyiz∈Luv^ixy^iz \in L for all i≥0i \ge 0.
  • Necessary condition only. You choose ss and ii; the adversary chooses pp and the decomposition.
  • Non-CFL: anbncna^nb^nc^n, wwww, an2a^{n^2}, aqa^q (qq prime), aibjckdla^ib^jc^kd^l with i=ki=k, j=lj=l, aibjcka^ib^jc^k with i<j<ki<j<k.
  • CFL: anbna^nb^n, wwRww^R, aibjcka^ib^jc^k with i=ji=j or j=kj=k, anbmcma^nb^m c^m.
  • ∣vxy∣≤p|vxy| \le p: the window touches at most two neighbouring blocks.

Traps and GATE patterns

Connections

It is the context-free counterpart of the regular pumping lemma (regular languages); it gives the witness anbncna^n b^n c^n 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

GATE 2026 · Set 2 · MSQQ3 · page 225 of the book
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
GATE 2025 · Set 1 · MCQQ7 · page 225 of the book
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.
GATE 2022 · MSQ · 1 markQ13 · page 227 of the book
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.
GATE 2021 · Set 2 · MSQ · 2 marksQ15 · page 227 of the book
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}*}
GATE 2020 · MCQ · 2 marksQ17 · page 227 of the book
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.
GATE 2019 · MCQ · 2 marksQ18 · page 227 of the book
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}
GATE 2018 · MCQ · 2 marksQ19 · page 227 of the book
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
GATE 2017 · Set 2 · MCQ · 2 marksQ24 · page 228 of the book
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
year not read yetQ26 · page 228 of the book
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.
GATE 2010 · MCQ · 2 marksQ31 · page 229 of the book
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.
GATE 2006 · MCQQ41 · page 231 of the book
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 vv and yy be pumped the same number of times?

Show answer

They come from the same repeated variable: A⇒∗vAyA \Rightarrow^* vAy, and each use of the loop adds one vv and one yy.

Question 2

What does ∣vxy∣≤p|vxy| \le p prevent?

Show answer

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 {anbncm:n,m≥0}\{a^n b^n c^m : n, m \ge 0\} context-free? Why?

Show answer

Yes: S→XCS \to XC with X→aXb∣εX \to aXb \mid \varepsilon and C→cC∣εC \to cC \mid \varepsilon; only one comparison is needed.

Question 4

Which block structure of ss do you choose for {ww}\{ww\}?

Show answer

apbpapbpa^p b^p a^p b^p: every window of length ≤p\le p touches at most two neighbouring blocks.

Revision card

One-screen revision

  • s=uvxyzs = uvxyz, ∣vxy∣≤p|vxy| \le p, ∣vy∣≥1|vy| \ge 1, uvixyiz∈Luv^ixy^iz \in L.
  • Proves non-CFL only; you choose ss and ii.
  • Non-CFL: anbncna^nb^nc^n, wwww, an2a^{n^2}, 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