CS Atlas GATE 2027
TOC course
0 / 91 complete

TOC/Phase 3 · CFG, PDA & Context-Free Languages

Lesson 3.4.1 · Item 59 of 91

Closure properties of CFLs and DCFLs

Read at your own pace. Try the questions before opening the answers.

On this page

Recall warm-up

  1. Which derivation does the grammar-built PDA simulate?
    Stuck? Peek at 3.3.4 CFG ↔ PDA equivalence
    • CFG → PDA: push S; expand (q, ε, A) → (q, α); match (q, a, a) → (q, ε); accept on Z₀. Simulates leftmost derivations; nondeterministic.
    • PDA → CFG: variables [p X q] (net pop of X from p to q).
    • CFG = NPDA (final state) = NPDA (empty stack). One working state suffices.

    Read lesson 3.3.4

  2. Are regular languages closed under intersection?
    Stuck? Peek at 2.4.1 Closure properties of regular languages
    • Closed: ∪ ∩ − complement, concat, *, ⁺, reversal, homomorphism, inverse homomorphism, quotient (by any language), prefix, suffix, substring, substitution, finite ∪/∩.
    • Not guaranteed: infinite ∪/∩, subset, superset.
    • Prove non-regular: L ∩ R non-regular (R regular) ⇒ L non-regular.

    Read lesson 2.4.1

  3. Product DFA accepting set for intersection?
    Stuck? Peek at 1.1.4 Product construction and complement
    • Product: states Q₁×Q₂, δ((p,q),a) = (δ₁(p,a), δ₂(q,a)), start (s₁,s₂).
    • ∩: F₁×F₂. ∪: F₁×Q₂ ∪ Q₁×F₂. −: F₁×(Q₂−F₂). ≤ |Q₁|·|Q₂| states.
    • Complement: complete the DFA (add dead state), swap F ↔ Q−F; same state count.
    • Regular languages closed under ∪, ∩, −, complement.

    Read lesson 1.1.4

The Hook

Regular languages were closed under everything. Context-free languages are not — and the failures are exactly what GATE tests. The key reason: two stacks can’t be merged into one. Learn the table with its reasons and every closure question becomes a lookup.

Predict First

L₁ = { aⁿbⁿcᵐ } and L₂ = { aᵐbⁿcⁿ } are both CFLs. What is L₁ ∩ L₂?

Core Concept

Prediction check

L₁ ∩ L₂ = { aⁿbⁿcⁿ } — which is not a CFL (3.4.3). So CFLs are not closed under intersection.

Two CFLs whose overlap is not context-free.
Two CFLs whose overlap is not context-free.

CFLs are closed under…

Union (S → S₁ | S₂), concatenation (S → S₁S₂), Kleene star (S → S₁S | ε), reversal (reverse every body), homomorphism and substitution (replace terminals), inverse homomorphism (PDA construction), and intersection with a regular language (run a PDA and a DFA in parallel: product states, one stack).

PDA states P
× DFA states Q
product states
one stack
shared, used only by the PDA part
L ∩ R is a CFL
Why CFL ∩ regular is a CFL: the DFA needs no stack.

CFLs are NOT closed under…

  • Intersection: aⁿbⁿcᵐ ∩ aᵐbⁿcⁿ = aⁿbⁿcⁿ.
  • Complement: if CFLs were closed under complement, then L₁ ∩ L₂ = (L̅₁ ∪ L̅₂)̅ would be a CFL — contradiction.
  • Difference L₁ − L₂: Σ* − L would give complement.

But L − R (R regular) = L ∩ R̅ is a CFL.

CFL closed under ∪
if also under complement
then L₁ ∩ L₂ = (L̅₁ ∪ L̅₂)̅
De Morgan
would be a CFL — false
Why complement must fail.

DCFLs

Closed under: complement (swap accepting states after making the DPDA always read its whole input), intersection with regular, union with regular, difference with regular, inverse homomorphism.

Not closed under: union, intersection, concatenation, Kleene star, reversal, homomorphism.

Example: {aⁿbⁿcᵐ} and {aᵐbⁿcⁿ} are DCFLs; their intersection is not even a CFL. Their union {aⁱbʲcᵏ : i = j or j = k} is a CFL but not a DCFL.

operationRegularDCFLCFL
union✓✗✓
intersection✓✗✗
complement✓✓✗
concatenation✓✗✓
Kleene star✓✗✓
reversal✓✗✓
homomorphism✓✗✓
inverse homomorphism✓✓✓
∩ regular✓✓✓
difference with regular (L − R)✓✓✓
The closure table so far. DCFL and CFL are almost opposite: DCFL keeps complement, CFL keeps union/concatenation/star.

Using closure to prove non-CFL

