Skip to main content

Theory of Computation

CFL Properties

CFL property questions are short but frequent: decide whether a language is context-free, pick the right closure property, and know why intersection and complement break closure. A few memorised languages and one table settle most MCQs.

~3 hrs ~0.5–1 marks · High 10 samples · 10 practice · 10 cards

GATE CSE properties of context-free languages — pumping lemma for CFLs, classic CFL and non-CFL languages, closure under union, concatenation, star and homomorphism, non-closure under intersection and complement, DCFL notes and Ogden’s lemma.

Learning outcomes

  • State the pumping lemma for CFLs and use it (MCQ level) to show that a^n b^n c^n, ww and a^(n²) are not context-free
  • Classify standard languages as regular, DCFL, CFL or non-CFL
  • Recall that CFLs are closed under union, concatenation, star, reversal and homomorphism but not under intersection or complement
  • Know the closure behaviour of DCFLs, the effect of intersecting with a regular language, and which decision problems for CFLs are undecidable

Pumping lemma for context-free languages

  • Statement: if L is a CFL, there is a pumping length p ≥ 1 such that every z ∈ L with |z| ≥ p can be written z = uvwxy with (1) |vwx| ≤ p, (2) |vx| ≥ 1 (at least one of v, x is non-empty) and (3) u v^i w x^i y ∈ L for all i ≥ 0.
  • Idea: a parse tree of a long string has a long path and so some variable A repeats on a root-to-leaf path. The subtree below the upper A can be replaced by the subtree below the lower A (pump down) or repeated (pump up). The two pumped parts v and x grow together.
  • Compare with the regular lemma: the CFL lemma pumps TWO substrings v and x simultaneously, which models a stack matching two parts, and the bound is |vwx| ≤ p rather than |xy| ≤ p.
  • It is a necessary condition only; it can prove non-context-freeness, never context-freeness.
  • Adversary game: you choose z ∈ L with |z| ≥ p; the adversary splits z = uvwxy under the three rules; you pick an i with u v^i w x^i y ∉ L.
  • Because |vwx| ≤ p, the window vwx is short: it cannot touch all three blocks of a^p b^p c^p, and in a^p b^p a^p b^p it cannot touch both ends. This is the key to every GATE-level use.
  • Pump up (i = 2) usually raises at most two kinds of symbols; pump down (i = 0) removes symbols and breaks a strict inequality such as i < j < k.
  • Naming caution: this lesson writes z = uvwxy (the middle part is w), while the figure writes w = uvxyz. The roles are identical — the two pumped pieces are the 2nd and 4th pieces, the middle piece in between is not pumped, and the window of the three middle pieces has length at most p.
  • Where v and x can be: both inside one block, or v inside one block and x inside the NEXT block (they can touch two adjacent blocks at most because |vwx| ≤ p is shorter than a block). Case analysis over these two situations finishes every proof.
  • Pumping length for a CFL given by a CNF grammar with k variables: p = 2^k works (a parse tree of height > k repeats a variable). This is why CNF is the standard tool for proving the lemma.
  • Difference from the regular lemma in one line: regular pumps ONE block (y) inside the first p symbols, CFL pumps TWO blocks (v and x) anywhere in a window of length ≤ p.

Formulas to remember

  • L CFL ⇒ ∃ p ∀ z ∈ L, |z| ≥ p ∃ z = uvwxy: |vwx| ≤ p, |vx| ≥ 1, ∀ i ≥ 0 u v^i w x^i y ∈ L
LanguageString zWhy pumping fails
{a^n b^n c^n}a^p b^p c^pvwx misses at least one letter type; pumping unbalances counts
{ww}a^p b^p a^p b^pvwx lies inside one half or straddles the middle; pumped halves differ
{a^(n²)}a^(p²)0 < |vx| ≤ p so p² < p² + |vx| < (p+1)²
{a^i b^j c^k | i < j < k}a^p b^(p+1) c^(p+2)pump up breaks one inequality, pump down breaks the other
{a^n b^n c^n d^n}a^p b^p c^p d^pvwx cannot contain all four letter types

