Compiler Construction Lecture 5-6
Compiler Construction Lecture 5-6
COMPILER CONSTRUCTION(CS-636)
______________________________________________________
GIMS- PMAS Arid Agriculture University, Gujrat Campus
Lecture 5 2
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.
NFA → DFA Construction 8
Algorithm:
Input:
NFA N with state set S,
alphabet S, start state
s0, final states F
Subset Construction 14
Output:
DFA D with state set
S’, alphabet S, start
states
s0’ = e-closure(s0),
final states F’,
transition
table: S’ x S → S’
Subset Construction Example
15
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
e
a 16
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}
e
a 17
2e 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.
e
18
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}.
e
19
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))
e
a 20
2e 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.
e 21
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.
e
22
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}
e 23
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
e 24
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
e
25
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
e
26
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
e
27
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)),
e
28
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))
e 29
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.
e
30
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 only 211 (!) different subsets of a
set of 11 states.
e 31
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.
Subset Construction 32
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}
e 33
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}
e 34
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}
Resulting DFA 35
a a
a b b
A B D E
a
b a
b
C
b
DFA for (a | b )*abb
Resulting DFA 36
a a
a b b
A B D E
a
b a
b
C
b a a a a b b
Final Transition Table
Input symbol
State a b
A B C
B B D
C B C
D B E
E B C
37