IAE Academy

GATE CS

Context-free languages

Closure properties and the pumping lemma

Context-free languages are closed under some operations (union, concatenation, star, reversal, homomorphism, substitution, inverse homomorphism, intersection with a regular language) and not under others (intersection, complement). The pumping lemma for CFLs is the standard tool for showing a language is not context-free. In GATE questions you use these closure facts, the decidability facts and the pumping argument to place a given language in the right class: regular, deterministic context-free, context-free or not context-free.

8 previous-year questions · 6 solved · 5 practice questions

Conventions and closure properties

Σ is the input alphabet, ε the empty string, |w| the length of w, and the complement of L with respect to . CFL means context-free language. DCFL means a language accepted by a deterministic pushdown automaton (DPDA) by final state.

For nondeterministic PDAs, acceptance by final state and acceptance by empty stack give the same class, the CFLs. For DPDAs they differ: a DPDA accepting by empty stack accepts only prefix-free languages (no string in the language is a proper prefix of another), so DCFLs are defined by acceptance by final state.

Closure means: if the operands are in the class, the result is also in the class. To show closure, build a grammar or automaton for the result. To show non-closure, give one counterexample.

Operation

CFL

DCFL

Regular

Union

Closed

Not closed

Closed

Concatenation

Closed

Not closed

Closed

Kleene star

Closed

Not closed

Closed

Reversal

Closed

Not closed

Closed

Homomorphism

Closed

Not closed

Closed

Substitution (each symbol replaced by a language of the same class)

Closed

Not closed

Closed

Inverse homomorphism

Closed

Closed

Closed

Intersection

Not closed

Not closed

Closed

Complement

Not closed

Closed

Closed

Intersection with a regular language

Closed

Closed

Closed

Union with a regular language

Closed

Closed

Closed

Difference (L in the class, R regular)

Closed

Closed

Closed

Difference (R regular, L in the class)

Not closed

Closed

Closed

Grammar constructions for CFLs. If and are the start symbols (with disjoint variables) and S is a new start symbol, then union uses , concatenation uses , star uses , and reversal reverses the right side of every production.

Why intersection and complement fail

Take and . Both are context-free (indeed deterministic context-free). Their intersection is , which is not context-free. So CFLs are not closed under intersection.

Complement fails by De Morgan. . If CFLs were closed under complement, then since they are closed under union, they would be closed under intersection, which is false. So CFLs are not closed under complement.

INTERSECTION WITH A REGULAR LANGUAGE

Proved by running a PDA and a DFA together; the same holds for DCFLs. Hence is a CFL, but need not be.

EXAM TIP

Use intersection with a regular language as a tool to show a language is not context-free. If is , then L cannot be context-free, because otherwise the intersection would be context-free.

EXAM TIP

A CFL whose complement is not a CFL exists. Over , the complement of (with respect to ) is context-free: it is the union of the strings not in and the strings with or . But its complement, , is not context-free.

Deterministic CFLs

DCFLs sit strictly between regular languages and CFLs: . is a DCFL that is not regular. is a CFL that is not a DCFL; informally, a DPDA cannot know where the middle is.

  1. DCFLs are closed under complement. This is the key difference from general CFLs.

  2. DCFLs are closed under intersection with a regular language, union with a regular language, difference with a regular language (either order) and inverse homomorphism.

  3. DCFLs are not closed under union, intersection, concatenation, Kleene star, reversal or homomorphism.

  4. Standard example of union failing: is a union of two DCFLs, is a CFL, and is not a DCFL.

  5. Every DCFL has an unambiguous grammar, so an inherently ambiguous CFL is never a DCFL.

  6. The converse fails: has the unambiguous grammar but is not a DCFL.

EXAM TIP

If a language is a DCFL then its complement is a DCFL, hence a CFL. So if you can show that is not context-free, then L is not a DCFL (it may still be a CFL if you are told so).

Decision properties

Here the language is given by a context-free grammar G. A property is decidable if some algorithm always halts with the correct yes/no answer.

