Introduction

The pumping lemma for context-free languages gives a necessary condition on every CFL. It is used to prove that languages such as {anbncn}\{a^n b^n c^n\} and {ww}\{ ww \} are not context-free. The decomposition has five pieces uvxyzuvxyz with constraints that reflect a repeated path in a CNF parse tree.

Prerequisites

Core concepts

Statement

If LL is context-free, there exists p≥1p \ge 1 such that every w∈Lw \in L with ∣w∣≥p|w| \ge p can be written w=uvxyzw = u v x y z with:

  1. ∣vxy∣≤p|vxy| \le p,
  2. ∣vy∣≥1|vy| \ge 1,
  3. uvkxykz∈Lu v^k x y^k z \in L for all integers k≥0k \ge 0.

Quantifier order for non-CFL proofs

∀p\forall p, ∃w∈L\exists w \in L with ∣w∣≥p|w|\ge p, ∀\forall decompositions obeying (1)-(2), ∃k\exists k with uvkxykz∉Lu v^k x y^k z \notin L.

Parse-tree intuition

In a CNF parse tree for a long string, a long path repeats a variable AA. The upper AA yields vAyvAy and the lower yields xx, giving A⇒∗vAyA \Rightarrow^* v A y and A⇒∗xA \Rightarrow^* x, so the yield pumps as vkxykv^k x y^k.

uvxyz decomposition on a CNF parse tree with a repeated variable
Figure 1. CNF parse tree with a repeated variable A on a long path; the yield splits as u v x y z where v and y are the strings pumped when the A-loop is repeated.

How it works

Template

  1. Assume LL is CFL; obtain pp.
  2. Choose w∈Lw \in L, ∣w∣≥p|w|\ge p, often apbpcpa^p b^p c^p.
  3. For any uvxyzuvxyz with ∣vxy∣≤p|vxy|\le p and vy≠εvy\neq\varepsilon, argue that vxyvxy lies in at most two of the three blocks a∗a^*, b∗b^*, c∗c^*.
  4. Pump k=0k=0 or k=2k=2 to break equal counts.
  5. Contradiction.

Intersection trick

To show LL not CFL, sometimes easier: if LL were CFL then L∩RL \cap R would be CFL for regular RR; pick RR so that L∩RL \cap R is a known non-CFL (often a∗b∗c∗a^* b^* c^*).

Algorithms and pseudocode

No decision procedure inside the lemma; it is a proof tool. Membership remains decidable for CFLs even when the language class boundary is shown by pumping.

Mathematical explanation

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

Take a grammar in CNF with m=∣V∣m = |V| variables and set p=2mp = 2^m. A CNF parse tree whose longest root-to-leaf path has hh edges yields at most 2h−12^{h-1} terminals, so a string of length ≥2m\ge 2^m has a path with at least m+1m+1 edges, hence at least m+1m+1 variable nodes. Among the lowest m+1m+1 variables on that path two are equal (pigeonhole). The subtree of the upper copy has height at most m+1m+1, so its yield vxyvxy has length at most 2m=p2^m = p; that is condition (1). In CNF the upper copy uses a rule A→BCA \to BC, and the lower copy lies under only one of BB, CC, so the other child contributes a nonempty string to vv or yy; that is condition (2). Sipser states the same lemma for any grammar with p=b∣V∣+1p = b^{|V|+1}, where bb is an upper bound at least 2 on all RHS lengths, for example b=max⁡(2,max⁡{∣α∣:A→α})b=\max(2,\max\{\lvert\alpha\rvert:A\to\alpha\}).

Why both vv and yy pump together

Both come from the same repeated variable's upper and lower yields; you cannot pump only one side independently in the basic lemma (Ogden's lemma strengthens control).

Worked examples

Example 1: {anbncn∣n≥0}\{a^n b^n c^n \mid n \ge 0\}

