Skip to main content

Welcome to a Grasp lesson

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

Lesson 32

Eliminating ε- and Unit Productions

Recommended resources

Hello! Welcome back to our journey through the Theory of Computation.

In our last lesson, we took the first step in "cleaning up" Context-Free Grammars by removing useless symbols—those that are non-generating or unreachable. This process is analogous to dead code elimination in software engineering, simplifying the grammar without altering the language it describes.

Today, we continue this process by tackling two other sources of complexity: productions that allow variables to vanish (A → ε) and productions that simply rename variables (A → B). Our learning outcome is to apply algorithms to eliminate ε-productions and unit productions from a CFG.

Mastering these simplification techniques is essential because they transform a complex, unwieldy grammar into a standardized and more predictable format. This is a crucial prerequisite for the next topic we'll cover: converting grammars into Chomsky Normal Form (CNF), a cornerstone for many parsing algorithms, like the ones used in compilers.

Context-Free Grammar Simplification Steps
In the last lesson, we covered 'Removal of Useless Symbols'. Today, we will address the other two simplification steps: 'Elimination of ε Production' and 'Removal of Unit Production'.

1. Eliminating ε-Productions (Null Productions)

An ε-production (or null production) is a rule of the form A → ε, where A is a variable. While useful for defining languages that include the empty string, they can complicate parsing algorithms and lead to a combinatorial explosion of derivation paths. Our goal is to remove them while preserving the language.

The core idea is to identify which variables can "vanish" (derive ε) and then explicitly create new production rules that account for their disappearance.

The Algorithm

  1. Find all nullable variables: A variable A is nullable if it can derive ε. This can happen directly (if A → ε is a rule) or indirectly (if A → BC and both B and C are nullable). We find this set iteratively, much like we found generating symbols in the last lesson.
  2. Construct new productions: For every production X → α in the grammar, we add new productions where every possible non-empty subset of nullable variables in α is removed.
  3. Finalize the grammar: The new grammar consists of all the newly created productions plus the original non-ε, non-unit productions. The original A → ε rules are discarded.

The following video provides a clear walkthrough of this algorithm.

Simplification of CFG (Removal of Null Productions)

This video from Neso Academy, "Simplification of CFG (Removal of Null Productions)", will guide us through the process. We'll start with the definition and the algorithm.

Watch from the beginning to 01:55. This section defines a null production and a nullable variable, then outlines the three-step procedure for their removal.

A Worked Example

Now, let's see this algorithm in action. The video continues with an example that demonstrates how to systematically replace nullable variables to generate the new set of productions.

Eliminating Epsilon Productions in CFG
This diagram visually summarizes the process: (1) Identify the nullable variable X, (2) Create new productions by substituting ε for X, and (3) Form the new grammar without the original ε-production.

Let's watch the full example from the Neso Academy video.

Simplification of CFG (Removal of Null Productions)

Let's watch the worked example. The grammar has two nullable variables, A and B, and the video shows how to eliminate them one by one.

