Design an unambiguous CFG in CNF that generates E?

Data Structure AlgorithmsComputer ScienceComputers

Problem

Define the language, E={aibj|i not equal to j and i not equal to 2j} and design an unambiguous context free grammar (CFG) in Chomsky normal form (CNF) that generates E.

Solution

The unambiguous CFG for the given language is as follows −

S->AC|CB

A->aA|a

B->Bb|b

C->aCb|aaCb|epsilon

Now, convert this CFG into CNF. You can follow the below mentioned steps for the successful conversion.

Step 1

First add a new start symbol S0

   S0->S

   S->AC|CB

   A->aA|a

   B->Bb|b

   C->aCb|aaCb|epsilon

Step 2

Next eliminate the epsilon symbol in the production other than the start symbol.

   C->epsilon is a null production

After eliminating null production, the new productions are as follows −

   S0->S

   S->AC|CB

   A->aA|a

   B->Bb|b

   C->aCb|aaCb|ab|aab

Step 3

Now remove the unit production, if it exists

S0->S is a unit production

Replace S, with S production

   S0->AC|CB

   S->AC|CB

   A->aA|a

   B->Bb|b

   C->aCb|aaCb|ab|aab

Step 4

There are no useless productions to remove.

Step 5

Now check the productions that are not in CNF, If not, let's convert into CNF

A context free grammar is in CNF, if the production rules satisfy one of the following conditions −

  • If there is start Symbol generating ε. Example − A->
  • If a non-terminal generates two non-terminals. Example − S->AB
  • If a non-terminal generates a terminal. Example − S->a

Now, our grammar that doesn't follow the rules given below −

   A->aA

   B->Bb

   C->aCb|aaCb

Convert this production into CNF as shown below −

   A->XA

   B->BY

   C->XCY|XXCY

   X->a

   Y->b

Still C->XCY|XXCY violating rules because RHS there are more than two symbols So, let's decompose the production

   C->XCY

   C->RY

   R->XC

And

   C->XXCY

   C->LM

   L->XX

   M->CY

The CNF that generates E is as follows −

   S0->AC|CB

   S->AC|CB

   A->XA|a

   B->BY|b

   C->RY

   C->LM

   X->a

   Y->b

   R->XC

   L->XX

   M->CY

Now, each production is generating either a terminal or two non-terminals.

raja
Published on 16-Jun-2021 12:27:06
Advertisements