Let w=apbpcpw = a^p b^p c^p. Since ∣vxy∣≤p|vxy| \le p, the window vxyvxy cannot touch both the aa-block and the cc-block (the bb-block of length pp lies between them). Two cases:

  • vv or yy contains two different letters: uv2xy2zuv^2xy^2z has letters out of order (a bb before an aa, or a cc before a bb), so it is not in a∗b∗c∗a^*b^*c^*.
  • vv and yy each lie inside one block: at most two of the three counts change and at least one of them strictly (∣vy∣≥1|vy| \ge 1). Then uv0xy0zuv^0xy^0z has unequal counts.

Either way some k∈{0,2}k \in \{0, 2\} leaves the language. A proof that treats only the case "vxyvxy inside one block" is incomplete.

Example 2: {ww∣w∈{0,1}∗}\{ ww \mid w \in \{0,1\}^* \}

Intersect with the regular language 0∗1∗0∗1∗0^*1^*0^*1^*: the result is {0i1j0i1j}\{0^i 1^j 0^i 1^j\}. Pump w=0p1p0p1pw = 0^p 1^p 0^p 1^p. The window vxyvxy has length ≤p\le p, so it meets at most two adjacent blocks. If it lies in the first half, pumping down changes the first 00-block or the first 11-block but not its partner in the second half; symmetrically for the second half; if it straddles the middle (1p1^p then 0p0^p), pumping down shortens the first 11-block or the second 00-block alone. All cases break the pattern, so {ww}\{ww\} is not context-free. The complement of {ww}\{ww\} and {wwR}\{ww^R\} are both context-free, which is a standard trap.

Example 3: {an!∣n≥0}\{a^{n!} \mid n \ge 0\}

