Regular grammar (Model regular grammars )
Last Updated :
26 Apr, 2022
Prerequisites: Chomsky hierarchy
Type-3 grammar/regular grammar:
Regular grammar generates regular language. They have a single non-terminal on the left-hand side and a right-hand side consisting of a single terminal or single terminal followed by a non-terminal.
The productions must be in the form:
A ⇢ xB
A ⇢ x
A ⇢ Bx
where A, B ∈ Variable(V) and x ∈ T* i.e. string of terminals.
Types of regular grammar:
- Left Linear grammar(LLG)
- Right linear grammar(RLG)
1. Left linear grammar(LLG):
In LLG, the productions are in the form if all the productions are of the form
A ⇢ Bx
A ⇢ x
where A,B ∈ V and x ∈ T*
2. Right linear grammar(RLG):
In RLG, the productions are in the form if all the productions are of the form
A ⇢ xB
A ⇢ x
where A,B ∈ V and x ∈ T*
The language generated by type-3 grammar is a regular language, for which a FA can be designed. The FA can also be converted into type-3 grammar
Example: FA for accepting strings that start with b
.
∑ = {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.
This grammar derives strings that are stated with B
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 FA 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).
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.
This represents the same procedure as above for converting RLG to LLG
Here L is a language for FA and LR is a reversal of the language L.
Example:
The above FA represents language L(i.e. set of all strings over input symbols a and b which start with b).
We are converting it into LLG.
Step1: The reversal of FA is
The reversal of FA represents all strings starting with b.
Step 2: The corresponding RLG for this reversed FA is
B ⇢ aB/bB/bA
A ⇢ ∈
Step 3: The reversing the above RLG we get
B ⇢ Ba/Bb/Ab
A ⇢ ∈
So this is LLG for language L( which represents all strings that start with b).
L= {b, ba, bb, baa, bab ,bba, bbb ….. }
Conversion of RLG to FA:
- Start from the first production.
- From every left alphabet (or variable) go to the symbol followed by it.
- Start state: It will be the first production state.
- Final state: Take those states which end up with terminals without further non-terminals.
Example: The RLL grammar for Language(L), represents a set of all strings which end with 0.
A ⇢ 0A/1B/0B
B ⇢ ∈
So the FA for corresponding to RLG can be found out as
Start with variable A and use its production.
- For production A ⇢ 0A, this means after getting input symbol 0, the transition will remain in the same state.
- For production, A ⇢ 1B, this means after getting input symbol 1, the state transition will take place from State A to B.
- For production A ⇢ 0B, this means after getting input symbol 0, the state transition will take place from State A to B.
- For production B ⇢ ∈, this means there is no need for state transition. This means it would be the final state in the corresponding FA as RHS is terminal.
So the final NFA for the corresponding RLG is
Set of all strings that end with 0
Conversion of LLG to FA:
Explanation: First convert the LLG which represents the Language(L) to RLG, which represents, the reversal of language L(i.e.LR) then design FA corresponding to it(i.e. FA for Language LR ). Then reverse the FA. Then the final FA is FA for language L).
Conversion of LLG to RLG: 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)
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)
Step 3: Write RLG corresponding to reversed FA.
A ⇢ bB
B ⇢ aB/bB/∈
They can be easily converted to other
All have the same power and can be converted to other
Similar Reads
Regular grammar (Model regular grammars )
Prerequisites: Chomsky hierarchy Type-3 grammar/regular grammar: Regular grammar generates regular language. They have a single non-terminal on the left-hand side and a right-hand side consisting of a single terminal or single terminal followed by a non-terminal. The productions must be in the form:
5 min read
Right and Left linear Regular Grammars
Regular grammar is a type of grammar that describes a regular language. A regular grammar is a mathematical object, G, which consists of four components, G = (N, E, P, S), where N: non-empty, finite set of non-terminal symbols,E: a finite set of terminal symbols, or alphabet, symbols,P: a set of gra
2 min read
Regular Expressions, Regular Grammar and Regular Languages
As discussed in Chomsky Hierarchy, Regular Languages are the most restricted types of languages and are accepted by finite automata. Regular Expressions Regular Expressions are used to denote regular languages. An expression is regular if: ? is a regular expression for regular language ?.? is a regu
6 min read
Regular Expressions, Regular Grammar and Regular Languages
As discussed in Chomsky Hierarchy, Regular Languages are the most restricted types of languages and are accepted by finite automata. Regular Expressions Regular Expressions are used to denote regular languages. An expression is regular if: ? is a regular expression for regular language ?.? is a regu
6 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 of computation (TOC). It is used to indicate the structural complexity of regular expressions and regular languages. Here complexity, relates to the maximum nesting depth of Kleene stars present in a regular expression. It may be noted here that a
6 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
Closure properties of Regular languages
Closure properties on regular languages are defined as certain operations on regular language that are guaranteed to produce regular language. Closure refers to some operation on a language, resulting in a new language that is of the same "type" as originally operated on i.e., regular. Regular langu
4 min read
Closure properties of Regular languages
Closure properties on regular languages are defined as certain operations on regular language that are guaranteed to produce regular language. Closure refers to some operation on a language, resulting in a new language that is of the same "type" as originally operated on i.e., regular. Regular langu
4 min read
Perl | Operators in Regular Expression
Prerequisite: Perl | Regular Expressions The Regular Expression is a string which is the combination of different characters that provides matching of the text strings. A regular expression can also be referred to as regex or regexp. The basic method for applying a regular expression is to use of bi
4 min read
∈-NFA of Regular Language L = {ab,ba}
Prerequisite - Introduction of Finite Automata Non-Deterministic Finite Automata and ∈-Non-Deterministic Finite Automata are almost the same except for their transition function and there are a few special rules for construction of ∈-NFA. ∈-NFA is defined in 5 tuple representation {Q, q0, Σ, δ, F} w
3 min read
Relationship between grammar and Language
Prerequisite - Regular Grammar, Regular Expressions, Chomsky hierarchy Overview :In this article, we will discuss the overview of Regular Grammar can be either Right Linear or Left Linear, and mainly focus will be on the relationship between grammar and Language. Let's discuss it one by one. Types :
2 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.
2 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
2 min read
∈-NFA of Regular Language L = bc(ab+c)*a+
Finite Automata can be classified into three types- DFANFA∈-NFA. The only difference between ∈-NFA and NFA is that ∈-NFA has a different transition function than regular NFA. ∈ represents empty inputs. ∈-NFA shows that an automaton can change its state without an input, i.e. even if the input is nul
3 min read
Hypothesis (language regularity) and algorithm (L-graph to NFA) in TOC
Prerequisite - Finite automata, L-graphs and what they represent L-graphs can generate context sensitive languages, but it’s much harder to program a context sensitive language over programming a regular one. This is why I’ve came up with a hypothesis about what kind of L-graphs can generate a regul
7 min read
∈-NFA of Regular Language L = (a+b)*bc+
∈-NFA is a part of Finite Automata. ∈ is a symbol that represents empty inputs. ∈-NFA is the representation that allows an automaton to change its state without an input, i.e. even if the input is null the automaton can change its state. ∈-Non-Deterministic Finite Automata has a different transition
2 min read
Python Regex: Replace Captured Groups
Regular Expressions, often abbreviated as Regex, are sequences of characters that form search patterns. They are powerful tools used in programming and text processing to search, match, and manipulate strings. Think of them as advanced search filters that allow us to find specific patterns within a
5 min read
Generating regular expression from Finite Automata
Prerequisite - Introduction of FA, Regular expressions, grammar and language, Designing FA from Regular Expression There are two methods to convert FA to the regular expression: 1. State Elimination Method:Step 1 - If the start state is an accepting state or has transitions in, add a new non-accepti
3 min read