Worked example

Problem. Show that L = {a^n b^n c^n | n ≥ 0} is not context-free.

Solution. Assume L is a CFL with pumping length p and take z = a^p b^p c^p = uvwxy. Since |vwx| ≤ p, vwx cannot contain both an a and a c. So pumping with i = 2 (or 0) changes the counts of at most two of the three letters while the third is unchanged, giving unequal counts and a string outside L. Contradiction.

A string split into u, v, x, y and z with v and y marked for pumping, a window of length at most p over v x y, and rows for i equals 0, 1 and 2
CFL pumping lemma: v and y (the two outer pumped pieces) grow or shrink together.

Worked example — {a^i b^j c^k | i < j < k} is not a CFL

  • Assume the language L is context-free with pumping length p and choose z = a^p b^(p+1) c^(p+2) ∈ L (since p < p+1 < p+2). Let z = uvwxy with |vwx| ≤ p and |vx| ≥ 1.
  • The b-block has length p + 1 > p ≥ |vwx|, so the window vwx cannot contain both an a and a c. There are only two cases: vx contains no c, or vx contains no a.
  • Case A (vx contains no c): pump UP with i = 2. If vx contains a b, then #b ≥ p + 2 = #c, violating j < k. If vx contains no b, it consists only of a’s and #a ≥ p + 1 = #b, violating i < j.
  • Case B (vx contains a c, hence no a): pump DOWN with i = 0. If vx contains a b then #b ≤ p = #a, violating i < j. Otherwise only c’s are removed and #c ≤ p + 1 = #b, violating j < k.
  • In every case some pumped string leaves L, a contradiction. Hence {a^i b^j c^k | i < j < k} is not context-free.
  • Strategy: choose z so that EVERY inequality is tight (consecutive values p, p+1, p+2); then pumping up breaks the upper inequality and pumping down breaks the lower one.
  • Equality version: for {a^n b^n c^n} choose a^p b^p c^p and use either i = 0 or i = 2 — only the inequality version needs both directions in a single proof.
CaseWhere is vx?PumpViolated condition
A1contains a b (no c)i = 2#b ≥ p + 2 = #c, so j < k fails
A2only a’si = 2#a ≥ p + 1 = #b, so i < j fails
B1contains a b and a ci = 0#b ≤ p = #a, so i < j fails
B2only c’si = 0#c ≤ p + 1 = #b, so j < k fails

Worked example

Problem. Show that {a^n b^n c^n | n ≥ 0} is not context-free using i = 0 only.

Solution. Take z = a^p b^p c^p. Because |vwx| ≤ p, vwx cannot contain both an a and a c. Pumping down (i = 0) removes at least one symbol but touches at most two letter types, so the third type keeps exactly p occurrences while the removed type drops below p. The three counts are no longer equal, so uwy ∉ L, a contradiction.