Every context-free language over a one-letter alphabet is regular (a consequence of Parikh's theorem), so for unary languages the regular pumping lemma or a gap argument suffices. For {an2}\{a^{n^2}\} take w=ap2w = a^{p^2}: pumping once gives length between p2+1p^2 + 1 and p2+p<(p+1)2p^2 + p < (p+1)^2, not a square. For {an!}\{a^{n!}\} the gaps between consecutive factorials eventually exceed any fixed ∣vy∣|vy|. The same gap argument works for primes: {aq∣q prime}\{a^q \mid q \text{ prime}\} is not context-free.

Example 4: a CFL that pumps

{anbn}\{a^n b^n\} is a CFL and satisfies the lemma with p=2p = 2: for w=anbnw = a^n b^n, n≥1n \ge 1, take u=an−1u = a^{n-1}, v=av = a, x=εx = \varepsilon, y=by = b, z=bn−1z = b^{n-1}. Then uvkxykz=an−1+kbn−1+kuv^kxy^kz = a^{n-1+k} b^{n-1+k} for every kk. The window sits on the aa/bb boundary, which is exactly where the grammar S→aSbS \to aSb loops.

Example 5: a non-CFL that still pumps

L={aibjckdl∣i=0 or j=k=l}L = \{a^i b^j c^k d^l \mid i = 0 \text{ or } j = k = l\} is not context-free, yet it satisfies the pumping lemma with p=1p = 1. If ww has i=0i = 0, pump the first letter: the string stays in b∗c∗d∗b^*c^*d^*, which lies in LL. If i≥1i \ge 1, pump a single aa: the b,c,db, c, d counts are untouched, and pumping down to i=0i = 0 still lands in LL. Ogden's lemma (mark at least pp positions; vxyvxy contains at most pp marked and vyvy at least one marked) handles it: mark the bb's, cc's and dd's of abpcpdpab^pc^pd^p. Since the lemma is only necessary, "it pumps" never proves a language context-free.

Which of these are context-free?

LanguageCFL?Reason
{anbncmdm}\{a^n b^n c^m d^m\}yestwo independent matchings in sequence
{anbmcmdn}\{a^n b^m c^m d^n\}yesnested matching, S→aSd∣TS \to aSd \mid T, T→bTc∣εT \to bTc \mid \varepsilon
{anbmcndm}\{a^n b^m c^n d^m\}nocrossing matchings; vxyvxy cannot reach both partners
{aibjck∣i=j or j=k}\{a^i b^j c^k \mid i = j \text{ or } j = k\}yesunion of two CFLs (inherently ambiguous)
{aibjck∣i=j=k}\{a^i b^j c^k \mid i = j = k\}noExample 1
{aibjck∣i≠j or j≠k}∪\{a^i b^j c^k \mid i \ne j \text{ or } j \ne k\} \cup (strings not in a∗b∗c∗a^*b^*c^*)yescomplement of {anbncn}\{a^nb^nc^n\}; CFLs are not closed under complement, but this one is a CFL
{anb2n}\{a^n b^{2n}\}, {ambn∣m≤n≤2m}\{a^m b^n \mid m \le n \le 2m\}yespush one or two symbols per aa
{wwR}\{ww^R\}, {w∣w=wR}\{w \mid w = w^R\}yesnested matching around the centre
{ww}\{ww\}, {wwRw}\{ww^Rw\}nocrossing copies
{an2}\{a^{n^2}\}, {a2n}\{a^{2^n}\}, {aq∣q prime}\{a^q \mid q \text{ prime}\}nounary, not regular
{anbncn∣n≤100}\{a^n b^n c^n \mid n \le 100\}yesfinite, hence regular

The pattern to remember: one stack can match nested pairs (like brackets) but not crossing pairs (aa with cc and bb with dd in a b c da\,b\,c\,d order) or three-way equalities.

Example 6: misuse

Choosing ww where vxyvxy can sit on the boundary without a case analysis fails. Always case-split on which blocks vxyvxy intersects.

Important properties and observations

  • Necessary but not sufficient for being CFL.
  • Combine with closure under intersection with regular languages.
  • ∣vy∣≥1|vy|\ge 1 prevents the trivial empty pump.
  • Regular pumping is the 3-piece special case; do not use 3-piece reasoning for CFL claims.
  • The constant is not unique: if pp works, so does every p′>pp' > p. The proof gives p=2∣V∣p = 2^{|V|} for a CNF grammar.
  • vxyvxy is a window of length at most pp anywhere in ww, not necessarily at the start (unlike xyxy in the regular lemma, which must be a prefix).
  • Either vv or yy may be empty, but not both.

Common misconceptions.

  • Pumping only vv while leaving yy fixed.
  • Forgetting case analysis on where vxyvxy sits.
  • Using CFL pumping to prove regularity.

GATE examination perspective

  • FIVE PIECES. u,v,x,y,zu,v,x,y,z with paired pumping of vv and yy.
  • CASE ANALYSIS. vxyvxy in one or two blocks only.
  • INTERSECTION FIRST. Often shortens the proof.
  • NESTED VERSUS CROSSING. Nested matching dependencies suggest stack-based constructions; the listed crossing-dependency examples require a proof of non-context-freeness, not a frequency heuristic.
  • UNARY LANGUAGES. A unary language is a CFL iff it is regular; squares, powers of 2 and primes are not CFLs.
  • WINDOW POSITION. In the regular lemma ∣xy∣≤p|xy| \le p fixes a prefix; in the CFL lemma vxyvxy may sit anywhere, so the case analysis must cover every position.
  • QUANTIFIERS. Write ∀p∃w∀uvxyz∃k\forall p \exists w \forall uvxyz \exists k.

Summary

Sources

  1. J. E. Hopcroft, R. Motwani, J. D. Ullman, Introduction to Automata Theory, Languages, and Computation, 3rd ed., Pearson Addison-Wesley: Section 7.2 (the size of parse trees, statement of the pumping lemma, applications of the pumping lemma for CFLs).
  2. M. Sipser, Introduction to the Theory of Computation, 3rd ed., Cengage Learning: Section 2.3 (non-context-free languages, the pumping lemma for context-free languages).
  3. P. Linz, An Introduction to Formal Languages and Automata, 6th ed., Jones & Bartlett Learning: Section 8.1 (two pumping lemmas: for context-free languages and for linear languages).

Next steps