Open In App
Related Articles

Converting Context Free Grammar to Greibach Normal Form

Like Article
Save Article
Report issue

Prerequisite – Context Free Grammars, Simplifying Context Free Grammars A context free grammar (CFG) is in Greibach Normal Form (GNF) if all production rules satisfy one of the following conditions:

  • A non-terminal generating a terminal (e.g.; X->x)
  • A non-terminal generating a terminal followed by any number of non-terminals (e.g.; X->xX1X2…XN)

Consider the following grammars:

G1 = {S->aA|bB, B->bB|b, A->aA|a} 
G2 = {S->aA|bB, B->bB|ε, A->aA|ε}

The grammar G1 is in CNF as production rules satisfy the rules specified for CNF so it can be directly used to convert to GNF. According to the rules G1 is also in GNF form. However, the grammar G2 is not in CNF as the production rules B -> ε and A-> ε do not satisfy the rules specified for CNF (only start symbol can generate ε), so first remove the unit and null production and convert it into GNF. 

Note –

  • For a given grammar, there can be more than one GNF
  • GNF produces the same language as generated by CFG

How to convert CFG to GNF – 

Step 1. If the given grammar is not in CNF, convert it to CNF. You can refer following article to convert CFG to CNF: Converting Context Free Grammar to Chomsky Normal Form 

Step 2. Change the names of non terminal symbols to A1 till AN in same sequence.

Step 3. Check for every production rule if RHS has first symbol as non terminal say Aj for the production of Ai, it is mandatory that i should be less than j. Not great and not even equal.

If i> j then replace the production rule of Aj at its place in Ai.

If i=j, it is the left recursion. Create a new state Z which has the symbols of the left recursive production, once followed by Z and once without Z, and change that production rule by removing that particular production and adding all other production once followed by Z.

Step 4. Replace very first non terminal symbol in any production rule with its production until production rule satisfies the above conditions.

For converting a CNF to GNF always move left to right for renaming the variables.

Example: Suppose this the production and we need to convert it into GNF.

S → XA|BB 
B → b|SB 
X → b 
A → a

For converting a CNF to GNF first rename the non terminal symbols to A1,A2 till AN in same sequence as they are used. 

A1 = S
A2 = X
A3 = A
A4 = B

Therefore, now the new production rule is, 

A1 → A2A3 | A4A4
A2 → b
A3 → a
A4 → b | A1A4

Now, check for every production Ai → Aj X, where  X can be any number of terminal symbols. If i<j in the production then it is good to go to the next step but if i>=j then change the production by replacing it with that terminal symbol’s production. if i=j then it is a left recursion and you need to remove left recursion.

Here for A4, 4 !< 1, so now replace it with A1‘s production rule.

A1 → A2A3 | A4A4
A2 → b
A3 → a
A4 → b | A2A3A4 | A4A4A4
A1 → A2A3 | A4A4
A2 → b
A3 → a
A4 → b | bA3A4 | A4A4A4

Here A4A4A4 in production rule of A4 is the example of left recursion. 

To replace the left most recursion take a new Non terminal symbol Z, which has the X part or the trailing part of the left most recursive production once followed by Z and once without Z. Here in A4A4A4, the part after the first A4 is A4A4, therefore  

Z → A4A4 | A4A4Z

Now change the above production rule by putting Z after every previous production of that Ai, and remove the left recursive production.

A1 → A2A3 | A4A4
A2 → b
A3 → a
A4 → b | bA3A4 | bZ | bA3A4Z
Z → A4A4 | A4A4Z

The Last step is to replace the production to the form of either 

Ai → x (any single terminal symbol) 


Ai → xX (any single non terminal followed by any number of non terminals)

So here we need to replace A2 in production rule of A1 and so on.

A1 → bA3 | bA4 | bA3A4A4 | bZA4 | bA3A4ZA4
A2 → b
A3 → a
A4 → b | bA3A4 | bZ | bA3A4Z
Z → bA4 | bA3A4A4 | bZA4 | bA3A4ZA4 | bA4Z | bA3A4A4Z | bZA4Z | bA3A4ZA4Z

The respective grammar is non in GNF form.

Last Updated : 20 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Similar Reads