Classic CFL and non-CFL languages

  • Context-free (one stack comparison or nested matching): {a^n b^n}, {a^n b^m c^n}, {a^n b^n c^m}, {a^n b^m c^m d^n}, {ww^R}, palindromes, {w | #a = #b}, {a^i b^j c^k | i = j or j = k}, {a^i b^j | i ≠ j}, and balanced parentheses.
  • Not context-free (need two independent counts or copying): {a^n b^n c^n}, {a^n b^n c^n d^n}, {ww}, {a^n b^m c^n d^m} (crossing dependency), {a^i b^j c^k | i < j < k}, {a^(n²)}, {a^(2^n)}, {a^p | p prime}, {a^n b^(n²)}, {0^n 1^n 0^n}.
  • Complements can surprise: the complement of {a^n b^n c^n} IS context-free (a union of CFLs: wrong block order, or a^i b^j c^k with i ≠ j or j ≠ k). The complement of {ww} is also context-free.
  • Unary alphabet: every context-free language over one letter is regular (Parikh’s theorem). So unary languages such as {a^(n²)}, {a^(2^n)} and {a^p | p prime} are not context-free because they are not regular.
  • {a^n b^m c^n d^m}: matching a with c and b with d crosses, which a stack cannot do. By contrast {a^n b^m c^m d^n} is nested, hence context-free.
  • Regular ⊂ DCFL ⊂ CFL ⊂ CSL ⊂ Recursive ⊂ RE. A language such as a^n b^n c^n is context-sensitive (and decidable) but not context-free.
  • To decide quickly: if one stack can check the dependencies in nested (last-in, first-out) order the language is context-free; if the dependencies cross, copy, or involve three equal counts, it is not.
  • Dependency picture: draw arcs between symbols that must be equal. Nested arcs (a^n b^n, ww^R) = CFL. Crossing arcs (ww, a^n b^m c^n d^m) = not CFL. Three-way equalities and arithmetic (n², 2^n, primes) = not CFL.
  • Languages with only an “or” of two single-count conditions stay context-free: {a^i b^j c^k | i = j or j = k} is a union of two CFLs. The “and” of the same two conditions is {a^n b^n c^n}, which is not.
  • {a^i b^j c^k | i ≠ j or j ≠ k} is context-free (a union of simpler CFLs) and equals the complement of {a^n b^n c^n} restricted to a*b*c*. Together with wrong-order strings this makes the complement of {a^n b^n c^n} a CFL.
  • Counting modulo a constant stays easy: {a^n b^m | n + m is even} is regular, and adding a mod condition to a CFL keeps it context-free: {a^n b^n c^k | k is even} needs the stack only for the a/b matching and finite control for the parity of k.
  • The Dyck language of balanced parentheses and {w | #a = #b} are both DCFLs (a single counter on the stack is enough and no guessing is needed).
LanguageClass
{a^n b^n}DCFL, not regular
{ww^R}CFL, not DCFL
{w c w^R}DCFL
{a^n b^n c^n}CSL, not CFL
complement of {a^n b^n c^n}CFL (not regular)
{ww}CSL, not CFL
complement of {ww}CFL
{a^n b^m c^n d^m}CSL, not CFL
{a^n b^m c^m d^n}CFL

Worked example

Problem. Is {a^n b^m c^n | n, m ≥ 0} context-free?

Solution. Yes. Push an A for each a, ignore the b’s, then pop an A for each c. A grammar is S → aSc | B, B → bB | ε. It is even a DCFL.

Closure properties of CFLs

  • CFLs ARE closed under: union, concatenation, Kleene star (and plus), reversal, homomorphism, inverse homomorphism, substitution, and intersection with a regular language. They are also closed under prefix and suffix operations.
  • CFLs are NOT closed under: intersection, complement, set difference L1 − L2 (when both are CFL), and infinite union.
  • Non-closure under intersection: L1 = {a^n b^n c^m | n, m ≥ 0} and L2 = {a^m b^n c^n | m, n ≥ 0} are CFLs, but L1 ∩ L2 = {a^n b^n c^n} is not.
  • Non-closure under complement follows from union closure: if CFLs were closed under complement then L1 ∩ L2 = (L1^c ∪ L2^c)^c would be a CFL by De Morgan, contradicting the previous point.
  • Useful special cases: CFL ∩ regular = CFL, and CFL − regular = CFL ∩ (regular complement) = CFL. The complement of a regular language is regular, so L − R is context-free whenever L is. But R − L (regular minus CFL) need not be CFL.
  • Closure under intersection with a regular language is the standard tool for non-CFL proofs: for L = {w | #a = #b = #c}, L ∩ a*b*c* = {a^n b^n c^n}, which is not a CFL.
  • Homomorphism and inverse homomorphism preserve context-freeness; so does substitution of CFLs for each terminal.
  • DCFLs: closed under complement, under intersection with a regular language and under inverse homomorphism; NOT closed under union, intersection, concatenation, star, reversal or homomorphism.
  • Why CFL union/concatenation/star are closed: build grammars from S1, S2 (S → S1 | S2, S → S1 S2, S → S1 S | ε). Reversal reverses each right-hand side; homomorphism substitutes strings for terminals in the grammar.
  • Why CFL ∩ regular is closed: run the PDA and the DFA in parallel (product of states); the stack belongs to the PDA only. CFL ∩ CFL fails because two stacks would be needed.
  • Difference: L1 − L2 = L1 ∩ complement(L2). If L2 is regular this is CFL ∩ regular = CFL. If both are CFLs the result need not be CFL (take L1 = Σ* and L2 = the complement of {a^n b^n c^n}, which is a CFL; then L1 − L2 = {a^n b^n c^n} is not).
  • Closure checking drill: test intersection claims with L1 = {a^n b^n c^m} and L2 = {a^m b^n c^n}; test complement claims with the complement of {a^n b^n c^n} (a CFL whose complement is not a CFL); test “with a regular language” claims with a*b*c*.
  • Statement “CFLs are closed under complement of a DCFL” is TRUE: the complement of a DCFL is again a DCFL, hence a CFL. Do not confuse DCFL closure with general CFL non-closure.
OperationRegularDCFLCFL
UnionYesNoYes
IntersectionYesNoNo
ComplementYesYesNo
ConcatenationYesNoYes
Kleene starYesNoYes
ReversalYesNoYes
HomomorphismYesNoYes
Intersection with regularYesYesYes

Worked example

Problem. L1 = {a^n b^n c^m} and L2 = {a^m b^n c^n} are CFLs. What is L1 ∩ L2 and what does it show?

Solution. A string in L1 has #a = #b; in L2 it has #b = #c; both force #a = #b = #c. So L1 ∩ L2 = {a^n b^n c^n}, which is not a CFL. Hence CFLs are not closed under intersection (and, by De Morgan, not under complement).

Table of ten operations with Yes and No cells for regular languages, DCFLs and CFLs, including union, intersection, complement and inverse homomorphism
Closure table: green = closed, red = not closed. Memorise the DCFL column; it is the usual trap.

Worked example — closure reasoning on CFLs

  • Question: L = {a^n b^n c^m | n, m ≥ 0} ∩ {a^m b^n c^n | m, n ≥ 0}. Is the result a CFL? The first requires #a = #b, the second requires #b = #c, so both conditions give {a^n b^n c^n}, which is NOT context-free.
  • Therefore CFLs are not closed under intersection. By De Morgan, L1 ∩ L2 = (L1ᶜ ∪ L2ᶜ)ᶜ; since union is closed, CFLs cannot be closed under complement either.
  • Question: is {a^n b^n c^n} ∪ (complement of {a^n b^n c^n}) context-free? The union is Σ*, which is regular, even though one of the two operands is not context-free. Union of a non-CFL and its complement can be regular.
  • Question: L1 = {a^n b^n}, L2 = {b^n c^n}. L1·L2 = {a^n b^n b^m c^m} is context-free (concatenation); L1 ∪ L2 is context-free; L1 ∩ L2 = {ε} is regular.
  • Question: is the complement of {ww} context-free? Yes: a string is not of the form ww if it has odd length, or if some position i has a different symbol than position i + n/2. An NPDA guesses such a mismatch, so the language is a CFL.
  • Question: L is a CFL and R is regular. L ∩ R is a CFL (product with a DFA), L − R is a CFL, but R − L need not be a CFL.
  • Take-away: closure facts help you transform an unfamiliar language into a known one: intersect with a regular language, then apply a homomorphism, then use the pumping lemma on the simplified language.
ClaimTrue?Reason
CFL ∩ CFL is a CFLNo{a^n b^n c^m} ∩ {a^m b^n c^n} = {a^n b^n c^n}
complement of a CFL is a CFLNoDe Morgan with union
CFL ∩ regular is a CFLYesproduct of PDA and DFA
CFL ∪ CFL is a CFLYesS → S1 | S2
complement of {a^n b^n c^n} is a CFLYesunion of mismatch cases
complement of {ww} is a CFLYesguess the mismatching position

Worked example

Problem. If L1 is a CFL and L2 is regular, is L1 − L2 a CFL? Is L2 − L1?

Solution. L1 − L2 = L1 ∩ complement(L2). The complement of a regular language is regular, and a CFL intersected with a regular language is a CFL, so L1 − L2 is context-free. L2 − L1 = L2 ∩ complement(L1) involves the complement of a CFL, which need not be a CFL, so L2 − L1 is not guaranteed to be context-free (take L2 = Σ* and L1 = the complement of {a^n b^n c^n} to get {a^n b^n c^n}).

DCFL notes, hierarchy and decision problems

  • Deterministic CFLs (DCFLs) are the languages accepted by DPDAs (by final state). Every regular language is a DCFL; a^n b^n is a DCFL that is not regular; ww^R and the set of all palindromes are CFLs that are not DCFLs.
  • Every DCFL has an unambiguous grammar, so every inherently ambiguous CFL (such as {a^i b^j c^k | i = j or j = k}) is not a DCFL.
  • DCFL ⊂ CFL: because DCFLs are closed under complement and CFLs are not, the complement of some CFL is not a CFL, so there are CFLs that are not DCFLs. The union {a^i b^j c^k | i = j} ∪ {a^i b^j c^k | j = k} of two DCFLs is a CFL that is not a DCFL.
  • Decidable for CFLs given by a grammar or PDA: membership (CYK, O(n³)), emptiness and finiteness.
  • Undecidable for CFGs: equivalence L(G1) = L(G2), universality L(G) = Σ*, ambiguity, whether L(G1) ∩ L(G2) = ∅, whether L(G) is regular, and whether L(G1) ⊆ L(G2).
  • Ogden’s lemma (optional, light): a strengthening of the pumping lemma where you MARK at least p positions of z and require vx to contain at least one marked position. It proves non-CFL for languages like {a^i b^j c^k d^l | i = 0 or j = k = l} where the ordinary lemma is awkward. GATE rarely needs it.
  • Parikh: the set of letter-count vectors of any CFL is semilinear, so a CFL over a unary alphabet is regular.
  • Chomsky hierarchy: Type 3 (regular, right-linear A → aB | a) ⊂ Type 2 (context-free, A → γ) ⊂ Type 1 (context-sensitive, |α| ≤ |β| for α → β) ⊂ Type 0 (unrestricted, α → β). Machines: DFA/NFA, NPDA, linear bounded automaton (LBA), Turing machine.
  • DCFL sits between Type 3 and Type 2 and is not a Chomsky type of its own; GATE draws it as Regular ⊂ DCFL ⊂ CFL. Recursive (decidable) languages lie between Type 1 and Type 0: CSL ⊂ Recursive ⊂ RE.
  • Each inclusion is proper, with witnesses: a^n b^n (DCFL, not regular), ww^R (CFL, not DCFL), a^n b^n c^n (CSL, not CFL), a language of an undecidable-halting nature (RE, not recursive).
  • Context-sensitive languages are closed under union, intersection, complement (Immerman–Szelepcsényi), concatenation and star; membership for a CSG is decidable (in PSPACE) but emptiness is undecidable.
  • Right-linear vs left-linear grammars: both generate exactly the regular languages, but a grammar mixing right-linear and left-linear rules can generate non-regular languages (S → aSb | ε is “linear” but not regular).
Problem for CFG / PDADecidable?
Is w ∈ L(G)?Yes (CYK)
Is L(G) empty?Yes
Is L(G) finite?Yes
Is L(G1) = L(G2)?No
Is L(G) = Σ*?No
Is G ambiguous?No
Is L(G1) ∩ L(G2) = ∅?No
Is L(G) regular?No

Worked example

Problem. Why is {w w^R} not a DCFL although it is context-free?

Solution. A deterministic PDA cannot tell where the middle of the string is: after reading aa the input may be the whole string (w = a) or only the start of a longer string such as aabbaa. It must decide when to switch from pushing to popping without lookahead, and it is a standard fact that no DPDA can do this for ww^R.

Nested ellipses for Type 3 regular inside Type 2 context-free inside Type 1 context-sensitive inside Type 0 recursively enumerable, with grammar forms and machines listed on the right
Chomsky hierarchy: each type is a proper subset of the next, from regular (Type 3) to RE (Type 0).

GATE exam tricks — CFL property MCQs

  • Memorise the two-line closure summary: CFLs closed under union, concatenation, star, reversal, homomorphism; NOT closed under intersection, complement. DCFLs: closed under complement but not union.
  • For “is L context-free?”, count dependencies. One matched pair (a^n b^n, a^n b^m c^n) is fine; three equal counts, ww, crossing pairs, squares, primes and powers of 2 are not CFL.
  • If a question states “L1 and L2 are CFLs, then L1 ∩ L2 is …”, the safe answer is “not necessarily context-free”; with a REGULAR second operand the answer is CFL.
  • The complement of a non-CFL can be a CFL (the complement of {a^n b^n c^n} and of {ww}). Never conclude non-CFL for the complement of a non-CFL without checking.
  • Use L ∩ a*b*c* to reduce “equal counts of a, b and c” languages to {a^n b^n c^n} and conclude non-CFL.
  • Hierarchy MCQs: every regular language is a DCFL, every DCFL is a CFL, every CFL is context-sensitive and decidable. The reverse inclusions are false.
  • When asked how a decision problem fares: membership, emptiness, finiteness are decidable for CFLs; equivalence, ambiguity, universality, intersection emptiness are not (but all become decidable for regular languages).

Worked example

Problem. Is the language {w ∈ {a,b,c}* | #a(w) = #b(w) = #c(w)} context-free?

Solution. No. If it were, intersecting with the regular language a*b*c* would give the CFL {a^n b^n c^n}, which is not context-free by the pumping lemma. Contradiction.

Exam tips

  • Closure summary: CFL closed under ∪, ·, *, reversal, homomorphism; NOT under ∩ or complement.
  • Use intersection with a regular language (such as a*b*c*) to turn a hard language into {a^n b^n c^n}.
  • For pumping-lemma MCQs, remember |vwx| ≤ p: the window cannot cover all three blocks of a^p b^p c^p.
  • Unary CFLs are regular, so non-regular unary languages (squares, primes, powers of 2) are not CFL.
  • DCFLs are closed under complement; the union of two DCFLs may fail to be a DCFL.
Exam pattern · 10 questions

GATE sample questions & answers

10 GATE-style MCQs for CFL Properties with full explanations. Attempt each question before revealing the answer.

Q1

Which of the following languages is NOT context-free?

  • A) {a^n b^n c^m | n, m ≥ 0}
  • B) {a^n b^m c^n | n, m ≥ 0}
  • C) {a^n b^n c^n | n ≥ 0}
  • D) {w w^R | w ∈ {a,b}*}
Show answer & explanation

Answer: C

A and B need only one stack comparison, D is a palindrome language. {a^n b^n c^n} needs two equalities simultaneously and fails the pumping lemma for CFLs.

Q2

Which of the following closure properties does NOT hold for context-free languages?

  • A) Union
  • B) Concatenation
  • C) Intersection
  • D) Kleene star
Show answer & explanation

Answer: C

CFLs are closed under union, concatenation and star but not under intersection: {a^n b^n c^m} ∩ {a^m b^n c^n} = {a^n b^n c^n}.

Q3

If L1 = {a^n b^n c^m | n, m ≥ 0} and L2 = {a^m b^n c^n | m, n ≥ 0}, then L1 ∩ L2 is:

  • A) {a^n b^n c^n | n ≥ 0}
  • B) {a^n b^m c^n}
  • C) regular
  • D) empty
Show answer & explanation

Answer: A

L1 forces #a = #b and L2 forces #b = #c, so the strings in both have #a = #b = #c, i.e., {a^n b^n c^n}. This language is not context-free.

Q4 Complement of a non-CFL may be CFL

Let L = {a^n b^n c^n | n ≥ 0}. The complement of L over {a, b, c} is:

  • A) regular
  • B) context-free but not regular
  • C) not context-free
  • D) not recursive
