You are on page 1of 9

Module - 3

DFA to Regular Grammar


Dr. S K Somasundaram
Assistant Professor Senior Grade 2
School of Computer Science and Engineering,
Vellore Institute of Technology,
Vellore – 632014
Phone No: +91 9843665115
Mail ID: somasundaram.sk@vit.ac.in
Location: PRP Block – 218D
Constructing Regular Grammar from DFA
• M = (Q, ∑, Ꟙ, q0, f) • G = (Vn, ∑, P, S)
1. Vn = {A0, A1, A2, …}
2. ∑ → same for both M & G
3. Production rule
a) If Ꟙ(P, a)=Q ∉F, then P→aQ
b) If Ꟙ(P, a)=Q ∈F, then P→aQ and P→a
4. First production is Start Symbol, S
Example1
• Construct a Regular grammar for the following DFA
b a,b

a b b D
A B C

• Soln: Vn = {A, B ,C, D}


Q = {A, B ,C, D} T = {a, b}
∑ = {a, b} P:
A → aB | bA
q0 = {A} B → aA | bC
F = {D} C → aA |bD | b
D → aD | bD | a| b
S = {A}
Example 2
• Construct a Regular grammar for the following DFA
1
start A B
1
0 0 0 0
1
C 1 D
Vn = {A, B ,C, D}
Soln: T = {0, 1}
Q = {A, B ,C, D} P:
∑ = {0, 1} A → 0C | 1B | ε
q0 = {A} B → 0D | 1A | 1
F = {A} C → 0A |1D | 0
D → 0B | 1C
S = {A}
Example 3
Give regular grammars equivalent to the following finite state
automata

Solution
Exercises
• Give regular grammars
equivalent to the following
finite state automata:
Regular Grammar to Finite Automata
• G = (Vn, T, P, S) • M = (Q, ∑, Ꟙ, q0, F)
Vn = {A0, A1, A2, …} Q = Vn
∑ → same for both M & G ∑=T
Q0 = S
Ꟙ:
If P→aQ then Ꟙ(P,a)=Q
If P→a then Ꟙ(P,a)={F}
Example 1
Construct a FA from the following regular grammar:
S → 0A | 1A
A → 0A | 1A | +B | -B
Ꟙ:
B → 0B | 1B | 0 | 1 Ꟙ(S,0) = A
Ꟙ(S,1) = A
Soln: Ꟙ(A,0) = A
Ꟙ(A,1) = A
M = (Q, ∑, Ꟙ, q0, F) Ꟙ(A,+) = B
Q = {S,A,B,C} Ꟙ(A,-) = B
Ꟙ(B,0) = B
∑ = {0, 1, +, -} Ꟙ(B,1) = B
Q0 = S Ꟙ(B,0) = C
Ꟙ(B,1) = C
Example 2
Construct a FA from the following regular grammar:
S → a | aA | bB | ε
A → aA | aS
B → cS | ε

Soln:
Convert to ε-free grammar
S → a | aA | bB | b
A → aA | aS |a
B → cS | c
S1 → a | aA | bB | b | ε

You might also like