Question for CFGs

Decidable?

Idea

Is ?

Yes

Mark variables that derive a terminal string; check whether the start symbol is marked

Is finite?

Yes

Remove useless symbols, ε- and unit productions; draw an edge A → B when B appears on the right of an A-production; L(G) is finite if and only if this graph has no cycle

Is ?

Yes

CYK algorithm on a Chomsky normal form grammar, time for a fixed grammar

Is (universality)?

No

Undecidable

Is (equivalence)?

No

Undecidable

Is ?

No

Undecidable

Is G ambiguous?

No

Undecidable

Is regular?

No

Undecidable

Is deterministic context-free?

No

Undecidable

Is for R regular?

Yes

Test

Is for R regular?

No

With this is universality

EXAM TIP

Universality and equivalence are decidable for regular languages and for DCFLs given by DPDAs (for universality, complement the DPDA and test emptiness; equivalence of DPDAs is decidable, a deep result). Inclusion and emptiness of stay undecidable even for DPDAs. All of these are undecidable for general CFGs. Check which class the question gives you.

The pumping lemma for CFLs

PUMPING LEMMA FOR CONTEXT-FREE LANGUAGES

A necessary condition only: it can prove a language is not context-free, never that it is. If L is generated by a Chomsky normal form grammar with k variables, works.

Compare the regular pumping lemma, where with and . For CFLs the string splits into five parts, v and x are pumped together (the same number of times), and the window is short. The usual consequence is that two positions of z that are p or more apart cannot both lie inside .

  1. Assume L is context-free and let p be its pumping length.

  2. Choose a string with , built from p, so that the argument works for every p.

  3. Consider every split with and . Do not pick the split; the opponent does.

  4. For each split (grouped into a few cases by where lies), find an i such that . Usually i is 0 or 2; pumping down () is allowed and often needed.

  5. Since every split fails, the assumption is false, and L is not context-free.

Language

Not context-free? String to choose (or reason)

Yes;

Yes;

Yes;

, ,

Yes; every CFL over a one-letter alphabet is regular, and these are not regular

, ,

No; these are context-free

EXAM TIP

The lemma gives only a necessary condition. A language that satisfies it may still fail to be context-free, so you can never use it to show a language is context-free.

Worked examples

WORKED EXAMPLE

Show that is not context-free.

Solution
  1. Assume L is context-free with pumping length p, and take , so .

  2. Let with and . The window is too short to contain both an a and a c, since the p b's lie between them.

  3. Case 1: contains an a. Then it contains no c. Pumping with gives at least a's but still c's, while membership needs the number of c's to be twice the number of a's, which is at least . So the pumped string is not in L.

  4. Case 2: contains no a. Then the number of a's stays p, so membership needs exactly p b's and c's. But contains a b or a c, so pumping with raises one of these counts. The pumped string is not in L.

  5. Every split fails, so L is not context-free.

Answer: L is not context-free.

WORKED EXAMPLE

Classify , where is the number of occurrences of x in w.

Solution
  1. Intersect L with (a regular language).

  2. A string in with equal numbers of a, b and c has the form , so .

  3. This intersection is not context-free (pumping lemma).

  4. CFLs are closed under intersection with a regular language, so if L were context-free then the intersection would be. It is not, so L is not context-free.

Answer: L is not context-free.

Classifying a language

Language

Regular

DCFL

CFL

Yes

Yes

Yes

No

Yes

Yes

No

Yes

Yes

No

No

Yes

No

No

Yes

No

No

No

No

No

No

  1. These are rules of thumb; confirm each with a construction or a proof.

  2. First ask whether a finite amount of memory suffices (regular). If it does, say so.

  3. If one counter or stack comparison matches a pattern read left to right (, a marker in the middle), it is usually a DCFL.

  4. If the machine must guess the middle or choose between two comparisons (palindromes, 'i = j or j = k'), it is a CFL and usually not a DCFL.

  5. If two comparisons must hold at once and they cross or share a block (, ), or a string must be compared with a copy of itself (), it is not context-free. Nested or side-by-side comparisons (, ) are still context-free.

  6. Confirm with closure: intersect with a regular language to reduce the question to a standard one.