Show answer & explanation

Answer: B

The complement is the union of the regular set of strings not of the form a*b*c* and the CFLs {a^i b^j c^k | i ≠ j} and {a^i b^j c^k | j ≠ k}. A union of CFLs is a CFL. It is not regular, otherwise L itself would be regular.

Q5

Which of the following languages over {a, b} is context-free?

  • A) {ww | w ∈ {a,b}*}
  • B) {w w^R | w ∈ {a,b}*}
  • C) {a^n b^n c^n | n ≥ 0}
  • D) {a^(n²) | n ≥ 0}
Show answer & explanation

Answer: B

ww^R has the nested (palindrome) structure S → aSa | bSb | ε. ww needs copying, a^n b^n c^n needs three equal counts, and a^(n²) is a non-regular unary language.

Q6

The language {a^(2^n) | n ≥ 0} is:

  • A) regular
  • B) context-free but not regular
  • C) not context-free
  • D) a deterministic CFL
Show answer & explanation

Answer: C

It is a non-regular unary language, and every unary CFL is regular (Parikh), so it is not context-free. Directly: pumping z = a^(2^m) once with 0 < |vx| ≤ p gives a length strictly between 2^m and 2^(m+1) for large m, which is not a power of 2.

Q7

Which of the following classes of languages is closed under complement?

  • A) Context-free languages
  • B) Deterministic context-free languages
  • C) Both
  • D) Neither
