9-Regular Grammar
9-Regular Grammar
1
Regular Grammar
• Two Types
• Right Linear Grammar
• Left Linear Grammar
• A grammar G = (V, T, S, P) is said to be right-linear if all productions are of the
form
• A → xB
• A→x where A, B ∈ V , and x ∈ T∗
• A grammar G = (V, T, S, P) is said to be left-linear if all productions are of the
form
• A → Bx
• A→x where A, B ∈ V , and x ∈ T∗
• A regular grammar is one that is either right-linear or left-linear. 2
Example 1
The grammar G1 = ({S} , {a, b} , S, P1), with P1 given as
S → abS|a
is right-linear.
5
Example 4
Construct a right-linear and left-linear grammar for the language
L((aaab∗ab)∗).
Right Linear Grammar Left Linear Grammar
S -> aaaA | λ S -> Aab | λ
A -> bA | abS A -> Ab | Saaa
6
Example 5
Construct right- and left-linear grammars for the language
L = {anbm : n ≥ 3, m ≥ 2} .
7
Example 6
Find the language generated by the given grammar. Generate the left-
linear grammar for the same.
S -> abA
A -> baB Language Left Linear Grammar
B -> aA | bb abbabb S -> Abb
abbaababb A -> Aaba | abba
abbaabaababb
L(abb(aab)*abb)
(or)
L(abba(aba)*bb)
8
Example 7
Generate the right-linear grammar
for
9
Example 8
Write the Regular grammar to recognize the strings with even number
of a’s and even no of b’s in any order. [Regular => right-linear grammar]
Right Linear Grammar
a S -> aA | bB | λ
00
a
10 A -> aS | bC
(S) (A)
B -> aC | bS
b b b b C -> aB | bA
a
01 11
(B) a (C)
10
Properties of Regular Languages
• If L1 and L2 are regular languages, then so are L1 ∪ L2, L1 ∩ L2, L1 L2, Lഥ1, and
L∗. We say that the family of regular languages is closed under union,
intersection, concatenation, complementation, and star-closure.
• The family of regular languages is closed under reversal (L1R).
• Let h be a homomorphism. If L is a regular language, then its homomorphic
image h(L) is also regular. The family of regular languages is therefore
closed under arbitrary homomorphisms.
• If L1 and L2 are regular languages, then L1 /L2 is also regular. We say that the
family of regular languages is closed under right quotient with a regular
language.
• If L1 and L2 are regular languages, then L1 - L2 is also regular.
(i.e) L1 − L2 = L1 ∩ Lഥ2.
11
Example 1
Prove the properties of regular languages on L1 = L(a∗baa∗), L2 = L(ab∗).
Solution:
DFA for L1 DFA for L2
a a b
b a a
A B C P Q
C
b b a
b
T R
a, b a, b
12
L1 ⋃ L2
• ẟ({A,P},a)={A,Q} • ẟ({B,Q},a)={C,R}
• ẟ({A,P},b)={B,R} • ẟ({B,Q},b)={T,Q}
• ẟ({A,Q},a)={A,R} • ẟ({C,R},a)={C,R} a
b a
• ẟ({A,Q},b)={B,Q} • ẟ({C,R},b)={T,R} A,P B,R C
C,R
• ẟ({B,R},a)={C,R} • ẟ({T,R},a)={T,R} a b b b
• ẟ({B,R},b)={T,R} • ẟ({T,R},b)={T,R} A,Q a B,Q
A,R
T,R
• ẟ({A,R},a)={A,R} • ẟ({T,Q},a)={T,R} b
a a a a, b
• ẟ({A,R},b)={B,R} • ẟ({T,Q},b)={T,Q} B,Q
b
T,Q
b
13
L1 ⋂ L2
• ẟ({A,P},a)={A,Q} • ẟ({B,Q},a)={C,R}
• ẟ({A,P},b)={B,R} • ẟ({B,Q},b)={T,Q}
• ẟ({A,Q},a)={A,R} • ẟ({C,R},a)={C,R} a
b a
• ẟ({A,Q},b)={B,Q} • ẟ({C,R},b)={T,R} A,P B,R C
C,R
• ẟ({B,R},a)={C,R} • ẟ({T,R},a)={T,R} a b b b
• ẟ({B,R},b)={T,R} • ẟ({T,R},b)={T,R} A,Q a B,Q
A,R
T,R
• ẟ({A,R},a)={A,R} • ẟ({T,Q},a)={T,R} b
a a a a, b
• ẟ({A,R},b)={B,R} • ẟ({T,Q},b)={T,Q} B,Q
b
T,Q
b
Since there are no final states, L1 ⋂ L2 = φ 14
L1.L2
• Concatenate both DFAs by combining the final state of L1 with initial
state of L2 using lambda (is lambda necessary always??)
• If there are more than one final states in L1 then ???
• If the resultant diagram is a DFA, then stop the process else convert
the NFA to DFA using the known procedure Remove the
final state
15
Lഥ1
• For the given DFA, complement the states (i.e.) make all the final
states as non-final states and vice versa
• Check if the resultant diagram is a DFA, if not convert it to DFA using
the procedure. Complete the resultant DFA.
• What if you have a trap state in the given language?
a a a a
b a b a
A B C A B C
b b
b b
T T
a, b a, b 16
L1*
• Apply the closure property to the DFA and convert to DFA
• Add lambda transitions with two added states
a a
b a
A B C
b
b
T
a, b
17
L1 R
• Change the initial state to final state, final state(s) to initial state(s)
• Reverse the arrows in the diagram. Convert to DFA if necessary.
• Add the trap state if necessary
• What if there are more final states?
a a
a a
b a
b a A B C
A B C
b
b b
b T
T
a, b
a, b 18
Homomorphism
Ex. 1:
If R = (0∪1)∗001(0∪1)∗ and h(0) = ab and h(1) = bc then
h(R) = (ab ∪ bc)∗ababbc(ab ∪ bc)*
Ex. 2:
Let L = {0n1n |n ≥ 0} and h(0) = ab and h(1) = ba. Then
h(L) = {(ab)n(ba)n |n ≥ 0}
Ex. 3:
If h : {0, 1} → {a, b}* where h(0) = ab and h(1) = ba. Then
h(0011) = ababbaba
19
L1/ L2 Right Quotient (Cutting in Right)
L1/L2 = { a | ab ∈ L1 for some b ∈ L2 }
Example 1: L1 = { hooray, sunray, defray, ray}, L2 = { ray}
so, L1/ L2 = { hoo , sun, def, ∈ }
Example 2: L1 = {∈, a, ab, aba, abab, ... }, L2 = { b, bb, bbb, bbbb, ... }
so, L1/ L2 = { a, aba, ababa, ... }
Left Quotient (Cutting in Left)
L1/L2 = { b | ab ∈ L1 for some a ∈ L2 }
Example 1 :
L1 = { match, matter, mat, matzoth }, L2 = { mat} so, L1/L2 = { ch , ter, ∈ , zoth }
Example 2:
L1 = { 10, 100, 1010, 101110}, L2 = { 10}
so, L1/L2 = { ∈ , 0, 10, 1110 }
23-05-2022 20
L1/L2
Prove the right quotient property of regular languages on L1 = L(a∗baa∗), L2 = L(ab∗).
DFA for L1 L(A) ⋂ L2 = Φ
a a L(B) ⋂ L2 ≠ Φ
L(C) ⋂ L2 ≠ Φ
b a
A B C L(T) ⋂ L2 = Φ
Hence the right quotient of two languages is
b a a
b
T b a
A B C
a, b b
b
T
a, b
21
Example 2
• If L1 = {anbm : n ≥ 1,m ≥ 0} ∪ {ba} and L2 = {bm : m ≥ 1} , then prove that
L1/L2 = {anbm : n ≥ 1,m ≥ 0}. L(q0) ⋂ L2 = Φ
L(q1) ⋂ L2 ≠ Φ
L(q2) ⋂ L2 ≠ Φ
L(q3) ⋂ L2 = Φ
L(q4) ⋂ L2 = Φ
L(q5) ⋂ L2 = Φ Hence the right quotient
of two languages is same
as the given DFA but
with final states as q1
Non-final
and q2 alone
22
Non-Regular languages
• Is the language L = {anbn : n ≥ 0} regular?
No
But why?
23
Pumping Lemma
Let L be an infinite regular language. Then there exists some positive integer m
such that any w ∈ L with |w| ≥ m can be decomposed as
w = xyz
with
|xy| ≤ m,
and
|y| ≥ 1,
such that
wi = xyiz,
is also in L for all i = 0, 1, 2, ....
24
Identify whether regular or not
1. L = {amblak : m+l+k > 5}
Regular
2. L = {anblak : n > 5, l > 3, k >= 0}
Regular
3. L = {anblak : n >= 5, l > 3, k <= l}
Non-Regular
4. L = {uwwRv : u, v, w ∈ {a, b}+} RE: (a+b)+(aa+bb)(a+b)+
Regular
5. L = {uwwRv : u, v, w ∈ {a, b}+ and |u|<=|v|}
Non-Regular
25
Exercises
1. Find a right linear and left linear grammar for the language L(abba(ab*a)ba*)
2. Find a regular grammar that generates the language
L = {w ∈ {a, b}∗ : na (w) + 3nb (w) is odd} .
3. Find regular grammar for the following languages on {a, b}:
L = {w : (na (w) − nb (w)) mod 3 = 1} and L = {anbm : n + m is odd}
4. Let L1 = L(a*baa*) and L2 = L(aba*). Prove union, intersection, and right quotient.
5. If 𝐿1 = 𝐿(011∗ ) and 𝐿2 = 𝐿(1∗ 0). Show that L1 R , L1 , L1 L2 is regular.
6. Show that the language L = {anbkcndk : n ≥ 0, k > n} is not regular.
7. Show that the language L = {w : na (w) = nb (w)} is not regular. Is L* regular?
8. Determine whether or not the following language on Σ = {a} is regular. Prove your
answer.
L = {an : n is not a prime number}.
26