TCS Lect 22 Regular Grammar
TCS Lect 22 Regular Grammar
Content
› Left Linear Grammar
› Right Linear Grammar
› Regular Grammar
› Linear Grammar
› Equivalence of Left-Linear and Right-Linear Grammar
› Equivalence of Finite Automata and Regular Expression
r = ( a b )* a
Prepared By: Mr. Vaibhav
Regular Grammar
› Example 2:
The Grammar G2 = ( { S, S1, S2 }, { a, b }, S, P2 ) with P1 is
given as
S → S1 a b
S1 → S1 a b | S2
S2 → a
The above grammar is Left-Linear Grammar
It is also Regular Grammar
S ⇒ S1 a b [Rule 1]
The derivation of the string aab is as follows:
⇒ S1 a b a b [Rule 2]
S ⇒ S1 a b [Rule 1]
⇒ S2 a b a b [Rule 3]
⇒ S2 a b [Rule 3]
⇒aabab [Rule 4]
⇒ a a b [Rule 4]
r = a a b (a b)*
Prepared By: Mr. Vaibhav
Linear Grammar
› Example 3:
The Grammar G3 = ( { S, A, B }, { a, b }, S, P ) with P1 is given as
S→A
A→aB|ε
S2 → A b
The above grammar is not Regular Grammar
It contains both type of productions Left Linear and Right Linear Forms
Definition:
It is Grammar in which at most one variable can occur on Right side of any productions
without restriction on position of Variable
› A Regular Grammar is always Linear but not all Linear Grammars are Regular
Grammars Prepared By: Mr. Vaibhav
Equivalence of Right-Linear and
Left-Linear Grammar
Definition
b b
S B C • Interchange the position of
Initial and final state
a
b
b b
ε B C • Reverse the direction of all
the transition
a
b
b b
ε B C • Reverse the direction of all
the transition
a
b
b b
ε B C Left Linear Grammar can be
rewritten as
a S→Ca S→Bb
b
C→Bb B→Ba
S
B→b
Left Linear Grammar can be Using Left Linear Using Right Linear
rewritten as Grammar: Grammar:
S→Ca S→Bb S⇒Bb S⇒bB
C→Bb B→Ba ⇒Bab ⇒baB
B→b ⇒Baab ⇒baaB
⇒baab ⇒baab
Check equivalence of the
Grammar
Prepared By: Mr. Vaibhav
Conversion of Left-Linear Grammar to equivalent
Right-Linear
Represent the
Grammar
given Left-Linear
grammar by Reverse the
Transition Diagram directions of all the
• Vertices labelled by transitions,
the symbol from { V U keeping the
( ε) }
• Transitions labelled by positions of all
the symbol from { V U intermediate
( ε) } states unchanged
1 1
S B ε • Interchange the position of
Initial and final state
0 0 1
0
0
A C
0
1
Prepared By: Mr. Vaibhav
Conversion of Left-Linear Grammar to
equivalent Right-Linear Grammar
Ex. 2: Convert the following Left-Linear Grammar to its equivalent Right-Linear
Grammar
S→B1|A0|C0 B→B1|1 C→A0
A→C0|A1|B1|0
Solution:
1
1 1
ε B S • Interchange the position of
Initial and final state
0 0 1
0
0
A C
0
1
Prepared By: Mr. Vaibhav
Conversion of Left-Linear Grammar to
equivalent Right-Linear Grammar
Ex. 2: Convert the following Left-Linear Grammar to its equivalent Right-Linear
Grammar
S→B1|A0|C0 B→B1|1 C→A0
A→C0|A1|B1|0
Solution:
1
1 1
ε B S • Reverse the direction of all
the transition
0 0 1
0
0
A C
0
1
Prepared By: Mr. Vaibhav
Conversion of Left-Linear Grammar to
equivalent Right-Linear Grammar
Ex. 2: Convert the following Left-Linear Grammar to its equivalent Right-Linear
Grammar
S→B1|A0|C0 B→B1|1 C→A0
A→C0|A1|B1|0
Solution:
1
1 1
ε B S Right Linear Grammar can be
rewritten as
0 0 1
S→0A|1B
0
0 A→1A|0C|0
A C
0 B→1B|1A|1
1
C → 0 APrepared
|0 By: Mr. Vaibhav
Conversion of Left-Linear Grammar to
equivalent Right-Linear Grammar
0
S ε • Interchange the position of
Initial and final state
10
We need to modify grammar.
ε 0
S A ε Introduce new variable and rewrite
productions
S→A
A→A10|0
10
Interchange the states
ε 0
ε A S
10
Interchange the position of initial and final
ε 0
A states
ε S
10
Reverse the direction of all the arrows
ε 0
ε A S
10
Rewriting the productions
ε 0
A S→0A
ε S
A→10A|ε
Automata
edge
ii. Each production Sk → a induces a transition from Qk to Qf with label ‘a’ onto
Prepared By: Mr. Vaibhav
Conversion from Regular Grammar to Finite Automata
a M = ( Q, Σ, δ, q0, F )
a
Where Q = { q0, q1, qf }
b
q q Σ = { a, b }
0 1 q0 = q0
b
F = { qf }
b
Transition Function can be written as
a
δ ( q0 , a ) = q0 δ ( q0 , b ) = q1 δ ( q0 , b ) = qf
qf
δ ( q1 , a ) = q1 δ ( q1 , b ) = q0 δ ( q1 , a ) = qf
follows: (P⇔δ)
i. If δ ( Qi , a ) = Qj where Qj ∉ F then
Solution:
Given DFA can be described as
q
a, b M = ( Q, Σ, δ, q0, F )
b 2 b
Where Q = { q1, q2, q3, q4 }
Σ = { a, b }
q a b q0 = q1
1 q4
F = { q4 }
a q
3 a Transition Function δ can be written as
δ ( q1 , b ) = q2 δ ( q1 , a ) = q3
δ ( q2 , a ) = q3 δ ( q2 , b ) = q4
δ ( q3 , a ) = q4 δ ( q3 , b ) = q2
δ ( q4 , a ) = q4 δ ( q4 , b ) = q4
Prepared By: Mr. Vaibhav
Conversion from Finite Automata to Regular Expression