Theory

The Pumping Lemma

The proof that a language is NOT regular — in one contradiction

The pumping lemma is a proof technique that shows a language is not regular. Every regular language has a pumping length p such that any accepted string s with |s| ≥ p can be split as s = xyz, where |xy| ≤ p, |y| ≥ 1, and every pumped variant xyiz (for all i ≥ 0) is still in the language. The lemma follows from the pigeonhole principle: a DFA with p states, reading p or more symbols, must revisit a state and so must contain a loop you can traverse any number of times. To prove a language is not regular, you assume it is, then exhibit a string the adversary can never split without pumping it out — a clean proof by contradiction. First published by Bar-Hillel, Perles, and Shamir in 1961.

  • ProvesA language is NOT regular
  • Cannot proveThat a language IS regular
  • Split (regular)s = xyz, |xy| ≤ p, |y| ≥ 1
  • Pump conditionxyiz ∈ L for all i ≥ 0
  • Underlying principlePigeonhole over DFA states
  • IntroducedBar-Hillel, Perles, Shamir (1961)

Interactive visualization

Press play, or step through manually. The visualization is yours to drive — try it before reading on.

Open visualization fullscreen ↗

Watch the 60-second explainer

A condensed visual walkthrough — narrated, captioned, under a minute.

Why the pumping lemma matters

Regular languages are exactly the languages a finite-state machine can recognize — no unbounded memory, just a fixed set of states. That makes them fast and cheap: linear-time recognition, constant memory, the workhorse behind lexers, regular-expression engines, and network intrusion filters. But the moment a language needs to count — to match an unbounded number of opening brackets against closing ones, or to check that two halves of a string are equal — a finite automaton runs out of states. The pumping lemma is the formal tool that turns the intuition "you can't count with finite memory" into a rigorous proof.

Without it, "this language isn't regular" is just a hunch. With it, you have a repeatable, mechanical argument. It is the first non-trivial impossibility result most computer scientists ever prove, and it establishes the boundary between the bottom two rungs of the Chomsky hierarchy: it separates regular languages from context-free languages, and a stronger version separates context-free from context-sensitive.

The formal statement

If L is a regular language, then there exists an integer p ≥ 1 (the pumping length) such that every string sL with |s| ≥ p can be written as s = xyz satisfying all three conditions:

  • |y| ≥ 1 — the pumpable middle is non-empty.
  • |xy| ≤ p — the loop lives entirely within the first p characters.
  • xyiz ∈ L for every i ≥ 0 — repeating (or deleting) y keeps the string in the language.

Read the logical structure carefully, because that is where every correct proof lives:

For all regular L, there exists p, for all s with |s| ≥ p, there exists a split xyz, for all i ≥ 0, xyiz ∈ L. When you use the contrapositive to prove non-regularity, the quantifiers you get to control flip: you choose s, the adversary chooses the split, and you choose the pump count i.

Why the lemma is true: the pigeonhole loop

Suppose L is recognized by a deterministic finite automaton with exactly p states. Take any accepted string s with |s| ≥ p. As the DFA reads the first p symbols of s, it passes through p + 1 states (the start state plus one after each symbol). By the pigeonhole principle, two of those p + 1 states must be the same state — call it q. The chunk of input read between the two visits to q forms a loop: the machine leaves q, reads some non-empty substring, and returns to q.

Split the string accordingly: x is what was read before first reaching q, y is the substring read around the loop (|y| ≥ 1 because the loop consumes at least one symbol), and z is the rest. Because the loop starts and ends in q, you can traverse it zero times, once, or a hundred times and always land in the same accepting final state. That is exactly xyiz ∈ L for all i ≥ 0. And since both q visits happen within the first p symbols, |xy| ≤ p. The lemma is nothing more than "a finite machine forced to read a long string must repeat a state, and a repeated state is a loop you can pump."

Worked example: anbn is not regular

The classic. Let L = { anbn : n ≥ 0 } — equal numbers of a's followed by b's, the essence of balanced brackets. We prove L is not regular by contradiction.

  1. Assume L is regular. Then the pumping lemma gives some pumping length p.
  2. Choose the string. Pick s = apbp. Note s ∈ L and |s| = 2p ≥ p, so the lemma applies. This choice is the whole art: the run of p leading a's is longer than the window |xy| ≤ p, trapping the loop inside the a's.
  3. The adversary splits. The lemma promises some s = xyz with |xy| ≤ p and |y| ≥ 1. Because the first p characters are all a's, the constraint |xy| ≤ p forces both x and y to sit entirely inside the a-block. So y = ak for some k ≥ 1.
  4. Pump. Take i = 2. Then xy2z = ap+kbp. This has p + k a's but only p b's, and since k ≥ 1 the counts differ. Therefore xy2z ∉ L.
  5. Contradiction. The lemma demanded xyiz ∈ L for every i, yet i = 2 escapes the language. The assumption fails: L is not regular. ∎

