Closure Properties of Context Free Languages

Last Updated : 13 Apr, 2026

Context-Free Languages (CFLs) form an important class of languages in automata theory and formal language theory. These languages are generated using context-free grammars (CFGs) and are recognized by pushdown automata (PDAs).

  • Context-Free Languages are generated by Context-Free Grammars (CFGs).
  • CFLs are accepted and recognized by Pushdown Automata (PDAs).
  • Closure properties explain whether applying an operation to CFLs results in another CFL.
  • CFLs are closed under some operations, meaning the result is always a CFL.
  • CFLs are not closed under certain operations, which may produce non-context-free languages.
  • A context-free grammar consists of production rules of the form A → α.

Properties of Context Free Languages 

CFLs are closed under the following operations:

1. Union

If L and M are two context-free languages, then their union L ∪ M is also a CFL.

Construction:

  1. Consider two context-free grammars, G and H, for L and M respectively.
  2. Assume that G and H have no common variables (this can be ensured by renaming variables if needed).
  3. Introduce a new start symbol S and add the rule: S → S₁ | S₂ Here, S₁ and S₂ are the start symbols of G and H, respectively.
  4. The resulting grammar generates L ∪ M, proving that CFLs are closed under union.
union_closed_

Example: Let L₁ = { aⁿbⁿcᵐ | m ≥ 0, n ≥ 0 } and L₂ = { aⁿbᵐcᵐ | n ≥ 0, m ≥ 0 }.

  • L₁ enforces that the number of a’s equals the number of b’s.
  • L₂ enforces that the number of b’s equals the number of c’s.
  • Their union states that either of these conditions must be satisfied, making the resulting language context-free.

Note: So CFL are closed under Union. 

2. Concatenation

If L and M are CFLs, then their concatenation LM is also a CFL.

Construction:

  1. Let G and H be the context-free grammars for L and M, respectively.
  2. Assume that G and H have no common variables.
  3. Introduce a new start symbol S and add the production: S → S₁ S₂ Here, S₁ and S₂ are the start symbols of G and H, respectively.
  4. This ensures that any derivation from S will first generate a string in L and then a string in M, proving that CFLs are closed under concatenation.
concatenation_closed_

Example 

  • L₁ ensures the number of a’s equals b’s.
  • L₂ ensures the number of c’s equals d’s.
  • Their concatenation states that first a’s equal b’s, then c’s equal d’s, making the language context-free.

Note: So CFL are closed under Concatenation. 

3. Kleene Closure

If L is a CFL, then its Kleene closure L* (zero or more repetitions of strings in L) is also a CFL.

Construction:

  1. Let G be the context-free grammar for L with start symbol S₁.
  2. Introduce a new start symbol S and add the rule: S → S₁ S | εThis rule ensures that S can derive zero or more copies of S₁, proving closure under the Kleene star.
kleene_closure_star_closed_

Example

  • L1 = { anbn | n >= 0 } 
  • L1* = { anbn | n >= 0 }* is also context free. 

Note :So CFL are closed under Kleene Closure. 

4. Intersection and complementation

If L1 and If L2 are two context free languages, their intersection L1 ? L2 need not be context free.

Example

  • L₁ ensures the number of a’s equals b’s (c’s independent).
  • L₂ ensures the number of b’s equals c’s (a’s independent).
  • Their intersection requires a = b = c, which PDA cannot handle.
    Hence, the language is not context-free.

Note : So CFL are not closed under Intersection and Complementation. 

5. Reversal

If L is a CFL, then its reversal L^R (where each string is reversed) is also a CFL.

Construction:

  1. Take the context-free grammar G for L.
  2. Modify each production so that the right-hand side of every rule is reversed.
  3. The resulting grammar generates L^R, proving that CFLs are closed under reversal.
reversal_closed_

Example:

  • Original Grammar: S → 0S1 | 01
  • Reversed Grammar: S → 1S0 | 10

6. Homomorphism

A homomorphism is a function that replaces each symbol in a string with another string.

If L is a CFL and h is a homomorphism, then h(L) is also a CFL.

homomorphism_closed_

Example:

  • Let G have the production S → 0S1 | 01.
  • Define a homomorphism h(0) = ab, h(1) = ε.
  • Then, h(L(G)) will be generated by a new grammar with productions: S → abS | ab

This shows that CFLs are closed under homomorphism.

7. Inverse Homomorphism

If L is a CFL and h is a homomorphism, then h⁻¹(L) is also a CFL.

Construction:

  • Instead of using a grammar, we use a PDA to construct the inverse homomorphism.
  • The PDA for h⁻¹(L) simulates the PDA for L but keeps track of the mapping of symbols.
_inverse_homomorphism_closed_

Deterministic Context-free Languages 

Deterministic CFL are subset of CFL which can be recognized by Deterministic PDA. Deterministic PDA has only one move from a given state and input symbol, i.e., it do not have choice. For a language to be DCFL it should be clear when to PUSH or POP. 

  • L₁ ensures the number of a’s equals b’s, while c’s are independent.
  • This can be handled deterministically by pushing for a’s and popping for b’s, so it is accepted by a DPDA.

Hence, L₁ is a DCFL.
Note : DCFL are closed only under complementation and Inverse Homomorphism. 

  • L₃ represents a choice: either a’s = b’s or b’s = c’s.
  • A DPDA cannot make such non-deterministic choices, as it must decide deterministically at each step.

Therefore, L₃ requires an NPDA for acceptance.
Hence, L₃ is context-free but not deterministic context-free (CFL but not DCFL).

Comment

Explore