Section 6 · 9 min read

Pumping lemma and closure properties

The pumping lemmas for regular and context-free languages, how to use them correctly, and the closure properties of every language class from regular to recursively enumerable.

On this page
  1. What the pumping lemmas are for
  2. Pumping lemma for regular languages
  3. Pumping lemma for context-free languages
  4. Closure properties
  5. Proving non-membership with closure
  6. Worked examples
  7. Common traps
  8. Quick revision

What the pumping lemmas are for

Both pumping lemmas describe a property that every language in a class has. They are used in one direction only: to show that a given language is not in the class. A language that passes the pumping test may still be outside the class, so the lemma can never prove that a language is regular or context-free.

In GATE the lemma itself is rarely asked as a proof. What is asked is the reasoning behind it ("which split is possible", "why does this choice of string work") and, much more often, closure properties, which are the fast route to the same conclusions.

Pumping lemma for regular languages

Statement. If LL is regular, there is a number p≥1p \ge 1 (the pumping length) such that every s∈Ls \in L with ∣s∣≥p|s| \ge p can be written as s=xyzs = xyz where

  1. ∣xy∣≤p|xy| \le p
  2. ∣y∣≥1|y| \ge 1
  3. xyiz∈Lxy^iz \in L for every i≥0i \ge 0.

Where it comes from. Take a DFA for LL with pp states. Reading the first pp symbols of ss visits p+1p + 1 states, so some state repeats. The part of the input read between the two visits is a loop, yy. Going round the loop any number of times, including zero, leads to the same state, so acceptance does not change.

Using it as a game

To prove LL is not regular, play against an opponent:

  1. The opponent picks pp. You must handle every pp, so keep it symbolic.
  2. You pick a string s∈Ls \in L with ∣s∣≥p|s| \ge p. Choose it so that the first pp symbols are boring, for example all the same letter.
  3. The opponent picks any split xyzxyz obeying conditions 1 and 2. You must handle every such split.
  4. You pick ii so that xyiz∉Lxy^iz \notin L.

The most common mistake is to choose the split yourself. You do not get to; you only know ∣xy∣≤p|xy| \le p and ∣y∣≥1|y| \ge 1.

Pumping down (i=0i = 0) is often the useful choice, for example when the language says "more aa's than bb's".

Pumping lemma for context-free languages

Statement. If LL is context-free, there is p≥1p \ge 1 such that every s∈Ls \in L with ∣s∣≥p|s| \ge p can be written s=uvxyzs = uvxyz where

  1. ∣vxy∣≤p|vxy| \le p
  2. ∣vy∣≥1|vy| \ge 1
  3. uvixyiz∈Luv^ixy^iz \in L for every i≥0i \ge 0.

Where it comes from. A long enough string has a tall parse tree, and on a long root-to-leaf path some variable repeats. The subtree under the upper copy can be substituted for the lower one (pump up) or the lower one for the upper (pump down). vv and yy are what the upper copy adds on the left and right.

The key consequence. The window vxyvxy has length at most pp, so if your string has blocks of length pp, the window touches at most two adjacent blocks. Pumping can change at most two adjacent counts. This is why anbncna^nb^nc^n (three counts must move together) and anbmcndma^nb^mc^nd^m (the aa and cc blocks are not adjacent) are not CFL.

Ogden's lemma

A stronger version lets you mark at least pp positions and guarantees that vv and yy together contain a marked position. It is needed for a few languages where plain pumping fails, such as some inherently ambiguous examples. GATE does not ask it directly; just know it exists.

Closure properties

This table is the single most useful thing to memorize for this section. Y means closed, N means not closed in general.

Operation Regular DCFL CFL Recursive RE
Union Y N Y Y Y
Intersection Y N N Y Y
Complement Y Y N Y N
Concatenation Y N Y Y Y
Kleene star Y N Y Y Y
Reversal Y N Y Y Y
Homomorphism Y N Y N Y
Inverse homomorphism Y Y Y Y Y
Intersection with a regular language Y Y Y Y Y
Difference L1−L2L_1 - L_2 Y N N Y N

