Removal of Useless Symbols in CFG



Context-free Grammars are crucial in many applications including compiler design. In defining the syntax of programming languages and other structured data using CFGs, it's often important to simplify them without altering the language they generate.

In this chapter, we will see the process of removing useless productions from a CFG and their importance in details.

What are Useless Productions?

A production in a CFG is can be termed as "useless" if it doesn't contribute to the derivation of any terminal string in the language. These productions can be seen as unwanted that complicates the grammar without adding any generative power. Our goal is to identify and eliminate these useless productions.

Two Key Principles: Derivability and Reachability

The process of removing useless productions relies on two fundamental principles −

  • Derivability − This idea focuses on whether a variable within the grammar can actually derive a terminal string. If a variable cannot produce a string composed solely of terminals, it's considered non-deriving and thus, useless.
  • Reachability − This idea checks whether a variable can be reached from the start variable of the grammar. If a variable is unreachable from the start variable, it implies that it can never participate in the derivation process and it is useless.

Steps to Remove Useless Productions

Let us see the steps for removing useless productions. Let's break down the process of removing useless productions into a series of clear steps −

Step 1: Identifying Useful Symbols

Terminals − As we know, terminals are the end symbols. The set of terminal symbols in the grammar forms the foundation of our useful symbols. This is because terminals are the building blocks of the strings generated by the grammar.

Deriving Variables − For each terminal symbol, identify the variables that directly derive them. These variables are also considered useful because they contribute to generating terminal strings.

Iteratively Expand the Set − Now continue examining the right-hand sides of productions for the newly identified useful variables. If a production contains a combination of terminals and/or already marked useful variables, the variable on the left-hand side is also useful.

Identify Useless Symbols − Any variable not marked as useful after this iterative process is considered useless.

Step 2: Eliminating Productions with Useless Symbols

Right-Hand Side Scan − Check the right-hand side of each production. If any useless symbol (terminal or variable) appears in the production, the entire production is useless and should be removed.

Step 3: Ensuring Reachability from the Start Variable

Trace from the Start − Starting from the start variable, follow the derivation paths allowed by the remaining productions. Any variable that cannot be reached through these derivations is considered unreachable.

Remove Unreachable Productions − Eliminate any productions containing unreachable variables on either the left-hand side or right-hand side.

Example of Removing Useless Symbols in CFG

Now, we will see the idea through a real example, let's consider a CFG with the following productions −

$$\mathrm{S\:\rightarrow\: aA \:|\: BC}$$

$$\mathrm{A\:\rightarrow\: b \:|\: bB}$$

$$\mathrm{B\:\rightarrow\: aB \:|\: \varepsilon}$$

$$\mathrm{C\:\rightarrow\: aC \:|\: D}$$

$$\mathrm{D\:\rightarrow\: bD}$$

Let's apply the steps to remove useless productions.

Step 1: Identify Useful Symbols

  • Terminals − {a, b}
  • Deriving Variables − 'A' derives 'b', 'B' derives 'ε' (which eventually leads to a string of terminals), 'C' derives 'a'. So, {A, B, C} are also useful.
  • Iterative Expansion − 'S' derives 'aA' (combination of useful symbols). Therefore, {S} is useful.

Step 2: Eliminate Productions with Useless Symbols

Useless Symbols − {D}

We eliminate the following productions containing 'D' −

$$\mathrm{C \: \rightarrow \: D}$$

$$\mathrm{D \: \rightarrow \: bD}$$

The grammar now becomes −

$$\mathrm{S \: \rightarrow \: aA \: |\: BC}$$

$$\mathrm{A \: \rightarrow \: b \: |\: bB}$$

$$\mathrm{B \: \rightarrow \: aB \: |\: \varepsilon}$$

$$\mathrm{C \: \rightarrow \: aC}$$

Step 3: Ensure Reachability

Reachable − From 'S', we can reach 'A' and 'B'. From 'A' we can reach 'B'. From 'C' we cannot reach any other variable.

Unreachable − 'C' is unreachable from the start variable 'S'.

Removing Unreachable Productions

$$\mathrm{S \: \rightarrow \: BC}$$

$$\mathrm{C \: \rightarrow \: aC}$$

The final simplified grammar becomes −

$$\mathrm{S \: \rightarrow \: aA}$$

$$\mathrm{A \: \rightarrow \: b \: |\: bB}$$

$$\mathrm{B \: \rightarrow \: aB \: |\: \varepsilon}$$

Conclusion

Removing useless productions is a crucial step while making our context free grammar simple. By using such principles like derivability and reachability, we can systematically identify and eliminate productions that don't contribute to the language generated by the grammar.

Advertisements