Table of contents
1.
Introduction
2.
What is CNF (Chomsky Normal Form):
2.1.
Example
3.
Advantages of CNF
4.
Disadvantages of CNF
5.
What is GNF (Greibach Normal Form):
5.1.
Example
6.
Advantages of GNF
7.
Disadvantages of GNF
8.
Example with CNF
8.1.
Python
9.
Example with GNF
9.1.
Python
10.
Difference Table: CNF vs GNF
11.
Frequently Asked Questions
11.1.
Can every context-free grammar be converted to both CNF and GNF?
11.2.
Is it easier to parse a language with a grammar in CNF or GNF?
11.3.
Are CNF and GNF only applicable to theoretical aspects of computer science, or do they have practical applications?
12.
Conclusion
Last Updated: Aug 13, 2025
Easy

Difference Between CNF and GNF

Author Gaurav Gandhi
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In computer science, particularly in parsing and formal language theory, two notable concepts are the Chomsky Normal Form (CNF) and the Greibach Normal Form (GNF). These forms are integral to understanding and processing formal languages, such as those used in programming and computational linguistics. 

Difference Between CNF and GNF

This article will explore CNF and GNF in detail, discussing their definitions, advantages, disadvantages, and providing code examples to illustrate their application.

See more, Application of frequent itemset mining 

What is CNF (Chomsky Normal Form):

Chomsky Normal Form (CNF) is a way of rewriting a context-free grammar to simplify parsing without changing the language it generates. A grammar in CNF has its production rules constrained to two forms:

  • A non-terminal symbol producing two non-terminal symbols.
     
  • A non-terminal producing a terminal symbol.

This constraint simplifies parsing algorithms, such as those used in natural language processing and compiler design.

Example

Consider a grammar with the following production rules:

S → ASA | aB
A → B | S
B → b | ε


Converted into CNF, the grammar becomes:

S → AS | aB
S → AB
A → B | AS
B → b

Here, each production rule adheres to the CNF format, making the grammar simpler for parsing algorithms.

Advantages of CNF

Simplified Parsing Algorithms: CNF allows for easier implementation of parsing algorithms, such as the CYK algorithm, which is crucial in natural language processing.

Deterministic Parsing: CNF reduces ambiguity in grammars, leading to more deterministic and efficient parsing.

Theoretical Importance: CNF is essential in theoretical computer science for proving properties of context-free languages.

Disadvantages of CNF

Increased Rule Set: Converting a grammar to CNF can lead to an increase in the number of production rules, making the grammar more complex.

Potential Loss of Readability: The original structure of the grammar may be lost, reducing the readability and intuitive understanding of the grammar.

What is GNF (Greibach Normal Form):

Greibach Normal Form (GNF) is another way of expressing context-free grammars. In GNF, every production rule begins with a terminal symbol, followed by any number of non-terminal symbols. This form is particularly useful for constructing simple top-down parsers.

Example

Given a grammar:

S → aSb | ε


In GNF, it remains the same:

S → aSb | ε


This grammar in GNF illustrates how each production starts with a terminal symbol ('a') followed by non-terminals ('Sb').

Advantages of GNF

  • Ease of Top-Down Parsing: GNF is ideal for constructing top-down parsers as it ensures that every rule begins with a terminal symbol.
     
  • Reduced Left Recursion: GNF eliminates left recursion, a common obstacle in parser design.
     
  • Simplicity in Constructing Parsers: The structure of GNF makes it straightforward to construct parsers, especially for simple languages.

Disadvantages of GNF

  • Complex Conversion Process: Transforming a context-free grammar into GNF can be complex and may not always lead to the most efficient form.
     
  • Potential Increase in Rule Length: The rules in GNF may become lengthier, which can complicate the parsing process.

Example with CNF

Let's consider a simple context-free grammar and its conversion to CNF. We'll then write a Python script to demonstrate a basic parsing operation on this CNF.

Grammar:

S → aSb | ε


Converted to CNF:

S → aX | ε
X → Sb


Python Script for Basic Parsing:

  • Python

Python

