The Wayback Machine - https://web.archive.org/web/20240607083332/https://www.geeksforgeeks.org/converting-context-free-grammar-greibach-normal-form/
Open In App

Converting Context Free Grammar to Greibach Normal Form

Last Updated : 20 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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.

1.
A1 → A2A3 | A4A4
A2 → b
A3 → a
A4 → b | A2A3A4 | A4A4A4
----------------------
2.
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) 

OR 

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.


Previous Article
Next Article

Similar Reads

Converting Context Free Grammar to Chomsky Normal Form
Prerequisite - Simplifying Context Free Grammars A context free grammar (CFG) is in Chomsky Normal Form (CNF) if all production rules satisfy one of the following conditions: A non-terminal generating a terminal (e.g.; X->x) A non-terminal generating two non-terminals (e.g.; X->YZ) Start symbol generating ε. (e.g.; S-> ε) Consider the following gra
4 min read
Ambiguity in Context free Grammar and Context free Languages
Before reading this article, we recommend you to first read about Pushdown Automata and Context Free Languages. Suppose we have a context free grammar G with production rules : S -&gt; aSb | bSa | SS | e Left Most Derivation (LMD) and Derivation Tree : Leftmost derivation of a string from starting symbol S is done by replacing leftmost non-terminal
5 min read
Ambiguity in Context free Grammar and Context free Languages
Prerequisite - Pushdown Automata and Context Free Languages . Suppose we have a context free grammar G with production rules: S-&gt;aSb|bSa|SS|? Left most derivation (LMD) and Derivation Tree: Leftmost derivation of a string from starting symbol S is done by replacing leftmost non-terminal symbol by RHS of corresponding production rule. For example
4 min read
Difference between Context Free Grammar and Regular Grammar
Noam Chomsky has divided grammar into four types : Type Name 0 Unrestricted Grammar1 Context Sensitive Grammar2 Context Free Grammar3 Regular Grammar 1. Context Free Grammar : Language generated by Context Free Grammar is accepted by Pushdown AutomataIt is a subset of Type 0 and Type 1 grammar and a superset of Type 3 grammar.Also called phase stru
2 min read
Removal of ambiguity (Converting an Ambiguous grammar into Unambiguous grammar)
Prerequisites : Context Free Grammars , Ambiguous Grammar, Difference between ambiguous and unambiguous grammar, Precedence and Associativity of operators, Recursive GrammarIn this article we are going to see the removal of ambiguity from grammar using suitable examples. Ambiguous vs Unambiguous Grammar :The grammars which have more than one deriva
6 min read
Regular Expression Vs Context Free Grammar
Regular Expressions are capable of describing the syntax of Tokens. Any syntactic construct that can be described by Regular Expression can also be described by the Context free grammar. Regular Expression: (a|b)(a|b|01) Context-free grammar: S --&gt; aA|bA A --&gt; aA|bA|0A|1A|e *e denotes epsilon. The Context-free grammar form NFA for the Regular
2 min read
CYK Algorithm for Context Free Grammar
Prerequisite - Converting Context Free Grammar to Chomsky Normal Form CYK algorithm is a parsing algorithm for context free grammar. In order to apply CYK algorithm to a grammar, it must be in Chomsky Normal Form. It uses a dynamic programming algorithm to tell whether a string is in the language of a grammar. Algorithm : Let w be the n length stri
2 min read
What is Context-Free Grammar?
Context Free Grammar is formal grammar, the syntax or structure of a formal language can be described using context-free grammar (CFG), a type of formal grammar. The grammar has four tuples: (V,T,P,S). V - It is the collection of variables or nonterminal symbols.T - It is a set of terminals. P - It is the production rules that consist of both termi
2 min read
Context-sensitive Grammar (CSG) and Language (CSL)
Context-Sensitive Grammar - A Context-sensitive grammar is an Unrestricted grammar in which all the productions are of form - Where α and β are strings of non-terminals and terminals. Context-sensitive grammars are more powerful than context-free grammars because there are some languages that can be described by CSG but not by context-free grammars
2 min read
Difference between Thread Context Switch and Process Context Switch
A program is a set of instructions that is aimed at performing specific tasks.A program is therefore a passive entity. A program that is executing (under action) is called a process. Thus a process is an active entity. When a program runs then it may contain various threads run accordingly. In single threaded processes, the thread itself is the pro
2 min read
Practice Tags :