Open In App

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.


Similar Reads