Show answer & explanation

Answer: B

DCFLs are closed under complement (a DPDA can be made to halt and its final states swapped). General CFLs are not closed under complement.

Q8

If L1 is a context-free language and L2 is a regular language, then L1 ∩ L2 and L1 − L2 are respectively:

  • A) both context-free
  • B) regular and context-free
  • C) both regular
  • D) not necessarily context-free
Show answer & explanation

Answer: A

CFLs are closed under intersection with regular languages (PDA × DFA product), and L1 − L2 = L1 ∩ (complement of L2) where the complement of a regular language is regular.

Q9

In proving {a^n b^n c^n} non-context-free with z = a^p b^p c^p, why does pumping fail to stay inside L?

  • A) vwx can contain all three letters, so pumping keeps the counts equal
  • B) vwx has length at most p, so it touches at most two distinct letters and pumping unbalances the counts
  • C) v and x are always empty
  • D) pumping only works for even strings
Show answer & explanation

Answer: B

|vwx| ≤ p means the window cannot span from the a-block to the c-block, so at least one letter keeps its count while at least one other changes (|vx| ≥ 1), giving unequal counts.

Q10

Which of the following problems for context-free grammars is undecidable?

  • A) Is L(G) empty?
  • B) Is L(G) finite?
  • C) Is a given string w in L(G)?
  • D) Is L(G) = Σ*?
