forked from codeon/cmlex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make-automaton.sml
201 lines (173 loc) · 7.77 KB
/
make-automaton.sml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
structure MakeAutomaton
:> MAKE_AUTOMATON
=
struct
type action = int * string
structure D = SymbolDict
(* Converts a 'a dict to an int array, with all missing entries filled with 0.
Return any entry with key ~1 (representing eof-of-stream) separately.
*)
fun dictToArray symbolLimit f d =
let
val a = Array.array (symbolLimit, 0)
val _ =
SymbolDict.app
(fn (symbol, x) =>
if symbol < 0 then
()
else
Array.update (a, symbol, f x))
d
val minusone =
(case SymbolDict.find d (~1) of
NONE =>
NONE
| SOME x =>
SOME (f x))
in
(a, minusone)
end
(* Put the final states at the front, put a sink state at 0,
and move everything into arrays.
*)
fun rearrange symbolLimit armcount (count, initial, final, trans) =
let
val rcount = count+1
val permutation = Array.array (rcount, ~1)
(* ~1 means the state's new position has not been determined yet. *)
val transArr = Array.fromList trans
val utilization = Array.array (armcount+1, false)
(* Place final-sink states (i.e., final states that only transition
to the sink state) in the permutation.
*)
val (firstNonfinalsink, rfinalRev) =
foldl
(fn ((state, (armnumber, action)), (n, rfinalAcc)) =>
if D.isEmpty (Array.sub (transArr, state)) then
(* This is a final-sink state. *)
if Array.sub (permutation, state) = ~1 then
(
Array.update (utilization, armnumber, true);
Array.update (permutation, state, n);
(n+1, action :: rfinalAcc)
)
else
(* Final state appears multiple times, violating
the determinization postcondition.
*)
raise (Fail "invariant")
else
(* Not a final-sink state, handle in second pass. *)
(n, rfinalAcc))
(1, [])
final
(* Place final states that are not final-sink states in the permutation. *)
val (firstNonfinal, rfinalRev') =
foldl
(fn ((state, (armnumber, action)), (n, rfinalAcc)) =>
if D.isEmpty (Array.sub (transArr, state)) then
(* This is a final-sink state, handled in first pass. *)
(n, rfinalAcc)
else
(* Not a final-sink state. *)
if Array.sub (permutation, state) = ~1 then
(
Array.update (utilization, armnumber, true);
Array.update (permutation, state, n);
(n+1, action :: rfinalAcc)
)
else
(* Final state appears multiple times, violating
the determinization invariant.
*)
raise (Fail "invariant"))
(firstNonfinalsink, rfinalRev)
final
(* Add non-final states to the permutation. *)
val _ =
foldl
(fn (d, (state, n)) =>
if Array.sub (permutation, state) = ~1 then
(* Not in permutation already, so not a final state. *)
(
Array.update (permutation, state, n);
(state+1, n+1)
)
else
(* a final state *)
(state+1, n))
(0, firstNonfinal)
trans
val rinitial =
(case initial of
[] =>
0
| [(state, ())] =>
Array.sub (permutation, state)
| _ =>
(* There are multiple initial-state entries. By construction,
there can be at most one initial state, so it appears multiple
times, violating the determinization postcondition.
*)
raise (Fail "invariant"))
val rfinal = Array.fromList (rev rfinalRev')
(* Initialize rtrans (an int array array) and rtransEos (an int array)
with bogus arrays. *)
val rtrans = Array.array (rcount, Array.array (0, 0))
val rtransEos = Array.array (rcount, 0)
(* Fill in the sink state, because it doesn't appear in trans. *)
val () = Array.update (rtrans, 0, Array.array (symbolLimit, 0))
val _ =
foldl
(fn (d, state) =>
let
val (rd, eos) = dictToArray symbolLimit (fn state => Array.sub (permutation, state)) d
val n = Array.sub (permutation, state)
val () =
if n = 0 then
(* Leave the sink state alone. *)
()
else
(
Array.update (rtrans, n, rd);
(case eos of
NONE => ()
| SOME m =>
Array.update (rtransEos, n, m))
)
in
state+1
end)
0
trans
val inexhaustive =
Array.sub (utilization, armcount)
val () = Array.update (utilization, armcount, true)
val redundancies =
Array.foldli
(fn (armnumber, used, redundancies) =>
if used then
redundancies
else
armnumber :: redundancies)
[]
utilization
in
((rcount, rinitial, firstNonfinalsink-1, firstNonfinal-1, rfinal, rtrans, rtransEos),
redundancies, inexhaustive)
end
fun compareAction ((m, _), (n, _)) = Int.compare (m, n)
fun makeAutomaton symbolLimit armcount res =
let
val res' = (Regexp.Epsilon, (armcount, "")) :: res (* add error state *)
val nfaRev = MakeNFA.makeRevNfa res'
val dfaRev = Determinize.determinize compareAction (fn _ => EQUAL) nfaRev
val nfa = ReverseDFA.reverseDfa dfaRev
val dfa = Determinize.determinize (fn _ => EQUAL) compareAction nfa
(* Brzozowski's Theorem:
dfaRev is deterministic and accessible (by construction), so dfa is minimal.
*)
in
rearrange symbolLimit armcount dfa
end
end