Hello! Welcome to our next lesson on the Theory of Computation.
In our previous lessons, we've built a clear hierarchy of language classes: Regular Languages are a subset of Deterministic Context-Free Languages (DCFLs), which are themselves a subset of the broader class of Context-Free Languages (CFLs). We've seen that nondeterminism gives Pushdown Automata more power than their deterministic counterparts.
This raises a crucial question: Are there limits to the power of PDAs and Context-Free Grammars? Just as we used the Pumping Lemma for Regular Languages to prove that a language like is not regular, we need a similar tool to identify the boundaries of context-free languages.
Today's lesson introduces that tool. Our learning outcome is to state the Pumping Lemma for Context-Free Languages (CFLs) and explain its structure. We will focus on understanding what the lemma says and, more importantly, why its structure is a direct consequence of the way context-free grammars generate strings.
1. From Loops in Automata to Recursion in Grammars
Recall the Pumping Lemma for Regular Languages. Its logic was rooted in the finite nature of a DFA. A sufficiently long string forces the DFA to revisit a state, creating a "loop" in its state diagram. This loop corresponds to a substring (y in xyz) that can be repeated or removed.
However, this logic won't work for CFLs. A PDA can handle languages like precisely because its stack provides unbounded memory, overcoming the finite-state limitation. The "memory" of a CFL isn't in states, but in the recursive structure of its grammar. Therefore, the Pumping Lemma for CFLs is based not on loops in an automaton, but on repetition within a parse tree.
2. The Core Idea: Tall Parse Trees and Repeated Variables
The proof of the Pumping Lemma for CFLs relies on a key insight from grammars in Chomsky Normal Form (CNF). As you'll remember, any CFL can be generated by a CFG in CNF, where production rules are of the form A → BC or A → a. This results in parse trees that are essentially binary trees.
Here's the chain of logic:
- Let's say our grammar has
kvariables. - If we generate a very long string, its parse tree must be very "tall".
- If the tree is tall enough (specifically, if the longest path from the root to a leaf has more than
kvariables), the Pigeonhole Principle dictates that at least one variable must appear more than once on that path. Let's call this repeated variableA.
This repetition means the grammar has a recursive way of defining A. One instance of A derives another instance of A further down the tree. This structure is what we will exploit for "pumping".
The following image provides a visual schematic of this idea.

