47.01.06 · theoretical-cs / formal-languages-automata

Pumping Lemma for Context-Free Languages and CYK Parsing

shipped3 tiersLean: nonepending prereqs

Anchor (Master): Sipser 2013 Introduction to the Theory of Computation 3e (Cengage) §2.3-2.4; Hopcroft, Motwani & Ullman 2006 Introduction to Automata Theory, Languages, and Computation 3e (Pearson) §7.4 (CYK with Chomsky normal form conversion, Ogden's lemma as a strengthening)

Intuition Beginner

Think of a family tree. Each person has two parents. If you go back enough generations, with enough people in each generation, you will eventually find that two people on the same ancestral path share the same last name. That repeated name creates a loop in the tree.

A parse tree for a context-free grammar works the same way. The grammar has rules like "a sentence is a noun phrase plus a verb phrase." Each rule expands one symbol into two or more children. For a sufficiently long sentence, the tree must be tall. And since there are only finitely many grammar symbols, some symbol must appear twice on the same path from root to leaf.

That repeated symbol is the key. The subtree between the two appearances can be copied or deleted. Because the grammar is recursive (the same symbol produces itself), you can repeat the middle part as many times as you want, or remove it entirely, and the result is still a valid sentence in the language.

This is the context-free pumping lemma. Unlike the regular version (which has one pumpable region), the context-free version has two. The two regions come from the two subtrees of the repeated nonterminal. And unlike the regular version, the pumpable regions are bounded in total length: they must fit within a window of symbols.

The CYK algorithm solves the other direction: given a string, is it in the language? It works bottom-up, building a table of which substrings each nonterminal can produce. For every pair of adjacent substrings that can produce from and (where is a rule), mark as producing the combined span. After filling the table, the start symbol produces the whole string if and only if the string is in the language.

Visual Beginner

A parse tree with a repeated nonterminal on a path:

         S
        / \
       A    ...
      / \
     A    y        ← A repeats on the same path
    / \
   u    v          ← v and y are the two pumpable parts
        |
        x

Pumping both and simultaneously:

Version String In language?
Remove both and Yes (by pumping lemma)
Original string Yes
Repeat both and once Yes
Repeat both three times Yes

The CYK table for deciding membership:

Input:  b  a  a  b  a

Span 1: [b] [a] [a] [b] [a]
Span 2: [ba] [aa] [ab] [ba]
Span 3: [baa] [aab] [aba]
Span 4: [baab] [aaba]
Span 5: [baaba]  ← Does S appear here?

Each cell stores the set of nonterminals that generate that substring.

Worked example Beginner

We prove that is not context-free.

Suppose is context-free with pumping length . Choose . By the pumping lemma, where , , and for all .

Since , the window cannot span all three letter types (, , and ) at once. So and together contain at most two types of letters.

Case 1: lies entirely in the s and s. Pumping up () adds more s and/or s without adding s. The pumped string has more s and/or s than s, so it is not in .

Case 2: lies entirely in the s and s. Pumping up adds more s and/or s without adding s. The pumped string has more s and/or s than s, so it is not in .

In every case, pumping produces a string outside . Contradiction. So is not context-free.

Check your understanding Beginner

Formal definition Intermediate+

Lemma (Pumping lemma for context-free languages). Let be a context-free language. Then there exists a constant such that every string with can be written as where:

  1. For each , .
  2. (at least one of is nonempty).
  3. (the two pumpable regions fit within a window of length ).

Proof. Let be a CFG in Chomsky normal form generating with nonterminals. Set . Let with . Consider the parse tree for . In CNF, every internal node has either 2 children (a nonterminal production ) or 1 child (a terminal production ). The yield of a subtree of height has length at most (binary branching doubles at each level).

Since , the parse tree has height at least . The longest path from root to leaf has at least internal nodes. By pigeonhole, some nonterminal appears at least twice on this path. Consider the upper and lower occurrences of .

The upper generates a subtree whose yield contains . The lower generates a subtree whose yield contains and separated by . Since the path from the upper to the lower has at most nodes, the yield of the subtree rooted at the upper has length at most , giving . The yield of the lower subtree has (otherwise , which is impossible in a proper CNF grammar without unit productions).

For any , replacing the upper subtree by its -fold repetition (pump copies of the derivation) produces a valid parse tree with yield .

Algorithm (CYK). Given a CFG in Chomsky normal form and input string :

  1. For : set .
  2. For length : for : set , then .
  3. Accept iff .

The running time is : there are table entries, and each entry examines split points and rules.

Counterexamples to common slips

  • "The CFL pumping lemma can prove a language is context-free." No. It is a necessary condition, not sufficient. There exist non-context-free languages that satisfy the pumping lemma conditions.

  • "Both and must be nonempty." Only their combined length is required. One of them can be empty.

  • "The CYK algorithm produces a parse tree." The basic CYK algorithm only decides membership. It can be extended to recover a parse tree by storing back-pointers, but this is an additional step.

Key theorem with proof Intermediate+

Theorem. The following languages are not context-free:

(a) . (b) . (c) .

Proof of (a). Given pumping length , choose . Since , the window cannot span all three letter types. If is within the s and s, pumping up increases s or s but not s; if within the s and s, pumping up increases s or s but not s. In both cases the pumped string has unequal counts.

Proof of (b). Choose . The window fits within at most two of the four blocks. Pumping changes the counts within those blocks without matching the change in the corresponding position of the would-be repeated half. The resulting string is not of the form .

Proof of (c). Choose . Pumping up gives . Since , we have . For : , so is not a perfect square.

Bridge. The CFL pumping lemma builds toward the Chomsky hierarchy where each level (regular, context-free, context-sensitive, recursively enumerable) is separated by pumping arguments of increasing sophistication. This is exactly the structural pattern established by the regular pumping lemma in 47.01.04, now generalised from one pumpable region to two. The lemma appears again in 47.02.01 where the P-vs-NP question asks whether polynomial-time computation can solve all of NP, an analogous containment question to whether context-free grammars generate all of . The central insight is that parse-tree structure — specifically the branching factor of grammar rules — constrains the kinds of patterns a language can exhibit, and this is exactly what makes the CFL pumping lemma a strictly stronger tool than its regular counterpart. Putting these together with the CYK algorithm gives a complete picture: the pumping lemma provides negative evidence (what CFLs cannot do) while CYK provides positive evidence (efficient membership testing for what CFLs can do), and this duality between lower bounds and algorithms is the foundational reason theoretical computer science studies both simultaneously.

Exercises Intermediate+

Lean formalization Intermediate+

Mathlib has Computability.ContextFreeGrammar with basic grammar definitions but lacks CNF conversion, the CFL pumping lemma, and the CYK algorithm. The foundational gap is the absence of a verified CNF conversion procedure, a ContextFree.pumping_lemma theorem, and a ContextFree.cyk_membership decision procedure with a correctness proof. The CYK algorithm would be a natural target for formalisation because its dynamic-programming structure maps well to induction on span length. This unit ships without a lean_module.

Advanced results Master

Three results sharpen the CFL pumping lemma and its algorithmic consequences.

Theorem 1 (Ogden's lemma). Let be context-free. Then there exists such that for any string with and any marking of at least positions of as "distinguished," with (1) contains at least one distinguished position, (2) contains at most distinguished positions, and (3) for all [Hopcroft, Motwani & Ullman §7.4].

Ogden's lemma is strictly stronger than the CFL pumping lemma. It can prove languages non-context-free that the ordinary pumping lemma cannot handle. For example, is not context-free, but the ordinary pumping lemma fails to prove it because the case gives too many strings that pump without issue. Ogden's lemma resolves this by marking the , , and positions as distinguished.

Theorem 2 (CYK optimality). The CYK algorithm runs in time and space. For general CFGs, this is optimal under the strong exponential time hypothesis (SETH), though faster algorithms exist for restricted grammar classes.

The Valiant algorithm (1975) reduces CYK-style parsing to Boolean matrix multiplication, achieving time using the current best matrix multiplication algorithms. However, the practical constant factors make CYK preferable for moderate-length inputs.

Theorem 3 (Parikh's theorem). Every context-free language over a unary alphabet is regular. More generally, the Parikh image of any CFL (the set of letter-count vectors) is a semilinear set [Sipser §2.3].

Parikh's theorem implies that the CFL pumping lemma can never prove non-context-freeness of a unary language, because all unary CFLs are regular and hence satisfy the regular pumping lemma. The theorem connects the algebraic structure of CFLs to Presburger arithmetic.

Synthesis. The CFL pumping lemma is the foundational reason that context-free languages form a proper superset of regular languages while remaining strictly below context-sensitive languages: the central insight is that the binary branching of parse trees allows two simultaneous pumping regions, and this is exactly what enables CFLs to match paired structures like while still failing on triple structures like . This builds toward the Chomsky hierarchy where each level is separated by increasingly sophisticated pumping arguments. The CYK algorithm provides the algorithmic dual: it exploits the same binary branching structure for efficient membership testing. The lemma appears again in 47.02.01 where NP-completeness plays an analogous role to non-context-freeness — proving that certain problems lie outside a complexity class. The generalises from the regular pumping lemma of 47.01.04 by replacing the state-sequence pigeonhole argument with a parse-tree pigeonhole argument, and putting these together gives the full picture of how structural properties of grammars constrain the languages they generate.

Full proof set Master

Proposition 1 (CFL pumping lemma). If is a CFL, then the pumping lemma conditions hold.

Proof. Let be a CFG in CNF generating with . Set . For with , consider a parse tree for . The yield of a subtree of height has length at most (each internal node has exactly 2 children in CNF). Since , the height is at least , so the longest root-to-leaf path has at least internal nodes with nonterminal labels. By pigeonhole, some appears at least twice.

Let the upper occurrence of be at node and the lower at . The subtree at has yield and the subtree at has yield (or , depending on position). Since is a proper descendant of and has no unit productions in CNF, . The path from to uses at most nonterminals, so the subtree at has height at most , giving .

For any , the derivation (from 's expansion to ) can be iterated times to get . This produces a valid derivation of .

Proposition 2 (CYK correctness). After running CYK on input with CNF grammar , if and only if .

Proof. By induction on span length . Base : iff iff . Inductive step: iff with , , . By induction, and . So . Conversely, if with , the first production step must be (CNF), and the yield splits at some with and . By induction, and , so .

Connections Master

  • 47.01.04 — The regular pumping lemma is the structural precursor: one pumpable region from DFA state repetition versus two from parse-tree nonterminal repetition.

  • 47.01.05 — Context-free grammars and pushdown automata define the CFL class; the pumping lemma and CYK algorithm provide the analytical and algorithmic tools for working with this class.

  • 47.02.01 — The P-vs-NP question mirrors the regular-vs-CFL containment question: both ask whether a richer computational model solves problems that a weaker one cannot.

  • 47.02.05 — Space complexity provides another hierarchy where pumping-style arguments (via the space hierarchy theorem) prove strict separations between complexity classes.

  • 40.04.01 — Graph-theoretic parsing methods (such as parse-forest intersection and shared parse forests) use the same dynamic-programming structure as CYK.

Historical & philosophical context Master

The context-free pumping lemma was proved by Bar-Hillel, Perles, and Shamir in 1961 as part of their systematic study of phrase-structure grammars. Their paper "On Formal Properties of Simple Phrase Structure Grammars" established both the pumping lemma and several closure properties of CFLs [Sipser §2.3].

The CYK algorithm is named after Cocke, Younger, and Kasami, who discovered it independently. Younger (1967) proved that context-free language recognition can be done in time using dynamic programming. Kasami described a similar algorithm in an internal technical report (1965). The connection to Chomsky normal form as the enabling preprocessing step was made explicit by Hopcroft and Ullman (1969).

Ogden's lemma (1968) strengthened the pumping lemma by introducing the concept of "distinguished positions," resolving several open questions about the boundaries of the CFL class. William Ogden's paper "A Helpful Result for Proving inherent Ambiguity" showed that the language is inherently ambiguous (every grammar for it is ambiguous), using his strengthened lemma as a key tool.

Parikh's theorem (1966) revealed a deep algebraic property of CFLs: their commutative image is always semilinear. This connected formal language theory to the theory of Presburger arithmetic and showed that the "counting" power of CFLs is fundamentally limited.

Bibliography Master

@book{sipser2013,
  author    = {Sipser, Michael},
  title     = {Introduction to the Theory of Computation},
  edition   = {3rd},
  publisher = {Cengage Learning},
  year      = {2013},
}
@book{hopcroft2006,
  author    = {Hopcroft, John E. and Motwani, Rajeev and Ullman, Jeffrey D.},
  title     = {Introduction to Automata Theory, Languages, and Computation},
  edition   = {3rd},
  publisher = {Pearson},
  year      = {2006},
}
@article{barhillel1961formal,
  author  = {Bar-Hillel, Yehoshua and Perles, Micha and Shamir, Eli},
  title   = {On Formal Properties of Simple Phrase Structure Grammars},
  journal = {Zeitschrift f\"ur Phonetik, Sprachwissenschaft und Kommunikationsforschung},
  volume  = {14},
  pages   = {143--172},
  year    = {1961},
}
@article{ogden1968helpful,
  author  = {Ogden, William},
  title   = {A Helpful Result for Proving Inherent Ambiguity},
  journal = {Mathematical Systems Theory},
  volume  = {2},
  number  = {3},
  pages   = {191--194},
  year    = {1968},
}
@article{youngers1967recognition,
  author  = {Youngers, Daniel H.},
  title   = {Recognition and Parsing of Context-Free Languages in Time $n^3$},
  journal = {Information and Control},
  volume  = {10},
  number  = {2},
  pages   = {189--208},
  year    = {1967},
}