0% found this document useful (0 votes)
13 views

TCS Lect 22 Regular Grammar

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

TCS Lect 22 Regular Grammar

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 39

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

Prepared By: Mr. Vaibhav


Right Linear Grammar
› Grammar G is represented as G (V, T, S, P)
› A grammar is said to be right-linear if all the productions of
the form
A→xB
A→x
where A, B ∈ V
x ∈ T*

Prepared By: Mr. Vaibhav


Left Linear Grammar
› Grammar G is represented as G (V, T, S, P)
› A grammar is said to be right-linear if all the productions of
the form
A→Bx
A→x
where A, B ∈ V
x ∈ T*

Prepared By: Mr. Vaibhav


Regular Grammar

› A Regular Grammar is one that is either Right Linear or Left


Linear but not both at the same time

› At most ONE VARIABLE on right side of any production

› The variable must consistently be either the rightmost or


leftmost symbol of the right side of any production

Prepared By: Mr. Vaibhav


Regular Grammar
› Example 1:

The Grammar G1 = ( { S }, { a, b }, S, P1 ) with P1 is


given as
S→abS|a
The above grammar is Right-Linear Grammar
It is also Regular Grammar

The derivation of the string aba is as follows:


S⇒abS
S⇒abS
⇒ababS
⇒aba
⇒ababa

L(G1) can be represented as Regular Expression as

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]

L(G2) can be represented as Regular Expression as

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

› The Grammar is neither Left Linear nor Right Linear


› It is an example of Linear Grammar

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

› The left and the right linear grammar is said to be equivalent if

the language generated by them are equal

› For every Right-Linear Grammar GR there exists an equivalent

Left Linear Grammar GL

Prepared By: Mr. Vaibhav


Conversion of Right-Linear Grammar to equivalent
Left-Linear
Represent the
Grammar
given Right-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

Interchange the Rewrite the


positions of the grammar from this
initial and final new Transition
states Graph in Left-
Linear fashion

Prepared By: Mr. Vaibhav


Conversion of Right-Linear Grammar to
equivalent Left-Linear Grammar
Ex. 1: Convert the following Right-Linear Grammar to its equivalent Left-Linear
Grammar
S→bB B→bC B→aB
B→b C→a
Solution:
a

b b
S B C • Interchange the position of
Initial and final state
a
b

Prepared By: Mr. Vaibhav


Conversion of Right-Linear Grammar to
equivalent Left-Linear Grammar
Ex. 1: Convert the following Right-Linear Grammar to its equivalent Left-Linear
Grammar
S→bB B→bC B→aB
B→b C→a
Solution:
a

b b
ε B C • Reverse the direction of all
the transition
a
b

Prepared By: Mr. Vaibhav


Conversion of Right-Linear Grammar to
equivalent Left-Linear Grammar
Ex. 1: Convert the following Right-Linear Grammar to its equivalent Left-Linear
Grammar
S→bB B→bC B→aB
B→b C→a
Solution:
a

b b
ε B C • Reverse the direction of all
the transition
a
b

Prepared By: Mr. Vaibhav


Conversion of Right-Linear Grammar to
equivalent Left-Linear Grammar
Ex. 1: Convert the following Right-Linear Grammar to its equivalent Left-Linear
Grammar
S→bB B→bC B→aB
B→b C→a
Solution:
a

b b
ε B C Left Linear Grammar can be
rewritten as
a S→Ca S→Bb
b
C→Bb B→Ba
S
B→b

Prepared By: Mr. Vaibhav


Conversion of Right-Linear Grammar to
equivalent Left-Linear Grammar
Ex. 1: Convert the following Right-Linear Grammar to its equivalent Left-Linear
Grammar
S→bB B→bC B→aB
B→b C→a
Solution:

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

Interchange the Rewrite the


positions of the grammar from this
initial and final new Transition
states Graph in Right-
Linear fashion

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
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

Ex. 3: Write an equivalent Right-Linear Grammar to for given Left-Linear


Grammar
S→S10|0
Solution:
10

0
S ε • Interchange the position of
Initial and final state

Prepared By: Mr. Vaibhav


Conversion of Left-Linear Grammar to
equivalent Right-Linear Grammar

Ex. 3: Write an equivalent Right-Linear Grammar to for given Left-Linear


Grammar
S→S10|0
Solution:
10

ε S • Reverse the direction of all


the transitions

Prepared By: Mr. Vaibhav


Conversion of Left-Linear Grammar to
equivalent Right-Linear Grammar

Ex. 3: Write an equivalent Right-Linear Grammar to for given Left-Linear


Grammar
S→S10|0
Solution:
10
Right Linear Grammar can be rewritten
0
S as:
ε
S→0ε
ε→10ε

Second production is invalid

Prepared By: Mr. Vaibhav


Conversion of Left-Linear Grammar to
equivalent Right-Linear Grammar
Ex. 3: Write an equivalent Right-Linear Grammar to for given Left-Linear
Grammar
S→S10|0
Solution:

10
We need to modify grammar.
ε 0
S A ε Introduce new variable and rewrite
productions
S→A
A→A10|0

Prepared By: Mr. Vaibhav


Conversion of Left-Linear Grammar to
equivalent Right-Linear Grammar
Ex. 3: Write an equivalent Right-Linear Grammar to for given Left-Linear
Grammar
S→S10|0
Solution:

10
Interchange the states
ε 0
ε A S

Prepared By: Mr. Vaibhav


Conversion of Left-Linear Grammar to
equivalent Right-Linear Grammar
Ex. 3: Write an equivalent Right-Linear Grammar to for given Left-Linear
Grammar
S→S10|0
Solution:

10
Interchange the position of initial and final
ε 0
A states
ε S

Prepared By: Mr. Vaibhav


Conversion of Left-Linear Grammar to
equivalent Right-Linear Grammar
Ex. 3: Write an equivalent Right-Linear Grammar to for given Left-Linear
Grammar
S→S10|0
Solution:

10
Reverse the direction of all the arrows
ε 0
ε A S

Prepared By: Mr. Vaibhav


Conversion of Left-Linear Grammar to
equivalent Right-Linear Grammar
Ex. 3: Write an equivalent Right-Linear Grammar to for given Left-Linear
Grammar
S→S10|0
Solution:

10
Rewriting the productions
ε 0
A S→0A
ε S
A→10A|ε

Prepared By: Mr. Vaibhav


Equivalence of Regular
Grammar and Finite Automata
Definition
› The Regular Grammar and Finite Automata is said to be

equivalent if the language generated by Grammar and Language

accepted by Finite Automata are equal

› For every Regular Grammar we can write its equivalent Finite

Automata

Prepared By: Mr. Vaibhav


Conversion from Regular Grammar to Finite Automata

Let Grammar G is represented as G (V, T, S, P)


Where V → Set of Variables
T → Set of Terminals
S → Start Symbol S ∈ V
P → Set of Production Rules

Let Finite automata be represented as


M = ( Q, Σ, δ, q0, F )
Where Q → Set of states
Σ → Input alphabet
δ → Transition Function
q0 → Initial state q0 ∈ Q
F → Set of final states F ⊆ Q
Prepared By: Mr. Vaibhav
Conversion from Regular Grammar to Finite Automata

Steps for Conversion:

1. States corresponds to variables ( V ⇔ Q )

2. Initial state corresponds to Start Symbol ( S ⇔ q0 )

3. Input alphabet corresponds to Terminal ( T ⇔ Σ )

4. Transitions in DFA corresponds to set of productions in P ( P ⇔ δ )

The transition function δ is defined as:

i. Each production Si → a Sj induces a transition from Qi to Qj with label ‘a’ onto

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

Ex. 1: Construct a Finite Automata recognizing L(G) where G is a grammar


given by
S→aS|bA|b
A→aA|bS|a
Solution: We can write Finite Automata as
M = ( Q, Σ, δ, q0, F )
G (V, T, S, P) is a grammar Where Q = { q0, q1, qf }
V = { S, A } Σ = { a, b }
T = { a, b } q0 = q0
Start Symbol = S F = { qf }
Set of productions P is given by
S→aS|bA|b Transition Function can be written as
A→aA|bS|a δ ( q0 , a ) = q0 δ ( q0 , b ) = q1 δ ( q0 , b ) = qf
δ ( q1 , a ) = q1 δ ( q1 , b ) = q0 δ ( q1 , a ) = qf

Prepared By: Mr. Vaibhav


Conversion from Regular Grammar to Finite Automata
Ex. 1: Construct a Finite Automata recognizing L(G) where G is a grammar given by
S→aS|bA|b
A→aA|bS|a
Solution:

We can write Finite Automata as

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

Prepared By: Mr. Vaibhav


Conversion from Finite Automata to Regular Grammar

Let Finite automata be represented as


M = ( Q, Σ, δ, q0, F )
Where Q → Set of states
Σ → Input alphabet
δ → Transition Function
q0 → Initial state q0 ∈ Q
F → Set of final states F ⊆ Q

Let Grammar G is represented as G (V, T, S, P)


Where V → Set of Variables
T → Set of Terminals
S → Start Symbol S ∈ V
P → Set of Production Rules
Prepared By: Mr. Vaibhav
Conversion from Finite Automata to Regular Grammar

Grammar G can be constructed as

1. Variables corresponds to States ( Q ⇔ V )

2. Terminals corresponds to Input Alphabet ( Σ ⇔ T )

3. Start symbol corresponds to Initial State ( q0 ⇔ S )

4. P is the set of production rules can be constructed using transition functions as

follows: (P⇔δ)

i. If δ ( Qi , a ) = Qj where Qj ∉ F then

Write production rule as Si → a Sj

ii. If δ ( Qi , a ) = Qj where Qj ∈ F then

Write production rule as Si → a Sj and Si → a


Prepared By: Mr. Vaibhav
Conversion from Finite Automata to Regular Expression

Ex. 1: Construct regular grammar for given Finite State Machine

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

Ex. 1: Construct regular grammar for given Finite State Machine


Solution:
Given DFA can be described as Grammar G can be mapped as G ( V, T, S, P)
M = ( Q, Σ, δ, q0, F ) Where V = { S1, S2, S3, S4 }
Where Q = { q1, q2, q3, q4 } T = { a, b }
Σ = { a, b } S = S1
q0 = q1 Here S1 ⇔ q1, S2 ⇔ q2, S3 ⇔ q3 and S4 ⇔ q4
F = { q4 }
Productions Rules can be written as
Transition Function δ can be written as S1 → b S2 S1 → a S3
δ ( q1 , b ) = q2 δ ( q1 , a ) = q3 S2 → a S3 S2 → b S4 S2 → b
δ ( q2 , a ) = q3 δ ( q2 , b ) = q4 S3 → a S4 S3 → b S2 S3 → a
δ ( q3 , a ) = q4 δ ( q3 , b ) = q2
δ ( q4 , a ) = q4 δ ( q4 , b ) = q4 S4 → a S4 S4 → b S4
S4 → a S4 → b
Prepared By: Mr. Vaibhav

You might also like