What is Greibach's normal form?
Overview
We use normal forms in advanced topics of computation theory. Greibach's normal form and Chomsky's normal form are two methods of simplifying context-free grammar.
Greibach's normal form (GNF)
Grammar is in GNF if all the productions follow either of the following set of rules:
- A nonterminal produces a terminal.
- A nonterminal produces a terminal with a string of nonterminal symbols.
Steps to convert grammar to GNF
The following steps convert context-free grammar into Greibach's normal form.
- Convert CFG to CNF.
- Remove all the indirect and direct left recursions that occur in the productions, if they exist.
- Convert all production rules into the GNF form. This is usually done by changing the names of the nonterminal symbols into
form, in ascending order of .
Recursion may also occur after step 3; regardless, it must be removed.
Note: To learn more about CNF, visit here.
Example
Let's consider the following grammar:
- This grammar is already in CNF.
- After converting the nonterminals in the
form, grammar becomes:
We'll appoint the values in order of the occurrence of the nonterminals. In this grammar
- We'll adjust the rules so that the nonterminals are now in ascending order. This is done when the production is in the form of
, where is a terminal or a nonterminal and and should not be . - The first production follows this property as
and .
- In the second production
. We will replace with the rules it produces.
- We'll check again if
in the new step. Since , we will replace the rules produced by .
The production
- Now we check again if
. Again as well as an occurrence of left recursion since . We'll now remove the left recursion from this production.
The grammar now becomes:
Note: To learn how to remove left recursion, visit here.
- Now the entire grammar looks like this:
- Now we know that in GNF, we need to have a terminal at the beginning of a rule, not a nonterminal, and a nonterminal exists only after a terminal. So we replace
, and by the rules, they produce for the first production, i.e., .
- Now,
and are in GNF. We'll now convert to GNF.
Now we have finally converted a CFG to GNF.
In conclusion,
Every grammar in GNF is context-free, and every CFG can be converted into GNF.
Free Resources