# Converting Context Free Grammar to Greibach Normal Form

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.

## Recommended Posts:

- Converting Context Free Grammar to Chomsky Normal Form
- Ambiguity in Context free Grammar and Context free Languages
- Ambiguity in Context free Grammar and Context free Languages
- TOC | Context-sensitive Grammar (CSG) and Language (CSL)
- Simplifying Context Free Grammars
- Check if the language is Context Free or Not
- Theory of Computation | Closure Properties of Context Free Languages
- DBMS | Introduction of 4th and 5th Normal form
- DBMS | Domain Key normal form
- Theory of Computation | Relationship between grammar and language
- Theory of Computation | Operator grammar and precedence parser
- Measure the time spent in context switch?
- Database Normalization | Normal Forms
- Mathematics | Probability Distributions Set 3 (Normal Distribution)
- Computer Network | Collision-Free Protocols

This article is contributed by **Sonal Tuteja**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.