EXAM TIP

is not context-free but its complement over is context-free. Do not assume that a language and its complement are in the same class unless the class is closed under complement (regular, DCFL).

PREVIOUS-YEAR QUESTIONS

8 questions from the CS papers

Try each question first, then open the answer. We placed each question under a topic ourselves; the exam paper does not label topics.

See these papers in full: GATE CSE 2026 Set 2 · GATE CSE 2025 Set 1 · GATE CSE 2025 Set 2 · GATE CSE 2022 · GATE CSE 2020 · GATE CSE 2018

GATE 2025 CS1 Q.45

GATE 2025 · CS1Q.45 · 2 marks · MCQ

Consider the following two languages over the alphabet , where and are natural numbers.


Which ONE of the following statements is CORRECT?

  1. (A)

    Both and are context-free languages.

  2. (B)

    is a context-free language but is not a context-free language.

  3. (C)

    is not a context-free language but is a context-free language.

  4. (D)

    Neither nor are context-free languages.

Official answer
See the question in the paper
GATE 2025 CS1 Q.45: official answer

GATE 2025 CS2 Q.30

GATE 2025 · CS2Q.30 · 1 mark · MSQ

Consider the two lists List I and List II given below:

For matching of items in List I with those in List II, which of the following option(s) is/are CORRECT?

  1. (A)

    (i) – (a), (ii) – (b), and (iii) – (c)

  2. (B)

    (i) – (b), (ii) – (a), and (iii) – (c)

  3. (C)

    (i) – (b), (ii) – (c), and (iii) – (a)

  4. (D)

    (i) – (a), (ii) – (c), and (iii) – (b)

Answer and solution
See the question in the paper
GATE 2025 CS2 Q.30: answer and solution

GATE 2022 CS Q.47

GATE 2022 · CSQ.47 · 2 marks · MSQ

Consider the following languages:

Note that is the reversal of the string . Which of the following is/are TRUE?

  1. (A)

    and are regular.

  2. (B)

    and are context-free.

  3. (C)

    is regular and is context-free.

  4. (D)

    and are context-free but not regular.

Answer and solution
See the question in the paper
GATE 2022 CS Q.47: answer and solution

GATE 2022 CS Q.48

GATE 2022 · CSQ.48 · 2 marks · MSQ

Consider the following languages:



Which of the following statements is/are FALSE?

  1. (A)

    is not context-free but and are deterministic context-free.

  2. (B)

    Neither nor is context-free.

  3. (C)

    , and all are context-free.

  4. (D)

    Neither nor its complement is context-free.

Answer and solution
See the question in the paper
GATE 2022 CS Q.48: answer and solution

GATE 2020 CS Q.10

GATE 2020 · CSQ.10 · 1 mark · MCQ

Consider the language and the following statements.

I. is deterministic context-free.

II. is context-free but not deterministic context-free.

III. is not LL() for any .

Which of the above statements is/are TRUE?

  1. (A)

    I only

  2. (B)

    II only

  3. (C)

    I and III only

  4. (D)

    III only

Official answer

Official answer: CSource: Official answer key, GATE 2020 CS (IIT Delhi)

See the question in the paper
GATE 2020 CS Q.10: official answer

GATE 2020 CS Q.32

GATE 2020 · CSQ.32 · 2 marks · MCQ

Consider the following languages.


Which one of the following is TRUE?

  1. (A)

    is regular and is context-free.

  2. (B)

    is context-free but not regular and is context-free.

  3. (C)

    Neither nor is context-free.

  4. (D)

    is context-free but is not context-free.

Answer and solution
See the question in the paper
GATE 2020 CS Q.32: answer and solution

GATE 2018 CS Q.35

GATE 2018 · CSQ.35 · 2 marks · MCQ

