-
Notifications
You must be signed in to change notification settings - Fork 0
/
write-automaton.sml
189 lines (166 loc) · 5.65 KB
/
write-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
structure WriteAutomaton
:> WRITE_AUTOMATON
=
struct
structure D = SymbolDict
structure S = SymbolSet
structure T =
HashTable
(structure Key = SetHashable (structure Set = S
structure Elem = SymbolHashable))
open Automaton
fun appSeparated f g l =
(case l of
[] =>
()
| h :: t =>
(
f h;
app (fn x => (g (); f x)) t
))
type ctx = { outs : TextIO.outstream,
rules : rule Vector.vector,
start : Symbol.symbol,
lookaheadCount : int ref,
lookaheadList : S.set list ref,
lookaheadTable : int T.table }
fun lookaheadNumber ({lookaheadCount, lookaheadList, lookaheadTable, ...}:ctx) set =
T.lookupOrInsert lookaheadTable set
(fn () =>
let
val n = !lookaheadCount
in
lookaheadCount := n + 1;
lookaheadList := set :: !lookaheadList;
n
end)
fun writeItem (ctx as {outs, rules, start, ...}:ctx) ((rulenum, read, remaining), lookahead) =
let
fun write str = TextIO.output (outs, str)
val (lhs, rhs) =
if rulenum = ~1 then
("start", [start])
else
let
val (_, _, lhs, rhs, _, _, _, _, _) = Vector.sub (rules, rulenum)
in
(Symbol.toValue lhs, rhs)
end
fun loop n l =
if n = 0 then
()
else
(case l of
[] =>
raise (Fail "invariant")
| symbol :: rest =>
(
write (Symbol.toValue symbol);
write " ";
loop (n-1) rest
))
in
if rulenum <> ~1 then
(
write (Int.toString rulenum);
write " : "
)
else
();
write lhs;
write " -> ";
loop read rhs;
write ". ";
app (fn symbol => (write (Symbol.toValue symbol); write " ")) remaining;
write " / ";
write (Int.toString (lookaheadNumber ctx lookahead));
write "\n"
end
fun writeState (ctx as {outs, ...}:ctx) statenum (action, goto, items) =
let
fun write str = TextIO.output (outs, str)
in
write "State ";
write (Int.toString statenum);
write ":\n\n";
app (writeItem ctx) items;
write "\n";
D.app
(fn (symbol, (actions, conflict)) =>
(
write (Symbol.toValue symbol);
write " => ";
appSeparated
(fn Shift n =>
(
write "shift ";
write (Int.toString n)
)
| Reduce (~1) =>
write "accept"
| Reduce n =>
(
write "reduce ";
write (Int.toString n)
))
(fn () => write ", ")
actions;
(case conflict of
NoConflict =>
()
| Resolved =>
write " PRECEDENCE"
| Conflict =>
write " CONFLICT");
write "\n"
))
action;
D.app
(fn (symbol, n) =>
(
write (Symbol.toValue symbol);
write " => goto ";
write (Int.toString n);
write "\n"
))
goto;
write "\n-----\n\n"
end
fun writeAutomaton outs (_, states, rules, start) =
let
fun write str = TextIO.output (outs, str)
val () = write "AUTOMATON LISTING\n=================\n\n"
val lookaheadList = ref []
val ctx =
{ outs=outs,
rules=rules,
start=start,
lookaheadCount=ref 0,
lookaheadList=lookaheadList,
lookaheadTable=T.table 40 }
val _ =
foldl
(fn (state, statenum) =>
(
writeState ctx statenum state;
statenum+1
))
0
states
val _ =
foldr
(fn (set, setnum) =>
(
write "lookahead ";
write (Int.toString setnum);
write " = ";
S.app (fn symbol => (write (Symbol.toValue symbol); write " ")) set;
write "\n";
setnum+1
))
0
(!lookaheadList)
in
()
end
end