Closure Properties

COS3701 - Theoretical Computer Science III · Context-Free Languages

Closure Properties of Context-Free Languages

Closure properties refer to the ability of a class of languages to remain within the same class when certain operations are applied to them. For context-free languages (CFLs), closure properties help us understand how these languages behave under various operations such as union, concatenation, and intersection. You will also learn about the limitations of context-free languages with respect to certain operations.

Union of Context-Free Languages

The union of two context-free languages is also a context-free language. If L1 and L2 are two context-free languages, then the language L = L1 ∪ L2 is also context-free.

To demonstrate this, consider two context-free grammars (CFGs) G1 and G2 that generate languages L1 and L2, respectively. We can construct a new CFG G that generates L:

  1. Start with a new start symbol S.
  2. Include the productions of G1 and G2.
  3. Add productions that allow S to derive either the start symbol of G1 or G2.

For example, let:

G1: S1 → aS1b | ε

This grammar generates the language L1 = { a^n b^n | n ≥ 0 }.

G2: S2 → cS2d | ε

This grammar generates the language L2 = { c^n d^n | n ≥ 0 }.

We can create a new grammar G for the union L = L1 ∪ L2:

G: S → S1 | S2

Now, G generates the language L = { a^n b^n | n ≥ 0 } ∪ { c^n d^n | n ≥ 0 }.

Remember: The union of two CFLs is always a CFL.

Concatenation of Context-Free Languages

The concatenation of two context-free languages is also a context-free language. If L1 and L2 are context-free languages, then L = L1 · L2 is context-free.

To construct a grammar for the concatenation of two languages, you can follow these steps:

  1. Let G1 be the grammar for L1 and G2 be the grammar for L2.
  2. Create a new start symbol S.
  3. Add productions that allow S to derive the start symbol of G1 followed by the start symbol of G2.

For example, consider the following grammars:

G1: S1 → aS1 | bS1 | ε

This grammar generates the language L1 = { a, b }*.

G2: S2 → c | d

This grammar generates the language L2 = { c, d }.

Now, we can create a new grammar G for the concatenation L = L1 · L2:

G: S → S1S2

Now, G generates the language L = { a, b }*{ c, d }, which includes strings like ac, bd, abc, and so on.

Remember: The concatenation of two CFLs is always a CFL.

Intersection of Context-Free Languages

The intersection of two context-free languages is not necessarily a context-free language. If L1 and L2 are context-free languages, L = L1 ∩ L2 may not be context-free.

To illustrate this, consider the languages L1 = { a^n b^n | n ≥ 0 } and L2 = { a^n b^m | n, m ≥ 0 }. The intersection L = L1 ∩ L2 is { a^n b^n | n ≥ 0 }, which is context-free. However, if you take L1 = { a^n b^n | n ≥ 0 } and L2 = { b^n a^n | n ≥ 0 }, their intersection is the empty set, which is context-free, but it is also an example of how intersection can lead to non-context-free results in other cases.

Watch out: Do not assume that the intersection of two CFLs is always a CFL. It can lead to non-context-free languages.

Complement of Context-Free Languages

The complement of a context-free language is also not necessarily a context-free language. If L is a context-free language, then the complement of L, denoted as L', is not guaranteed to be context-free.

For instance, consider the context-free language L = { a^n b^n | n ≥ 0 }. The complement L' contains strings like aa, bb, ab, and so on, which is not context-free. This demonstrates that the closure properties do not hold for complementation.

Watch out: The complement of a CFL is not necessarily a CFL.

Reversal of Context-Free Languages

The reversal of a context-free language is also a context-free language. If L is a context-free language, then the reversed language L^R is also context-free.

To create a grammar for the reversal of a language, you can reverse the productions of the original grammar. For example, if G is a grammar for L, then you can construct a grammar G' for L^R by reversing all the productions.

For example, consider the grammar:

G: S → aS | bS | ε

This grammar generates the language L = { a, b }*. The reversed language L^R is generated by:

G': S → Sa | Sb | ε

Now, G' generates the language L^R = { a, b }* as well, which is context-free.

Remember: The reversal of a CFL is always a CFL.

Summary of Closure Properties

  • The union of two context-free languages is context-free.
  • The concatenation of two context-free languages is context-free.
  • The intersection of two context-free languages is not necessarily context-free.
  • The complement of a context-free language is not necessarily context-free.
  • The reversal of a context-free language is context-free.

Check your understanding

  1. What is the result of the union of two context-free languages?
  2. Is the intersection of two context-free languages always context-free? Explain your answer.
  3. What happens to a context-free language when it is reversed?
  4. Provide an example of two context-free languages where their intersection is not context-free.