GATE 2026 CS2 Q.48
Let and let .
Which of the following constraints ensure(s) that the language is context-free?
- (A)
- (B)
and
- (C)
and
- (D)
GATE CS
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
Σ 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.
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.
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.
DCFLs are closed under complement. This is the key difference from general CFLs.
DCFLs are closed under intersection with a regular language, union with a regular language, difference with a regular language (either order) and inverse homomorphism.
DCFLs are not closed under union, intersection, concatenation, Kleene star, reversal or homomorphism.
Standard example of union failing: is a union of two DCFLs, is a CFL, and is not a DCFL.
Every DCFL has an unambiguous grammar, so an inherently ambiguous CFL is never a DCFL.
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).
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.
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 .
Assume L is context-free and let p be its pumping length.
Choose a string with , built from p, so that the argument works for every p.
Consider every split with and . Do not pick the split; the opponent does.
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.
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 EXAMPLE
Show that is not context-free.
Assume L is context-free with pumping length p, and take , so .
Let with and . The window is too short to contain both an a and a c, since the p b's lie between them.
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.
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.
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.
Intersect L with (a regular language).
A string in with equal numbers of a, b and c has the form , so .
This intersection is not context-free (pumping lemma).
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.
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 |
These are rules of thumb; confirm each with a construction or a proof.
First ask whether a finite amount of memory suffices (regular). If it does, say so.
If one counter or stack comparison matches a pattern read left to right (, a marker in the middle), it is usually a DCFL.
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.
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.
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
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
Let and let .
Which of the following constraints ensure(s) that the language is context-free?
and
and
Consider the following two languages over the alphabet , where and are natural numbers.
Which ONE of the following statements is CORRECT?
Both and are context-free languages.
is a context-free language but is not a context-free language.
is not a context-free language but is a context-free language.
Neither nor are context-free languages.
Official answer: CSource: Official answer key, GATE 2025 CS1 (IIT Roorkee)
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?
(i) – (a), (ii) – (b), and (iii) – (c)
(i) – (b), (ii) – (a), and (iii) – (c)
(i) – (b), (ii) – (c), and (iii) – (a)
(i) – (a), (ii) – (c), and (iii) – (b)
Consider the following languages:
Note that is the reversal of the string . Which of the following is/are TRUE?
and are regular.
and are context-free.
is regular and is context-free.
and are context-free but not regular.
Consider the following languages:
Which of the following statements is/are FALSE?
is not context-free but and are deterministic context-free.
Neither nor is context-free.
, and all are context-free.
Neither nor its complement is context-free.
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?
I only
II only
I and III only
III only
Official answer: CSource: Official answer key, GATE 2020 CS (IIT Delhi)
Consider the following languages.
Which one of the following is TRUE?
is regular and is context-free.
is context-free but not regular and is context-free.
Neither nor is context-free.
is context-free but is not context-free.
Consider the following languages:
I.
II.
III.
IV.
Which of the languages above are context-free?
I and IV only
I and II only
II and III only
II and IV only
OTHER EXAMS
From ISRO's Scientist/Engineer written test: ISRO CS 2023. They are not counted in the GATE numbers on this page.
Which of the following statements is FALSE:
The intersection of a regular language and a context-free language is context-free
The intersection of a regular language and context-free language is regular
The union of two context-free languages is context-free
The union of two regular languages is regular
PRACTICE
Which of the following languages over is NOT context-free?
Answer: (D)
A: generated by , , so context-free.
B: generated by , so context-free.
C: union of and , both context-free, so context-free.
D: take . The window cannot contain both an a and a c.
Case 1: contains an a (so no c). Pump up with : the a count exceeds p while the c count stays p, violating .
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 .
Case 3: contains only c's. Pump down with : the c count drops below p while the b count stays p, violating .
Every split fails, so D is not context-free.
Let and . Which statements are true?
is context-free
is context-free
and are both deterministic context-free languages
is not context-free
Answer: (B), (C), (D)
, which is not context-free, so A is false and D is true.
The union of two CFLs is a CFL, so B is true.
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.
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:
A string of length 6 in L has three a's and three b's.
A string in R has the form , so all its b's form one block: .
Then , giving .
Each choice gives a distinct string (bbbaaa, abbbaa, aabbba, aaabbb), so the count is 4.
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?
Answer: (C)
, and is regular, so is context-free by closure under intersection with a regular language.
A: CFLs are not closed under complement.
B: ; taking gives , which need not be context-free.
D: ; with this is , again not always context-free.
For context-free grammars , , and a string , which of the following problems is undecidable?
Is ?
Is finite?
Is ?
Is ?
Answer: (D)
Emptiness is decidable by marking variables that derive terminal strings.
Finiteness is decidable from the dependency graph after cleaning the grammar.
Membership is decidable with the CYK algorithm.
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.