13 Linear Grammars
13 Linear Grammars
Example:
Example of non-linear:
S → αSb S → SS
S → αSb | ε S→ε
S→ε
S → αSb
S → bSα
Grammars with more than one non-terminal at the
right side of a production are known as non-linear
Example: Linear Grammar Derivation
S → αSb
S → αSb | ε
S→ε
S → Ab S0 → S
S → Sb S → Ab
S→α S → Sb
A →Aα S→α
Α→α A →Aα
Α→α
Example: Left-linear to right-linear
Solution:
2. If the left-linear grammar has a rule S → p, then keep that rule in the right-
linear grammar
S0 → S S0 → α
S → Ab
S → Sb
S→α
A → Aα
Α→α
Example: Left-linear to right-linear
Solution:
3. If the left-linear grammar has a rule 𝑊1 → 𝑝, then add the following rule to the
right-linear grammar: 𝑆 → 𝑝𝑊1
S0 → S S0 → α
S → Ab S0 → αΑ
S → Sb
S→α
A → Aα
Α→α
Example: Left-linear to right-linear
Solution:
4. If the left-linear grammar has a rule 𝑊1 → 𝑊2 𝑝, add the following rule to the
right-linear grammar: 𝑊2 → 𝑝𝑊1
S0 → S S0 → α
S → Ab S0 → αΑ
S → Sb A → bS
S→α A → αΑ
A → Aα S → bS
Α→α
Example: Left-linear to right-linear
Solution:
5. If the left-linear grammar has a rule 𝑆 → 𝑊2 𝑝, then add the following rule to
the right-linear grammar: 𝑊2 → 𝑝
S0 → S S0 → α
S → Ab S0 → αΑ
S → Sb A → bS
S→α A → αΑ Both grammars generate {a+b+}
A → Aα S → bS
Α→α S→ε
Example: Left-linear to right-linear
Convert the following left-linear grammar to the equivalent right-linear:
S → Aα
A → Bb
B → Aα
A → αb
Β→α
Example: Left-linear to right-linear
Solution:
1. If the left-linear grammar has a rule with the start symbol S on the right-hand
side, simply add a new initial state and the following rule: S0 → S1
S → Aα S → Aα
A → Bb A → Bb
B → Aα B → Aα
A → αb A → αb
Β→α Β→α
Example: Left-linear to right-linear
Solution:
2. If the left-linear grammar has a rule S → p, then keep that rule in the right-
linear grammar
S → Aα
A → Bb
B → Aα
A → αb
Β→α
Example: Left-linear to right-linear
Solution:
3. If the left-linear grammar has a rule 𝑊1 → 𝑝, then add the following rule to the
right-linear grammar: 𝑆 → 𝑝𝑊1
S → Aα S → αbA
A → Bb S → αB
B → Aα
A → αb
Β→α
Example: Left-linear to right-linear
Solution:
4. If the left-linear grammar has a rule 𝑊1 → 𝑊2 𝑝, add the following rule to the
right-linear grammar: 𝑊2 → 𝑝𝑊1
S → Aα S → αbA
A → Bb S → αB
B → Aα Α → αΒ
A → αb Β → bA
Β→α
Example: Left-linear to right-linear
Solution:
5. If the left-linear grammar has a rule 𝑆 → 𝑊2 𝑝, then add the following rule to
the right-linear grammar: 𝑊2 → 𝑝
S → Aα S → αbA
A → Bb S → αB
B → Aα Α → αΒ
A → αb Β → bA
Both grammars generate {a+b+}
Β→α A→α
Recall: Left-linear and Right-linear Grammars
A left-linear grammar is one in which all rules obey one of:
• A → α - where A is a non-terminal in N and α is a terminal in Σ
• A → Bw - where A and B are in N and w is in Σ
• A → ε - where A is in N and ε is the empty string
A right-linear grammar is one in which all rules obey one of:
• A → α - where A is a non-terminal in N and α is a terminal in Σ
• A → wB - where A and B are in N and w is in Σ
• A → ε - where A is in N and ε is the empty string
Extended Left-linear and Right-linear
Grammars
An extended left-linear grammar is one in which all rules obey one of:
• A → α - where A is a non-terminal in N and α is a terminal in Σ
• A → Bw - where A and B are in N and w is in Σ*
• A → ε - where A is in N and ε is the empty string
An extended right-linear grammar is one in which all rules obey one of:
• A → α - where A is a non-terminal in N and α is a terminal in Σ
• A → wB - where A and B are in N and w is in Σ*
• A → ε - where A is in N and ε is the empty string
Linear Grammars and REs
➢A regular grammar is a left or right linear grammar and vice versa and
can produce regular expressions
S → αA|bB|α|b
A → αS|bB|b
B → αA|bS
S → αA|bB|α|b
α
A → αS|bB|b S A
b
B → αA|bS
B
S → αA|bB|α|b α
S A
A → αS|bB|b b
α, b
B → αA|bS C
B
α
S → αA|bB|α|b S
α
A
b
A → αS|bB|b b α, b b
B → αA|bS B C
α
S → αA|bB|α|b S
α
A
A → αS|bB|b b b α, b b b
α
B → αA|bS B C
α U b U (α(αα)*b) e
D C E
α(αα)*b e
D C E D E
b
(α U b U (α(αα)*b)) U (α U b U (α(αα)*b)) U
B (b(bb)*α(αα)*b)
bb (b(bb)*α(αα)*b)
Example: Regular Grammars to REs
Construct an RE from the following regular grammar:
S → bA|b
A → αA|b
Example: Regular Grammars to REs
Solution
S → bA|b
A → αA|b
S → bA|b
A → αA|b S
b
A
Step 2: b
B
For the production S → bA, the transitional diagram is
shown on the right α
For the production S → b, the transitional diagram S
b
A
including the previous one is shown on the right
For the production A → αA|b, the transitional diagram b
b
including the previous one is shown on the right B
Example: Regular Grammars to REs
Solution: Step 3: State elimination
α α
b e b
S A C S A
b b
b b e
B B D
α
b
C A
b U bα*b e b U bα*b
b C B D C D
b e
B D
Linear Grammars and REs
➢A regular grammar is a left or right linear grammar and vice versa and
can produce regular expressions
Step 1:
Construct the equivalent NFA for the given RE (Thompson's
construction algorithm)
α
The NFA for the RE α*(α U b)b* is: α b
q0 q1
b
Example: RE to Regular Grammar (via NFA)
Solution:
Step 2:
Eliminate the null moves (by transforming to the equivalent DFA)
No null moves exist
α
α b
q0 q1
b
Example: RE to Regular Grammar (via NFA)
Solution:
Step 3:
• The NFA has two states which means that there will be two non-terminals in the
grammar
• We consider A for q0 and B for q1
• For the transition δ(q0, α) → q0, the production rule will be A → αA
• For the transition δ(q0, α) → q1, the production rule will be A → αB | α as q1 is a
final state
• Similarly: α
α
b
• δ(q0, b) → q1: A → bB | b (as q1 is a final state)
• δ(q1, b) → q1: B → bB | b (as q1 is a final state) q0 q1
• The start symbol will be A since q0 is the initial state b
Example: RE to Regular Grammar (via NFA)
Solution:
Step 3:
• The regular grammar G for the RE α*(α U b)b* is {VN, Σ, P, S} where
• VN = {A, B}
• Σ = {α, b}
• P : {A → αA|bB|α|b, B → bB|b}
• S :{A} α
α
b
q0 q1
b
Example: RE to Regular Grammar (via NFA)
Construct a possibly extended regular grammar for the following RE:
α*(α U eα*b)b*
Example: RE to Regular Grammar (via NFA)
Solution:
Step 1:
Construct the equivalent NFA for the given RE (Thompson's
construction algorithm) α
The NFA for the RE α*(α U b)b* is: b
q0 q1
α
b
e
q2
α
Example: RE to Regular Grammar (via NFA)
Solution:
Step 2:
Eliminate the null moves (by transforming α
to the equivalent DFA) b
We do not ned to do this step as an q0 q1
α
extended grammar is acceptable
b
e
q2
α
Example: RE to Regular Grammar (via NFA)
Solution:
Step 3:
• The number of states of the FA is three, as many as the non-terminals in
the grammar α
• S :{A} b
q0 q1
α
b
e
q2
α
Bonus: Right-linear to left-linear via NFA
α b
S0 → α
S0 → αΑ S0 Α S
A → bS α b
A → αΑ α
S → bS
S→ε
Reverse
S → Ab
S → Sb
S→α α b
A → Aα
Α→α S0 A b S
α
α
Mixing Left-linear and Right-linear grammars
• If we mix left-linear and right-linear rules in a single grammar, the
result will not necessarily generate a regular language
• For instance, given w ∈ {α, b}*, a left linear regular L can be:
• A → Aw | ε
• LR will be right linear regular:
• B → wRB | ε
• But L’ = {wwR} is not regular:
• A → Aw | B
• B → wRB | ε
https://transang.me/formal-language-theory/