Simplification of Context Free Grammar

Context Free Grammar has recursive structure. The languages that are accepted with Context Free Grammar are called Context Free Languages. Context Free Grammar has one condition for production rules, i.e., on the left-hand side of each rule, there must be only single variable, and on the right-hand side, there may be combination of variables and terminals including ε.

In Context Free Grammar, sometimes all the productions rules and symbols are not needed for the derivation to solve. Some productions rules are never used during derivation of any string. Elimination of these types of productions or symbols is called Simplification of Context Free Grammar.

We will use the various simple methods to simplify the given context free grammar without changing the resulting language. The following types of productions rules are never used during derivation of any string from context free grammar:

  • Useless productions
  • Null productions
  • Unit productions

Useless productions/ Reduced Context Free Grammar

The productions in context free grammar that contains useless symbols are called Useless productions. The grammar that we obtain after deleting useless production rules are called reduced Context Free Grammar. Our main focus is to remove productions and symbols that are never used in any derivation.

Besides this, it may also contain some null production or unit production in context free grammar.

How to eliminate useless productions?

To eliminate useless productions, we apply following two steps:

Step 1: In step1, we will construct a new grammar equivalent to given grammar. Every variable in new grammar derives some terminal string.

Step 2: In step2, we construct a new grammar equivalent to the grammar obtain in step1. Every symbol in new grammar appears in some sentential form.

Example:

S -> ABC|a

A -> b

B -> c

C -> d

E -> e

F -> f

G -> g

In this grammar G, we have useless productions i.e.

E -> e

F -> f

G -> g

After eliminating we have reduced grammar whose productions rules are:

S -> ABC|a

A -> b

B -> c

C -> d

Elimination of Null Productions/Removal of Null Production:

In Context Free Grammar, the productions of the form B -> ε are called null productions. It is not always possible to eliminate all null productions in context free grammar because sometimes eliminating all null productions may change the resulting language.

Example:

Consider the context free grammar, whose productions are

S -> aS

S -> aA

S -> ε

A -> ε

In these productions, we have two null productions

S -> ε and A -> ε. We can eliminate one production A -> ε, the resulting grammar G1 will be:

S -> aS

S -> aA

S -> ε

This does not change the language defined by given context free grammar. But if we delete S -> ε, then we will be unable to get the language

L (G1) = L (G) = {an | n>=0}

Thus, it is possible to eliminate production A -> ε, but if we eliminate S -> ε, we are unable to generate ε in L (G). We can eliminate all null productions in context free grammar, if ε is not derived by L (G). If ε is in L (G), then G must have some ε productions.

Elimination of Unit Productions/ Removal of unit Production:

If any given Context free Grammar production in the form of

A -> B

Where A and B are variables, then this type of production is called unit production (or chin rule).

Example:

If we have following set of production rules:

S -> B

B -> C

C -> D

D -> E

E -> F

F -> G

G -> 01

The language defined by grammar is L (G) = 01.

Solution:

Let us see the derivation of string 01 from given grammar.

S => B

   => C

  => D

  => E

  => F

  => G

  => 01

All productions like

B -> C

C -> D

D -> E

E -> F

F -> G

G -> 01

Are useful for replacing B with G.

As G -> 01

L (G) = 01

We can write above productions as:

 S -> 01

L (G) = 01

Thus we have L (G1) =  L (G) = 01

Pin It on Pinterest

Share This