Skip to content
GATEiit

GATE CSE · Theory of Computation

Regular and context-free languages; pumping lemma

Telling regular, context-free and non-context-free languages apart: the Chomsky hierarchy, closure properties of regular, deterministic CF and CF languages, and the pumping lemmas used to prove a language is not regular or not context-free.

Before you start

This topic comes before: Turing machines and undecidability

Concept notes

1. Classifying a language: how much memory does it need?

Ask how much the machine must remember while reading the string left to right.

  • Bounded memory (a fixed number of facts, such as a count modulo k, or the last few symbols) → regular.
  • One unbounded counter or one stack, used last-in first-out (match a's with later b's, nested or mirrored structure) → context-free.
  • Two or more independent unbounded comparisons, or copying (ww) → usually not context-free.
Language Class
a∗b∗, {w:#a(w) mod 3=0}, any finite language regular
{anbn}, {w:#a(w)=#b(w)}, {ambn:m≠n} DCFL, not regular
{wwR}, {aibjck:i=j or j=k} CFL, not DCFL
{anbncn}, {ww}, {an2}, {ap:p prime} context-sensitive, not CFL

Two quick rules:

  1. Every finite language is regular, so {anbn:n≤1000} is regular.
  2. Every context-free language over a one-letter alphabet is regular. So a unary language is either regular or not context-free at all: {a2n} is regular, {an2} is not CFL.

Watch for disguised regular languages. {ambn:m+n even} only needs parity: regular. {ambn:m≥n} needs a count: CFL, not regular. {w:#a(w)=#b(w)} is not regular, but {w:#a(w)≡#b(w)(mod2)} is.

Recursively enumerable (Turing machine)e.g. halting problemRecursive (TM that always halts)Context-sensitive (LBA)e.g. aⁿbⁿcⁿ, wwContext-free (NPDA)e.g. wwᴿDeterministic CF (DPDA)e.g. aⁿbⁿ, wcwᴿRegular (DFA)e.g. a*b*
The Chomsky hierarchy: each class is a proper subset of the one around it

2. Closure properties

A class is closed under an operation if applying it to languages of the class always gives a language of the class.

Operation Regular DCFL CFL
Union Yes No Yes
Intersection Yes No No
Intersection with a regular language Yes Yes Yes
Complement Yes Yes No
Concatenation Yes No Yes
Kleene star Yes No Yes
Reversal Yes No Yes
Homomorphism Yes No Yes
Inverse homomorphism Yes Yes Yes

The standard counterexample for CFL intersection: L1={anbncm} and L2={ambncn} are both CFLs, but L1∩L2={anbncn} is not. Since L1∩L2=L1‾∪L2‾‾ and CFLs are closed under union, they cannot be closed under complement either.

Using closure to classify.

  • CFL − regular =L∩R‾ is a CFL. Regular − CFL need not be a CFL.
  • To show L is not regular, intersect with a regular language to reach a known non-regular one. {w:#a(w)=#b(w)}∩a∗b∗={anbn}, so the first language is not regular.

Closure is for one application or finitely many. A finite union of regular languages is regular, but an infinite union need not be: ⋃n{anbn} is a union of finite (regular) languages and is not regular. Also, "L1∪L2 is regular" does not mean L1 and L2 are regular.

3. Pumping lemma for regular languages

Lemma. If L is regular, there is a constant p≥1 (the pumping length) such that every w∈L with ∣w∣≥p can be split as w=xyz with

  1. ∣xy∣≤p,
  2. ∣y∣≥1,
  3. xyiz∈L for every i≥0.

Why it holds. A DFA with p states reading p symbols visits p+1 states, so some state repeats within the first p symbols. The loop between the repeats is y, and it can be taken any number of times. So p can be taken as the number of states of any DFA for L.

Using it (proof by contradiction) as a game.

  1. Assume L is regular with pumping length p.
  2. You choose a string w∈L with ∣w∣≥p, chosen to make the split easy to analyse.
  3. The opponent chooses any split satisfying 1 and 2. You must handle every such split.
  4. You choose i (often 0 or 2) so that xyiz∉L.

For {anbn} take w=apbp. Since ∣xy∣≤p, y=ak with k≥1. Then xy2z=ap+kbp∉L.

The lemma is a necessary condition only. Every regular language satisfies it, but some non-regular languages also satisfy it. So the lemma can prove a language is not regular; it can never prove a language is regular.

The minimum pumping length of a regular language is the smallest p for which the conditions hold. It is at most the number of states of the minimal DFA, and can be smaller.

4. Pumping lemma for context-free languages

Lemma. If L is context-free, there is a constant p such that every z∈L with ∣z∣≥p can be split as z=uvwxy with

  1. ∣vwx∣≤p,
  2. ∣vx∣≥1 (at least one of v,x is non-empty),
  3. uviwxiy∈L for every i≥0.

Why it holds. In a CNF parse tree of a long enough string, some root-to-leaf path repeats a variable A. The upper A derives vAx, so this part can be repeated or removed. Two pieces, v and x, are pumped together, which is why a single stack can match two counts but not three.

Example: L={anbncn} is not context-free. Take z=apbpcp. Because ∣vwx∣≤p, the window vwx cannot contain both an a and a c (there are p b's between them). So pumping changes at most two of the three counts. With i=2, at least one count grows and at least one stays the same, so uv2wx2y∉L. (If v or x mixes two letters, pumping also breaks the order a∗b∗c∗.)

When to use which tool.

Goal Tool
Not regular regular pumping lemma, or closure (intersect with a regular set)
Not CFL CFL pumping lemma, or closure (intersect with a regular set to reach anbncn)
Is regular / is CFL build a DFA/regex or a CFG/PDA; pumping cannot prove this

As with the regular version, the CFL lemma is necessary but not sufficient: some non-CFLs satisfy it.

Formulas to remember

Regular pumping conditions
w=xyz,  ∣xy∣≤p,  ∣y∣≥1,  xyiz∈L  ∀i≥0
CFL pumping conditions
z=uvwxy,  ∣vwx∣≤p,  ∣vx∣≥1,  uviwxiy∈L  ∀i≥0
Pumping length from a DFANumber of states of the minimal DFA works as a pumping length
p≤∣Qmin⁡∣
Intersection via complementWhy CFLs cannot be closed under complement
L1∩L2=L1‾∪L2‾‾

Common mistakes

  • Using the pumping lemma to claim a language is regular or context-free; it can only prove that a language is not.
  • Choosing the split yourself in a pumping proof; the opponent chooses the split, so your argument must work for every legal split.
  • Assuming CFLs are closed under intersection or complement; only intersection with a regular language is safe.
  • Treating an infinite union of regular languages as regular; closure holds only for finitely many operations.
  • Missing that a finite language is always regular, even when it looks like aⁿbⁿ with a bound on n.
  • Calling a unary language context-free but not regular; for one-letter alphabets the two classes coincide.

Worked examples

Try each step yourself, then reveal the working.

Example 1: Proving a language is not regular

conceptual

Show that L={anb2n:n≥0} is not regular using the pumping lemma.

  1. Step 1: Pick a string that pins the split.

    Try it on paper first.

Example 2: Intersection of two CFLs

conceptual

Let L1={anbncm:n,m≥0} and L2={ambncn:n,m≥0}. Classify L1, L2, L1∪L2 and L1∩L2.

  1. Step 1: Classify $L_1$ and $L_2$.

    Try it on paper first.

Example 3: Minimum pumping length

numerical

Find the minimum pumping length of L=ab∗ (an a followed by any number of b's).

  1. Step 1: Test $p = 1$.

    Try it on paper first.

Example 4: Unary languages

conceptual

Classify L1={a3n+1:n≥0} and L2={an2:n≥0}.

  1. Step 1: $L_1$: find a regular expression.

    Try it on paper first.

Previous year questions

25 official GATE CSE questions on this topic, newest first.

Practise these 25 PYQs →

Topic test

10 questions: up to 4 official PYQs and original practice questions, with GATE marking (MCQ wrong answers lose marks). Score 60% or more to pass. A new set is drawn each time.

Mark as mastered

Loading your progress…

My revision notes

Notes written by the GATEiit team against the official GATE CSE syllabus; being checked by a reviewer. Updated 2026-11-21. Spotted a mistake? Use Report an issue on any question, or contact us.