If L ∩ R (R regular) is not a CFL, L is not a CFL. Example: L = { w ∈ {a,b,c}* : #a = #b = #c }. L ∩ a*b*c* = aⁿbⁿcⁿ, not a CFL ⇒ L is not a CFL.

flowplay illustration
Intersect with a regular shape.

Putting it together

  • CFLs keep union, concatenation, star, reversal, homomorphisms and ∩ regular — not ∩ or complement.
  • DCFLs keep complement and ∩ regular — not union, ∩, concatenation, star, reversal.
  • Prove non-CFL by intersecting with a regular shape.

Visual

memory hook for the table:
  CFL : "U C S R H"  — Union, Concatenation, Star, Reversal, Homomorphism   (+ ∩R, h⁻¹)
        NOT ∩, NOT complement
  DCFL: complement ✓, ∩R ✓, h⁻¹ ✓ — everything else ✗ (union, ∩, concat, star, reversal, h)

Worked Examples

Easy

L is a CFL, R is regular. Is L − R a CFL?

Show the full solution

Yes: L − R = L ∩ R̅, and R̅ is regular.

Medium

L₁, L₂ DCFLs. Is L₁ ∪ L₂ necessarily a DCFL? A CFL?

Show the full solution

Not necessarily a DCFL (aⁿbⁿcᵐ ∪ aᵐbⁿcⁿ is not). Always a CFL (both are CFLs).

GATE-level

L₁ CFL, L₂ DCFL, R regular. Which are always CFLs? (i) L₁ ∩ R (ii) L̅₂ (iii) L₁ ∩ L₂ (iv) L̅₁

Show the full solution

(i) ✓. (ii) ✓ — complement of a DCFL is a DCFL, hence a CFL. (iii) ✗ — intersection of two CFLs can fail. (iv) ✗. Answer: (i) and (ii).

GATE Traps

  • CFL ∩ CFL is not guaranteed; CFL ∩ regular is.
  • Complement: DCFL yes, CFL no.
  • DCFL union is not closed; CFL union is.
  • The intersection of two CFLs might still happen to be a CFL — “not closed” means “not guaranteed”.

Memory Hook

One stack can’t do two jobs at once: that is why CFLs lose intersection. A deterministic stack can be flipped (complement); a guessing one can’t.

Connect the Dots

Next: which questions about CFLs have algorithms — and the efficient membership test (CYK) that every parser descends from.

Self-Test

  1. MSQ

    CFLs are closed under:

    1. union
    2. intersection
    3. Kleene star
    4. intersection with a regular language
  2. MSQ

    DCFLs are closed under:

    1. complement
    2. union
    3. intersection with a regular language
    4. concatenation
  3. MCQ

    Why are CFLs not closed under complement?

    1. PDAs cannot be complemented
    2. Closure under ∪ and complement would give closure under ∩, which fails
    3. CFLs are infinite
    4. Grammars have no complement
  4. MCQ

    { w ∈ {a,b,c}* : #a = #b = #c } is:

    1. regular
    2. DCFL
    3. CFL but not DCFL
    4. not a CFL

Rule Card

  • CFL closed: ∪, concat, *, reversal, h, h⁻¹, substitution, ∩R, −R. Not: ∩, complement, difference.
  • DCFL closed: complement, ∩R, ∪R, −R, h⁻¹. Not: ∪, ∩, concat, *, reversal, h.
  • Counterexample: aⁿbⁿcᵐ ∩ aᵐbⁿcⁿ = aⁿbⁿcⁿ. Non-CFL proof: L ∩ R non-CFL ⇒ L non-CFL.

Concept Ledger

  • closure of CFLs
  • closure of DCFLs
  • CFL ∩ regular construction
  • non-closure under ∩ and complement

Answer Vault

Reveal answers when you’re ready

Warm-up answers

  1. Leftmost.
  2. Yes (product construction).
  3. F₁ × F₂.

Self-test answers

  1. ACD — See the table.
    • B: aⁿbⁿcⁿ counterexample.
  2. AC — See the table.
    • B: Not closed.
    • D: Not closed.
  3. B — De Morgan argument.
    • A: Not a proof.
    • C: Irrelevant.
    • D: Not a proof.
  4. D — Intersect with a*b*c*.
    • A: No.
    • B: No.
    • C: Not even a CFL.

Flashcards

  1. Are CFLs closed under intersection? Counterexample?
    Reveal answerNo: aⁿbⁿcᵐ ∩ aᵐbⁿcⁿ = aⁿbⁿcⁿ.
  2. Are DCFLs closed under complement?
    Reveal answerYes.
  3. Are DCFLs closed under union?
    Reveal answerNo.
  4. Is CFL ∩ regular a CFL?
    Reveal answerYes.

Ready for the next idea?

Mark this item complete when you’ve finished reading and working through it.