I am trying to learn how to convert any context free grammar to Chomsky Normal Form. In the example below, I tried to apply Chomsky Normal Form logic, to result in a grammar, where every symbol either produces two symbols, or every symbol produces a terminal.
I am not %100 sure my implementation is correct, and it is important that I understand how to do this, I would appreciate if someone would let me know if I am on the right track.
Context Free Grammar
S -> ASA | aB
A -> B | S
B -> b | epsilon
After converting to:
Chomsky Normal Form
S-> CA | CB
C -> AS | a | S
B -> b | epsilon
Many Thanks in advance!
Conversion from Context Free Grammar to Chomsky Normal Form : (I ll tell you the steps and will also solve the example you asked simultaneously)
Step 1 : Introduce New Non-terminal $S_0$ and make it derive the start variable which is S
Thus
Step 2 : Eliminate all $\varepsilon$ transitions
THus we need to eliminate B -> $\varepsilon$
For this we must to replace B with $\varepsilon$ in RHS of every production having B
THus we get,
Now new $\varepsilon$ transition is introduced which is A -> $\varepsilon$ .. thus we need to eliminate it too
Step 3 : Eliminate all Unit transitions i.e. those productions having exactly one non-terminal in RHS .
thus we need to eliminate A -> B , A->S ,$S_0$ -> S
THus,first removing A-> B
NOw removing A-> S
NOw removing $S_0$ -> S
Step 4 : Now eliminate all the productions that are non in CNF
The above CFG is in CNF .
:)