Explain removing unit productions in context free grammar

Data Structure AlgorithmsComputer ScienceComputers

All grammars are not always optimized, which means the grammar may consist of some extra symbols (non-terminals) which increase the length of grammar.

So, we have to reduce the grammar by removing the useless symbols.

Properties

The properties to reduce grammar are explained below −

  • Each non-terminal and terminal of G appears in the derivation of some word in L.
  • There should not be any production as X->Y where X and Y are non-terminals.
  • If epsilon is not in language L then there need not be in the production X-> ε.

The diagram given herewith describe the properties to reduce grammar −

The unit productions are the productions in which one non-terminal gives another nonterminal

Remove unit production

The steps to remove the unit production are given below −

  • Step 1 − To remove X->Y add production X->a to the grammar rule whenever Y->a occurs in the grammar.
  • Step 2 − Now delete X->Y from the grammar
  • Step 3 − Repeat Step 1 and 2 until all unit productions are removed

Example

Consider the context free grammar given below and remove unit production for the same.

S->0A|1B|C

A->0S|00

B->1|A

C->01

Explanation

Step 1

S->C is unit production but while removing S->C we have to consider what C gives so we can add a rule to S.

   S->0A|1B|01

Step 2

B->A is also unit production

   B->1|0S|00

Finally, we can write CFG without unit production as follows −

S->0A|1B|01

A->0S|00

B->1|0S|00

C->01

raja
Published on 16-Jun-2021 12:37:48
Advertisements