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

Converting Context Free Grammar to Chomsky Normal Form

Last Updated : 21 May, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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 grammars,

G1 = {S->a, S->AZ, A->a, Z->z} 
G2 = {S->a, S->aZ, Z->a}

The grammar G1 is in CNF as production rules satisfy the rules specified for CNF. However, the grammar G2 is not in CNF as the production rule S->aZ contains terminal followed by non-terminal which does not satisfy the rules specified for CNF.

Note –

  • For a given grammar, there can be more than one CNF.
  • CNF produces the same language as generated by CFG.
  • CNF is used as a preprocessing step for many algorithms for CFG like CYK(membership algo), bottom-up parsers etc.
  • For generating string w of length ‘n’ requires ‘2n-1’ production or steps in CNF.
  • Any Context free Grammar that do not have ε in it’s language has an equivalent CNF.

How to convert CFG to CNF?

Step 1. Eliminate start symbol from RHS.
If start symbol S is at the RHS of any production in the grammar, create a new production as:
S0->S
where S0 is the new start symbol.

Step 2. Eliminate null, unit and useless productions.
If CFG contains null, unit or useless production rules, eliminate them. You can refer the this article to eliminate these types of production rules.

Step 3. Eliminate terminals from RHS if they exist with other terminals or non-terminals. e.g,; production rule X->xY can be decomposed as:
X->ZY
Z->x

Step 4. Eliminate RHS with more than two non-terminals.
e.g,; production rule X->XYZ can be decomposed as:
X->PZ
P->XY

Example – Let us take an example to convert CFG to CNF. Consider the given grammar G1:

S → ASB
A → aAS|a|Îĩ 
B → SbS|A|bb

Step 1. As start symbol S appears on the RHS, we will create a new production rule S0->S. Therefore, the grammar will become:

S0->S
S → ASB
A → aAS|a|Îĩ 
B → SbS|A|bb

Step 2. As grammar contains null production A-> Îĩ, its removal from the grammar yields:

S0->S
S → ASB|SB
A → aAS|aS|a 
B → SbS| A|Îĩ|bb

Now, it creates null production B→ Îĩ, its removal from the grammar yields:

S0->S
S → AS|ASB| SB| S
A → aAS|aS|a 
B → SbS| A|bb

Now, it creates unit production B->A, its removal from the grammar yields:

S0->S
S → AS|ASB| SB| S
A → aAS|aS|a 
B → SbS|bb|aAS|aS|a

Also, removal of unit production S0->S from grammar yields:

S0-> AS|ASB| SB| S
S → AS|ASB| SB| S
A → aAS|aS|a 
B → SbS|bb|aAS|aS|a

Also, removal of unit production S->S and S0->S from grammar yields:

S0-> AS|ASB| SB
S → AS|ASB| SB
A → aAS|aS|a 
B → SbS|bb|aAS|aS|a

Step 3. In production rule A->aAS |aS and B-> SbS|aAS|aS, terminals a and b exist on RHS with non-terminates. Removing them from RHS:

S0-> AS|ASB| SB
S → AS|ASB| SB
A → XAS|XS|a
B → SYS|bb|XAS|XS|a
X →a
Y→b

Also, B->bb can’t be part of CNF, removing it from grammar yields:

S0-> AS|ASB| SB
S → AS|ASB| SB
A → XAS|XS|a
B → SYS|VV|XAS|XS|a
X → a
Y → b
V → b

Step 4: In production rule S0->ASB, RHS has more than two symbols, removing it from grammar yields:

S0-> AS|PB| SB
S → AS|ASB| SB
A → XAS|XS|a
B → SYS|VV|XAS|XS|a
X → a
Y → b
V → b
P → AS

Similarly, S->ASB has more than two symbols, removing it from grammar yields:

S0-> AS|PB| SB
S → AS|QB| SB
A → XAS|XS|a
B → SYS|VV|XAS|XS|a
X → a
Y → b
V → b
P → AS
Q → AS

Similarly, A->XAS has more than two symbols, removing it from grammar yields:

S0-> AS|PB| SB
S → AS|QB| SB
A → RS|XS|a
B → SYS|VV|XAS|XS|a
X → a
Y → b
V → b
P → AS
Q → AS
R → XA

Similarly, B->SYS has more than two symbols, removing it from grammar yields:

S0 -> AS|PB| SB
S → AS|QB| SB
A → RS|XS|a
B → TS|VV|XAS|XS|a
X → a
Y → b
V → b
P → AS
Q → AS
R → XA
T → SY

Similarly, B->XAX has more than two symbols, removing it from grammar yields:

S0-> AS|PB| SB
S → AS|QB| SB
A → RS|XS|a
B → TS|VV|US|XS|a
X → a
Y → b
V → b
P → AS
Q → AS
R → XA
T → SY
U → XA

So this is the required CNF for given grammar.


Previous Article
Next Article

Similar Reads

Converting Context Free Grammar to Greibach Normal Form
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)
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 -> 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->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[caption width="800"]Chomsky Hierarchy[/caption] 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 s
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 --> aA|bA A --> 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 :