Professional Documents
Culture Documents
Lecture#13, Chap#3 (Lexical Analyzer Generator (Part-III), NFA To DFA Conversion)
Lecture#13, Chap#3 (Lexical Analyzer Generator (Part-III), NFA To DFA Conversion)
Lecture#13, Chap#3 (Lexical Analyzer Generator (Part-III), NFA To DFA Conversion)
Compiler Construction
Lexical Analyzer Generators (NFA to DFA Conversion),
Thompson Construction Algorithm,
(Part-III)
by Safdar Hussain
Khwaja Fareed University, RYK
Pakistan
Topics
• Lexical Analyzer Generator Design, NFA & DFA
• Subset Construction Algorithm, Simulating a DFA
• NFA to DFA Conversion (Thompson Construction Algorithm)
Overview
This chapter contains comprehensive material
• Building a simple Lexical Analyzer
– Lexical Analysis (Every perspective)
– Interaction of Lexical analyzer & Parsing
– Implementing Transition Diagrams
– Design of a Lexical Analyzer (RENFADFA)
– The Subset Construction Algorithm
– Minimization of number of states of a DFA
– Conversion RE to DFA directly (Algorithm & Tree
annotation)
regular
NFA DFA
expressions
Simulating a DFA s s0
c nextchar;
while c eof do
s move(s,c);
c nextchar;
end;
if s is in F then return “yes”
else return “no”
Safdar Hussain, Khwaja Fareed University, RYK 10
Example – NFA to DFA
NFA: RE = (a | b)*abb
a
start a b b
0 1 2 3
DFA:
b
a
start a b b
0 1 2 3
a
b a
What Language is Accepted?
Safdar Hussain, Khwaja Fareed University, RYK 11
The Subset Construction Algorithm
Initially, -closure(s0) is the only state in Dstates and it is unmarked
while there is an unmarked state T in Dstates do
mark T
for each input symbol a do
U := -closure(move(T,a))
if U is not in Dstates then
add U as an unmarked state to Dstates
end if
Dtran[T,a] := U
end do
end do
b : -closure(move(A,b)) = -closure(move({0,1,2,4,7},b))
adds {5} ( since move(4,b)=5)
b : -closure(move(B,b)) = -closure(move({1,2,3,4,6,7,8},b))}
= {1,2,4,5,6,7,9} = D
Define Dtran [B,b] = D.
b : -closure(move(C,b)) = -closure(move({1,2,4,5,6,7},b))}
= {1,2,4,5,6,7} = C
Define Dtran [C,b] = C.
Safdar Hussain, Khwaja Fareed University, RYK 16
Conversion Example – continued
5th , we calculate for state D on {a,b}
a : -closure(move(D,a)) = -closure(move({1,2,4,5,6,7,9},a))}
= {1,2,3,4,6,7,8} = B
Define Dtran[D,a] = B.
b : -closure(move(D,b)) = -closure(move({1,2,4,5,6,7,9},b))}
= {1,2,4,5,6,7,10} = E
Define Dtran[D,b] = E.
b : -closure(move(E,b)) = -closure(move({1,2,4,5,6,7,10},b))}
= {1,2,4,5,6,7} = C
Define Dtran[E,b] = C.
Safdar Hussain, Khwaja Fareed University, RYK 17
Conversion Example – continued
This gives the transition table Dtran for the DFA of:
Input Symbol
Dstates a b
A B C
B B D
C B C
D B E
E B C
b
b C b
start A a B b D b E
a
a a
?
C A = {0,1,3,7}
b a
b b B = {2,4,7}
start C = {8}
A D
D = {7}
a a
b b E = {5,8}
B E F
Solve step-by-step by yourself F = {6,8}
a1 a3 a2 a3
Safdar Hussain, Khwaja Fareed University, RYK 19
Converting NFA to DFA – Example 3
a 3 b 4
2
0 1 5 8
6 c 7
0, 1, 2, 6, 8 a 3
a
c b
1, 2, 5, 6, 7, 8
c 1, 2, 4, 5, 6, 8
c
Which States are FINAL States ?
a
A How do we handle
B
a a alphabet symbols not
c b defined for A, B, C, D ?
D
c C
c
a
1 C
start a b
A B E 3 4 5
b
b #
2 D FF 6
b
b
a a a 22
The End
23