The same recipe cracks { ww : w ∈ {a,b}* } (needs the copy-detection string apb apb), the language of palindromes, { anbm : n > m }, and { a : n ≥ 0 } (where pumping a fixed-size loop can never keep the length a perfect square, since consecutive squares grow farther apart than p).

The pumping lemma as an adversary game

The cleanest mental model treats the proof as a two-player game. You want to prove non-regularity; the adversary insists the language is regular. Moves alternate, and the quantifier structure decides who picks what:

StepWho choosesWhat they chooseYour winning strategy
1Adversarythe pumping length pKeep p symbolic — never assume a value.
2Youa string s ∈ L, |s| ≥ pPick s so any legal split traps y in a countable region.
3Adversarya split xyz with |xy| ≤ p, |y| ≥ 1Argue about every split the constraints allow.
4Youa pump count i ≥ 0Find one i (often 2, sometimes 0) that leaves L.

You win — meaning you have a valid proof — if and only if you have a strategy that beats every adversary response in steps 1 and 3. That is why step 2 must handle all splits at once, and why "let p = 5" or "suppose the adversary splits it as..." are proof-killing mistakes.

The context-free pumping lemma

There is a stronger cousin, the Bar-Hillel lemma (or context-free pumping lemma), that pushes the boundary up one rung. If L is context-free, there is a pumping length p such that any s ∈ L with |s| ≥ p can be split into five parts s = uvwxy with:

  • |vwx| ≤ p — the two pumped regions plus their middle fit in a window of p.
  • |vx| ≥ 1 — at least one of the two pumped pieces is non-empty.
  • uviwxiy ∈ L for all i ≥ 0 — v and x are pumped together, in lockstep.

The two synchronized pumps come from a repeated non-terminal in a parse tree taller than the number of variables in the grammar (again pigeonhole, now over a context-free grammar's non-terminals and realized by a pushdown automaton's stack). This version proves { anbncn : n ≥ 0 } and { ww : w ∈ {a,b}* } are not context-free — a claim the regular lemma cannot make, since it speaks only about regularity, not context-freeness. Intuitively, a stack can count one pair but not two independent pairs.

Regular vs context-free vs Myhill-Nerode

Regular pumping lemmaContext-free pumping lemmaMyhill-Nerode theorem
String splitxyz (3 parts)uvwxy (5 parts)equivalence classes of prefixes
What gets pumpedyv and x togethern/a
Proves non-membership inregular languagescontext-free languagesregular languages
Can prove regularity?NoNo (CFL-ness)Yes — exact characterization
Condition typeNecessary, not sufficientNecessary, not sufficientNecessary and sufficient
Underlying objectDFA statesGrammar variables / PDA stackRight-congruence index
Kills anbn?YesApplies but stays CFLYes (infinitely many classes)
Kills anbncn?Yes (not even regular)Yes (not context-free)Yes (not regular)

The proof as an algorithm

Every pumping-lemma proof follows the same skeleton. Written as pseudocode, the "proof" is a decision you make about strings and pump counts — the code below is the template you instantiate per language, not a decision procedure (non-regularity is only semi-decidable in general).

# Template for a pumping-lemma non-regularity proof.
# You supply choose_s(p) and choose_i(); the adversary supplies (p, split).
# The proof is VALID iff, for every p and every legal split, some i escapes L.

def in_language(s: str) -> bool:
    # Example: L = { a^n b^n : n >= 0 }
    n = 0
    while n < len(s) and s[n] == 'a':
        n += 1
    return s[n:] == 'b' * n            # rest is exactly n b's

def choose_s(p: int) -> str:
    return 'a' * p + 'b' * p          # |s| = 2p >= p, and s in L

def pump(x: str, y: str, z: str, i: int) -> str:
    return x + y * i + z              # xy^i z  (i = 0 deletes y)

def proof_wins_against(p: int, x: str, y: str, z: str) -> bool:
    # adversary guarantees: x+y+z == choose_s(p), |xy| <= p, |y| >= 1
    assert x + y + z == choose_s(p) and len(x + y) <= p and len(y) >= 1
    for i in (0, 2):                  # you get to pick i; try a couple
        if not in_language(pump(x, y, z, i)):
            return True               # this i escapes L -> contradiction
    return False

# Because |xy| <= p and the first p chars are all 'a', y is a block of a's.
# Pumping to i = 2 adds a's without b's, so a^(p+k) b^p leaves L for every
# legal split -> L = { a^n b^n } is NOT regular.

Common misconceptions and pitfalls

  • Using it to prove a language IS regular. The lemma is a one-way street. Satisfying the pumping property does not imply regularity — the language { aibjck : i = 0 or j = k } passes the pumping lemma yet is not regular. For proofs of regularity, build a DFA/regex or use Myhill-Nerode.
  • Choosing the split yourself. You do not get to pick xyz. The adversary does, and your proof must defeat every split that obeys |xy| ≤ p and |y| ≥ 1. A proof that reasons about only one convenient split is wrong.
  • Assigning p a concrete value. p is whatever the adversary declares. Your string s and your argument must be parameterized by symbolic p, e.g. s = apbp, not a5b5.
  • Picking a string shorter than p. The lemma only speaks about |s| ≥ p. If your s could be shorter than p, the adversary is under no obligation, and the proof collapses.
  • Forgetting |xy| ≤ p. This constraint is what pins the loop into a specific region of the string. Dropping it lets the adversary place y anywhere and usually escape your argument. It is the difference between anbn (provable) and a much weaker version of the lemma.
  • Assuming i = 2 always works. Sometimes you must pump down with i = 0, or reason about a general i. Choose the pump count that produces the contradiction for the language at hand.

A short history

The pumping lemma was introduced in 1961 by Yehoshua Bar-Hillel, Micha Perles, and Eli Shamir in their paper on formal properties of phrase-structure grammars — the same paper that gave the context-free version now called the Bar-Hillel lemma. It arrived alongside the crystallization of the Chomsky hierarchy and the Myhill-Nerode theorem (Myhill 1957, Nerode 1958), which together mapped the exact frontier of finite-state computation. Today the lemma is a rite of passage in every automata-theory and theory-of-computation course, and its pigeonhole core — "finite memory forces repetition" — echoes through complexity theory, communication complexity, and streaming-algorithm lower bounds.

Frequently asked questions

What does the pumping lemma actually prove?

The pumping lemma proves a language is NOT regular — never that one is. It states a necessary property of every regular language: a pumping length p exists such that long strings can be pumped. If a language fails this property, it cannot be regular. But passing the property does not make a language regular — some non-regular languages satisfy the lemma, so it is a one-way test used only for negative results.

What is the pumping length p?

The pumping length p is a constant that exists for every regular language, guaranteed to be at most the number of states in a DFA recognizing the language. Any accepted string of length ≥ p forces the automaton to revisit some state, creating a loop. The substring read along that loop is the pumpable part y — it can be repeated any number of times and the machine still accepts. You never need to know the exact value of p to run the proof; you only use that some p exists.

How do you use the pumping lemma to prove a^n b^n is not regular?

Assume the language is regular with pumping length p. Choose the string s = a^p b^p, which has length 2p ≥ p. By the lemma, s = xyz with |xy| ≤ p and |y| ≥ 1. Since |xy| ≤ p, both x and y lie entirely within the leading a's, so y = a^k for some k ≥ 1. Pumping to i = 2 gives a^(p+k) b^p, which has more a's than b's and is therefore not in the language — contradicting the lemma. So a^n b^n is not regular.

Why does the proof pick the string instead of letting the adversary choose?

The pumping lemma is a game with alternating quantifiers. The adversary picks p and later picks the split xyz and the pump count. You — the prover — pick the string s and the pump count i. To win, you must choose an s that no matter how the adversary splits it (subject to |xy| ≤ p and |y| ≥ 1) can be pumped out of the language. Getting the quantifier order wrong is the single most common mistake in these proofs.

Can pumping down (i = 0) be used in a proof?

Yes. The lemma quantifies over all i ≥ 0, including i = 0, which deletes y entirely — often called pumping down. For some languages, deleting y produces the contradiction more cleanly than repeating it. For example, with the language of strings where a's outnumber b's, pumping down removes a's and can drop the count below the threshold. A valid proof only needs one value of i that leaves the language.

Is there a pumping lemma for context-free languages?

Yes. The context-free pumping lemma (Bar-Hillel lemma) splits a long string into five parts s = uvwxy with |vwx| ≤ p and |vx| ≥ 1, and pumps two substrings simultaneously: uv^i w x^i y stays in the language for all i ≥ 0. It proves languages like a^n b^n c^n and w w are not context-free. The two pumped regions come from a repeated non-terminal in a parse tree taller than the number of grammar variables.

What is the difference between the pumping lemma and the Myhill-Nerode theorem?

The pumping lemma gives a necessary but not sufficient condition — it can only prove non-regularity, and some non-regular languages fool it. The Myhill-Nerode theorem is an exact characterization: a language is regular if and only if its set of distinguishable prefix classes (the index of the equivalence relation) is finite. Myhill-Nerode can prove both regularity and non-regularity and never gives a false negative, but the pumping lemma is often faster to apply for a quick contradiction.