×

Close

Type:
**Note**Institute:
**
KIET GROUP OF INSTITUTIONS
**Views:
**20**Uploaded:
**8 months ago**Add to Favourite

Lexical Analysis - Part 3
Converting an NFA to a DFA
Given:
A non-deterministic finite state machine (NFA)
Goal:
Convert to an equivalent deterministic finite state machine (DFA)
Why?
Faster recognizer!
Approach:
Consider simulating a NFA.
Work with sets of states.
IDEA: Each state in the DFA will correspond to a set of NFA states.
Worst-case:
There can be an exponential number O(2N) of sets of states.
The DFA can have exponentially many more states than the NFA
... but this is rare.
© Harry H. Porter, 2005
1
Lexical Analysis - Part 3
NFA to DFA
Input: A NFA
S = States = { s0, s1, ..., sN} = SNFA
! = Move function = MoveNFA
Move’(S, a) " Set of states
Output: A DFA
S = States = {?, ?, ..., ?} = SDFA
! = Move function = MoveDFA
Move(s, a) " Single state from SDFA
Main Idea:
Each state in SDFA will be a set of states from the NFA
SDFA = { {...}, {...} , ..., {...} }
3
5
7
a
a
{3}
NFA
a
{5,7}
DFA
(The names of the states is arbitrary and can be changed later, if desired.)
1
© Harry H. Porter, 2005
a
2
2

Lexical Analysis - Part 3
Algorithm: Convert NFA to DFA
We’ll use...
MoveNFA(S,a)
#-Closure(s)
#-Closure(S)
the transition function from NFA
where s is a single state from NFA
where S is a set of states from NFA
We’ll construct...
SDFA
the set of states in the DFA
Initially, we’ll set SDFA to {}
Add X to SDFA where X is some set of NFA states
Example: “Add {3,5,7} to SDFA”
We’ll “mark” some of the states in the DFA.
Marked = “We’ve done this one” ($)
Unmarked = “Still need to do this one”
The transition function from DFA
To add an edge to the growing DFA...
MoveDFA(T,b)
Set MoveDFA(T,b) to S
b
T
S
...where S and T are sets of NFA states
© Harry H. Porter, 2005
3
Lexical Analysis - Part 3
Example
0
#
#
1
#
Start state:
#-Closure (0)
=
© Harry H. Porter, 2005
2
4
a
b
#
3
5
%= {a,b}
#
6
#
#
7
a
8
b
9
b
10
#
4

Lexical Analysis - Part 3
Example
0
#
#
1
#
Start state:
#-Closure (0)
= {0, 1, 2, 4, 7}
2
4
a
b
#
3
5
%= {a,b}
#
6
#
7
#
a
8
b
9
b
10
#
© Harry H. Porter, 2005
5
Lexical Analysis - Part 3
Example
0
#
#
1
#
Start state:
#-Closure (0)
= {0, 1, 2, 4, 7} = A
© Harry H. Porter, 2005
2
4
a
b
#
3
5
%= {a,b}
#
6
#
#
7
a
8
b
9
b
10
#
6

Lexical Analysis - Part 3
Example
0
#
#
1
#
Start state:
#-Closure (0)
= {0, 1, 2, 4, 7} = A
2
4
a
b
#
3
5
%= {a,b}
#
6
#
7
#
a
8
b
9
b
10
#
MoveDFA(A,a)
=
MoveDFA(A,b)
=
© Harry H. Porter, 2005
7
Lexical Analysis - Part 3
Example
0
#
#
1
#
Start state:
#-Closure (0)
= {0, 1, 2, 4, 7} = A
2
4
a
b
#
3
5
%= {a,b}
#
6
#
#
7
a
8
b
9
b
10
#
MoveDFA(A,a)
= #-Closure (MoveNFA(A,a))
=
MoveDFA(A,b)
=
© Harry H. Porter, 2005
8

## Leave your Comments