Tutorial Six
Tutorial Six
Models of Computation
Tutorial Six:
Correspondence between NFA and Regular Grammars
Conversion from NFA to Regular Grammar
Conversion from Regular Grammars to Regular Expressions
Lecture Instructor : Prof. Dr. Ahmed El Nahas
Tutorial Instructor: Eng. Ahmed Abdelhamid Waheed Abdelwahab
1
Representations of Finite State Machines
So far we have discovered a single representation of finite state
machines using state graphs.
Complexity of representing graphs is expensive in terms of memory.
Hard to deduce corresponding regular expression for the finite automaton we
built (state elimination procedure covered later on).
Another representation is with using finite set of rules that
corresponds to the candidate finite automaton.
Once the rules are given, they can be solved in algebraic like notion to deduce
the regular expression.
2
Quick Review: Right Linear Grammars and NFA(1)
• A right linear grammar G is a quadruple G=(V, Ʃ, S, P)
V is a finite set of (meta) symbols or variables.
Ʃ is a finite set of terminal symbols.
SϵV is a distinguished element of V called the start symbol.
P is a finite set of productions having one of two forms:
o VƩ*V OR VƩ*
3
Quick Review: Right Linear Grammars and NFA(2)
4
Quick Review: Right Linear Grammars and NFA(3)
• Additional Hint
• If there exists a rule A σ1σ2σ3σn for A ϵ V and σ1,σ2,σ3,…σn ϵ Ʃ
• Then corresponding conversion is in the form:
σ1 σn
σ2
A
5
Illustration
6
Exercises(1)
1. Construct NFA that recognizes the language generated by the
regular grammar G=(V, Ʃ, S, P) where V={S, A}, Ʃ={0, 1}, and the
productions in P are:
S1A, S0, SԐ, A0A, A1
7
Illustration
8
Solution
1 1
S A 3
9
Exercises(2)
2. Construct NFA that recognizes the language generated by the
regular grammar G=(V, Ʃ, S, P) where V={S, A, B}, Ʃ={0, 1}, and the
productions in P are:
S0A, S1B, A0, AB, B0
10
Illustration
11
Solution
0 0
S A 3
1 Ԑ
0
B 3
12
Exercises(3)
3. Construct NFA that recognizes the language generated by the
regular grammar G=(V, Ʃ, S, P) where V={S, A, B}, Ʃ={0, 1}, and the
productions in P are:
S1A, S0, SԐ, A0B, B1B, B1
13
Illustration
14
Solution
1 0 1
S A B 3
15
Exercises(4)
4. Construct NFA that recognizes the language generated by the
regular grammar G=(V, Ʃ, S, P) where V={S, A, B}, Ʃ={0, 1}, and the
productions in P are:
S1B, S0, A1A, A0B, A1, A0, B1
16
Illustration
17
Solution
1 1
S B 3
0
0 3
3 1
A
3
1
0
18
Exercises(5)
5. Find a regular grammar generating the language accepted by the FA
shown below. a
b
b
a a
A B B
C
b
D
a, b
19
Illustration
20
Solution
• G=( {A, B, C, D}, {a, b}, P, A) where P is given by:
• A aB A bD AԐ
• B aB B aC B bC
• C bC CԐ
• D aD D bD
21
Exercises(6)
6. Find a regular grammar that generates the regular set recognized by
the NFA shown below 0, 1
0 1
S A
B B
22
Illustration
23
Solution
G=( {S, A, B}, {0, 1}, P, S) where P is given by:
• S0A S1B
• A1B AԐ
• B0B B1B
24
Exercises(7)
7. Find a regular grammar that generates the regular language
recognized by the FA shown below
1
1
S A
0
0, 1
0
B
C
B
0
1
25
Illustration
26
Solution
G=( {S, A, B, C}, {0, 1}, P, S) where P is given by:
• S0B S1A
• A0B A1A AԐ
• B0B B1C
• C0C C1C CԐ
27
Exercises(8)
8. Find a regular grammar generating the language accepted by the FA
shown below. a
b
b
a
A B B
C
b
D
a, b
28
Illustration
29
Solution
• G=({A, B, C, D}, {a, b}, P, A) where P is given by:
• A aB A bD AԐ
• B aB B bC
• C aB C bC CԐ
• D aD D bD
30
Quick Review: RE Simplification Rules
31
Exercises(9)
9. Convert following regular grammar into regular expression:
G=({S,R}, {a, b}, S, P) where P is
SaS, SbR, SԐ
RaS
32
Illustration
33
Solution
• Write Productions as system of linear equations (Replace epsilons
with 1):
S=aS+bR+1 eq.(1)
R=aS eq.(2)
• Substitute eq.(2) in eq.(1)
S=aS + baS+1
=(a + ba)S+1
Apply Rule(22) : Q=P+RQ=R*P
S=(a+ba)*1
=(a+ba)*
34
Exercises(10)
10. Convert following regular grammar into regular expression:
G=({a,b}, {S,A,B}, ϕ, S) where ϕ consists of:
SaA SbB Sb
AbA AԐ
BbS
35
Illustration
36
Solution
• Write Productions as system of linear equations (Replace epsilons
with 1):
S=aA+bB+b eq.(1)
A=bA+1 eq.(2)
B=bS eq.(3)
• From eq.(2) we deduce that A=b* eq.(4) (Q=RQ+P=R*P (Rule 22))
• Substitute eq.(3) and eq.(4)
S=ab* + bbS+b
=bbS+(ab*+b)
Apply Rule(22) : Q=R*P=P+RQ
=(bb)*(ab*+b)
37
Exercises(11)
11. Convert following regular grammar into regular expression:
G=({X,Y,Z}, {a, b, c}, ϕ, X) where ϕ consists of:
XaX XbY
YcX YaZ YbZ
ZԐ
38
Illustration
39
Solution
• Write Productions as system of linear equations (Replace epsilons with 1):
X=aX + bY eq (1)
Y=cX + aZ+ bZ eq (2)
Z=1 eq (3)
• Substitute eq.(3) in eq.(2)
Y = cX + a + b eq (4)
• Substitute eq.(4) in eq.(1)
X = aX + b(cX + a + b)
X = aX + bcX + ba +bb
X = (a+bc)X + ba + bb
Apply (Q=RQ+P=R*P (Rule 22)) on above getting: (a+bc)*(ba+bb)
40