0% found this document useful (0 votes)
5 views

Lecture 08

This document describes the algorithm to convert a non-deterministic finite automaton (NFA) to a deterministic finite automaton (DFA). It involves constructing the DFA states as sets of NFA states, and using epsilon-closure and transition functions to build the DFA transitions and states. The example and steps clearly illustrate how to apply the subset construction algorithm.

Uploaded by

Hammad Rajput
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lecture 08

This document describes the algorithm to convert a non-deterministic finite automaton (NFA) to a deterministic finite automaton (DFA). It involves constructing the DFA states as sets of NFA states, and using epsilon-closure and transition functions to build the DFA transitions and states. The example and steps clearly illustrate how to apply the subset construction algorithm.

Uploaded by

Hammad Rajput
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 39

Compiler Construction

LECTURE 8
NFA → DFA Construction 2
 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
NFA → DFA Construction 3

The general idea behind NFA-


to-DFA construction is that
each DFA state corresponds to
a set of NFA states.
NFA → DFA Construction 4

The DFA uses its state to keep


track of all possible states the
NFA can be in after reading
each input symbol.
NFA → DFA Construction 5

We will use the following


operations.
-closure(T):
set of NFA states reachable
from some NFA state s in T on
-transitions alone.
NFA  DFA Construction 6

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

Before it sees the first input


symbol, NFA can be in
any of the state in the set
-closure(s0), where s0 is the
start state of the NFA.
NFA → DFA Construction 8

Suppose that exactly the states


in set T are reachable from s0
on a given sequence of input
symbols.
NFA → DFA Construction 9

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).
NFA → DFA Construction 10

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).
NFA → DFA Construction 11

When we allow for


-transitions, NFA can be in
any of the states in
-closure(move(T,a))
after seeing a.
Subset Construction 12

Algorithm:
Input:
NFA N with state set S,
alphabet , start state s0,
final states F
Subset Construction 13

Output:
DFA D with state set S’,
alphabet , start states
s0’ = -closure(s0), final
states F’, transition
table: S’ x  → S’
Algorithm 14

// initially, e-closure(s0) is the only


// state in D states S’ and it is
// unmarked
s0’ = -closure(s0)
S’ = {s0’ } (unmarked)
while there is some unmarked state T 15
in S’
mark state T
for all a in  do
U = -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
Algorithm 16

F’:
for each DFA state S
if S contains an NFA final state
mark S as DFA final state
Subset Construction Example 17

a
2 3
 
  a b b
0 1 6 7 8 9 10
 
b
4 5

NFA for (a | b )*abb

a 18
2 3
 
  a b b
0 1 6 7 8 9 10
 
b
4 5

 The start state of equivalent DFA is -closure(0), which is
A = {0,1,2,4,7}

a 19
2 3
 
  a b b
0 1 6 7 8 9 10
 
b
4 5

 A = {0,1,2,4,7}, these are exactly the states reachable from state 0 via -
transition.

20
2 a 3
 
  a b b
0 1 6 7 8 9 10
 
b
4 5

 The input symbol alphabet here is {a,b}.

21
2 a 3
 
  a b b
0 1 6 7 8 9 10
 
b
4 5

 The algorithm tells us to mark A and then compute
-closure(move(A,a))

a 22
2 3
 
  a b b
0 1 6 7 8 9 10
 
b
4 5

 move(A,a)), is the set of states of NFA that have transition on ‘a’ from
members of A.
 23
2 a
3
 
  a b b
0 1 6 7 8 9 10
 
b
4 5

 Only 2 and 7 have such transition, to 3 and 8.

24
2 a 3
 
  a b b
0 1 6 7 8 9 10
 
b
4 5

 So, -closure(move(A,a)) =
-closure({3,8}) =
{1,2,3,4,6,7,8}
 25
2 a 3
 
  a b b
0 1 6 7 8 9 10
 
b
4 5

 Let B = {1,2,3,4,6,7,8}.
 Thus Dtran[A,a] = B
 26
2 a 3
 
  a b b
0 1 6 7 8 9 10
 
b
4 5

 For input b, among states in A, only 4 has transition on b to 5

27
2 a 3
 
  a b b
0 1 6 7 8 9 10
 
b
4 5

 C = -closure({5})
= {1,2,4,5,6,7}
 Thus, Dtran[A,b] = C

28
a
2 3
 
  a b b
0 1 6 7 8 9 10
 
b
4 5

 We continue this process with the unmarked sets B and C

29
a
2 3
 
  a b b
0 1 6 7 8 9 10
 
b
4 5

 i.e., -closure(move(B,a)),
-closure(move(B,b)),

30
a
2 3
 
  a b b
0 1 6 7 8 9 10
 
b
4 5

 -closure(move(C,a)) and
-closure(move(C,b))
 31
a
2 3
 
  a b b
0 1 6 7 8 9 10
 
b
4 5

 Until all sets and states of DFA are marked.

32
a
2 3
 
  a b b
0 1 6 7 8 9 10
 
b
4 5

 This is certain since there are only 211 (!) different subsets of a set of 11
states.
 33
a
2 3
 
  a b b
0 1 6 7 8 9 10
 
b
4 5

 And a set, once marked, is marked forever.
Subset Construction 34
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}
 35
a
2 3
 
  a b b
0 1 6 7 8 9 10
 b

4 5

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}
 36
a
2 3
 
  a b b
0 1 6 7 8 9 10
 b

4 5

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 37
a a
a b b
A B D E
a
b a
b
C

b
DFA for (a | b )*abb
Resulting DFA 38
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 C39

You might also like