Watch from 01:55 to 08:17. Pay close attention to how for a production like S → ABAC, new productions are created by considering all combinations of A vanishing: S → BAC (first A vanishes), S → ABC (second A vanishes), and S → BC (both A's vanish).

For those who appreciate the formal mathematical foundation, the algorithm to find nullable variables is equivalent to computing the set E(G) of "erasable" nonterminals. This is done using a fixed-point algorithm, as detailed in the resource below.

Chapter 3 Context-Free Grammars, ...

For a more formal treatment, this excerpt from a textbook, titled 'Chapter 3 Context-Free Grammars, ...', defines the process of finding nullable variables (called 'erasable nonterminals') and constructing the new grammar.

Read the first part of section 3.3, starting from 'The first step to eliminate ε-rules is to compute the set E(G)' down to 'All the conditions of the lemma are now met.' (pages 53-54). Note how the set E(G) is built iteratively, formalizing the intuition we've discussed.

Important Note: If the start symbol S is nullable, it means the language L(G) contains the empty string ε. In this case, we typically add a new start symbol S' with productions S' → S | ε and ensure S' does not appear on the right-hand side of any production. The simplification process is then carried out on the rest of the grammar.



2. Eliminating Unit Productions

A unit production is a rule of the form A → B, where both A and B are single variables. These rules act as simple "renaming" steps in a derivation (e.g., S ⇒ A ⇒ B ⇒ ...). They add length to derivations without generating any terminals, and our goal is to bypass them.

The Algorithm

  1. Identify unit production chains: For each variable A, find the set of all variables B such that A can derive B through a sequence of one or more unit productions (written as A ⇒* B).
  2. Add shortcut productions: For each pair (A, B) where A ⇒* B, add a new production A → α for every non-unit production B → α.
  3. Finalize the grammar: The new grammar consists of all the productions from step 2 plus all original non-unit productions. All original unit productions are removed.

Let's watch a video that explains and demonstrates this procedure.

Simplification of CFG (Removal of Unit Productions)

This Neso Academy video, "Simplification of CFG (Removal of Unit Productions)", explains the process clearly.

Watch the entire video (00:00 to 08:16). It defines unit productions, outlines the removal algorithm, and provides a comprehensive example. Notice how at the end (around 06:40), removing unit productions reveals that some symbols have become unreachable, reinforcing what we learned in the last lesson.

As with ε-productions, this process can be described more formally. The algorithm involves computing, for each nonterminal A, the set I_A of all nonterminals it can derive via unit rules. You can find this formal description in the same UPenn textbook resource (LINK, section 1, Lemma 3.3.2).



3. The Correct Order of Simplification

We have now learned three simplification techniques:

  1. Eliminating ε-productions.
  2. Eliminating unit productions.
  3. Eliminating useless symbols.

The order in which you apply these algorithms is critical. Applying them in the wrong order can lead to an incorrect or incompletely simplified grammar.

Simplifying Context Free Grammars

The article 'Simplifying Context Free Grammars' from GeeksforGeeks provides a concise note on this very important point.

Read the 'Note' at the very end of the article. It explicitly states the correct sequence.

The correct order for a full simplification is:

  1. Eliminate ε-productions.
  2. Eliminate unit productions.
  3. Eliminate useless symbols.

Why this order?

  • Eliminating ε-productions can create new unit productions. For example, if you have A → BC and C is a nullable variable, the algorithm will add the new production A → B, which is a unit production. If you had already removed unit productions, you would now have to do it again.
  • Eliminating unit productions can create useless symbols. As seen in the video LINK, after replacing unit rules, some variables might no longer be reachable from the start symbol. Therefore, the cleanup of useless symbols should happen last.
Test your understanding!

Simplify the following grammar by applying the algorithms in the correct order.

Grammar G:

  • S → A | C
  • A → aAb | ε
  • C → c

What is the final set of productions?

Show answer

Let's follow the correct three-step simplification process.

Original Grammar:

  • S → A | C
  • A → aAb | ε
  • C → c

Step 1: Eliminate ε-Productions

  1. Find nullable variables: A is nullable because of the production A → ε. S is not nullable because C is not.
  2. Create new productions: We look for productions with A on the RHS.
    • The production A → aAb contains A. When we replace A with ε, we get a new production: A → ab.
  3. Form the new grammar (G'): We add the new production and remove the ε-production.
    • S → A | C
    • A → aAb | ab (The ε rule is gone, ab is added)
    • C → c

Step 2: Eliminate Unit Productions (from G')

  1. Find unit productions: The grammar G' has two unit productions: S → A and S → C.
  2. Add shortcut productions:
    • For S → A: The non-unit productions for A are A → aAb and A → ab. So, we add S → aAb and S → ab.
    • For S → C: The non-unit production for C is C → c. So, we add S → c.
  3. Form the new grammar (G''): We combine the new productions for S and keep the other non-unit rules. The original unit productions are removed.
    • S → aAb | ab | c
    • A → aAb | ab
    • C → c

Step 3: Eliminate Useless Symbols (from G'')

  1. Remove non-generating symbols: All symbols (S, A, C, a, b, c) are generating. S generates ab or c. A generates ab. C generates c. So, no symbols are removed in this sub-step.
  2. Remove unreachable symbols:
    • Start with Reachable = {S}.
    • From S → aAb | ab | c, we can reach a, A, b, c. So, Reachable = {S, a, A, b, c}.
    • From A → aAb | ab, we can reach a, A, b (already in the set).
    • The variable C is never reached from S in the grammar G''!
  3. Form the final grammar: We remove the unreachable symbol C and its associated production C → c.

Final Simplified Grammar:

  • S → aAb | ab | c
  • A → aAb | ab


Conclusion

In this lesson, you have learned the systematic algorithms for removing two key types of "inefficient" productions from a Context-Free Grammar: ε-productions and unit productions.

Key Takeaways:

  • ε-productions (A → ε) are removed by first identifying all nullable variables and then creating new productions that represent all the ways those variables could vanish.
  • Unit productions (A → B) are removed by creating "shortcut" productions that bypass the renaming chain, directly connecting a variable to the non-unit productions of the variables it can derive.
  • The order of simplification is crucial: First eliminate ε-productions, then unit productions, and finally clean up any newly created useless symbols.

Preview of the Next Lesson:
With these simplification tools in hand, our grammars are now "clean." They are free of useless symbols, ε-productions (except for S → ε if needed), and unit productions. This clean state is the perfect starting point for our next objective: converting any CFG into an equivalent grammar in Chomsky Normal Form (CNF). This highly restricted format is fundamental to many important algorithms in computer science, including parsing and deciding membership in a context-free language.