def parse_CNF(string):

   n = len(string)

   table = [[set() for j in range(n)] for i in range(n)]

   # Filling in the table for substrings of length 1

   for i in range(n):

       if string[i] == 'a':

           table[i][i].add('S')

   # Filling in the table for substrings of length > 1

   for length in range(2, n + 1):

       for i in range(n - length + 1):

           j = i + length - 1

           for k in range(i, j):

               if 'S' in table[i][k] and 'X' in table[k + 1][j]:

                   table[i][j].add('S')



   return 'S' in table[0][n - 1]

# Example usage

input_string = "aabb"

result = parse_CNF(input_string)

print(f"String '{input_string}' can be generated by the CNF: {result}")
You can also try this code with Online Python Compiler
Run Code

Output

Output

This script demonstrates a simple parsing algorithm for a CNF grammar. It checks if a given string can be generated by the specified CNF grammar.

Example with GNF

For GNF, we will use the same grammar and demonstrate a top-down parsing approach.

Grammar in GNF:

 

S → aSb | ε


Python Script for Basic Parsing:

  • Python

Python

def parse_GNF(string):

   stack = ['S']

   index = 0

   while stack and index < len(string):

       top = stack.pop()

       if top == 'S':

           if index < len(string) - 1 and string[index] == 'a' and string[index + 1] == 'b':

               stack.append('b')

               stack.append('S')

               stack.append('a')

       elif top.islower() and top == string[index]:

           index += 1

       else:

           return False

   return not stack and index == len(string)

# Example usage

input_string = "aabb"

result = parse_GNF(input_string)

print(f"String '{input_string}' can be generated by the GNF: {result}")
You can also try this code with Online Python Compiler
Run Code

 

Output

output

This script uses a stack to implement a top-down parsing algorithm for a GNF grammar. It verifies if the input string can be generated by the GNF grammar.

Also read , Difference between procedural and object oriented programming

Difference Table: CNF vs GNF

Feature CNF (Chomsky Normal Form) GNF (Greibach Normal Form)
Rule Structure Non-terminal → Two non-terminals OR Non-terminal → Terminal Non-terminal → Terminal followed by any number of non-terminals
Parsing Method Suitable for bottom-up parsing methods Ideal for top-down parsing methods
Grammar Complexity Can increase the number of production rules Potentially increases the length of production rules
Recursion Allows both left and right recursion Eliminates left recursion
Use Case Used in many theoretical aspects of computer science Used in practical parser construction, especially for simple languages
Conversion Complexity Relatively straightforward conversion from general CFG More complex conversion process

Also read about, loop and while loop, procedural programming

Also see, AMD vs Intel

Frequently Asked Questions

Can every context-free grammar be converted to both CNF and GNF?

Yes, every context-free grammar can be converted to both Chomsky Normal Form (CNF) and Greibach Normal Form (GNF). These conversions, however, may result in an increase in the number of production rules for CNF or longer production rules for GNF, but the language generated by the grammar remains unchanged.

Is it easier to parse a language with a grammar in CNF or GNF?

The ease of parsing depends on the parsing technique used. CNF is better suited for bottom-up parsing algorithms like CYK, while GNF is more advantageous for top-down parsing approaches. The choice between CNF and GNF depends on the specific requirements and constraints of the parsing task.

Are CNF and GNF only applicable to theoretical aspects of computer science, or do they have practical applications?

While CNF and GNF have significant theoretical importance in formal language theory and automata, they also have practical applications. CNF is often used in the analysis of algorithms for parsing and language recognition, while GNF is used in the construction of parsers, especially simple top-down parsers for specific language processing tasks.

Conclusion

The exploration of Chomsky Normal Form (CNF) and Greibach Normal Form (GNF) in this article reveals their crucial role in the realm of formal languages and Automata theory. CNF, with its simplified parsing algorithms and deterministic parsing capabilities, and GNF, with its aptness for top-down parsing and elimination of left recursion, both offer unique advantages and pose certain challenges. Understanding these forms and their application in parsing algorithms is essential for anyone delving into language processing or compiler design. The provided code examples illustrate their practical application, offering a glimpse into how these theoretical concepts are implemented in real-world scenarios.

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass