The Wayback Machine - https://web.archive.org/web/20240830022403/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
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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
Context-free grammar (CFG) and Greibach Normal Form (GNF) are fundamental concepts in formal language theory, particularly in the field of compiler design and automata theory. This article delves into what CFG and GNF are, provides examples, and outlines the steps to convert a CFG into GNF. What is Context Free Grammar?Context-Free Grammar (CFG) is
6 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 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 --> 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 non-terminal symbols.T - It is a set of terminals. P - It is the production rules that consist of both term
3 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
Computational Complexity v/s Chomsky Hierarchy
1. Computational complexity :Computational complexity, a measure of the number of computing resources(time and space) that a particular algorithm consumes when it runs. The time complexity of a Turing machine is given by the function T(n), where T(n) = Maximum number of moves made by the TM to process a string of length n. The space complexity of t
4 min read
Chomsky Hierarchy in Theory of Computation
According to Chomsky hierarchy, grammar is divided into 4 types as follows: Type 0 is known as unrestricted grammar.Type 1 is known as context-sensitive grammar.Type 2 is known as a context-free grammar.Type 3 Regular Grammar.Type 0: Unrestricted Grammar: Type-0 grammars include all formal grammar. Type 0 grammar languages are recognized by turing
2 min read
Check if the language is Context Free or Not
Prerequisite - Pumping Lemma, How to identify if a language is regular or not We typically face questions to identify which of the given languages are context free. In case of regular languages, it is comparatively easy to answer this, but for Context Free languages, it is tricky sometimes. Pumping Lemma provides us with ability to perform negative
4 min read
Various Properties of context free languages (CFL)
Context-Free Language (CFL) is a language which is generated by a context-free grammar or Type 2 grammar(according to Chomsky classification) and gets accepted by a Pushdown Automata. Some very much important properties of a context-free language is: Regularity- context-free languages are Non-Regular PDA language. Closure properties : The context-f
2 min read
Classification of Context Free Grammars
Context Free Grammars (CFG) can be classified on the basis of following two properties: 1) Based on number of strings it generates. If CFG is generating finite number of strings, then CFG is Non-Recursive (or the grammar is said to be Non-recursive grammar)If CFG can generate infinite number of strings then the grammar is said to be Recursive gramm
2 min read
Closure Properties of Context Free Languages
Context Free Languages (CFLs) are accepted by pushdown automata. Context free languages can be generated by context free grammars, which have productions (substitution rules) of the form : A -> ? (where A ? N and ? ? (T ? N)* and N is a non-terminal and T is a terminal) Properties of Context Free Languages Union : If L1 and L2 are two context fr
9 min read
Simplifying Context Free Grammars
The definition of context free grammars (CFGs) allows us to develop a wide variety of grammars. Most of the time, some of the productions of CFGs are not useful and are redundant. This happens because the definition of CFGs does not restrict us from making these redundant productions. By simplifying CFGs we remove all these redundant productions fr
6 min read
Domain Key Normal Form in DBMS
Prerequisites - Normal Forms, 4th and 5th Normal form, find the highest normal form of a relation It is basically a process in database to organize data efficiently. Basically there are two goals of doing normalization these are as follows: To remove repeated data or in simple words we can say to remove redundant data.Second one is to ensure that t
4 min read
Introduction of 4th and 5th Normal Form in DBMS
Two of the highest levels of database normalization are the fourth normal form (4NF) and the fifth normal form (5NF). Multivalued dependencies are handled by 4NF, whereas join dependencies are handled by 5NF. If two or more independent relations are kept in a single relation or we can say multivalue dependency occurs when the presence of one or mor
5 min read
First Normal Form (1NF)
If a table has data redundancy and is not properly normalized, then it will be difficult to handle and update the database, without facing data loss. It will also eat up extra memory space and Insertion, Update, and Deletion Anomalies are very frequent if the database is not normalized. Normalization is the process of minimizing redundancy from a r
3 min read
Minimum Relations Satisfying First Normal Form (1NF)
A relation that does not contain any composite or multivalued attribute, then the relation is in its First Normal Form. Relations that contain a single-valued attribute comes under First Normal Form. In this article, we will be going to discuss the minimum relations satisfying the First Normal Form. Before proceeding to First Normal Form, let's dis
4 min read
Third Normal Form (3NF)
Although Second Normal Form (2NF)relations have less redundancy than those in 1NF, they may still suffer from update anomalies. If we update only one tuple and not the other, the database will be in an inconsistent state. This update anomaly is caused by a transitive dependency. We need to remove such dependencies by progressing to the Third Normal
3 min read
How to find the highest normal form of a relation
To understand this topic, you should have a basic idea about Functional Dependency & Candidate keys and Normal forms .Steps to find the highest normal form of relation: Find all possible candidate keys of the relation.Divide all attributes into two categories: prime attributes and non-prime attributes.Check for 1st normal form then 2nd and so o
5 min read
Second Normal Form (2NF)
First Normal Form (1NF) does not eliminate redundancy, but rather, it's that it eliminates repeating groups. Instead of having multiple columns of the same kind of data in a record, (0NF or Unnormalized form) you remove the repeated information into a separate relation and represent them as rows. This is what constitutes 1NF. Second Normal FormThe
4 min read
Boyce-Codd Normal Form (BCNF)
Pre-Requisite: First Normal Form, Second Normal Form, Third Normal Form Application of the general definitions of 2NF and 3NF may identify additional redundancy caused by dependencies that violate one or more candidate keys. However, despite these additional constraints, dependencies can still exist that will cause redundancy to be present in 3NF r
6 min read
Relationship between grammar and language in Theory of Computation
A grammar is a set of production rules which are used to generate strings of a language. In this article, we have discussed how to find the language generated by a grammar and vice versa as well. Language generated by a grammar - Given a grammar G, its corresponding language L(G) represents the set of all strings generated from G. Consider the foll
4 min read
Operator grammar and precedence parser in TOC
A grammar that is used to define mathematical operators is called an operator grammar or operator precedence grammar. Such grammars have the restriction that no production has either an empty right-hand side (null productions) or two adjacent non-terminals in its right-hand side. Examples - This is an example of operator grammar: E->E+E/E*E/id H
6 min read
Removing Direct and Indirect Left Recursion in a Grammar
Prerequisite - Classification of Context Free Grammars, Ambiguity and Parsers Introduction: Left recursion is a common problem that occurs in grammar during parsing in the syntax analysis part of compilation. It is important to remove left recursion from grammar because it can create an infinite loop, leading to errors and a significant decrease in
10 min read
Difference between Ambiguous and Unambiguous Grammar
Prerequisite - Context Free Grammars 1. Ambiguous Grammar : A context-free grammar is called ambiguous grammar if there exists more than one derivation tree or parse tree. Example - S -> S + S / S * S / S / a 2. Unambiguous Grammar : A context-free grammar is called unambiguous grammar if there exists one and only one derivation tree or parse tr
2 min read
Introduction To Grammar in Theory of Computation
Prerequisite - Theory of ComputationGrammar :It is a finite set of formal rules for generating syntactically correct sentences or meaningful correct sentences.Constitute Of Grammar :Grammar is basically composed of two basic elements - Terminal Symbols -Terminal symbols are those which are the components of the sentences generated using a grammar a
3 min read
Practice Tags :