Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 32

Compiler

Construction

Lecture 5
NFA → DFA Construction
 The algorithm is called subset
construction.

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
 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.
4
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.

5
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.

6
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).

7
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).

8
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.

9
Subset Construction Example
e
a
2 3
e e
e e a b b
0 1 6 7 8 9 10
e e
b
4 5
e
NFA for (a | b )*abb
10
e
a
2e 3
e e
e e a b b
0 1 6 7 8 9 10
e e
b
4 5
e
 The start state of equivalent
DFA is e-closure(0), which is
A = {0,1,2,4,7}
11
e
2e a 3
e e
e e a b b
0 1 6 7 8 9 10
e e
b
4 5
e
 A = {0,1,2,4,7}, these are
exactly the states reachable
from state 0 via e-transition.
12
e
2e a 3
e e
e e a b b
0 1 6 7 8 9 10
e e
b
4 5
e

 The input symbol alphabet


here is {a,b}.
13
e
2e a 3
e e
e e a b b
0 1 6 7 8 9 10
e e
b
4 5
e
 The algorithm tells us to mark
A and then compute
e-closure(move(A,a))
14
e
2e a 3
e e
e e a b b
0 1 6 7 8 9 10
e e
b
4 5
e
 move(A,a)), is the set of states
of NFA that have transition on
‘a’ from members of A.
15
e
2e a 3
e e
e e a b b
0 1 6 7 8 9 10
e e
b
4 5
e

 Only 2 and 7 have such


transition, to 3 and 8.
16
e
2e a 3
e e
e e a b b
0 1 6 7 8 9 10
e e
b
4 5
e
 So, e-closure(move(A,a)) =
e-closure({3,8}) =
{1,2,3,4,6,7,8}
17
e
2e a 3
e e
e e a b b
0 1 6 7 8 9 10
e e
b
4 5
e
 Let B = {1,2,3,4,6,7,8}.
 Thus Dtran[A,a] = B
18
e
2e a 3
e e
e e a b b
0 1 6 7 8 9 10
e e
b
4 5
e
 For input b, among states in A,
only 4 has transition on b to 5
19
e
2e a 3
e e
e e a b b
0 1 6 7 8 9 10
e e
b
4 5
e
 C = e-closure({5})
= {1,2,4,5,6,7}
 Thus, Dtran[A,b] = C
20
e
a
2e 3
e e
e e a b b
0 1 6 7 8 9 10
e e
b
4 5
e

 We continue this process with


the unmarked sets B and C
21
e
a
2e 3
e e
e e a b b
0 1 6 7 8 9 10
e e
b
4 5
e

 i.e., e-closure(move(B,a)),
e-closure(move(B,b)),
22
e
a
2e 3
e e
e e a b b
0 1 6 7 8 9 10
e e
b
4 5
e

 e-closure(move(C,a)) and
e-closure(move(C,b))
23
e
a
2e 3
e e
e e a b b
0 1 6 7 8 9 10
e e
b
4 5
e
 Until all sets and states of DFA
are marked.
24
e
a
2e 3
e e
e e a b b
0 1 6 7 8 9 10
e e
b
4 5
e
 This is certain since there are
11
only 2 (!) different subsets of
a set of 11 states.
25
e
a
2e 3
e e
e e a b b
0 1 6 7 8 9 10
e e
b
4 5
e

 And a set, once marked, is


marked forever.
26
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}
27
e
a
2e 3
e e
e e a b b
0 1 6 7 8 9 10
e b
e
4 5
e
A is start 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}
28
e
a
2e 3
e e
e e a b b
0 1 6 7 8 9 10
e b
e
4 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}
29
Final Transition Table
Input symbol
State a b
A B C
B B D
C B C
D B E
E B C
30
Resulting DFA
a a
a b b
A B D E
a
b a
b
C

b
DFA for (a | b )*abb
31
Resulting DFA
a a
a b b
A B D E
a
b a
b
C

b a a a a b b
32

You might also like