CFG stands for context free grammar and CNF stands for Chomsky’s Normal Form in the theory of computation.
A context free grammar (CFG) is a forma grammar which is used to generate all possible patterns of strings in a given formal language.
It is defined as four tuples −
A context free grammar is in CNF, if the production rules satisfy one of the following conditions
Follow the steps given below to successfully convert the CFG to CNF: −
Step 1 − Eliminate start symbol from right hand side (RHS)
If the start symbol S is at the right-hand side of any production,
Create a production as follows −
Where, S1 is the new start symbol
Step 2 − In the grammar try to remove the null, unit and useless productions.
Step 3 − Eliminate terminals from RHS of the production if they exist with other non terminals or terminals.
Example − S->aA can be decomposed as follows −
Finally, it is nothing but S->aA only.
Step 4 − Eliminate the RHS with more than two non-terminals.
Example − S->ABS can be decomposed as given below −