You are on page 1of 50

COMPILER CONSTRUCTION

Instructor:
Mr. Sheraz Babar

1
Lecture 08

2
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
3
NFA → DFA Construction
 The general idea behind
NFA-to-DFA construction is
that each DFA state
corresponds to a set of
NFA states.

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

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

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

8
NFA → DFA Construction
 Suppose that exactly the
states in set T are
reachable from s0 on a
given sequence of input
symbols.

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

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

12
Subset Construction
Algorithm:
Input:
NFA N with state set S,
alphabet S, start state
s0, final states F
13
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’
14
Algorithm
// initially, e-closure(s0) is the only
// state in D states S’ and it is
// unmarked
s0’ = e-closure(s0)
S’ = {s0’ } (unmarked)

15
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
16
Algorithm
F’:
for each DFA state S
if S contains an NFA final state
mark S as DFA final state

17
Conversion from NFA to DFA
An NFA can have zero, one or more than one move from a given
state on a given input symbol. An NFA can also have NULL moves
(moves without input symbol). On the other hand, DFA has one and
only one move from a given state on a given input symbol.
Steps for converting NFA to DFA:
Step 1: Convert the given NFA to its equivalent transition table
To convert the NFA to its equivalent transition table, we need to list
all the states, input symbols, and the transition rules. The transition
rules are represented in the form of a matrix, where the rows
represent the current state, the columns represent the input
symbol, and the cells represent the next state.
Step 2: Create the DFA’s start state
The DFA’s start state is the set of all possible starting states in the
NFA. This set is called the “epsilon closure” of the NFA’s start state.
The epsilon closure is the set of all states that can be reached from
the start state by following epsilon (λ) transitions. 18
Conversion from NFA to DFA
Step 3: Create the DFA’s transition table
The DFA’s transition table is similar to the NFA’s transition table,
but instead of individual states, the rows and columns represent
sets of states. For each input symbol, the corresponding cell in the
transition table contains the epsilon closure of the set of states
obtained by following the transition rules in the NFA’s transition
table.
Step 4: Create the DFA’s final states
The DFA’s final states are the sets of states that contain at least
one final state from the NFA.

19
Conversion from NFA to DFA
Step 5: Simplify the DFA
The DFA obtained in the previous steps may contain unnecessary
states and transitions. To simplify the DFA, we can use the
following techniques:
•Remove unreachable states: States that cannot be reached from
the start state can be removed from the DFA.
•Remove dead states: States that cannot lead to a final state can
be removed from the DFA.
•Merge equivalent states: States that have the same transition
rules for all input symbols can be merged into a single state.
Step 6: Repeat steps 3-5 until no further simplification is
possible
After simplifying the DFA, we repeat steps 3-5 until no further
simplification is possible. The final DFA obtained is the minimized
DFA equivalent to the given NFA.
20
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
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
 The start state of equivalent
DFA is e-closure(0), which is
A = {0,1,2,4,7}
22
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.
23
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}.
24
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))
25
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.
26
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.
27
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}
28
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
29
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
30
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
31
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
32
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)),
33
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))
34
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.
35
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.
36
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.
37
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}
38
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}
39
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}
40
Resulting DFA
a a
a b b
A B D E
a
b a
b
C

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

b a a a a b b
42
Final Transition Table
Input symbol
State a b
A B C
B B D
C B C
D B E
E B C
43
Example:
 Consider the following NFA
shown in Figure 1.

44
Example:
 Following are the various parameters for
NFA. Q = { q0, q1, q2 } ∑ = ( a, b ) F = { q2 }
δ (Transition Function of NFA)

45
Example:
 Step 1: Q’ = ɸ Step 2: Q’ = {q0} Step 3: For each state in Q’, find
the states for each input symbol. Currently, state in Q’ is q0, find
moves from q0 on input symbol a and b using transition function
of NFA and update the transition table of DFA. δ’ (Transition
Function of DFA)

46
Example:
 Now { q0, q1 } will be considered as a single state. As its entry
is not in Q’, add it to Q’. So Q’ = { q0, { q0, q1 } } Now, moves
from state { q0, q1 } on different input symbols are not present
in transition table of DFA, we will calculate it like: δ’ ( { q0, q1 },
a ) = δ ( q0, a ) ∪ δ ( q1, a ) = { q0, q1 } δ’ ( { q0, q1 }, b ) = δ (
q0, b ) ∪ δ ( q1, b ) = { q0, q2 } Now we will update the transition
table of DFA. δ’ (Transition Function of DFA)

47
Example:
 Now { q0, q2 } will be considered as a single state. As its entry
is not in Q’, add it to Q’. So Q’ = { q0, { q0, q1 }, { q0, q2 } } Now,
moves from state {q0, q2} on different input symbols are not
present in transition table of DFA, we will calculate it like: δ’ ( {
q0, q2 }, a ) = δ ( q0, a ) ∪ δ ( q2, a ) = { q0, q1 } δ’ ( { q0, q2 }, b
) = δ ( q0, b ) ∪ δ ( q2, b ) = { q0 } Now we will update the
transition table of DFA. δ’ (Transition Function of DFA)

48
Example:
 As there is no new state generated, we are done with the
conversion. Final state of DFA will be state which has q2 as its
component i.e., { q0, q2 } Following are the various parameters
for DFA. Q’ = { q0, { q0, q1 }, { q0, q2 } } ∑ = ( a, b ) F = { { q0,
q2 } } and transition function δ’ as shown above. The final DFA
for above NFA has been shown in Figure 2.

49
The End

50

You might also like