Regular & Context-Free Languages: Closure and the Pumping Lemma
The pumping lemma is the standard tool for proving a language is not in a class, and it is the single most misapplied result in the subject.
The misapplication almost always comes from the quantifiers. The lemma is a nested statement with alternating "for all" and "there exists", and swapping any two of them turns a valid proof into nonsense.
The reliable way to keep them straight is to read the lemma as a game against an adversary. The adversary makes the choices bound by "there exists" in the lemma, and you make the choices bound by "for all". You win — proving the language is not regular — if you can defeat every choice the adversary is permitted to make.
The second organising fact is that the lemma is a one-way tool. It states a property every regular language has, so violating it proves non-regularity. Satisfying it proves nothing at all, because non-regular languages can satisfy it too.
That asymmetry is why Myhill-Nerode is the stronger instrument for regular languages, and why the pumping lemma survives mainly because the context-free case has no equally convenient alternative.
1. The Regular Pumping Lemma
For every regular language there exists a pumping length such that every string with can be written as satisfying three conditions:
First, . The pumped part occurs within the first symbols.
Second, . The pumped part is non-empty.
Third, for every , including , which deletes entirely.
Where the lemma comes from is worth knowing, because it makes the conditions obvious rather than arbitrary.
A DFA for has some finite number of states, say . Reading a string of length at least visits at least states, so by the pigeonhole principle some state repeats within the first symbols.
The segment between the two visits to that state is a loop. Traversing it zero times, once, or many times all end in the same state, so all the resulting strings are accepted. That loop is , and it lies within the first symbols because the repetition occurs there — which is exactly condition one.
2. The Game
Reading the lemma as a game fixes the quantifiers permanently.
| Step | Who chooses | What |
|---|---|---|
| 1 | Adversary | The pumping length |
| 2 | You | A string with |
| 3 | Adversary | The decomposition satisfying the constraints |
| 4 | You | The pumping count |
You win if , and you must win against every legal decomposition the adversary might choose.
The strategy follows from the structure. Choose so that the constraint forces into a region you can exploit. Since must lie within the first symbols, a string beginning with copies of one symbol guarantees consists only of that symbol.
Then choose to break the language's defining property, most often to add symbols or to remove them.
The commonest error is choosing after seeing the decomposition, or assuming a particular decomposition. The adversary chooses it, so the argument must handle all of them at once.
3. Using It Correctly
Consider .
The adversary gives . Choose , which is in and has length .
The adversary must decompose with . Since the first symbols of are all s, both and consist entirely of s, and means has at least one .
Choose . Then has more s than the original but exactly the same number of s, so the counts no longer match and the string is not in .
Every legal decomposition is defeated, because the constraint left the adversary no choice about which symbols could contain.
Notice what the choice of accomplished. Had we chosen , the adversary could pick , and pumping would give — which is not in either, but the reasoning becomes fiddlier. A well-chosen removes the adversary's freedom.
4. The Context-Free Pumping Lemma
For every context-free language there exists a pumping length such that every with can be written as satisfying:
First, . The pumped region is confined to a window of length .
Second, . At least one of the two pumped pieces is non-empty.
Third, for every . Both pieces are pumped the same number of times.
Two pieces are pumped rather than one, and the reason comes from the proof, which uses a parse tree rather than a state sequence.
In a grammar in Chomsky normal form, a long enough string forces a path in the parse tree that repeats some variable . The subtree rooted at the upper derives , and the subtree at the lower derives .
Replacing the lower subtree with a copy of the upper one duplicates both and together, because they sit on opposite sides of the inner subtree. That structural fact is why the two pieces must be pumped in lockstep.
The window constraint is the analogue of , arising from bounding how far apart the two occurrences of can be.
5. Applying the Context-Free Version
The context-free lemma is harder to use because the adversary has more freedom: five pieces instead of three, and the pumped region can sit anywhere within a window rather than at the start.
Consider .
Choose .
The adversary picks with . Because the window has length at most and each symbol block has length exactly , the window cannot span all three blocks. It touches at most two adjacent blocks.
Choose . Pumping increases the counts of the symbols inside the window and leaves the third block untouched.
Whichever two blocks the window covers, the third is left behind, so the three counts can no longer be equal and the string leaves .
The case analysis is what makes context-free proofs longer, but it is usually short: enumerate which blocks the window can touch and show each case fails.
6. What the Lemma Cannot Do
The lemma states a necessary condition, not a sufficient one.
Every regular language satisfies it, so a language violating it is not regular. But a non-regular language can satisfy it, so satisfying it proves nothing.
There exist non-regular languages that pump perfectly, and one is . Any string with no s can be pumped in the region trivially, and any string with at least one can be pumped on that to stay in the language via the first clause.
The language is not regular, but the pumping lemma cannot show it. Myhill-Nerode can, which is why it is the stronger tool.
The practical consequence for exams is precise. A question asking you to prove a language is not regular expects the pumping lemma or Myhill-Nerode. A question asking whether a language is regular cannot be answered by pumping, and an option claiming "it satisfies the pumping lemma, therefore it is regular" is always wrong.
7. The Chomsky Hierarchy
The four language classes nest strictly, each with its own grammar restriction and machine model.
| Type | Class | Grammar restriction | Machine |
|---|---|---|---|
| 3 | Regular | or | Finite automaton |
| 2 | Context-free | Pushdown automaton | |
| 1 | Context-sensitive | Linear bounded automaton | |
| 0 | Recursively enumerable | Any production | Turing machine |
Each containment is strict. The language separates types 3 and 2. The language separates types 2 and 1. The halting problem's language separates decidable from recursively enumerable.
Context-sensitive grammars are non-contracting: the right-hand side is never shorter than the left. That single restriction is what bounds the tape and gives the linear bounded automaton.
The closure properties across the hierarchy are examined as a table.
| Operation | Regular | CFL | CSL | Recursive | RE |
|---|---|---|---|---|---|
| Union | Yes | Yes | Yes | Yes | Yes |
| Intersection | Yes | No | Yes | Yes | Yes |
| Complement | Yes | No | Yes | Yes | No |
| Concatenation | Yes | Yes | Yes | Yes | Yes |
| Kleene star | Yes | Yes | Yes | Yes | Yes |
The two gaps are the examinable content. Context-free languages fail intersection and complement; recursively enumerable languages fail complement.
Recursively enumerable languages fail complement for a specific reason. A language and its complement being both recursively enumerable would make the language decidable, by running both semi-deciders in parallel until one halts. Since undecidable recursively enumerable languages exist, the class cannot be closed under complement.
8. Worked Examples
Example 1. Prove that is not regular.
The adversary gives .
Choose , which is in with , and has length .
The adversary decomposes with and . Since the first symbols are all s, consists of copies of for some .
Choose , deleting .
The result is .
For this to be in it would have to split into two equal halves. Its length is .
If is odd the length is odd, so no split into two equal halves exists at all.
If is even, the first half ends in an while the second half ends in a , since the only two s sit at position and at the very end. The halves therefore differ.
Either way the string is not in , so the language is not regular.
Note the choice of . A simpler candidate like fails, because deleting s from it can still leave a string of the form . Including the s pins the structure down.
Example 2. Prove that is not regular.
The adversary gives .
Choose , whose length is .
The adversary picks with , since confines it.
Choose . The pumped string is .
For this to be in , the exponent must be a perfect square. The next square after is .
Since , we have .
The pumped length falls strictly between two consecutive squares, so it is not a square and the string is not in .
The language is therefore not regular. The argument works because squares grow further apart as they grow larger, while the pumping lemma can only add a bounded amount.
Example 3. Prove that is not context-free.
The adversary gives .
Choose , whose length is .
The adversary decomposes with and .
The window has length at most , and each symbol block has length exactly , so the window cannot reach from the block into the block. It touches at most two adjacent blocks.
Three cases arise.
Case 1: the window lies within one block. Pumping with increases that symbol's count and leaves the other two unchanged, so the counts differ.
Case 2: the window spans the and blocks. Pumping increases the count, the count, or both, but the count is untouched, so equality fails.
Case 3: the window spans the and blocks. By the same argument the count is untouched.
In every case , so the language is not context-free.
Note that this language is context-sensitive, sitting one level up in the hierarchy, which is exactly what makes it the standard separator between types 2 and 1.
Example 4. Explain why the language satisfies the pumping lemma despite not being regular.
Take the pumping length and consider any string with .
Case A: begins with an . Then , so the language membership must come from the second clause, meaning . Decompose with , , and the rest.
Pumping gives for various . Every such string still has , so it remains in regardless of how many s there are.
Case B: begins with a or a . Then , and the first clause holds. Decompose with as the first symbol.
Pumping changes the or count, but remains 0, so the first clause still holds and the string stays in .
Every string pumps successfully, so the language satisfies the lemma.
Yet the language is not regular. Myhill-Nerode shows this: the strings are pairwise distinguishable, since appending accepts exactly the one with matching s.
This is the concrete demonstration that the pumping lemma is necessary but not sufficient, and why an exam option asserting regularity from pumping success is always wrong.
Example 5. Prove that the recursively enumerable languages are not closed under complement.
Suppose, for contradiction, that they were.
Take any recursively enumerable language . By assumption its complement is also recursively enumerable, so there are Turing machines semi-deciding and semi-deciding its complement.
Semi-deciding means halting and accepting on members, and possibly running forever on non-members.
Now build a machine that runs and in parallel on the input, interleaving their steps.
Every string is in exactly one of or its complement, so exactly one of the two machines will eventually halt and accept.
When halts, output "yes". When halts, output "no". The combined machine always halts, so it decides .
That would make every recursively enumerable language decidable.
But undecidable recursively enumerable languages exist — the halting problem's language is the standard example, being semi-decidable by simulation and undecidable by diagonalisation.
The contradiction shows the assumption is false, so the class is not closed under complement.
The same argument gives the useful positive result: if a language and its complement are both recursively enumerable, the language is decidable. That equivalence is examined directly.
Example 6. Where does each of these sit in the Chomsky hierarchy: , , , and the set of strings encoding halting Turing machines?
is context-free but not regular. A grammar with generates it, and the pumping lemma or Myhill-Nerode rules out regularity.
is context-sensitive but not context-free. The context-free pumping lemma rules out type 2, as shown above. A linear bounded automaton can verify the three counts by making multiple passes over the tape, so it is type 1.
is context-sensitive but not context-free. This is more surprising than it looks, since — the palindromes — is context-free. The reversal is what a stack can check; matching a copy in the same order is not, because the stack would deliver the first half backwards.
The halting language is recursively enumerable but not recursive. A universal machine can simulate the encoded machine and halt if it does, which semi-decides it. Diagonalisation shows no machine can decide it, so it sits strictly inside type 0 and outside the recursive languages.
The pattern worth extracting is that each level is separated by a specific structural demand: one nesting for type 2, more than one comparison for type 1, and unbounded search for type 0.
Summary
The pumping lemma is a game: the adversary chooses the pumping length and the decomposition, you choose the string and the pumping count, and you win by defeating every legal decomposition.
For regular languages, with , , and for all . It comes from a repeated state in a DFA, which is why the loop lies within the first symbols.
Choose so that the window constraint forces into a single symbol block, then pump to break the counting property.
For context-free languages, with , , and both and pumped together. Two pieces appear because a repeated variable in a parse tree duplicates the material on both sides of the inner subtree.
The window cannot span three blocks of length each, which is what makes the three-symbol language provably non-context-free by case analysis.
The lemma is necessary and not sufficient. Non-regular languages exist that pump perfectly, so satisfying the lemma proves nothing, and Myhill-Nerode is the stronger tool for regular languages.
The Chomsky hierarchy nests strictly: regular, context-free, context-sensitive, recursively enumerable, with separating languages at each boundary and a machine model for each level.
Context-free languages fail intersection and complement; recursively enumerable languages fail complement, because a language and its complement both being semi-decidable would make it decidable.
