-
Notifications
You must be signed in to change notification settings - Fork 0
/
automaton.sml
82 lines (70 loc) · 2.78 KB
/
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
structure SymbolOrdered = SymbolOrderedFun (structure Symbol = Symbol)
structure SymbolHashable = SymbolHashableFun (structure Symbol = Symbol)
structure SymbolDict = SplayDict (structure Key = SymbolOrdered)
structure SymbolSet = SplaySet (structure Elem = SymbolOrdered)
structure Automaton =
struct
type precedence = int option
type label = Syntax.label
(* Ideally, this should be a record. *)
type rule =
int (* rule number *)
*
int (* rule number within nonterminal *)
*
Symbol.symbol (* lhs *)
*
Symbol.symbol list (* rhs *)
*
label option list (* arguments *)
*
bool (* sole argument:
True iff the arguments contain exactly one label
and that label is 1.
*)
*
Symbol.symbol (* action *)
*
precedence (* precedence *)
*
bool ref (* reduced? *)
datatype action =
Shift of int
| Reduce of int
type item =
int (* rule number *)
*
int (* number of symbols read *)
*
Symbol.symbol list (* remaining symbols *)
datatype conflict =
NoConflict
| Resolved
| Conflict
type state =
(action list * conflict) SymbolDict.dict (* action table: each list nonempty, ordered in decreasing priority *)
*
int SymbolDict.dict (* goto table *)
*
(item * SymbolSet.set) list (* LR(1) items *)
type automaton =
int (* total states *)
*
state list (* states *)
*
rule Vector.vector (* rules *)
*
Symbol.symbol (* start symbol *)
type parser =
string SymbolDict.dict (* options *)
*
SymbolSet.set (* type arguments *)
*
(Symbol.symbol option * precedence * bool ref) SymbolDict.dict (* terminals *)
*
(int list * Symbol.symbol * bool ref) SymbolDict.dict (* nonterminals *)
*
(Symbol.symbol * (Syntax.label * Symbol.symbol) list * Symbol.symbol) list (* actions *)
*
automaton (* the automaton *)
end