Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Aptitude
  • Engineering Mathematics
  • Discrete Mathematics
  • Operating System
  • DBMS
  • Computer Networks
  • Digital Logic and Design
  • C Programming
  • Data Structures
  • Algorithms
  • Theory of Computation
  • Compiler Design
  • Computer Org and Architecture
Open In App
Next Article:
Regular Expressions, Regular Grammar and Regular Languages
Next article icon

Regular Grammar (Model Regular Grammars)

Last Updated : 30 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Regular grammar is a formal grammar used to describe regular languages, which are the languages that can be recognized by finite automata. It serves as one of the simplest and most fundamental models in the Chomsky hierarchy of grammars. Regular grammars are widely used in computer science for lexical analysis, pattern matching, and text processing due to their efficiency and straightforward structure.

There are two standard forms of regular grammar:

  • Right-Linear Grammar
  • Left-Linear Grammar

In both cases, the production rules follow strict formats that ensure the language remains regular.

Right Linear Grammar

Right Linear Grammars are special type of CFGs, where each production rule has at most 1 variable on RHS & that variable is on right most position.

A ⇢ xB
A ⇢ x
where A,B ∈ V and x ∈ T*

Example 1: S -> aA | B
A -> aaB
B -> bB | a

Grammar G is right-linear

Example 2: FA for accepting strings that start with b

Finite Automata
Finite Automata

∑ = {a, b}
Initial state(q0) = A
Final state(F) = B

The RLG corresponding to FA is 

A ⇢ bB
B ⇢ ∈/aB/bB

The above grammar is RLG, which can be written directly through FA.

Parse-Tree
Parse Tree

The above RLG can derive strings that start with b and after that any input symbol (i.e. ∑ ={a, b} can be accepted).

The regular language corresponding to RLG is
L= {b, ba, bb, baa, bab, bba, bbb, ... }

If we reverse the above production of the above RLG, then we get

A ⇢ Bb
B ⇢ ∈/Ba/Bb 
It derives the language that contains all the strings which end with b.
i.e. L' = {b, bb, ab, aab, bab, abb, bbb, ...}

So we can conclude that if we have Finite Automata that represents language L and if we convert it, into RLG, which again represents language L, but after reversing RLG we get LLG which represents language L'(i.e. reverse of L). 

Conversion of RLG to LLG

RLG-to-LLG
RLG to LLG

For converting the RLG into LLG for language L, the following procedure needs to be followed: 

Step 1: Reverse the FA for language L
Step 2: Write the RLG for it.
Step 3: Reverse the right linear grammar.
after this we get the grammar that generates the language that represents the LLG for the same language L.

Conversion of Right Linear Grammar (RLG) to Finite Automaton (FA)

To convert a Right Linear Grammar (RLG) into its equivalent Finite Automaton (FA), follow the steps below:

LLG-to-FA
LLG to FA

Steps for Conversion

1.Start from the first production rule: Identify the start symbol (non-terminal) of the grammar. This becomes the start state of the FA.

2. For each production rule, create transitions based on the structure:

  • If the production is of the form A → aB, draw a transition from state A to state B on input symbol a.
  • If the production is of the form A → a (i.e., no non-terminal follows the terminal), create a transition from state A to a final state on symbol a.

3. Define the final state(s): Any state that appears in a rule of the form A → a or A → ε (i.e., ends with a terminal or empty string) becomes a final state in the FA.

Left Linear Grammar

Left Linear Grammars are Special type of CFGs, where Each Production Rule has At Most 1 Variable on RHS & that variable is on Left Most position.

A ⇢ Bx
A ⇢ x
where A,B ∈ V and x ∈ T*

Example: A -> Da | Bc | b
B -> Bf | Ca | a
C -> Ca | D
D -> 𝛆

Conversion of LLG to FA

1. Convert the LLG to a Right Linear Grammar (RLG): Do this by reversing the strings in the productions (represents the reversed language L^R).

2. Build an FA for the reversed language (L^R): Use the standard method: non-terminals become states, and transitions are based on the productions.

3. Reverse the FA:

  • Reverse all transitions.
  • Make final states into start states and the original start state into the final state.

The resulting FA accepts the original language L.

For example, the above grammar is taken which represents language L(i.e. set of all strings that start with b)
The LLG for this grammar is

B ⇢ Ba/Bb/Ab
A ⇢ ∈

Step 1: Convert the LLG into FA (i.e. the conversion procedure is the same as above)

RegularGrammar

Step 2: Reverse the FA(i.e. initial state is converted into final state and convert final state to initial state and  reverse all edges)

RegularGrammar

Step 3: Write RLG corresponding to reversed FA.

A ⇢ bB
B ⇢ aB/bB/∈

RegularGrammarConversion

Read more about Right and Left Linear Grammar

Type-3 grammar/regular grammar

  • Every Right Linear Grammar is a type of Regular Grammar.
  • Every Left Linear Grammar is also a type of Regular Grammar.

Important Note: All production rules in a regular grammar must follow either right-linear or left-linear form consistently. You cannot mix left-linear and right-linear rules in the same grammar.

The productions must be in the form:

A ⇢ xB A ⇢ xA ⇢ Bx
where A, B ∈ Variable(V) and x ∈ T* i.e. string of terminals.


Next Article
Regular Expressions, Regular Grammar and Regular Languages

G

goutamnagpal
Improve
Article Tags :
  • GATE CS
  • Theory of Computation

Similar Reads

    Regular grammar (Model regular grammars )
    Regular grammar is a formal grammar used to describe regular languages, which are the languages that can be recognized by finite automata. It serves as one of the simplest and most fundamental models in the Chomsky hierarchy of grammars. Regular grammars are widely used in computer science for lexic
    4 min read
    Regular Expressions, Regular Grammar and Regular Languages
    To work with formal languages and string patterns, it is essential to understand regular expressions, regular grammar, and regular languages. These concepts form the foundation of automata theory, compiler design, and text processing.Regular ExpressionsRegular expressions are symbolic notations used
    7 min read
    Right and Left linear Regular Grammars
    Regular Grammar is a type of grammar that describes a regular language. It is a set of rules used to describe very simple types of languages called regular languages that can be processed by computers easily, especially with finite automata. A regular grammar is a mathematical object, G, which consi
    3 min read
    Parsing ambiguous grammars using LR parser
    LR parser can be used to parse ambiguous grammars. LR parser resolves the conflicts (shift/reduce or reduce/reduce) in parsing table of ambiguous grammars based on certain rules (precedence and/or associativity of operators) of the grammar. Example: Lets take the following ambiguous grammar: E ->
    3 min read
    Star Height of Regular Expression and Regular Language
    The star height relates to the field of theoretical computation (TOC). It is used to indicate the structural complexity of regular expressions and regular languages. In this context, complexity refers to the maximum nesting depth of Kleene stars present in a regular expression.A regular language may
    3 min read
    Removing Direct and Indirect Left Recursion in a Grammar
    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 performance. We will discuss how to remov
    11 min read
geeksforgeeks-footer-logo
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
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@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
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences