{
  "id": "pumping-lemma",
  "title": "Pumping Lemma",
  "lang": "en",
  "source_lang": "en",
  "summary": "A pumping lemma gives a necessary repetition property of a class of formal languages, commonly used to prove that a language is not regular or not context-free.",
  "category": "computer-science",
  "subcategory": "algorithms-theory",
  "type": "concept",
  "aliases": [
    "Pumping lemma for regular languages",
    "Pumping lemma for context-free languages"
  ],
  "generated_at": "2026-11-05",
  "model": "pro/gpt-6.1-sol",
  "url": "https://aiwiki.page/en/pumping-lemma/",
  "translations": {
    "zh-Hans": {
      "url": "https://aiwiki.page/zh/pumping-lemma/",
      "json": "https://aiwiki.page/zh/pumping-lemma.json"
    }
  },
  "sections": [
    {
      "heading": "",
      "text": "A pumping lemma is a theorem stating that sufficiently long strings in certain classes of formal languages contain parts that can be repeated or removed while remaining in the language. The two standard versions concern regular languages and context-free languages. Their repetition properties arise from repeated states in finite automata or repeated nonterminals in grammatical derivations. Pumping lemmas are chiefly used to prove that a language does not belong to one of these classes: if a language fails the required property, it cannot belong to the class."
    },
    {
      "heading": "Notation and meaning",
      "text": "A formal language \\(L\\) is a set of finite strings over an alphabet \\(\\Sigma\\). The set of all such strings is written \\(\\Sigma^*\\). For a string \\(w\\), its length is \\(|w|\\), and juxtaposition denotes concatenation.\n\nFor a string \\(y\\), the expression \\(y^i\\) means \\(i\\) consecutive copies of \\(y\\), where \\(i\\) is a nonnegative integer. In particular,\n\n\\[y^0=\\varepsilon,\\]\n\nwhere \\(\\varepsilon\\) is the empty string. Thus, “pumping” includes both repeating a segment and deleting it.\n\nA pumping length is a positive integer \\(p\\) beyond which the lemma guarantees a suitable decomposition of every string in the language. The value depends on the language or on a particular representation of it. It need not be the smallest possible value, and every larger integer is also a valid pumping length.\n\nThe lemmas concern strings already in the language. They do not claim that every sufficiently long string over the alphabet belongs to it."
    },
    {
      "heading": "Pumping lemma for regular languages",
      "text": "If \\(L\\) is regular, there is an integer \\(p\\geq 1\\) such that every \\(w\\in L\\) with \\(|w|\\geq p\\) can be written\n\n\\[w=xyz\\]\n\nwith\n\n\\[|xy|\\leq p,\\qquad |y|\\geq 1,\\]\n\nand\n\n\\[xy^iz\\in L\\quad\\text{for every integer }i\\geq 0.\\]\n\nThe conditions have distinct roles:\n\n- \\(|xy|\\leq p\\) locates the repeated segment within the first \\(p\\) symbols.\n- \\(|y|\\geq 1\\) makes the repetition nontrivial.\n- The universal condition on \\(i\\) requires the same decomposition to work for every number of repetitions.\n\nOnly the existence of one suitable decomposition is guaranteed. Other decompositions of the same string may fail to preserve membership.\n\nWhy the lemma holds\n\nA regular language is recognized by a deterministic finite automaton. Suppose an automaton recognizing \\(L\\) has \\(n\\) states, and take \\(p=n\\).\n\nWhile reading the first \\(n\\) symbols of a string, the automaton visits \\(n+1\\) states when the initial state is included. By the pigeonhole principle, two of these visits must be to the same state.\n\nSplit the input into:\n\n- \\(x\\): the prefix before the first of these two visits;\n- \\(y\\): the nonempty segment read between the visits;\n- \\(z\\): the remainder.\n\nReading \\(y\\) returns the automaton to the same state. It can therefore traverse that loop zero times, once, or any number of times, then read \\(z\\) and reach the same accepting state. The loop occurs within the first \\(n\\) symbols, giving the required length bound.\n\nThis argument explains the underlying restriction: an automaton with finitely many states must repeat a state on a sufficiently long computation."
    },
    {
      "heading": "Proving that a language is not regular",
      "text": "The standard proof uses proof by contradiction:\n\n- Assume that the language is regular.\n- Let \\(p\\) be the pumping length guaranteed by the lemma.\n- Choose a string \\(w\\in L\\), depending on \\(p\\), with \\(|w|\\geq p\\).\n- Consider every decomposition \\(w=xyz\\) satisfying the length conditions.\n- Show that, for each such decomposition, some \\(i\\geq 0\\) gives \\(xy^iz\\notin L\\).\n\nThe fourth step is essential. Demonstrating that one particular decomposition fails does not contradict the lemma, because a different decomposition might work.\n\nExample: equal numbers of two symbols\n\nConsider\n\n\\[L=\\{a^nb^n:n\\geq 0\\}.\\]\n\nAssume \\(L\\) is regular and let \\(p\\) be its pumping length. Choose\n\n\\[w=a^pb^p.\\]\n\nFor any permitted decomposition \\(w=xyz\\), the condition \\(|xy|\\leq p\\) places \\(y\\) entirely within the initial block of \\(a\\)'s. Therefore,\n\n\\[y=a^k\\]\n\nfor some \\(1\\leq k\\leq p\\).\n\nTaking \\(i=0\\) gives\n\n\\[xz=a^{p-k}b^p,\\]\n\nwhich has unequal numbers of \\(a\\)'s and \\(b\\)'s and is not in \\(L\\). This contradicts the pumping lemma, so \\(L\\) is not regular.\n\nThe conclusion is specifically nonregularity. The language is nevertheless context-free."
    },
    {
      "heading": "Pumping lemma for context-free languages",
      "text": "If \\(L\\) is context-free, there is an integer \\(p\\geq 1\\) such that every \\(w\\in L\\) with \\(|w|\\geq p\\) can be written\n\n\\[w=uvxyz\\]\n\nwith\n\n\\[|vxy|\\leq p,\\qquad |vy|\\geq 1,\\]\n\nand\n\n\\[uv^ixy^iz\\in L \\quad\\text{for every integer }i\\geq 0.\\]\n\nHere two segments, \\(v\\) and \\(y\\), are pumped together using the same exponent. Either may be empty, but they cannot both be empty. The middle segment \\(x\\) is unchanged.\n\nThe bounded window \\(vxy\\) may occur anywhere in the string. Unlike the regular-language lemma, this lemma does not restrict the pumpable material to a prefix.\n\nLetters used for the five segments vary between presentations. The mathematical content is determined by which segments are repeated and by the length constraints.\n\nWhy the lemma holds\n\nA context-free language is generated by a context-free grammar. Its derivations can be represented by parse trees, whose internal nodes are nonterminal symbols and whose leaves yield the generated string.\n\nThe standard proof converts the grammar to Chomsky normal form, treating the empty string separately. In this form, productions have the shapes\n\n\\[A\\to BC \\quad\\text{or}\\quad A\\to a.\\]\n\nA sufficiently long string requires a sufficiently deep parse tree. Because the grammar has finitely many nonterminals, some nonterminal \\(A\\) repeats along a root-to-leaf path. The relevant portions of the derivation have the form\n\n\\[S\\Rightarrow^*uAz,\\qquad A\\Rightarrow^*vAy,\\qquad A\\Rightarrow^*x.\\]\n\nRepeating the middle derivation, or omitting it, produces\n\n\\[uv^ixy^iz.\\]\n\nChoosing repeated occurrences near the bottom of a sufficiently long path bounds the yield \\(vxy\\). The branching structure ensures that at least one of \\(v\\) and \\(y\\) is nonempty.\n\nThe two pumped segments represent material generated on opposite sides of a recursively expanded nonterminal."
    },
    {
      "heading": "Proving that a language is not context-free",
      "text": "The proof strategy has the same quantifier structure as for regular languages, but it must account for two pumped segments.\n\nExample: three equal blocks\n\nConsider\n\n\\[K=\\{a^nb^nc^n:n\\geq 0\\}.\\]\n\nAssume \\(K\\) is context-free, let \\(p\\) be its pumping length, and choose\n\n\\[w=a^pb^pc^p.\\]\n\nFor any permitted decomposition \\(w=uvxyz\\), the window \\(vxy\\) has length at most \\(p\\). It cannot contain both an \\(a\\) and a \\(c\\), because doing so would require spanning the entire block of \\(p\\) intervening \\(b\\)'s as well.\n\nConsequently, the pumped segments \\(v\\) and \\(y\\) together contain symbols from at most two of the three blocks. At least one symbol type is untouched.\n\nTaking \\(i=0\\) removes at least one symbol because \\(|vy|\\geq 1\\), while leaving all \\(p\\) occurrences of at least one other symbol type unchanged. The resulting string cannot have equal counts of \\(a\\), \\(b\\), and \\(c\\). It is therefore outside \\(K\\), contradicting the lemma.\n\nThus \\(K\\) is not context-free. This excludes recognition by a pushdown automaton, not recognition by more powerful computational models."
    },
    {
      "heading": "Quantifiers and common errors",
      "text": "The regular-language lemma has the logical structure\n\n\\[\\exists p\\geq 1\\ \\forall w\\in L,\\ |w|\\geq p\\ \\exists x,y,z\\ \\forall i\\geq 0,\\]\n\nwith the decomposition constraints understood.\n\nA nonregularity proof establishes the opposite pattern:\n\n\\[\\forall p\\geq 1\\ \\exists w\\in L,\\ |w|\\geq p\\ \\forall\\text{ permitted decompositions}\\ \\exists i\\geq 0\\]\n\nsuch that the pumped string is outside \\(L\\).\n\nThis order explains several frequent mistakes:\n\n- Selecting a convenient decomposition. The proof must defeat every permitted decomposition.\n- Using a string outside the language. The lemma makes no promise about such strings.\n- Choosing a fixed string without accommodating \\(p\\). The string must be long enough for an arbitrary pumping length.\n- Requiring one exponent to work for all decompositions. The exponent may depend on the decomposition.\n- Pumping the two context-free segments independently. Both must use the same exponent.\n- Treating successful pumping as a membership proof. The lemmas provide necessary conditions, not characterizations.\n\nThe context-free lemma has the corresponding quantifier structure, with five segments instead of three."
    },
    {
      "heading": "Scope and limitations",
      "text": "Neither standard pumping lemma has a valid converse: satisfying its pumping property does not, by itself, establish regularity or context-freeness. To establish membership, a proof normally constructs an appropriate automaton, grammar, or equivalent description, or uses established closure properties.\n\nFinite languages satisfy both lemmas vacuously when \\(p\\) is greater than every string length in the language. There are then no strings to which the pumping requirement applies.\n\nThe lemmas are structural proof tools, not general-purpose algorithms for classifying arbitrarily specified languages. Their usefulness depends on finding a string whose possible decompositions can all be controlled. Some languages resist a direct pumping argument even when their nonmembership can be established by other methods.\n\nFor regular languages, the Myhill–Nerode theorem gives a necessary and sufficient characterization using distinguishable prefixes. For context-free languages, Ogden's lemma strengthens the pumping method by allowing selected positions in a string to be marked, constraining where pumping must occur. Closure properties can also transform a difficult language into one with a simpler nonmembership proof.\n\nThese methods help distinguish expressive capabilities relevant to compiler construction: finite-state techniques are suited to many lexical patterns, while context-free grammars describe many recursively nested syntactic structures. Pumping arguments establish limits of those formal models; they do not imply that the same language is impossible to recognize using a more powerful model."
    }
  ],
  "links": [
    "formal-language",
    "regular-language",
    "finite-state-machine",
    "pigeonhole-principle",
    "proof-by-contradiction",
    "algorithm",
    "compiler"
  ],
  "sources": [],
  "license": "https://creativecommons.org/licenses/by/4.0/"
}