Show answer & explanation

Answer: D

Emptiness, finiteness and membership are decidable (CYK, graph arguments); universality L(G) = Σ* is undecidable for CFGs.

Topic drill · 10 questions

10 practice questions — CFL Properties

Extra page-specific practice beyond the GATE samples. Attempt all 10, then reveal answers to check your reasoning.

P1

Context-free languages are closed under reversal.

  • A) True
  • B) False
  • C) True only for DCFLs
  • D) True only for regular languages
Show answer & explanation

Answer: A) True

Reverse every production right-hand side of a CFG: A → α becomes A → α^R, giving a grammar for L^R.

P2

Which of the following languages is not context-free?

  • A) {a^i b^j c^k | i < j < k}
  • B) {a^i b^j c^k | i = j or j = k}
  • C) {a^i b^j | i ≠ j}
  • D) {a^n b^m c^m d^n}
Show answer & explanation

Answer: A) {a^i b^j c^k | i < j < k}

Two strict inequalities need two independent comparisons; pumping up or down breaks one of them. The others are context-free.

P3

If L1 and L2 are context-free languages, which of the following is NOT necessarily context-free?

  • A) L1 ∪ L2
  • B) L1 L2
  • C) L1*
  • D) L1 ∩ L2
Show answer & explanation

Answer: D) L1 ∩ L2

Union, concatenation and star preserve context-freeness; intersection does not in general.

P4

If h is a homomorphism and L is context-free, then h(L) is:

  • A) always regular
  • B) context-free
  • C) not necessarily context-free
  • D) always non-regular
