3/27/25, 11:36 PM Converting Context Free Grammar to Greibach Normal Form | GeeksforGeeks
Converting Context Free Grammar to Greibach
Normal Form
Last Updated : 05 Aug, 2024
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 a set of recursive rewriting rules used to
generate patterns of strings. A CFG consists of a set of production rules
that describe all possible strings in a given formal language.
What is the Greibach Normal Form?
A 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→xX→x)
A non-terminal generates a terminal followed by any number of non-
terminals (e.g., X→xX1X2…XNX→xX1X2…XN)
Consider the following grammar:
G1={S→aA∣bB,B→bB∣b,A→aA∣a}G1={S→aA∣bB,B→bB∣b,A→aA∣a}
G2={S→aA∣bB,B→bB∣ϵ,A→aA∣ϵ}G2={S→aA∣bB,B→bB∣ϵ,A→aA∣ϵ}
We use cookies
The grammarto ensure
G1 you
Ghave
1 istheinbestCNF
browsing
as experience on our website.
production rules By using ourthe
satisfy site, you
rules
acknowledge that you have read and understood our Cookie Policy & Privacy Policy
specified for CNF, so it can be directly used to convert to GNF.
Got It !
[Link] 1/11
3/27/25, 11:36 PM Converting Context Free Grammar to Greibach Normal Form | GeeksforGeeks
According to the rules, G1G1 is also in GNF form. However, the
grammar G2G2 is not in CNF as the production rules B→ϵB→ϵ and
A→ϵA→ϵ do not satisfy the rules specified for CNF (only the start
symbol can generate ϵϵ), so first remove the unit and null production
and convert it into GNF.
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 generates a terminal followed by any number of non-
terminals (e.g.; X->xX1X2…XN)
Consider the following grammar:
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.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
Step 3. acknowledge
Check for that you have production
every read and understood
ruleourif Cookie
RHSPolicy
has &first
Privacysymbol
Policy as non
terminal say Aj for the production of Ai, it is mandatory that i should be
[Link] 2/11
3/27/25, 11:36 PM Converting Context Free Grammar to Greibach Normal Form | GeeksforGeeks
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.
GATE 2026 Aptitude Engineering Mathematics Discrete Mathematics Operating System DBMS C
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
ofuse
We terminal
cookies to symbols. If the
ensure you have i<j best
in the production
browsing experience onthen it is good
our website. By usingto
ourgo
site,to
youthe
acknowledge
next step but if that
i>=jyouthen
have read and understood
change our Cookie Policy
the production & Privacy Policy
by replacing it with that
[Link] 3/11
3/27/25, 11:36 PM Converting Context Free Grammar to Greibach Normal Form | GeeksforGeeks
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
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Ai → xX (any single terminal followed by any number of non terminals)
[Link] 4/11
3/27/25, 11:36 PM Converting Context Free Grammar to Greibach Normal Form | GeeksforGeeks
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.
Conclusion
Converting a CFG into GNF is a systematic process that involves
renaming non-terminals, replacing left recursions, and ensuring the
production rules fit the GNF criteria. This transformation is crucial for
various applications in compiler design and formal language processing.
27. Introduction to CNF and GNF in TOC Visit Course
Converting Context Free Grammar to Greibach
Normal Form – FAQs
Why is Greibach Normal Form important?
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
GNF is important because it ensures that the resulting grammar is
acknowledge that you have read and understood our Cookie Policy & Privacy Policy
more suitable for certain parsing algorithms, particularly top-
down parsers.
[Link] 5/11
3/27/25, 11:36 PM Converting Context Free Grammar to Greibach Normal Form | GeeksforGeeks
Can every CFG be converted to GNF?
Yes, every context-free grammar can be converted to an
equivalent grammar in GNF.
What are the main differences between CNF and GNF?
CNF allows productions to be either two non-terminals or a single
terminal, while GNF requires productions to start with a terminal
followed by non-terminals.
Is there a unique GNF for every CFG?
No, there can be multiple equivalent GNF representations for a
given CFG.
Dreaming of [Link] in IIT? Get AIR under 100 with our GATE 2026
CSE & DA courses! Get flexible weekday/weekend options, live
mentorship, and mock tests. Access exclusive features like All India
Mock Tests, and Doubt Solving—your GATE success starts now!
Comment More info Next Article
Pumping Lemma in Theory of
Advertise with us Computation
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
Similar Reads
acknowledge that you have read and understood our Cookie Policy & Privacy Policy
[Link] 6/11
3/27/25, 11:36 PM Converting Context Free Grammar to Greibach Normal Form | GeeksforGeeks
Converting Context Free Grammar to Chomsky Normal Form
Chomsky Normal Form (CNF) is a way to simplify context-free grammars
(CFGs) so that all production rules follow specific patterns. In CNF, each…
5 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…
5 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…
2 min read
Removal of Ambiguity (Converting an Ambiguous Grammar into…
Ambiguous grammar, in computer languages and compilers, refers to
when certain elements from source code may be parsed according to mo…
8 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…
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…
2 min read
What is Context-Free Grammar?
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
A context-free
acknowledge grammar
that you have(CFG)
read andisunderstood
a formalour system used& to
Cookie Policy describe
Privacy Policy a class
of languages known as context-free languages (CFLs). purpose of…
[Link] 7/11
3/27/25, 11:36 PM Converting Context Free Grammar to Greibach Normal Form | GeeksforGeeks
5 min read
Ambiguity in Context free Grammar and Languages
Context-Free Grammars (CFGs) are essential in formal language theory
and play a crucial role in programming language design, compiler…
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 α…
2 min read
Difference between Thread Context Switch and Process Context…
A context switch is the process where the operating system switches the
CPU's attention from one task to another, ensuring multitasking. This…
5 min read
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate
Tower, Sector- 136, Noida, Uttar Pradesh
(201305)
Registered Address:
K 061, Tower K, Gulshan Vivante
Apartment, Sector 137, Noida, Gautam
Buddh Nagar, Uttar Pradesh, 201305
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read andAdvertise
understood
withour
usCookie Policy & Privacy Policy
[Link] 8/11
3/27/25, 11:36 PM Converting Context Free Grammar to Greibach Normal Form | GeeksforGeeks
Company Explore
About Us Job-A-Thon Hiring Challenge
Legal Hack-A-Thon
Privacy Policy GfG Weekly Contest
Careers Offline Classes (Delhi/NCR)
In Media DSA in JAVA/C++
Contact Us Master System Design
GfG Corporate Solution Master CP
Placement Training Program GeeksforGeeks Videos
Geeks Community
Languages DSA
Python Data Structures
Java Algorithms
C++ DSA for Beginners
PHP Basic DSA Problems
GoLang DSA Roadmap
SQL DSA Interview Questions
R Language Competitive Programming
Android Tutorial
Data Science & ML Web Technologies
Data Science With Python HTML
Data Science For Beginner CSS
Machine Learning JavaScript
ML Maths TypeScript
Data Visualisation ReactJS
Pandas NextJS
NumPy NodeJs
NLP Bootstrap
Deep Learning Tailwind CSS
Python Tutorial Computer Science
Python Programming Examples GATE CS Notes
Django Tutorial Operating Systems
Python Projects Computer Network
Python Tkinter Database Management System
Web Scraping Software Engineering
OpenCV Tutorial Digital Logic Design
Python Interview Question Engineering Maths
DevOps System Design
Git High Level Design
AWS Low Level Design
Docker UML Diagrams
Kubernetes Interview Guide
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
Azure Design Patterns
acknowledge that you have read and understood our Cookie Policy & Privacy Policy
GCP OOAD
DevOps Roadmap System Design Bootcamp
[Link] 9/11
3/27/25, 11:36 PM Converting Context Free Grammar to Greibach Normal Form | GeeksforGeeks
Interview Questions
School Subjects Software and Tools
Mathematics AI Tools Directory
Physics Marketing Tools Directory
Chemistry Accounting Software Directory
Biology HR Management Tools
Social Science Editing Software Directory
English Grammar Microsoft Products and Apps
Figma Tutorial
Databases Preparation Corner
SQL Company-Wise Recruitment Process
MYSQL Resume Templates
PostgreSQL Aptitude Preparation
PL/SQL Puzzles
MongoDB Company-Wise Preparation
Companies
Colleges
Competitive Exams More Tutorials
JEE Advanced Software Development
UGC NET Software Testing
UPSC Product Management
SSC CGL Project Management
SBI PO Linux
SBI Clerk Excel
IBPS PO All Cheat Sheets
IBPS Clerk Recent Articles
Free Online Tools Write & Earn
Typing Test Write an Article
Image Editor Improve an Article
Code Formatters Pick Topics to Write
Code Converters Share your Experiences
Currency Converter Internships
Random Number Generator
Random Password Generator
DSA/Placements Development/Testing
DSA - Self Paced Course JavaScript Full Course
DSA in JavaScript - Self Paced Course React JS Course
DSA in Python - Self Paced React Native Course
C Programming Course Online - Learn C with Data Structures Django Web Development Course
Complete Interview Preparation Complete Bootstrap Course
Master Competitive Programming Full Stack Development - [LIVE]
We useCore
cookies to ensure
CS Subject you havePreparation
for Interview the best browsing experience on our
JAVAwebsite.
BackendBy using our site,
Development you
- [LIVE]
acknowledge that you have read
Mastering System Design: LLD to HLD and understood our Cookie Policy & Privacy Policy
Complete Software Testing Course [LIVE]
Tech Interview 101 - From DSA to System Design [LIVE] Android Mastery with Kotlin [LIVE]
[Link] 10/11
3/27/25, 11:36 PM Converting Context Free Grammar to Greibach Normal Form | GeeksforGeeks
DSA to Development [HYBRID]
Placement Preparation Crash Course [LIVE]
Machine Learning/Data Science Programming Languages
Complete Machine Learning & Data Science Program - [LIVE] C Programming with Data Structures
Data Analytics Training using Excel, SQL, Python & PowerBI - C++ Programming Course
[LIVE] Java Programming Course
Data Science Training Program - [LIVE] Python Full Course
Mastering Generative AI and ChatGPT
Data Science Course with IBM Certification
Clouds/Devops GATE 2026
DevOps Engineering GATE CS Rank Booster
AWS Solutions Architect Certification GATE DA Rank Booster
Salesforce Certified Administrator Course GATE CS & IT Course - 2026
GATE DA Course 2026
GATE Rank Predictor
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our Cookie Policy & Privacy Policy
[Link] 11/11