Introduction
The pumping lemma for context-free languages gives a necessary condition on every CFL. It is used to prove that languages such as and are not context-free. The decomposition has five pieces with constraints that reflect a repeated path in a CNF parse tree.
Prerequisites
- Closure and Decision Properties of CFLs: CNF size ideas; when to combine with intersection against a regular language.
Core concepts
Statement
If is context-free, there exists such that every with can be written with:
- ,
- ,
- for all integers .
Quantifier order for non-CFL proofs
, with , decompositions obeying (1)-(2), with .
Parse-tree intuition
In a CNF parse tree for a long string, a long path repeats a variable . The upper yields and the lower yields , giving and , so the yield pumps as .

How it works
Template
- Assume is CFL; obtain .
- Choose , , often .
- For any with and , argue that lies in at most two of the three blocks , , .
- Pump or to break equal counts.
- Contradiction.
Intersection trick
To show not CFL, sometimes easier: if were CFL then would be CFL for regular ; pick so that is a known non-CFL (often ).
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
Take a grammar in CNF with variables and set . A CNF parse tree whose longest root-to-leaf path has edges yields at most terminals, so a string of length has a path with at least edges, hence at least variable nodes. Among the lowest variables on that path two are equal (pigeonhole). The subtree of the upper copy has height at most , so its yield has length at most ; that is condition (1). In CNF the upper copy uses a rule , and the lower copy lies under only one of , , so the other child contributes a nonempty string to or ; that is condition (2). Sipser states the same lemma for any grammar with , where is an upper bound at least 2 on all RHS lengths, for example .
Why both and 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:
Let . Since , the window cannot touch both the -block and the -block (the -block of length lies between them). Two cases:
- or contains two different letters: has letters out of order (a before an , or a before a ), so it is not in .
- and each lie inside one block: at most two of the three counts change and at least one of them strictly (). Then has unequal counts.
Either way some leaves the language. A proof that treats only the case " inside one block" is incomplete.
Example 2:
Intersect with the regular language : the result is . Pump . The window has length , so it meets at most two adjacent blocks. If it lies in the first half, pumping down changes the first -block or the first -block but not its partner in the second half; symmetrically for the second half; if it straddles the middle ( then ), pumping down shortens the first -block or the second -block alone. All cases break the pattern, so is not context-free. The complement of and are both context-free, which is a standard trap.
Example 3:
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 take : pumping once gives length between and , not a square. For the gaps between consecutive factorials eventually exceed any fixed . The same gap argument works for primes: is not context-free.
Example 4: a CFL that pumps
is a CFL and satisfies the lemma with : for , , take , , , , . Then for every . The window sits on the / boundary, which is exactly where the grammar loops.
Example 5: a non-CFL that still pumps
is not context-free, yet it satisfies the pumping lemma with . If has , pump the first letter: the string stays in , which lies in . If , pump a single : the counts are untouched, and pumping down to still lands in . Ogden's lemma (mark at least positions; contains at most marked and at least one marked) handles it: mark the 's, 's and 's of . Since the lemma is only necessary, "it pumps" never proves a language context-free.
Which of these are context-free?
| Language | CFL? | Reason |
|---|---|---|
| yes | two independent matchings in sequence | |
| yes | nested matching, , | |
| no | crossing matchings; cannot reach both partners | |
| yes | union of two CFLs (inherently ambiguous) | |
| no | Example 1 | |
| (strings not in ) | yes | complement of ; CFLs are not closed under complement, but this one is a CFL |
| , | yes | push one or two symbols per |
| , | yes | nested matching around the centre |
| , | no | crossing copies |
| , , | no | unary, not regular |
| yes | finite, hence regular |
The pattern to remember: one stack can match nested pairs (like brackets) but not crossing pairs ( with and with in order) or three-way equalities.
Example 6: misuse
Choosing where can sit on the boundary without a case analysis fails. Always case-split on which blocks intersects.
Important properties and observations
- Necessary but not sufficient for being CFL.
- Combine with closure under intersection with regular languages.
- 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 works, so does every . The proof gives for a CNF grammar.
- is a window of length at most anywhere in , not necessarily at the start (unlike in the regular lemma, which must be a prefix).
- Either or may be empty, but not both.
Common misconceptions.
- Pumping only while leaving fixed.
- Forgetting case analysis on where sits.
- Using CFL pumping to prove regularity.
GATE examination perspective
- FIVE PIECES. with paired pumping of and .
- CASE ANALYSIS. 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 fixes a prefix; in the CFL lemma may sit anywhere, so the case analysis must cover every position.
- QUANTIFIERS. Write .
Summary
Sources
- 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).
- 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).
- 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).