Notes on the less obvious entries:

  • CFL, intersection: {anbncm}∩{ambncn}={anbncn}\{a^nb^nc^m\} \cap \{a^mb^nc^n\} = \{a^nb^nc^n\}.
  • CFL, complement: follows from intersection, because closure under union and complement would give intersection by De Morgan.
  • DCFL, complement: a DPDA can be made to always read its whole input, then accepting and rejecting states can be swapped.
  • DCFL, union and intersection: fail for the same reason as CFL intersection. {anbncm}\{a^nb^nc^m\} and {ambncn}\{a^mb^nc^n\} are both DCFL.
  • Recursive, homomorphism: an erasing homomorphism can hide the "witness" part of a string, turning a decidable language into an RE-complete one.
  • RE, complement: if both LL and L‾\overline{L} are RE, then LL is recursive. The halting language is RE and not recursive, so its complement is not RE.
  • Infinite union is not a closure property of any of these classes: every language is a union of singletons.
  • Subsets: none of the classes is closed under taking subsets. Σ∗\Sigma^* is regular and contains every language.

Mixed-class rules

These come up in "which is necessarily..." questions. Let RR be regular, LL a CFL, DD a DCFL:

  • L∩RL \cap R, L−RL - R, L∪RL \cup R are CFL. R−LR - L need not be.
  • D∩RD \cap R and D‾\overline{D} are DCFL.
  • RE minus recursive is RE; recursive minus RE need not be RE.

Proving non-membership with closure

The pattern: assume LL is in the class, apply operations the class is closed under, and land on a language known to be outside the class. Contradiction.

Typical moves:

  • Intersect with a regular language such as a∗b∗a^*b^* to strip away unwanted strings.
  • Complement (only for classes closed under it).
  • Apply a homomorphism to rename or delete symbols.
  • Use the fact that a unary CFL is regular.

Worked examples

Worked example 1: Pumping down

Show L={aibj:i>j}L = \{a^i b^j : i > j\} is not regular.

Let pp be the pumping length. Choose s=ap+1bp∈Ls = a^{p+1}b^p \in L. Any split with ∣xy∣≤p|xy| \le p puts yy inside the leading aa's, so y=aky = a^k with k≥1k \ge 1. Pump down (i=0i = 0): xz=ap+1−kbpxz = a^{p+1-k}b^p. Since k≥1k \ge 1, the number of aa's is at most pp, so it is no longer greater than the number of bb's. xz∉Lxz \notin L. So LL is not regular.

Pumping up would not work here: more aa's keeps the string in LL.

Worked example 2: Growing gaps

Show L={an2:n≥0}L = \{a^{n^2} : n \ge 0\} is not regular.

Choose s=ap2s = a^{p^2}. Any split has 1≤∣y∣≤p1 \le |y| \le p. Pumping once more gives length p2+∣y∣≤p2+p<p2+2p+1=(p+1)2p^2 + |y| \le p^2 + p < p^2 + 2p + 1 = (p+1)^2. Also p2+∣y∣>p2p^2 + |y| > p^2. So the length lies strictly between two consecutive squares and xy2z∉Lxy^2z \notin L.

The same idea works for any language whose lengths have unbounded gaps (powers of 2, primes, factorials). Since unary CFLs are regular, these languages are not even context-free.

Worked example 3: Closure instead of pumping

Show L={w∈{a,b}∗:na(w)=nb(w)}L = \{w \in \{a,b\}^* : n_a(w) = n_b(w)\} is not regular.

If LL were regular, then L∩a∗b∗L \cap a^*b^* would be regular (closure under intersection). But L∩a∗b∗={anbn}L \cap a^*b^* = \{a^nb^n\}, which is not regular. So LL is not regular.

Same trick for {wwR}\{ww^R\}: intersect with a∗bba∗a^*bba^* to get {anbban}\{a^nbba^n\}.

Worked example 4: CFL pumping on a copy language

Show L={ww:w∈{a,b}∗}L = \{ww : w \in \{a,b\}^*\} is not context-free.

First simplify with closure: L∩a∗b∗a∗b∗={anbmanbm}L \cap a^*b^*a^*b^* = \{a^nb^ma^nb^m\}. If LL were CFL, so would this be. Now pump s=apbpapbps = a^pb^pa^pb^p.

The window vxyvxy has length at most pp, so it lies inside one block or spans two adjacent blocks.

  • Inside one block: pumping changes one count only, so the two aa-blocks (or the two bb-blocks) become unequal.
  • Across two adjacent blocks (for example the first aa-block and the first bb-block): it changes those counts but not the matching blocks in the second half.

In every case uv2xy2zuv^2xy^2z is not of the form anbmanbma^nb^ma^nb^m. So LL is not CFL.

Worked example 5: Passing the test is not enough

Let L={aibjck:i≥1,j=k}∪{bjck:j,k≥0}L = \{a^ib^jc^k : i \ge 1, j = k\} \cup \{b^jc^k : j, k \ge 0\}.

It satisfies the regular pumping lemma with p=1p = 1:

  • If ss starts with aa, take yy = the first aa. Pumping up keeps i≥1i \ge 1 with j=kj = k. Pumping down either keeps i≥1i \ge 1 or removes the only aa, leaving bjcjb^jc^j, which is in the second part.
  • If ss has no aa, it is bjckb^jc^k; pump its first symbol and it stays in b∗c∗b^*c^*.

But LL is not regular: L∩ab∗c∗={abjcj}L \cap ab^*c^* = \{ab^jc^j\}, which is not regular. This is the standard reason the lemma is only a one-way test.

Common traps

  • Choosing the split. You choose ss and ii; the opponent chooses pp and the split.
  • Strings shorter than pp. The lemma says nothing about them; your ss must have length at least pp.
  • CFL window misuse. vxyvxy can straddle two adjacent blocks; handle that case, not only "inside one block".
  • Subset reasoning. "L⊆RL \subseteq R and RR regular, so LL is regular" is false.
  • CFL intersection. Closed with a regular language, not with another CFL.
  • DCFL complement. DCFLs are closed under complement even though CFLs are not.
  • Homomorphism and recursive languages. Recursive is the one column in the table that fails homomorphism while RE passes it.
  • L∗L^* regular does not mean LL regular: {ap:p prime}∗={ϵ}∪{an:n≥2}\{a^p : p \text{ prime}\}^* = \{\epsilon\} \cup \{a^n : n \ge 2\}.

Quick revision

  • Regular pumping: ∣xy∣≤p|xy| \le p, ∣y∣≥1|y| \ge 1, xyiz∈Lxy^iz \in L for all i≥0i \ge 0.
  • CFL pumping: ∣vxy∣≤p|vxy| \le p, ∣vy∣≥1|vy| \ge 1, uvixyiz∈Luv^ixy^iz \in L for all i≥0i \ge 0.
  • Both are necessary conditions only; they prove non-membership.
  • Pick strings whose first pp symbols are uniform; try i=0i = 0 and i=2i = 2.
  • CFL window touches at most two adjacent blocks.
  • Regular: closed under everything in the table.
  • CFL: not closed under intersection, complement, difference.
  • DCFL: closed under complement, inverse homomorphism and intersection with regular; little else.
  • Recursive: closed under all Boolean operations, not homomorphism.
  • RE: closed under union, intersection, concatenation, star, homomorphism; not complement.
  • Fastest non-regularity proof is often intersecting with a∗b∗a^*b^* to reach anbna^nb^n.

Further reading

Test this section