Upload
dangduong
View
256
Download
5
Embed Size (px)
Citation preview
1
Lexical Analysis - Part 3
© Harry H. Porter, 2005
Converting an NFA to a DFAGiven:
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.
2
Lexical Analysis - Part 3
© Harry H. Porter, 2005
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 = { {...}, {...} , ..., {...} }
NFA DFA
(The names of the states is arbitrary and can be changed later, if desired.)
37
5
a
a
a{3} {5,7}
a1 2
3
Lexical Analysis - Part 3
© Harry H. Porter, 2005
Algorithm: Convert NFA to DFAWe’ll use...
MoveNFA(S,a) the transition function from NFA
#-Closure(s) where s is a single state from NFA
#-Closure(S) 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 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”
MoveDFA(T,b) The transition function from DFA
To add an edge to the growing DFA...
Set MoveDFA(T,b) to S
...where S and T are sets of NFA states
{3,5,7}
bT S
4
Lexical Analysis - Part 3
© Harry H. Porter, 2005
Example
Start state:
#-Closure (0)
=
2a
4b
15
3 #
##
#
60 # #
#
#
7 a8 b 9 b
%= {a,b}
10
5
Lexical Analysis - Part 3
© Harry H. Porter, 2005
Example
Start state:
#-Closure (0)
= {0, 1, 2, 4, 7}
2a
4b
15
3 #
##
#
60 # #
#
#
7 a8 b 9 b
%= {a,b}
10
6
Lexical Analysis - Part 3
© Harry H. Porter, 2005
Example
Start state:
#-Closure (0)
= {0, 1, 2, 4, 7} = A
2a
4b
15
3 #
##
#
60 # #
#
#
7 a8 b 9 b
%= {a,b}
10
7
Lexical Analysis - Part 3
© Harry H. Porter, 2005
Example
Start state:
#-Closure (0)
= {0, 1, 2, 4, 7} = A
MoveDFA(A,a)
=
MoveDFA(A,b)
=
2a
4b
15
3 #
##
#
60 # #
#
#
7 a8 b 9 b
%= {a,b}
10
8
Lexical Analysis - Part 3
© Harry H. Porter, 2005
Example
Start state:
#-Closure (0)
= {0, 1, 2, 4, 7} = A
MoveDFA(A,a)
= #-Closure (MoveNFA(A,a))
=
MoveDFA(A,b)
=
2a
4b
15
3 #
##
#
60 # #
#
#
7 a8 b 9 b
%= {a,b}
10
9
Lexical Analysis - Part 3
© Harry H. Porter, 2005
Example
Start state:
#-Closure (0)
= {0, 1, 2, 4, 7} = A
MoveDFA(A,a)
= #-Closure (MoveNFA(A,a))
= #-Closure ({3,8})
=
MoveDFA(A,b)
=
2a
4b
15
3 #
##
#
60 # #
#
#
7 a8 b 9 b
%= {a,b}
10
10
Lexical Analysis - Part 3
© Harry H. Porter, 2005
Example
Start state:
#-Closure (0)
= {0, 1, 2, 4, 7} = A
MoveDFA(A,a)
= #-Closure (MoveNFA(A,a))
= #-Closure ({3,8})
= {1,2,3,4,6,7,8}
MoveDFA(A,b)
=
2a
4b
15
3 #
##
#
60 # #
#
#
7 a8 b 9 b
%= {a,b}
10
11
Lexical Analysis - Part 3
© Harry H. Porter, 2005
Example
Start state:
#-Closure (0)
= {0, 1, 2, 4, 7} = A
MoveDFA(A,a)
= #-Closure (MoveNFA(A,a))
= #-Closure ({3,8})
= {1,2,3,4,6,7,8} = B
MoveDFA(A,b)
=
2a
4b
15
3 #
##
#
60 # #
#
#
7 a8 b 9 b
%= {a,b}
10
12
Lexical Analysis - Part 3
© Harry H. Porter, 2005
Example
Start state:
#-Closure (0)
= {0, 1, 2, 4, 7} = A
MoveDFA(A,a)
= #-Closure (MoveNFA(A,a))
= #-Closure ({3,8})
= {1,2,3,4,6,7,8} = B
MoveDFA(A,b)
= #-Closure (MoveNFA(A,b))
=
2a
4b
15
3 #
##
#
60 # #
#
#
7 a8 b 9 b
%= {a,b}
10
13
Lexical Analysis - Part 3
© Harry H. Porter, 2005
Example
Start state:
#-Closure (0)
= {0, 1, 2, 4, 7} = A
MoveDFA(A,a)
= #-Closure (MoveNFA(A,a))
= #-Closure ({3,8})
= {1,2,3,4,6,7,8} = B
MoveDFA(A,b)
= #-Closure (MoveNFA(A,b))
= #-Closure ({5})
=
2a
4b
15
3 #
##
#
60 # #
#
#
7 a8 b 9 b
%= {a,b}
10
14
Lexical Analysis - Part 3
© Harry H. Porter, 2005
Example
Start state:
#-Closure (0)
= {0, 1, 2, 4, 7} = A
MoveDFA(A,a)
= #-Closure (MoveNFA(A,a))
= #-Closure ({3,8})
= {1,2,3,4,6,7,8} = B
MoveDFA(A,b)
= #-Closure (MoveNFA(A,b))
= #-Closure ({5})
= {1,2,4,5,6,7} = C
2a
4b
15
3 #
##
#
60 # #
#
#
7 a8 b 9 b
%= {a,b}
10
15
Lexical Analysis - Part 3
© Harry H. Porter, 2005
Example
Start state:
#-Closure (0)
= {0, 1, 2, 4, 7} = A
MoveDFA(A,a)
= #-Closure (MoveNFA(A,a))
= #-Closure ({3,8})
= {1,2,3,4,6,7,8} = B
MoveDFA(A,b)
= #-Closure (MoveNFA(A,b))
= #-Closure ({5})
= {1,2,4,5,6,7} = C
C
B
So far:
A a
b
2a
4b
15
3 #
##
#
60 # #
#
#
7 a8 b 9 b
%= {a,b}
10
16
Lexical Analysis - Part 3
© Harry H. Porter, 2005
Example
Start state:
#-Closure (0)
= {0, 1, 2, 4, 7} = A
MoveDFA(A,a)
= #-Closure (MoveNFA(A,a))
= #-Closure ({3,8})
= {1,2,3,4,6,7,8} = B
MoveDFA(A,b)
= #-Closure (MoveNFA(A,b))
= #-Closure ({5})
= {1,2,4,5,6,7} = C
A is now done; mark it!B and C are unmarked.Let’s do B next...
C
B
$A a
b
2a
4b
15
3 #
##
#
60 # #
#
#
7 a8 b 9 b
%= {a,b}
10
So far:
17
Lexical Analysis - Part 3
© Harry H. Porter, 2005
Process B = {1,2,3,4,6,7,8}
Example2
a
4b
15
3 #
##
#
60 # #
#
#
7 a8 b 9 b
%= {a,b}
10
C
B
$A a
b
18
Lexical Analysis - Part 3
© Harry H. Porter, 2005
Process B = {1,2,3,4,6,7,8}
MoveDFA(B,a)
=
Example2
a
4b
15
3 #
##
#
60 # #
#
#
7 a8 b 9 b
%= {a,b}
10
C
B
$A a
b
19
Lexical Analysis - Part 3
© Harry H. Porter, 2005
Process B = {1,2,3,4,6,7,8}
MoveDFA(B,a)
= #-Closure (MoveNFA(B,a))
=
Example2
a
4b
15
3 #
##
#
60 # #
#
#
7 a8 b 9 b
%= {a,b}
10
C
B
$A a
b
20
Lexical Analysis - Part 3
© Harry H. Porter, 2005
Process B = {1,2,3,4,6,7,8}
MoveDFA(B,a)
= #-Closure (MoveNFA(B,a))
= #-Closure ({3,8})
=
Example2
a
4b
15
3 #
##
#
60 # #
#
#
7 a8 b 9 b
%= {a,b}
10
C
B
$A a
b
21
Lexical Analysis - Part 3
© Harry H. Porter, 2005
Process B = {1,2,3,4,6,7,8}
MoveDFA(B,a)
= #-Closure (MoveNFA(B,a))
= #-Closure ({3,8})
= {1,2,3,4,6,7,8} = B
Example2
a
4b
15
3 #
##
#
60 # #
#
#
7 a8 b 9 b
%= {a,b}
10
C
B
$A a
b
22
Lexical Analysis - Part 3
© Harry H. Porter, 2005
Process B = {1,2,3,4,6,7,8}
MoveDFA(B,a)
= #-Closure (MoveNFA(B,a))
= #-Closure ({3,8})
= {1,2,3,4,6,7,8} = B
Example2
a
4b
15
3 #
##
#
60 # #
#
#
7 a8 b 9 b
%= {a,b}
10
C
B
$A a
b
a
23
Lexical Analysis - Part 3
© Harry H. Porter, 2005
Process B = {1,2,3,4,6,7,8}
MoveDFA(B,a)
= #-Closure (MoveNFA(B,a))
= #-Closure ({3,8})
= {1,2,3,4,6,7,8} = B
MoveDFA(B,b)
=
Example2
a
4b
15
3 #
##
#
60 # #
#
#
7 a8 b 9 b
%= {a,b}
10
C
B
$A a
b
a
24
Lexical Analysis - Part 3
© Harry H. Porter, 2005
Process B = {1,2,3,4,6,7,8}
MoveDFA(B,a)
= #-Closure (MoveNFA(B,a))
= #-Closure ({3,8})
= {1,2,3,4,6,7,8} = B
MoveDFA(B,b)
= #-Closure (MoveNFA(B,b))
=
Example2
a
4b
15
3 #
##
#
60 # #
#
#
7 a8 b 9 b
%= {a,b}
10
C
B
$A a
b
a
25
Lexical Analysis - Part 3
© Harry H. Porter, 2005
Process B = {1,2,3,4,6,7,8}
MoveDFA(B,a)
= #-Closure (MoveNFA(B,a))
= #-Closure ({3,8})
= {1,2,3,4,6,7,8} = B
MoveDFA(B,b)
= #-Closure (MoveNFA(B,b))
= #-Closure ({5,9})
=
Example2
a
4b
15
3 #
##
#
60 # #
#
#
7 a8 b 9 b
%= {a,b}
10
C
B
$A a
b
a
26
Lexical Analysis - Part 3
© Harry H. Porter, 2005
Process B = {1,2,3,4,6,7,8}
MoveDFA(B,a)
= #-Closure (MoveNFA(B,a))
= #-Closure ({3,8})
= {1,2,3,4,6,7,8} = B
MoveDFA(B,b)
= #-Closure (MoveNFA(B,b))
= #-Closure ({5,9})
= {1,2,4,5,6,7,9} = D
Example2
a
4b
15
3 #
##
#
60 # #
#
#
7 a8 b 9 b
%= {a,b}
10
C
B
$A a
b
a
27
Lexical Analysis - Part 3
© Harry H. Porter, 2005
Process B = {1,2,3,4,6,7,8}
MoveDFA(B,a)
= #-Closure (MoveNFA(B,a))
= #-Closure ({3,8})
= {1,2,3,4,6,7,8} = B
MoveDFA(B,b)
= #-Closure (MoveNFA(B,b))
= #-Closure ({5,9})
= {1,2,4,5,6,7,9} = D
Example2
a
4b
15
3 #
##
#
60 # #
#
#
7 a8 b 9 b
C
B
%= {a,b}
Db
a
$A
b
a
10
28
Lexical Analysis - Part 3
© Harry H. Porter, 2005
Process B = {1,2,3,4,6,7,8}
MoveDFA(B,a)
= #-Closure (MoveNFA(B,a))
= #-Closure ({3,8})
= {1,2,3,4,6,7,8} = B
MoveDFA(B,b)
= #-Closure (MoveNFA(B,b))
= #-Closure ({5,9})
= {1,2,4,5,6,7,9} = D
Example2
a
4b
15
3 #
##
#
60 # #
#
#
7 a8 b 9 b
C
B
%= {a,b}
D$
b
a
$A
b
a
10
29
Lexical Analysis - Part 3
© Harry H. Porter, 2005
Example
Process C = {1,2,4,5,6,7}
C
B
a
$$A
b
a
2a
4b
15
3 #
##
#
60 # #
#
#
7 a8 b 9 b
%= {a,b}
10
Db
30
Lexical Analysis - Part 3
© Harry H. Porter, 2005
Example
Process C = {1,2,4,5,6,7}
MoveDFA(C,a) =
MoveDFA(C,b) =
C
B
a
$$A
b
a
2a
4b
15
3 #
##
#
60 # #
#
#
7 a8 b 9 b
%= {a,b}
10
Db
31
Lexical Analysis - Part 3
© Harry H. Porter, 2005
Example
Process C = {1,2,4,5,6,7}
MoveDFA(C,a) = {1,2,3,4,6,7,8} = B
MoveDFA(C,b) =
C
B
a
$$A
b
a
2a
4b
15
3 #
##
#
60 # #
#
#
7 a8 b 9 b
%= {a,b}
10
Db
32
Lexical Analysis - Part 3
© Harry H. Porter, 2005
Example
Process C = {1,2,4,5,6,7}
MoveDFA(C,a) = {1,2,3,4,6,7,8} = B
MoveDFA(C,b) = {1,2,4,5,6,7} = C
C
B
a
$$A
b
a
2a
4b
15
3 #
##
#
60 # #
#
#
7 a8 b 9 b
%= {a,b}
10
Db
33
Lexical Analysis - Part 3
© Harry H. Porter, 2005
Example
Process C = {1,2,4,5,6,7}
MoveDFA(C,a) = {1,2,3,4,6,7,8} = B
MoveDFA(C,b) = {1,2,4,5,6,7} = C
C
B
a
a
b
$
$$A
b
a
2a
4b
15
3 #
##
#
60 # #
#
#
7 a8 b 9 b
%= {a,b}
10
Db
34
Lexical Analysis - Part 3
© Harry H. Porter, 2005
Example
Process C = {1,2,4,5,6,7}
MoveDFA(C,a) = {1,2,3,4,6,7,8} = B
MoveDFA(C,b) = {1,2,4,5,6,7} = C
Process D = {1,2,4,5,6,7,9}
MoveDFA(D,a) =
MoveDFA(D,b) =
C
B Db
a
a
b
$
$$A
b
a
2a
4b
15
3 #
##
#
60 # #
#
#
7 a8 b 9 b
%= {a,b}
10
35
Lexical Analysis - Part 3
© Harry H. Porter, 2005
Example
Process C = {1,2,4,5,6,7}
MoveDFA(C,a) = {1,2,3,4,6,7,8} = B
MoveDFA(C,b) = {1,2,4,5,6,7} = C
Process D = {1,2,4,5,6,7,9}
MoveDFA(D,a) = {1,2,3,4,6,7,8} = B
MoveDFA(D,b) =
C
B Db
a
a
b
$
$$A
b
a
2a
4b
15
3 #
##
#
60 # #
#
#
7 a8 b 9 b
%= {a,b}
10
36
Lexical Analysis - Part 3
© Harry H. Porter, 2005
Example
Process C = {1,2,4,5,6,7}
MoveDFA(C,a) = {1,2,3,4,6,7,8} = B
MoveDFA(C,b) = {1,2,4,5,6,7} = C
Process D = {1,2,4,5,6,7,9}
MoveDFA(D,a) = {1,2,3,4,6,7,8} = B
MoveDFA(D,b) = {1,2,4,5,6,7,10} = E
C
B Db
a
a
b
$
$$A
b
a
2a
4b
15
3 #
##
#
60 # #
#
#
7 a8 b 9 b
%= {a,b}
10
37
Lexical Analysis - Part 3
© Harry H. Porter, 2005
Example
Process C = {1,2,4,5,6,7}
MoveDFA(C,a) = {1,2,3,4,6,7,8} = B
MoveDFA(C,b) = {1,2,4,5,6,7} = C
Process D = {1,2,4,5,6,7,9}
MoveDFA(D,a) = {1,2,3,4,6,7,8} = B
MoveDFA(D,b) = {1,2,4,5,6,7,10} = E
C
B Db
a
a
b
b
a
$
$$$A
b
a
2a
4b
15
3 #
##
#
60 # #
#
#
7 a8 b 9 b
%= {a,b}
10
E
38
Lexical Analysis - Part 3
© Harry H. Porter, 2005
Example
Process C = {1,2,4,5,6,7}
MoveDFA(C,a) = {1,2,3,4,6,7,8} = B
MoveDFA(C,b) = {1,2,4,5,6,7} = C
Process D = {1,2,4,5,6,7,9}
MoveDFA(D,a) = {1,2,3,4,6,7,8} = B
MoveDFA(D,b) = {1,2,4,5,6,7,10} = E
C
B Db
a
Process E = {1,2,4,5,6,7,10}
MoveDFA(E,a) =
MoveDFA(E,b) =
a
b
b
a
$
$$$A
b
a
2a
4b
15
3 #
##
#
60 # #
#
#
7 a8 b 9 b
%= {a,b}
10
E
39
Lexical Analysis - Part 3
© Harry H. Porter, 2005
Example
Process C = {1,2,4,5,6,7}
MoveDFA(C,a) = {1,2,3,4,6,7,8} = B
MoveDFA(C,b) = {1,2,4,5,6,7} = C
Process D = {1,2,4,5,6,7,9}
MoveDFA(D,a) = {1,2,3,4,6,7,8} = B
MoveDFA(D,b) = {1,2,4,5,6,7,10} = E
C
B Db
a
Process E = {1,2,4,5,6,7,10}
MoveDFA(E,a) = {1,2,3,4,6,7,8} = B
MoveDFA(E,b) =
a
b
b
a
$
$$$A
b
a
2a
4b
15
3 #
##
#
60 # #
#
#
7 a8 b 9 b
%= {a,b}
10
E
40
Lexical Analysis - Part 3
© Harry H. Porter, 2005
Example
Process C = {1,2,4,5,6,7}
MoveDFA(C,a) = {1,2,3,4,6,7,8} = B
MoveDFA(C,b) = {1,2,4,5,6,7} = C
Process D = {1,2,4,5,6,7,9}
MoveDFA(D,a) = {1,2,3,4,6,7,8} = B
MoveDFA(D,b) = {1,2,4,5,6,7,10} = E
C
B Db
a
Process E = {1,2,4,5,6,7,10}
MoveDFA(E,a) = {1,2,3,4,6,7,8} = B
MoveDFA(E,b) = {1,2,4,5,6,7} = C
a
b
b
a
$
$$$A
b
a
2a
4b
15
3 #
##
#
60 # #
#
#
7 a8 b 9 b
%= {a,b}
10
E
41
Lexical Analysis - Part 3
© Harry H. Porter, 2005
Example
Process C = {1,2,4,5,6,7}
MoveDFA(C,a) = {1,2,3,4,6,7,8} = B
MoveDFA(C,b) = {1,2,4,5,6,7} = C
Process D = {1,2,4,5,6,7,9}
MoveDFA(D,a) = {1,2,3,4,6,7,8} = B
MoveDFA(D,b) = {1,2,4,5,6,7,10} = E
C
B Db
a
Process E = {1,2,4,5,6,7,10}
MoveDFA(E,a) = {1,2,3,4,6,7,8} = B
MoveDFA(E,b) = {1,2,4,5,6,7} = C
a
b
b
a
a
b$
$ $$$A
b
a
2a
4b
15
3 #
##
#
60 # #
#
#
7 a8 b 9 b
%= {a,b}
10
E
42
Lexical Analysis - Part 3
© Harry H. Porter, 2005
Example
Process C = {1,2,4,5,6,7}
MoveDFA(C,a) = {1,2,3,4,6,7,8} = B
MoveDFA(C,b) = {1,2,4,5,6,7} = C
Process D = {1,2,4,5,6,7,9}
MoveDFA(D,a) = {1,2,3,4,6,7,8} = B
MoveDFA(D,b) = {1,2,4,5,6,7,10} = E
C
B Db
a
Process E = {1,2,4,5,6,7,10}
MoveDFA(E,a) = {1,2,3,4,6,7,8} = B
MoveDFA(E,b) = {1,2,4,5,6,7} = C
a
b
b
a
a
b$
$ $$$A
b
a
2a
4b
15
3 #
##
#
60 # #
#
#
7 a8 b 9 b
%= {a,b}
10
E
Final States in DFA?
...which state(s) contain 10?
43
Lexical Analysis - Part 3
© Harry H. Porter, 2005
Example
Process C = {1,2,4,5,6,7}
MoveDFA(C,a) = {1,2,3,4,6,7,8} = B
MoveDFA(C,b) = {1,2,4,5,6,7} = C
Process D = {1,2,4,5,6,7,9}
MoveDFA(D,a) = {1,2,3,4,6,7,8} = B
MoveDFA(D,b) = {1,2,4,5,6,7,10} = E
C
B Db
a
Process E = {1,2,4,5,6,7,10}
MoveDFA(E,a) = {1,2,3,4,6,7,8} = B
MoveDFA(E,b) = {1,2,4,5,6,7} = C
E
a
b
b
a
a
b$
$ $$
Final Result:
$A
b
a
2a
4b
15
3 #
##
#
60 # #
#
#
7 a8 b 9 b
%= {a,b}
10
Final States in DFA?
...which state(s) contain 10?
44
Lexical Analysis - Part 3
© Harry H. Porter, 2005
Algorithm: Convert NFA to DFASDFA = {}
Add #-Closure(s0) to SDFA as the start stateSet the only state in SDFA to “unmarked”
while SDFA contains an unmarked state do
Let T be that unmarked state
Mark T
for each a in % do S = #-Closure(MoveNFA(T,a)) if S is not in SDFA already then
Add S to SDFA (as an “unmarked” state)
endIf
Set MoveDFA(T,a) to S
endFor
endWhile
for each S in SDFA do
if any s&S is a final state in the NFA then Mark S an a final state in the DFA
endIf
endFor
Everywhere you could
possibly get to on an a
A set of NFA states
a{t1,t2,...} {s1,s2,...}
i.e, add an edge to the DFA...
T S
45
Lexical Analysis - Part 3
© Harry H. Porter, 2005
Resulting DFA for (a|b)*abb
Is it minimal?
C
BA a
b
Db
a
E
a
b
b
a
a
b
46
Lexical Analysis - Part 3
© Harry H. Porter, 2005
Resulting DFA for (a|b)*abb
Is it minimal?
C
BA a
b
Db
a
E
a
b
b
a
a
b
BA a Db Eb
47
Lexical Analysis - Part 3
© Harry H. Porter, 2005
Resulting DFA for (a|b)*abb
Is it minimal?
C
BA a
b
Db
a
E
a
b
b
a
a
b
BA a Db Eb
48
Lexical Analysis - Part 3
© Harry H. Porter, 2005
Resulting DFA for (a|b)*abb
Is it minimal?
C
BA a
b
Db
a
E
a
b
b
a
a
b
BA a Db Eb
b
b
49
Lexical Analysis - Part 3
© Harry H. Porter, 2005
Resulting DFA for (a|b)*abb
Is it minimal?
C
BA a
b
Db
a
E
a
b
b
a
a
b
BA a Db
a
Eb
a
a
b
b
50
Lexical Analysis - Part 3
© Harry H. Porter, 2005
Resulting DFA for (a|b)*abb
Is it minimal?
C
BA a
b
Db
a
E
a
b
b
a
a
b
BA a Db
a
Eb
a
a
b
b
Every Regular Set is recognized
by a minimal DFA!