Prerequisite – Context Free Grammars, Simplifying Context Free Grammars
A context free grammar (CGF) is in Geribach 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->X1X2…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.
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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Computer Network | Open shortest path first (OSPF) protocol States
- Compiler Design | Syntax Directed Definition
- Computer Network | Open shortest path first (OSPF) protocol fundamentals
- Types of computer memory (RAM and ROM)
- Computer Network | Address Resolution in DNS
- Advanced master theorem for divide and conquer recurrences
- Construct Pushdown Automata for all length palindrome
- Cauchy’s Mean Value Theorem
- DBMS | Concurrency Control Protocol | Multiple Granularity Locking
- Computer Network | Features of Enhanced Interior Gateway Routing Protocol (EIGRP)
Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.