Show answer & explanation

Answer: B) context-free

Replace each terminal a in the grammar by h(a); the new grammar generates h(L).

P5

Which statement about deterministic context-free languages is correct?

  • A) They are closed under union
  • B) They are closed under complement
  • C) They are closed under concatenation
  • D) They are closed under reversal
Show answer & explanation

Answer: B) They are closed under complement

DCFLs are closed under complement but not under union, concatenation, star or reversal.

P6

Which of the following is the correct chain of proper inclusions?

  • A) Regular ⊂ CFL ⊂ DCFL ⊂ Recursive
  • B) Regular ⊂ DCFL ⊂ CFL ⊂ Recursive
  • C) DCFL ⊂ Regular ⊂ CFL ⊂ Recursive
  • D) Regular ⊂ DCFL ⊂ Recursive ⊂ CFL
Show answer & explanation

Answer: B) Regular ⊂ DCFL ⊂ CFL ⊂ Recursive

Every regular language is a DCFL, every DCFL is a CFL, and every CFL is decidable (CYK), so it is recursive.

P7

The language {a^n b^m c^p | n = m or m = p} is:

  • A) not context-free
  • B) context-free
  • C) regular
  • D) not recursive
Show answer & explanation

Answer: B) context-free

It is the union of {a^n b^n c^p} and {a^n b^m c^m}, each context-free, and CFLs are closed under union.

P8

In the pumping lemma for CFLs, a long string z is decomposed into how many parts?

  • A) 3
  • B) 4
  • C) 5
  • D) 6
Show answer & explanation

Answer: C) 5

z = uvwxy: five parts, of which v and x are pumped together.

P9

The language {a^n b^m c^n d^m | n, m ≥ 1} is:

  • A) context-free
  • B) regular
  • C) not context-free
  • D) deterministic context-free
Show answer & explanation

Answer: C) not context-free

The dependencies a↔c and b↔d cross, which a single stack cannot handle; the pumping lemma with a^p b^p c^p d^p confirms it.

P10

To show that L = {w ∈ {a,b,c}* | #a = #b = #c} is not context-free using closure properties, one should intersect L with:

  • A) (a + b + c)*
  • B) a*b*c*
  • C) c*b*a*
  • D) (abc)*
Show answer & explanation

Answer: B) a*b*c*

L ∩ a*b*c* = {a^n b^n c^n}, which is not context-free. Since CFLs are closed under intersection with regular languages, L cannot be context-free.

Topic FAQs

Why are CFLs not closed under intersection?

{a^n b^n c^m} and {a^m b^n c^n} are both context-free, but their intersection {a^n b^n c^n} is not, because a single stack cannot enforce two independent equalities at once.

Is the complement of a context-free language always context-free?

No. CFLs are not closed under complement; this follows from closure under union plus non-closure under intersection (De Morgan). For instance the complement of {a^n b^n c^n} is context-free, but the complement of that CFL is {a^n b^n c^n}, which is not.

Does the pumping lemma for CFLs prove that a language is context-free?

No. It is a necessary condition. It can only be used to prove that a language is not context-free.

What happens when a CFL is intersected with a regular language?

The result is again a CFL (product of a PDA with a finite automaton). This is how intersection with a*b*c* turns hard-looking languages into {a^n b^n c^n}.

Quick revision · 10 cards

10 flashcards — CFL Properties

Hover a card to reveal the answer with a 3D flip. On touch devices, tap the card or use Show answer.

Q1 What does the pumping lemma for context-free languages state?
Q2 Which string is used to show that {a^n b^n c^n} is not context-free?
Q3 Under which operations are context-free languages closed?
Q4 Why are context-free languages not closed under intersection?
Q5 Why are context-free languages not closed under complement?
Q6 What is the result of intersecting a context-free language with a regular language?
Q7 Are deterministic CFLs closed under complement and union?
Q8 Is every context-free language over a one-letter alphabet regular?
Q9 Which of ww and ww^R is context-free?
Q10 Which decision problems for CFGs are decidable and which are not?