Consider the following languages:
I.
II.
III.
IV.
Which of the languages above are context-free?

  1. (A)

    I and IV only

  2. (B)

    I and II only

  3. (C)

    II and III only

  4. (D)

    II and IV only

Answer and solution
See the question in the paper
GATE 2018 CS Q.35: answer and solution

OTHER EXAMS

ISRO: 1 question

From ISRO's Scientist/Engineer written test: ISRO CS 2023. They are not counted in the GATE numbers on this page.

ISRO CS 2023 Q.59

ISRO 2023 · CSQ.59 · 1 mark · MCQ

Which of the following statements is FALSE:

  1. (A)

    The intersection of a regular language and a context-free language is context-free

  2. (B)

    The intersection of a regular language and context-free language is regular

  3. (C)

    The union of two context-free languages is context-free

  4. (D)

    The union of two regular languages is regular

Answer and solution
See the question in the paper
ISRO CS 2023 Q.59: answer and solution

PRACTICE

5 questions to test yourself

PRACTICE 11 mark · MCQ

Which of the following languages over is NOT context-free?

  1. (A)

  2. (B)

  3. (C)

  4. (D)

Answer and solution

Answer: (D)

  1. A: generated by , , so context-free.

  2. B: generated by , so context-free.

  3. C: union of and , both context-free, so context-free.

  4. D: take . The window cannot contain both an a and a c.

  5. Case 1: contains an a (so no c). Pump up with : the a count exceeds p while the c count stays p, violating .

  6. Case 2: contains a b but no a (it may also contain c's). Pump down with : the b count drops below p while the a count stays p, violating .

  7. Case 3: contains only c's. Pump down with : the c count drops below p while the b count stays p, violating .

  8. Every split fails, so D is not context-free.

PRACTICE 22 marks · MSQ

Let and . Which statements are true?

  1. (A)

    is context-free

  2. (B)

    is context-free

  3. (C)

    and are both deterministic context-free languages

  4. (D)

    is not context-free

Answer and solution

Answer: (B), (C), (D)

  1. , which is not context-free, so A is false and D is true.

  2. The union of two CFLs is a CFL, so B is true.

  3. A DPDA for pushes for a's, pops for b's, checks the bottom-of-stack marker, then reads any number of c's and accepts by final state. For it reads the a's without pushing, pushes for b's and pops for c's. Neither ever has to guess, so both are deterministic and C is true.

PRACTICE 32 marks · NAT

Let and let R be the regular language denoted by . How many strings of length exactly 6 belong to ? (Here is the number of occurrences of x in w.)

Answer and solution

Answer:

  1. A string of length 6 in L has three a's and three b's.

  2. A string in R has the form , so all its b's form one block: .

  3. Then , giving .

  4. Each choice gives a distinct string (bbbaaa, abbbaa, aabbba, aaabbb), so the count is 4.

PRACTICE 42 marks · MCQ

Let L be a context-free language and R a regular language over the same alphabet. Which of the following languages is always context-free?

  1. (A)

  2. (B)

  3. (C)

  4. (D)

Answer and solution

Answer: (C)

  1. , and is regular, so is context-free by closure under intersection with a regular language.

  2. A: CFLs are not closed under complement.

  3. B: ; taking gives , which need not be context-free.

  4. D: ; with this is , again not always context-free.

PRACTICE 51 mark · MCQ

For context-free grammars , , and a string , which of the following problems is undecidable?

  1. (A)

    Is ?

  2. (B)

    Is finite?

  3. (C)

    Is ?

  4. (D)

    Is ?

Answer and solution

Answer: (D)

  1. Emptiness is decidable by marking variables that derive terminal strings.

  2. Finiteness is decidable from the dependency graph after cleaning the grammar.

  3. Membership is decidable with the CYK algorithm.

  4. Equivalence of two context-free grammars is undecidable.

OFFICIAL GATE 2027 SYLLABUS WORDING

Copied from the GATE 2027 syllabus; see all 2027 syllabi.

GATE CS

context-free languages, pumping lemma

Preparing in Kolkata? IAE Academy's course for GATE CSE.