3. Decomposing the String and "Pumping"
The repeated variable A allows us to break the string w into five pieces, which we'll call u, v, x, y, z.
w = uvxyz
As the diagram above shows:
- The top-level derivation generates
u, the upperA, andz. So,S ⇒* uAz. - The upper
Arecursively generatesv, the lowerA, andy. So,A ⇒* vAy. - The lower
Aeventually generates the terminal stringx. So,A ⇒* x.
Because of the recursive rule A ⇒* vAy, we can "pump" it.
- Pumping up (i > 1): We can apply the rule
A ⇒* vAymultiple times before finally applyingA ⇒* x. This generates strings of the formuv^2xy^2z,uv^3xy^3z, and so on. - Pumping down (i = 0): We can skip the
A ⇒* vAyrule entirely. In the derivationS ⇒* uAz, we can directly substitute the derivation for the smaller part,A ⇒* x, yielding the stringuxz.
This logic is the heart of the lemma. The following video provides a detailed walkthrough of this derivation.
Pumping Lemma for Context-Free Languages, Statement and FULL PROOF
This video from Easy Theory explains the origin of the Pumping Lemma for CFLs, starting from the idea of a tall parse tree in CNF and deriving the five-part string decomposition.
Watch the specified clips to understand the lemma's structure: 00:00 - 03:51: Focus on how a long string forces a tall parse tree, leading to a repeated variable. 09:42 - 11:38: Observe how this repeated variable leads to the uvxyz decomposition. 11:38 - 15:55: Understand how the recursive nature (A ⇒* vAy) allows for the pumping of v and y simultaneously.
4. The Formal Statement of the Pumping Lemma
Now that we have the intuition, we can state the lemma formally. It establishes a property that all context-free languages must satisfy.
Theorem (Pumping Lemma for Context-Free Languages):
If L is a context-free language, then there exists a number p (the pumping length) such that for any string w in L with |w| ≥ p, w can be divided into five substrings, w = uvxyz, satisfying the following three conditions:
-
For all
i ≥ 0, the stringuv^i x y^i zis inL.
This is the "pumping" property itself. It states that we can repeat thevandysections any number of times (including zero), and the resulting string will still be in the language. -
|vy| > 0(or equivalently,|v| + |y| > 0).
This means that the parts being pumped are not both empty. If bothvandywere the empty stringε, the pumping action would do nothing, making the lemma trivial and useless. This condition ensures we are pumping a non-empty substring. -
|vxy| ≤ p.
This condition constrains the location of the pumpable parts. It says that the substringvxy(the part derived from the upperAin our parse tree) is not arbitrarily long; its length is at most the pumping lengthp. This meansv,x, andymust be relatively close to each other within the stringw.
Lecture 11 - Proving that languages are not context-free
The following document provides a concise, formal statement of the lemma and an excellent comparison with its regular language counterpart. This will help solidify your understanding of the formal structure.
Read Section 11.1, "The pumping lemma for context-free languages". Pay close attention to the formal theorem statement and the author's comparison of the regular and context-free pumping lemmas, particularly the differences in decomposition (xyz vs. uvxyz) and constraints (|xy| ≤ n vs. |uyv| ≤ n). Note: This resource uses a slightly different variable naming (w = xuyvz), but the underlying conditions are identical.
5. Why These Conditions? A Deeper Look
Let's briefly revisit why conditions 2 and 3 must hold, as this deepens the understanding of the lemma's structure.
-
Why is
|vy| > 0? (Condition 2)
This follows from the properties of Chomsky Normal Form. The proof relies on a grammar in CNF whereε-productions are eliminated (except possibly forS → ε). When the upperAderivesvAy,A ⇒* vAy, the productions used cannot be only unit productions orε-productions. This forces at least one ofvoryto contain terminals. If both were empty, the two occurrences ofAin the parse tree would be the same node, which contradicts our premise of finding a repeated variable along a path. -
Why is
|vxy| ≤ p? (Condition 3)
The pumping lengthpis related to the number of variableskin the grammar (e.g.,p = 2^k). The proof of the lemma is constructed by choosing the lowest pair of repeated variables on the longest path in the parse tree. By doing this, we can guarantee that the subtree rooted at the upperAhas a height no greater thank+1. A binary tree of a bounded height can only generate a string of a bounded length. This ensures that the yield of that subtree,vxy, has a length at mostp. This condition is crucial because it limits the number of ways a string can be split when we later use the lemma.
You can review these points in the following optional video segments.
Pumping Lemma for Context-Free Languages, Statement and FULL PROOF
These segments from the Easy Theory video provide the formal justification for conditions 2 and 3.
This is optional, but recommended for a deeper understanding. 16:35 - 21:28: Explains why v and y cannot both be empty (|vy| > 0). 21:28 - 25:55: Explains the reasoning behind the length constraint |vxy| ≤ p.
Test your understanding!
In the Pumping Lemma for CFLs, why is it that we pump two substrings (v and y) simultaneously, whereas in the Pumping Lemma for Regular Languages, we only pumped a single substring?
Show answer
The difference arises from the underlying model of computation. The regular lemma is based on a DFA, which has a linear, sequential structure. A loop in a DFA corresponds to a single, contiguous substring (y).
The CFL lemma is based on a CFG (in CNF), where rules are typically of the form A → BC. This creates a branching, tree-like structure. The recursive step A ⇒* vAy generates content on both the "left" (v) and "right" (y) of the inner recursive variable, reflecting the two children of a node in the parse tree. Therefore, to maintain the grammatical structure, both parts must be pumped together.
Conclusion
In this lesson, we have introduced the Pumping Lemma for Context-Free Languages, a fundamental tool for exploring the limits of this language class.
Key Takeaways:
- The Pumping Lemma for CFLs provides a property that every context-free language must have.
- Its logical foundation is the Pigeonhole Principle applied to parse trees: a sufficiently long string implies a tall parse tree, which must contain a repeated variable along some path.
- This repeated variable allows any sufficiently long string
wto be decomposed into five parts,w = uvxyz. - This decomposition must satisfy three critical conditions:
- Pumping:
uv^ixy^izis in the language for alli ≥ 0. - Non-empty: At least one of the pumped parts,
vory, must be non-empty (|vy| > 0). - Length Constraint: The core pumpable segment
vxymust be no longer than the pumping lengthp(|vxy| ≤ p).
- Pumping:
Preview of the Next Lesson:
Understanding the statement of the lemma is the first step. The real power comes from using it. In our next lesson, we will learn how to apply the Pumping Lemma in a formal proof by contradiction to demonstrate that a given language is not context-free. We will finally be able to prove formally why languages like cannot be generated by any CFG.