forked from codeon/cmlex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reverse-dfa.sml
44 lines (35 loc) · 1.45 KB
/
reverse-dfa.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
structure ReverseDFA
:> REVERSE_DFA
=
struct
structure D = SymbolDict
fun reverseDfa (count, initial, final, trans) =
let
val rtrans = Array.array (count, (D.empty, []))
fun loop state trans =
(case trans of
[] =>
()
| d :: rest =>
let
val () =
D.app
(fn (symbol, state') =>
(* Automaton transitions from state to state' on symbol. *)
let
val (rd, rde) = Array.sub (rtrans, state')
(* In fact, rde = [], but we won't rely on that. *)
val rd' =
D.insertMerge rd symbol [state] (fn states => state :: states)
in
Array.update (rtrans, state', (rd', rde))
end)
d
in
loop (state+1) rest
end)
val () = loop 0 trans
in
(final, initial, rtrans)
end
end