What is a compiler?

Compiler
Construction
Sohail Aslam
Lecture 8
NFA → DFA Construction
 The algorithm is called subset
construction.
 In the transition table of an
NFA, each entry is a set of
states.
 In DFA, each entry is a single
state
2
NFA → DFA Construction
 The general idea behind
NFA-to-DFA construction is
that each DFA state
corresponds to a set of
NFA states.
3
NFA → DFA Construction
 The DFA uses its state to
keep track of all possible
states the NFA can be in
after reading each input
symbol.
4
NFA → DFA Construction
 We will use the following
operations.
 e-closure(T):
set of NFA states reachable
from some NFA state s in T
on e-transitions alone.
5
NFA  DFA Construction
 move(T,a):
set of NFA states to which
there is a transition on input
a from some NFA state s in
set of states T.
6
NFA → DFA Construction
 Before it sees the first input
symbol, NFA can be in
any of the state in the set
e-closure(s0), where s0 is the
start state of the NFA.
7
NFA → DFA Construction
 Suppose that exactly the
states in set T are
reachable from s0 on a
given sequence of input
symbols.
8
NFA → DFA Construction
 Let a be the next input
symbol.
 On seeing a, the NFA can
move to any of the states in
the set move(T,a).
9
NFA → DFA Construction
 Let a be the next input
symbol.
 On seeing a, the NFA can
move to any of the states in
the set move(T,a).
10
NFA → DFA Construction
 When we allow for
e-transitions, NFA can be in
any of the states in
e-closure(move(T,a))
after seeing a.
11
Subset Construction
Algorithm:
Input:
NFA N with state set S,
alphabet S, start state
s0, final states F
12
Subset Construction
Output:
DFA D with state set S’,
alphabet S, start states
s0’ = e-closure(s0), final
states F’, transition
table: S’ x S → S’
13
Algorithm
// initially, e-closure(s0) is the only
// state in D states S’ and it is
// unmarked
s0’ = e-closure(s0)
S’ = {s0’ } (unmarked)
14
while there is some unmarked state T
in S’
mark state T
for all a in S do
U = e-closure( move(T,a) );
if U not already in S’
add U as an unmarked state to S’
Dtran(T,a) = U;
end for
end while
15
Algorithm
F’:
for each DFA state S
if S contains an NFA final state
mark S as DFA final state
16
Subset Construction Example
e
0
e
e
2
a
3
e
6
1
e
4
b
e
e
7
a
8
b
9
b
10
e
5
NFA for (a | b )*abb
17
e
0
e
e
2e
a
3
6
1
e
e
e
4
b
e
e
7
a
8
b
9
b
10
5
 The start state of equivalent
DFA is e-closure(0), which is
A = {0,1,2,4,7}
18
e
0
e
e
2e a 3
6
1
e
e
e
4
b
e
7
a
8
b
9
b
e
10
5
 A = {0,1,2,4,7}, these are
exactly the states reachable
from state 0 via e-transition.
19
e
0
e
e
2e a 3
6
1
e
e
e
4
b
e
7
a
8
b
9
b
e
10
5
 The input symbol alphabet
here is {a,b}.
20
e
0
e
e
2e a 3
6
1
e
e
e
4
b
e
e
7
a
8
b
9
b
10
5
 The algorithm tells us to mark A
and then compute
e-closure(move(A,a))
21
e
0
e
e
2e a 3
6
1
e
e
e
4
b
e
e
7
a
8
b
9
b
10
5
 move(A,a)), is the set of states
of NFA that have transition on
‘a’ from members of A.
22
e
0
e
e
2e a 3
6
1
e
e
e
4
b
e
7
a
8
b
e
9
b
10
5
 Only 2 and 7 have such
transition, to 3 and 8.
23
e
0
e
e
2e a 3
6
1
e
e
e
4
b
e
7
a
8
b
9
b
e
10
5
 So, e-closure(move(A,a)) =
e-closure({3,8}) =
{1,2,3,4,6,7,8}
24
e
0
e
e
2e a 3
6
1
e
e
e
4
b
e
7
a
8
b
e
9
b
10
5
 Let B = {1,2,3,4,6,7,8}.
 Thus Dtran[A,a] = B
25
e
0
e
e
2e a 3
6
1
e
e
e
4
b
e
e
7
a
8
b
9
b
10
5
 For input b, among states in A,
only 4 has transition on b to 5
26
e
0
e
e
2e a 3
6
1
e
e
e
4
b
e
7
a
8
e
b
9
b
10
5
 C = e-closure({5})
= {1,2,4,5,6,7}
 Thus, Dtran[A,b] = C
27
e
0
e
e
2e
a
3
6
1
e
e
e
4
b
e
e
7
a
8
b
9
b
10
5
 We continue this process with
the unmarked sets B and C
28
e
0
e
e
2e
a
3
6
1
e
e
e
4
b
e
7
a
8
b
9
b
e
10
5
 i.e., e-closure(move(B,a)),
e-closure(move(B,b)),
29
e
0
e
e
2e
a
3
6
1
e
e
e
4
b
e
7
a
8
b
9
e
b
10
5
 e-closure(move(C,a)) and
e-closure(move(C,b))
30
e
0
e
e
2e
a
3
6
1
e
e
e
4
b
e
e
7
a
8
b
9
b
10
5
 Until all sets and states of DFA
are marked.
31
e
0
e
e
2e
a
3
6
1
e
e
e
4
b
e
e
7
a
8
b
9
b
10
5
 This is certain since there are
11
only 2 (!) different subsets of
a set of 11 states.
32
e
0
e
e
2e
a
3
6
1
e
e
e
4
b
e
7
a
8
b
9
b
e
10
5
 And a set, once marked, is
marked forever.
33
Subset Construction
Eventually, the 5 sets are:
A={0,1,2,4,7}
B={1,2,3,4,6,7,8}
C={1,2,4,5,6,7}
D={1,2,4,5,6,7,9}
E={1,2,4,5,6,7,10}
34
e
0
e
e
2e
a
3
6
1
e
e
4
b
e
e
7
a
8
b
9
b
10
5
e
A is start state
A={0,1,2,4,7}
B={1,2,3,4,6,7,8}
C={1,2,4,5,6,7}
D={1,2,4,5,6,7,9}
E={1,2,4,5,6,7,10}
35
e
0
e
e
2e
a
3
6
1
e
e
4
b
e
e
7
a
8
b
9
b
10
5
e
E is accepting state
A={0,1,2,4,7}
D={1,2,4,5,6,7,9}
B={1,2,3,4,6,7,8} E={1,2,4,5,6,7,10}
C={1,2,4,5,6,7}
36
Resulting DFA
a
a
A
b
a
B
a
b
D
b
E
a
b
C
b
DFA for (a | b )*abb
37
Resulting DFA
a
a
A
b
a
B
a
b
D
b
E
a
b
C
b
a a a a b b
38
Final Transition Table
State
A
B
C
D
E
Input symbol
a
b
B
C
B
D
B
C
B
E
B
C
39