Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 45

Closure Properties of CFLs: Union, Concatenation, and Kleene Star

Recommended resources

Hello! Welcome back to our study of the Theory of Computation.

In our last lesson, we developed a powerful tool, the Pumping Lemma for Context-Free Languages, and used it to prove that certain languages, like {aⁿbⁿcⁿ | n ≥ 0}, are not context-free. Today, we'll explore the other side of the coin: which operations can you perform on CFLs that are guaranteed to result in another CFL? This is the study of closure properties.

Our goal for this lesson is to prove that Context-Free Languages are closed under the regular operations: union, concatenation, and Kleene star. We will do this by using grammar-based constructions, which means we will learn how to take the grammars of existing CFLs and systematically combine them to build a new grammar for the resulting language.

Why Grammars?

For these proofs, we have a choice between our two equivalent models for CFLs: Pushdown Automata (PDAs) and Context-Free Grammars (CFGs). While it's possible to prove closure by combining PDAs, it's often much more complex. Stitching grammars together, on the other hand, is surprisingly elegant and intuitive. Your experience in software development, where you combine different modules or components to build a larger system, will provide a helpful parallel for what we're about to do.

To get a high-level overview of the constructions we'll be discussing, please start by watching the first part of this video.

Closure Properties of Context-Free Languages

This video from Easy Theory provides a clear and intuitive introduction to the closure properties of CFLs. It explains why we use grammars and walks through the core ideas behind the constructions for union, concatenation, and Kleene star.

Please watch from the beginning until 09:00. Focus on the main strategy for each of the three operations (Union, Concatenation, Star) and the new rules that are introduced.

Now that you have the main ideas, let's formalize them and prove each property one by one.



The Setup

For the proofs of union and concatenation, let's assume we have two context-free languages, and . Since they are CFLs, they must be generated by some CFGs, say and .

  • where .
  • where .

A crucial first step in combining these grammars is to ensure they don't interfere with each other. Specifically, their sets of variables must be disjoint (). If they aren't, we can simply rename the variables in one of the grammars (e.g., change every variable X in to X'). This is analogous to avoiding namespace collisions in programming. We will assume this has been done for all the constructions below.


1. Closure Under Union

Claim: If and are CFLs, then is also a CFL.

Construction: We will build a new grammar that generates any string from or any string from . The idea is to create a new start symbol that acts as a switch, choosing to proceed with either the grammar for or the grammar for .

Let , where:

  1. Variables : , where is a brand new start symbol not in or .
  2. Alphabet : .
  3. Start Symbol: .
  4. Rules : . We take all the rules from both original grammars and add two new rules from our new start symbol.

Why it works: A derivation in must start with . The very first step must be either or .

  • If we choose , the rest of the derivation can only use rules from (since the variable sets are disjoint). Therefore, we will generate a string in .
  • If we choose , the rest of the derivation can only use rules from , generating a string in .

Since any derivable string must belong to either or , the language of is exactly .

Closure Properties of Context-Free Languages: Union Proof Start
This image provides a concise visual summary of the theorem and the grammar-based construction for proving closure under union.

2. Closure Under Concatenation

Claim: If and are CFLs, then their concatenation is also a CFL.

Construction: We'll build a new grammar where the start symbol generates the start symbol of followed by the start symbol of .

Let , where:

  1. Variables : , where is a new start symbol.
  2. Alphabet : .
  3. Start Symbol: .
  4. Rules : .

Why it works: Any derivation must start with . Because the variable sets are disjoint, the part of the derivation that starts from can only produce a string from , let's call it . Similarly, the part starting from can only produce a string from , call it . The final string will be , which is precisely the definition of a string in the language .


3. Closure Under Kleene Star

Claim: If is a CFL, then is also a CFL.

Construction: For the Kleene star, we need to generate zero or more concatenations of strings from . This suggests a recursive definition. Our new grammar, , will have a start symbol that can either stop (producing the empty string ) or generate one string from and then recursively call itself to generate the rest.

Let , where:

  1. Variables : , where is a new start symbol.
  2. Alphabet : .
  3. Start Symbol: .
  4. Rules : .

Why it works:

  • To generate the empty string (which is always in ), we use the rule .
  • To generate a non-empty string like (where each ), we can use the rule times, resulting in a sentential form like . We then use to terminate the recursion, leaving . Each can then derive a string from , giving us the desired concatenation.

To solidify your understanding and see these constructions presented with formal proofs, please read the following document.

Lecture 10 - Closure Properties for Context-Free Languages

These lecture notes from the University of Waterloo provide a rigorous, formal proof for each of the three closure properties. It's an excellent resource for understanding the precise logic behind why these constructions work.

Please read section 10.1, "The regular operations" (pages 1-2). For each operation (Union, Concatenation, Star), focus on the formal construction of the new grammar and the two-part argument that proves L(G) is the desired language.

Test your understanding!

Let's say you have two grammars:

  • with start symbol for the language . Its rules are .
  • with start symbol for the language . Its rules are .

Construct the set of rules for a new grammar that generates the language . What is one example of a string (not in or ) that your new grammar can generate?

Show answer

The new grammar will have a new start symbol, let's call it . We combine the rules from and and add one new rule for .

The full set of rules for would be:

  1. (The new rule)
  2. (From )
  3. (From )
  4. (From )
  5. (From )

An example of a string generated could be aabbcc.
Here's the derivation:

(using )
(using again)
(using )
(using )
(using )


Conclusion

In this lesson, we have formally proven that the set of context-free languages is closed under union, concatenation, and Kleene star.

Key Takeaways:

  • Closure means that applying an operation to languages within a class produces a language that is also in that class.
  • We can prove these closure properties for CFLs using constructive proofs based on grammars.
  • Union: Create a new start symbol with rules .
  • Concatenation: Create a new start symbol with the rule .
  • Kleene Star: Create a new start symbol with recursive rules .
  • A critical prerequisite for these constructions is ensuring the variable sets of the original grammars are disjoint.

Preview of the Next Lesson:
We've seen that CFLs are closed under the same operations as regular languages. But does this hold for all set operations? Consider the languages and . Both are context-free. What is their intersection, ? It's {aⁿbⁿcⁿ | n ≥ 0}, which we proved in our last lesson is not a CFL! This strongly suggests that CFLs are not closed under intersection. In the next lesson, we will formalize this and also investigate closure under complement.