Related Articles

Related Articles

Converting Context Free Grammar to Greibach Normal Form
  • Difficulty Level : Easy
  • Last Updated : 07 Feb, 2019

Prerequisite – Context Free Grammars, Simplifying Context Free Grammars

A context free grammar (CGF) 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)
  • Start symbol generating ε. (e.g.; S-> ε)

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 GNF as production rules satisfy the rules specified for GNF. However, the grammar G2 is not in GNF as the production rules B-> ε and A-> ε do not satisfy the rules specified for GNF (only start symbol can generate ε).
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. Convert the grammar into CNF.
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. Eliminate left recursion from grammar if it exists.
If CFG contains left recursion, eliminate them. You can refer following article to eliminate left recursion: Parsing | Set 1 (Introduction, Ambiguity and Parsers)

Step 3. Convert the production rules into GNF form.
If any production rule is not in the GNF form, convert them. Let us take an example to convert CFG to GNF. Consider the given grammar G1:

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

As G1 is already in CNF and there is not left recursion, we can skip step 1and 2 and directly move to step 3.
The production rule B->SB is not in GNF, therefore, we substitute S -> XA|BB in production rule B->SB as:

S → XA|BB 
B → b|XAB|BBB 
X → b 
A → a

The production rules S->XA and B->XAB is not in GNF, therefore, we substitute X->b in production rules S->XA and B->XAB as:

S → bA|BB 
B → b|bAB|BBB 
X → b 
A → a

Removing left recursion (B->BBB), we get:

S → bA|BB 
B → bC|bABC
C → BBC| ε
X → b 
A → a

Removing null production (C-> ε), we get:

S → bA|BB 
B → bC|bABC|b|bAB
C → BBC|BB
X → b 
A → a

The production rules S->BB is not in GNF, therefore, we substitute B → bC|bABC|b|bAB in production rules S->BB as:

S → bA| bCB|bABCB|bB|bABB 
B → bC|bABC|b|bAB
C → BBC|BB
X → b 
A → a

The production rules C->BB is not in GNF, therefore, we substitute B → bC|bABC|b|bAB in production rules C->BB as:

S → bA| bCB|bABCB|bB|bABB 
B → bC|bABC|b|bAB
C → BBC
C → bCB|bABCB|bB|bABB
X → b 
A → a

The production rules C->BBC is not in GNF, therefore, we substitute B → bC|bABC|b|bAB in production rules C->BBC as:

S → bA| bCB|bABCB|bB|bABB 
B → bC|bABC|b|bAB
C → bCBC|bABCBC|bBC|bABBC
C → bCB|bABCB|bB|bABB
X → b 
A → a

This is the GNF form for the grammar G1.

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :