1 (* Modified by Vesa Karvonen on
2007-12-18.
2 * Create line directives
in output
.
4 (* ML
-Yacc Parser
Generator (c
) 1989, 1991 Andrew W
. Appel
, David R
. Tarditi
*)
8 type pos
= {line
: int, col
: int}
9 val pos
: {line
: int ref
, start
: int ref
}
10 val text
: string list ref
13 val newSource
: string * TextIO.instream
* TextIO.outstream
-> inputSource
14 val error
: inputSource
-> pos
-> string -> unit
15 val warn
: inputSource
-> pos
-> string -> unit
16 val errorOccurred
: inputSource
-> unit
-> bool
18 datatype symbol
= SYMBOL
of string * pos
19 val symbolName
: symbol
-> string
20 val symbolPos
: symbol
-> pos
21 val symbolMake
: string * pos
-> symbol
24 val tyName
: ty
-> string
25 val tyMake
: string -> ty
27 (* associativities
: each kind
of associativity is assigned a unique
30 datatype prec
= LEFT | RIGHT | NONASSOC
31 datatype control
= NODEFAULT | VERBOSE | PARSER_NAME
of symbol |
32 FUNCTOR
of string | START_SYM
of symbol |
33 NSHIFT
of symbol list | POS
of string | PURE |
34 PARSE_ARG
of string * string |
35 TOKEN_SIG_INFO
of string
37 datatype rule
= RULE
of {lhs
: symbol
, rhs
: symbol list
,
38 code
: {text
: string, pos
: pos
},
41 datatype declData
= DECL
of
43 keyword
: symbol list
,
44 nonterm
: (symbol
* ty option
) list option
,
45 prec
: (prec
* (symbol list
)) list
,
46 change
: (symbol list
* symbol list
) list
,
47 term
: (symbol
* ty option
) list option
,
48 control
: control list
,
49 value
: (symbol
* string) list
}
51 val join_decls
: declData
* declData
* inputSource
* pos
-> declData
54 val getResult
: parseResult
-> string * declData
* rule list
57 signature PARSE_GEN_PARSER
=
59 structure Header
: HEADER
60 val parse
: string -> Header
.parseResult
* Header
.inputSource
65 val parseGen
: string -> unit
71 datatype term
= T
of int
72 datatype nonterm
= NT
of int
73 datatype symbol
= TERM
of term | NONTERM
of nonterm
76 terminals should be numbered from
0 to terms
-1,
77 nonterminals should be numbered from
0 to nonterms
-1,
78 rules should be numbered between
0 and (length rules
) - 1,
79 higher precedence binds tighter
,
80 start nonterminal should not occur on the rhs
of any rule
83 datatype grammar
= GRAMMAR
of
84 {rules
: {lhs
: nonterm
, rhs
: symbol list
,
85 precedence
: int option
, rulenum
: int } list
,
91 precedence
: term
-> int option
,
92 termToString
: term
-> string,
93 nontermToString
: nonterm
-> string}
96 (* signature for internal version
of grammar
*)
98 signature INTGRAMMAR
=
100 structure Grammar
: GRAMMAR
101 structure SymbolAssoc
: TABLE
102 structure NontermAssoc
: TABLE
104 sharing type SymbolAssoc
.key
= Grammar
.symbol
105 sharing type NontermAssoc
.key
= Grammar
.nonterm
107 datatype rule
= RULE
of
108 {lhs
: Grammar
.nonterm
,
109 rhs
: Grammar
.symbol list
,
111 (* internal number
of rule
- convenient for producing LR graph
*)
115 precedence
: int option
}
117 val gtTerm
: Grammar
.term
* Grammar
.term
-> bool
118 val eqTerm
: Grammar
.term
* Grammar
.term
-> bool
120 val gtNonterm
: Grammar
.nonterm
* Grammar
.nonterm
-> bool
121 val eqNonterm
: Grammar
.nonterm
* Grammar
.nonterm
-> bool
123 val gtSymbol
: Grammar
.symbol
* Grammar
.symbol
-> bool
124 val eqSymbol
: Grammar
.symbol
* Grammar
.symbol
-> bool
126 (* Debugging information will be generated only
if DEBUG is
true. *)
130 val prRule
: (Grammar
.symbol
-> string) * (Grammar
.nonterm
-> string) *
131 (string -> 'b
) -> rule
-> unit
132 val prGrammar
: (Grammar
.symbol
-> string)*(Grammar
.nonterm
-> string) *
133 (string -> unit
) -> Grammar
.grammar
-> unit
138 structure Grammar
: GRAMMAR
139 structure IntGrammar
: INTGRAMMAR
140 sharing Grammar
= IntGrammar
.Grammar
142 datatype item
= ITEM
of
143 { rule
: IntGrammar
.rule
,
146 (* rhsAfter
: The portion
of the rhs
of a rule that lies after the dot
*)
148 rhsAfter
: Grammar
.symbol list
}
150 (* eqItem
and gtItem compare items
*)
152 val eqItem
: item
* item
-> bool
153 val gtItem
: item
* item
-> bool
155 (* functions for maintaining ordered item lists
*)
157 val insert
: item
* item list
-> item list
158 val union
: item list
* item list
-> item list
160 (* core
: a set
of items
. It is represented by an ordered list
of items
.
161 The list is
in ascending order The rule numbers
and the positions
of the
162 dots are used to order the items
. *)
164 datatype core
= CORE
of item list
* int (* state #
*)
166 (* gtCore
and eqCore compare the lists
of items
*)
168 val gtCore
: core
* core
-> bool
169 val eqCore
: core
* core
-> bool
171 (* functions for debugging
*)
173 val prItem
: (Grammar
.symbol
-> string) * (Grammar
.nonterm
-> string) *
174 (string -> unit
) -> item
-> unit
175 val prCore
: (Grammar
.symbol
-> string) * (Grammar
.nonterm
-> string) *
176 (string -> unit
) -> core
-> unit
179 signature CORE_UTILS
=
182 structure Grammar
: GRAMMAR
183 structure IntGrammar
: INTGRAMMAR
184 structure Core
: CORE
186 sharing Grammar
= IntGrammar
.Grammar
= Core
.Grammar
187 sharing IntGrammar
= Core
.IntGrammar
189 (* mkFuncs
: create functions for the set
of productions derived from a
190 nonterminal
, the cores that result from shift
/gotos from a core
,
191 and return a list
of rules
*)
193 val mkFuncs
: Grammar
.grammar
->
194 { produces
: Grammar
.nonterm
-> IntGrammar
.rule list
,
196 (* shifts
: take a core
and compute all the cores that result from shifts
/gotos
199 shifts
: Core
.core
-> (Grammar
.symbol
*Core
.item list
) list
,
200 rules
: IntGrammar
.rule list
,
202 (* epsProds
: take a core compute epsilon productions for it
*)
204 epsProds
: Core
.core
-> IntGrammar
.rule list
}
209 structure Grammar
: GRAMMAR
210 structure IntGrammar
: INTGRAMMAR
211 structure Core
: CORE
213 sharing Grammar
= IntGrammar
.Grammar
= Core
.Grammar
214 sharing IntGrammar
= Core
.IntGrammar
217 val edges
: Core
.core
* graph
-> {edge
:Grammar
.symbol
,to
:Core
.core
} list
218 val nodes
: graph
-> Core
.core list
219 val shift
: graph
-> int * Grammar
.symbol
-> int (* int = state #
*)
220 val core
: graph
-> int -> Core
.core (* get core for a state
*)
222 (* mkGraph
: compute the
LR(0) sets
of items
*)
224 val mkGraph
: Grammar
.grammar
->
226 produces
: Grammar
.nonterm
-> IntGrammar
.rule list
,
227 rules
: IntGrammar
.rule list
,
228 epsProds
: Core
.core
-> IntGrammar
.rule list
}
230 val prGraph
: (Grammar
.symbol
-> string)*(Grammar
.nonterm
-> string) *
231 (string -> unit
) -> graph
-> unit
236 structure Grammar
: GRAMMAR
237 structure IntGrammar
: INTGRAMMAR
238 sharing Grammar
= IntGrammar
.Grammar
240 val union
: Grammar
.term list
* Grammar
.term list
-> Grammar
.term list
241 val make_set
: Grammar
.term list
-> Grammar
.term list
243 val mkFuncs
: {rules
: IntGrammar
.rule list
, nonterms
: int,
244 produces
: Grammar
.nonterm
-> IntGrammar
.rule list
} ->
245 {nullable
: Grammar
.nonterm
-> bool,
246 first
: Grammar
.symbol list
-> Grammar
.term list
}
248 val prLook
: (Grammar
.term
-> string) * (string -> unit
) ->
249 Grammar
.term list
-> unit
252 signature LALR_GRAPH
=
254 structure Grammar
: GRAMMAR
255 structure IntGrammar
: INTGRAMMAR
256 structure Core
: CORE
257 structure Graph
: LRGRAPH
259 sharing Grammar
= IntGrammar
.Grammar
= Core
.Grammar
= Graph
.Grammar
260 sharing IntGrammar
= Core
.IntGrammar
= Graph
.IntGrammar
261 sharing Core
= Graph
.Core
263 datatype lcore
= LCORE
of (Core
.item
* Grammar
.term list
) list
* int
264 val addLookahead
: {graph
: Graph
.graph
,
265 first
: Grammar
.symbol list
-> Grammar
.term list
,
266 eop
: Grammar
.term list
,
268 nullable
: Grammar
.nonterm
-> bool,
269 produces
: Grammar
.nonterm
-> IntGrammar
.rule list
,
270 rules
: IntGrammar
.rule list
,
271 epsProds
: Core
.core
-> IntGrammar
.rule list
,
272 print
: string -> unit
, (* for debugging
*)
273 termToString
: Grammar
.term
-> string,
274 nontermToString
: Grammar
.nonterm
-> string} ->
276 val prLcore
: (Grammar
.symbol
-> string) * (Grammar
.nonterm
-> string) *
277 (Grammar
.term
-> string) * (string -> unit
) ->
281 (* LR_ERRS
: errors found
while constructing an LR table
*)
285 structure LrTable
: LR_TABLE
287 (* RR
= reduce
/reduce
,
289 NS
: non
-shiftable terminal found on the rhs
of a rule
290 NOT_REDUCED n
: rule number n was not reduced
291 START n
: start symbol found on the rhs
of rule n
*)
293 datatype err
= RR
of LrTable
.term
* LrTable
.state
* int * int
294 | SR
of LrTable
.term
* LrTable
.state
* int
295 | NS
of LrTable
.term
* int
299 val summary
: err list
-> {rr
: int, sr
: int,
300 not_reduced
: int, start
: int,nonshift
: int}
302 val printSummary
: (string -> unit
) -> err list
-> unit
306 (* PRINT_STRUCT
: prints a
structure which includes a value
'table
' and a
307 structure Table whose
signature matches LR_TABLE
. The table
in the printed
308 structure will contain the same information
as the one passed to
309 printStruct
, although the representation may be different
. It returns
310 the number
of entries left
in the table after compaction
.*)
312 signature PRINT_STRUCT
=
314 structure LrTable
: LR_TABLE
316 {table
: LrTable
.table
,
318 print
: string -> unit
,
323 (* VERBOSE
: signature for a
structure which takes a table
and creates a
324 verbose description
of it
*)
328 structure Errs
: LR_ERRS
330 {table
: Errs
.LrTable
.table
,
332 termToString
: Errs
.LrTable
.term
-> string,
333 nontermToString
: Errs
.LrTable
.nonterm
-> string,
334 stateErrs
: Errs
.LrTable
.state
-> Errs
.err list
,
335 errs
: Errs
.err list
,
336 print
: string -> unit
,
337 printCores
: (string -> unit
) -> Errs
.LrTable
.state
-> unit
,
338 printRule
: (string -> unit
) -> int -> unit
} -> unit
341 (* MAKE_LR_TABLE
: signature for a
structure which includes a
structure
342 matching the
signature LR_TABLE
and a function which maps grammars
345 signature MAKE_LR_TABLE
=
347 structure Grammar
: GRAMMAR
348 structure Errs
: LR_ERRS
349 structure LrTable
: LR_TABLE
350 sharing Errs
.LrTable
= LrTable
352 sharing type LrTable
.term
= Grammar
.term
353 sharing type LrTable
.nonterm
= Grammar
.nonterm
355 (* boolean value determines whether default reductions will be used
.
356 If it is
true, reductions will be used
. *)
358 val mkTable
: Grammar
.grammar
* bool ->
360 (LrTable
.state
-> Errs
.err list
) * (* errors
in a state
*)
361 ((string -> unit
) -> LrTable
.state
-> unit
) *
362 Errs
.err
list (* list
of all errors
*)
365 (* SHRINK_LR_TABLE
: finds unique action entry rows
in the action table
368 signature SHRINK_LR_TABLE
=
370 (* Takes an action table represented
as a list
of action rows
.
371 It returns the number
of unique rows left
in the action table
,
372 a list
of integers which maps each original row to a unique
373 row
, and a list
of unique rows
*)
374 structure LrTable
: LR_TABLE
375 val shrinkActionList
: LrTable
.table
* bool ->
377 ((LrTable
.term
,LrTable
.action
) LrTable
.pairlist
*
378 LrTable
.action
) list
) * int