- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Convert the given Context free grammar to CNF

## Problem

Convert the given grammar into Chomsky's Normal Form (CNF)

S->AAA|B

A->aA|B

B-> ε

Solution

Follow the steps given below to convert CFG to CNF −

**Step 1 **− Eliminate epsilon productions

To eliminate epsilon productions, the variable which is producing epsilon must be replaced on the RHS of all other productions for the epsilon production to be removed.

Replacing B with ε in all other productions gives the following production set −

S-> AAA | ε | B

A->aA | ε | B

Replacing A with \epsilon in all other productions gives the following −

S ->AAA | AA | A | B | ε [replacing 1, 2, and 3 A's with ε respectively]

A->aA | a | B

Since the grammar produces the string ε, the remaining ε -production cannot be eliminated for the grammar to remain equivalent. Removing the final ε -production gives a grammar G' which has the following relation with the original grammar G −

L(G') = L(G) -{ ε }

The productions in G' are as follows −

S -> AAA | AA | A | B

A -> aA | a | B

**Step 2 **− Eliminate unit productions

The process for eliminating unit productions is as follows −

Select a production A-> B, such that there exists non-unit production B-> a

For every non-unit production, B-> a repeat the following step −

Add production A->a to the grammar.

Eliminate A->B from the grammar.

Eliminating the unit-production S-> A from the grammar, the following grammar is obtained −

S ->AAA | AA | aA | a | B

A -> aA | a | B

There is no production for B, hence the unit productions of type V -> B cannot be eliminated.

**Step 3 −** Useless symbols

B is a useless symbol, as it has no productions. So it can be removed.

A-> a results in producing a terminal string, and it is reachable from S, hence A is not useless

Similarly S is not useless as terminals can be produced from it.

So the resultant CFG after removing useless symbols is −

S->AAA | AA | aA | a

A->aA | a

## Chomsky Normal Form (CNF)

To convert the grammar into Chomsky normal form, all productions with more than two elements on the right hand side, need to be split into two or more productions by adding more variables.

Terminals in productions with at least two symbols on the right hand side need to be replaced by a variable symbol.

The following production is added to produce terminal a −

Z->a

The resultant grammar is as follows −

S-> AAA | AA | ZA | a

A-> ZA | a

Z -> a

Production S ->AAA is modified by adding an additional variable, T.

This gives the following grammar −

S -> TA | AA | ZA | a

T ->AA

A -> ZA | a

Z -> a

In the above grammar, all productions are of the form A -> BC or A ->b. Therefore, it is in Chomsky normal form (CNF).

- Related Questions & Answers
- Generate a CNF for a given context free grammar
- What is Context-Free Grammar?
- How to convert context free grammar to push down automata?
- How to generate the language for context free grammar?
- Explain the simplification of context free grammar in TOC
- What is context free grammar? Explain with examples
- Explain about CYK Algorithm for Context Free Grammar
- Explain removing unit productions in context free grammar
- Generate a Context-free grammar for the language L = {anbm| m≠n}?
- What is Context-sensitive Grammar?
- Explain how to convert CFG to CNF
- Explain the context free language closure under concatenation?
- What are the closure properties for context free language?
- Explain the context free language closure under union operation?
- Explain Pumping lemma for context free language