CFG Converter
Theory
A grammar is a set of production rules which are used to generate strings of a language.
We can converts context-free grammars to different normal forms.
Simplifying Context Free Grammars
By simplifying CFGs we remove all the redundant productions from a grammar , while keeping the transformed grammar equivalent to the original grammar. Two grammars are called equivalent if they produce the same language. Simplifying CFGs is necessary to later convert them into Normal forms.
Types of redundant productions
Useless productions
The productions that can never take part in derivation of any string , are called useless productions. Similarly , a variable that can never take part in derivation of any string is called a useless variable.S -> abS | abA | abB
A -> cd
B -> aB
C -> dc
production ‘C -> dc’ is useless because the variable ‘C’ will never occur in derivation of any string. The other productions are written in such a way that variable ‘C’ can never reached from the starting variable ‘S’.
Production ‘B ->aB’ is also useless because there is no way it will ever terminate . If it never terminates , then it can never produce a string. Hence the production can never take part in any derivation.
To remove useless productions , we first find all the variables which will never lead to a terminal string such as variable ‘B’. We then remove all the productions in which variable ‘B’ occurs. So the modified grammar becomes –
S -> abS | abA A -> cd C -> dc
Try to identify all the variables that can never be reached from the starting variable such as variable ‘C’. We then remove all the productions in which variable ‘C’ occurs. The grammar below is now free of useless productions –
Unit productions
The productions of type ‘X -> Y’ are called unit productions.
Example:
S->0A|1B|C
A->0S|00
B->1|A
C->01>
step1:
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
we can write CFG without unit production as follows −
S->0A|1B|01
A->0S|00
B->1|0S|00
C->01
Chomsky normal form (CNF)
A context free grammar is in chomsky normal form if every rule is of the form
where a is any terminal and A,B are any non-terminals (variables) execpt that B and C may not be the start variable. in addition we permit the rule S-> ε,where S is the start variable
Greibach normal form (CNF)
A Context Free Grammar (CFG) is said to be in Greibach Normal Form(GNF), if production rules satisfy one of the following criteria −