author | wenzelm |
Wed, 31 Jan 2018 11:26:50 +0100 | |
changeset 67551 | 4a087b9a29c5 |
parent 67550 | 3b666615e3ce |
child 67552 | 679253fef277 |
permissions | -rw-r--r-- |
18 | 1 |
(* Title: Pure/Syntax/parser.ML |
67545 | 2 |
Author: Carsten Clasohm, Sonia Mahjoub |
3 |
Author: Makarius |
|
18 | 4 |
|
67545 | 5 |
General context-free parser for the inner syntax of terms and types. |
18 | 6 |
*) |
7 |
||
8 |
signature PARSER = |
|
15752 | 9 |
sig |
1507 | 10 |
type gram |
11 |
val empty_gram: gram |
|
42288
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42282
diff
changeset
|
12 |
val extend_gram: Syntax_Ext.xprod list -> gram -> gram |
45632
b23c42b9f78a
prefer Parser.make_gram over Parser.merge_gram, to approximate n-ary merges on theory import;
wenzelm
parents:
44102
diff
changeset
|
13 |
val make_gram: Syntax_Ext.xprod list -> gram |
1507 | 14 |
val pretty_gram: gram -> Pretty.T list |
15 |
datatype parsetree = |
|
16 |
Node of string * parsetree list | |
|
17 |
Tip of Lexicon.token |
|
42374
b9a6b465da25
clarified pretty_parsetree: suppress literal tokens;
wenzelm
parents:
42288
diff
changeset
|
18 |
exception PARSETREE of parsetree |
42205
22f5cc06c419
direct pretty printing of parsetrees -- prevent diagnostic output from crashing due to undeclared entities;
wenzelm
parents:
41378
diff
changeset
|
19 |
val pretty_parsetree: parsetree -> Pretty.T |
45641 | 20 |
val parse: gram -> string -> Lexicon.token list -> parsetree list |
26678 | 21 |
val guess_infix_lr: gram -> string -> (string * bool * bool * int) option |
41378 | 22 |
val branching_level: int Config.T |
15752 | 23 |
end; |
1507 | 24 |
|
15752 | 25 |
structure Parser: PARSER = |
18 | 26 |
struct |
15752 | 27 |
|
18 | 28 |
(** datatype gram **) |
29 |
||
67541 | 30 |
(* nonterminals *) |
31 |
||
67545 | 32 |
(*production for the NTs are stored in a vector, indexed by the NT tag*) |
67541 | 33 |
type nt = int; |
34 |
||
67545 | 35 |
type tags = nt Symtab.table; |
36 |
val tags_empty: tags = Symtab.empty; |
|
37 |
fun tags_content (tags: tags) = sort_by #1 (Symtab.dest tags); |
|
38 |
fun tags_lookup (tags: tags) = Symtab.lookup tags; |
|
39 |
fun tags_insert tag (tags: tags) = Symtab.update_new tag tags; |
|
40 |
fun tags_name (tags: tags) = the o Inttab.lookup (Inttab.make (map swap (Symtab.dest tags))); |
|
41 |
||
67541 | 42 |
type nts = Inttab.set; |
43 |
val nts_empty: nts = Inttab.empty; |
|
44 |
val nts_merge: nts * nts -> nts = Inttab.merge (K true); |
|
45 |
fun nts_insert nt : nts -> nts = Inttab.insert_set nt; |
|
46 |
fun nts_member (nts: nts) = Inttab.defined nts; |
|
47 |
fun nts_fold f (nts: nts) = Inttab.fold (f o #1) nts; |
|
48 |
fun nts_subset (nts1: nts, nts2: nts) = Inttab.forall (nts_member nts2 o #1) nts1; |
|
49 |
fun nts_is_empty (nts: nts) = Inttab.is_empty nts; |
|
50 |
fun nts_is_unique (nts: nts) = nts_is_empty nts orelse Inttab.is_single nts; |
|
51 |
||
52 |
||
67539 | 53 |
(* tokens *) |
54 |
||
67549 | 55 |
structure Tokens = Table(type key = Lexicon.token val ord = Lexicon.tokens_match_ord); |
67539 | 56 |
|
57 |
type tokens = Tokens.set; |
|
58 |
val tokens_empty: tokens = Tokens.empty; |
|
59 |
val tokens_merge: tokens * tokens -> tokens = Tokens.merge (K true); |
|
60 |
fun tokens_insert tk : tokens -> tokens = Tokens.insert_set tk; |
|
61 |
fun tokens_remove tk : tokens -> tokens = Tokens.remove_set tk; |
|
62 |
fun tokens_member (tokens: tokens) = Tokens.defined tokens; |
|
63 |
fun tokens_is_empty (tokens: tokens) = Tokens.is_empty tokens; |
|
64 |
fun tokens_fold f (tokens: tokens) = Tokens.fold (f o #1) tokens; |
|
65 |
val tokens_union = tokens_fold tokens_insert; |
|
66 |
val tokens_subtract = tokens_fold tokens_remove; |
|
67 |
fun tokens_find P (tokens: tokens) = |
|
68 |
Tokens.get_first (fn (tok, ()) => if P tok then SOME tok else NONE) tokens; |
|
67541 | 69 |
fun tokens_add (nt: nt, tokens) = if tokens_is_empty tokens then I else cons (nt, tokens); |
67533
f253e5eaf995
clarified types and operations: potentially more efficient add_prods;
wenzelm
parents:
67532
diff
changeset
|
70 |
|
1147
57b5f55bf879
removed 'raw' productions from gram datatype; replaced mk_gram by add_prods;
clasohm
parents:
890
diff
changeset
|
71 |
|
67545 | 72 |
(* productions *) |
73 |
||
38712
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
74 |
datatype symb = |
67545 | 75 |
Terminal of Lexicon.token | |
76 |
Nonterminal of nt * int; (*(tag, prio)*) |
|
38712
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
77 |
|
67540 | 78 |
type prods = (symb list * string * int) list Tokens.table; (*start_token ~> [(rhs, name, prio)]*) |
79 |
||
80 |
val prods_empty: prods = Tokens.empty; |
|
81 |
fun prods_lookup (prods: prods) = Tokens.lookup_list prods; |
|
82 |
fun prods_update entry : prods -> prods = Tokens.update entry; |
|
83 |
fun prods_content (prods: prods) = distinct (op =) (maps #2 (Tokens.dest prods)); |
|
84 |
||
67545 | 85 |
type nt_gram = (nts * tokens) * prods; (*dependent_nts, start_tokens, prods*) |
86 |
(*depent_nts is a set of all NTs whose lookahead depends on this NT's lookahead*) |
|
1147
57b5f55bf879
removed 'raw' productions from gram datatype; replaced mk_gram by add_prods;
clasohm
parents:
890
diff
changeset
|
87 |
|
67540 | 88 |
val nt_gram_empty: nt_gram = ((nts_empty, tokens_empty), prods_empty); |
67533
f253e5eaf995
clarified types and operations: potentially more efficient add_prods;
wenzelm
parents:
67532
diff
changeset
|
89 |
|
67531 | 90 |
type chains = unit Int_Graph.T; |
91 |
fun chains_preds (chains: chains) = Int_Graph.immediate_preds chains; |
|
92 |
fun chains_all_preds (chains: chains) = Int_Graph.all_preds chains; |
|
93 |
fun chains_all_succs (chains: chains) = Int_Graph.all_succs chains; |
|
94 |
val chains_empty: chains = Int_Graph.empty; |
|
95 |
fun chains_member (chains: chains) = Int_Graph.is_edge chains; |
|
96 |
fun chains_declare nt : chains -> chains = Int_Graph.default_node (nt, ()); |
|
97 |
fun chains_insert (from, to) = |
|
98 |
chains_declare from #> chains_declare to #> Int_Graph.add_edge (from, to); |
|
99 |
||
1147
57b5f55bf879
removed 'raw' productions from gram datatype; replaced mk_gram by add_prods;
clasohm
parents:
890
diff
changeset
|
100 |
datatype gram = |
38712
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
101 |
Gram of |
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
102 |
{nt_count: int, |
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
103 |
prod_count: int, |
67531 | 104 |
tags: tags, |
105 |
chains: chains, |
|
67533
f253e5eaf995
clarified types and operations: potentially more efficient add_prods;
wenzelm
parents:
67532
diff
changeset
|
106 |
lambdas: nts, |
38712
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
107 |
prods: nt_gram Vector.vector}; |
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
108 |
(*"tags" is used to map NT names (i.e. strings) to tags; |
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
109 |
chain productions are not stored as normal productions |
67517
add9a9f6a290
explicit graph for chains, which contains all nts as nodes;
wenzelm
parents:
67516
diff
changeset
|
110 |
but instead as an entry in "chains": from -> to; |
38712
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
111 |
lambda productions are stored as normal productions |
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
112 |
and also as an entry in "lambdas"*) |
1147
57b5f55bf879
removed 'raw' productions from gram datatype; replaced mk_gram by add_prods;
clasohm
parents:
890
diff
changeset
|
113 |
|
38712
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
114 |
(*productions for which no starting token is |
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
115 |
known yet are associated with this token*) |
38713 | 116 |
val unknown_start = Lexicon.eof; |
38712
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
117 |
|
67539 | 118 |
fun get_start tks = |
119 |
(case Tokens.min tks of |
|
120 |
SOME (tk, _) => tk |
|
121 |
| NONE => unknown_start); |
|
67516 | 122 |
|
67543 | 123 |
fun add_production prods (lhs, new_prod as (rhs, _, pri)) (chains, lambdas) = |
124 |
let |
|
125 |
(*store chain if it does not already exist*) |
|
126 |
val (chain, new_chain, chains') = |
|
127 |
(case (pri, rhs) of |
|
128 |
(~1, [Nonterminal (from, ~1)]) => |
|
129 |
if chains_member chains (from, lhs) |
|
130 |
then (SOME from, false, chains) |
|
131 |
else (SOME from, true, chains_insert (from, lhs) chains) |
|
132 |
| _ => (NONE, false, chains_declare lhs chains)); |
|
1147
57b5f55bf879
removed 'raw' productions from gram datatype; replaced mk_gram by add_prods;
clasohm
parents:
890
diff
changeset
|
133 |
|
67543 | 134 |
(*propagate new chain in lookahead and lambdas; |
135 |
added_starts is used later to associate existing |
|
136 |
productions with new starting tokens*) |
|
137 |
val (added_starts, lambdas') = |
|
138 |
if not new_chain then ([], lambdas) |
|
139 |
else |
|
140 |
let (*lookahead of chain's source*) |
|
141 |
val ((_, from_tks), _) = Array.sub (prods, the chain); |
|
1175
1b15a4b1a4f7
added comments; fixed a bug; reduced memory usage slightly
clasohm
parents:
1147
diff
changeset
|
142 |
|
67543 | 143 |
(*copy from's lookahead to chain's destinations*) |
144 |
fun copy_lookahead to = |
|
145 |
let |
|
146 |
val ((to_nts, to_tks), ps) = Array.sub (prods, to); |
|
1147
57b5f55bf879
removed 'raw' productions from gram datatype; replaced mk_gram by add_prods;
clasohm
parents:
890
diff
changeset
|
147 |
|
67543 | 148 |
val new_tks = tokens_subtract to_tks from_tks; (*added lookahead tokens*) |
149 |
val to_tks' = tokens_merge (to_tks, new_tks); |
|
150 |
val _ = Array.update (prods, to, ((to_nts, to_tks'), ps)); |
|
151 |
in tokens_add (to, new_tks) end; |
|
1175
1b15a4b1a4f7
added comments; fixed a bug; reduced memory usage slightly
clasohm
parents:
1147
diff
changeset
|
152 |
|
67543 | 153 |
val tos = chains_all_succs chains' [lhs]; |
154 |
in |
|
155 |
(fold copy_lookahead tos [], |
|
156 |
lambdas |> nts_member lambdas lhs ? fold nts_insert tos) |
|
157 |
end; |
|
38712
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
158 |
|
67543 | 159 |
(*test if new production can produce lambda |
160 |
(rhs must either be empty or only consist of lambda NTs)*) |
|
161 |
val new_lambdas = |
|
162 |
if forall |
|
163 |
(fn Nonterminal (id, _) => nts_member lambdas' id |
|
164 |
| Terminal _ => false) rhs |
|
165 |
then SOME (filter_out (nts_member lambdas') (chains_all_succs chains' [lhs])) |
|
166 |
else NONE; |
|
167 |
val lambdas'' = fold nts_insert (these new_lambdas) lambdas'; |
|
1147
57b5f55bf879
removed 'raw' productions from gram datatype; replaced mk_gram by add_prods;
clasohm
parents:
890
diff
changeset
|
168 |
|
67543 | 169 |
(*list optional terminal and all nonterminals on which the lookahead |
170 |
of a production depends*) |
|
171 |
fun lookahead_dependency _ [] nts = (NONE, nts) |
|
172 |
| lookahead_dependency _ (Terminal tk :: _) nts = (SOME tk, nts) |
|
173 |
| lookahead_dependency lambdas (Nonterminal (nt, _) :: symbs) nts = |
|
174 |
if nts_member lambdas nt then |
|
175 |
lookahead_dependency lambdas symbs (nts_insert nt nts) |
|
176 |
else (NONE, nts_insert nt nts); |
|
330
2fda15dd1e0f
changed the way a grammar is generated to allow the new parser to work;
clasohm
parents:
258
diff
changeset
|
177 |
|
67543 | 178 |
(*get all known starting tokens for a nonterminal*) |
179 |
fun starts_for_nt nt = snd (fst (Array.sub (prods, nt))); |
|
38712
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
180 |
|
67543 | 181 |
(*update prods, lookaheads, and lambdas according to new lambda NTs*) |
182 |
val (added_starts', lambdas') = |
|
183 |
let |
|
184 |
(*propagate added lambda NT*) |
|
185 |
fun propagate_lambda [] added_starts lambdas = (added_starts, lambdas) |
|
186 |
| propagate_lambda (l :: ls) added_starts lambdas = |
|
187 |
let |
|
188 |
(*get lookahead for lambda NT*) |
|
189 |
val ((dependent, l_starts), _) = Array.sub (prods, l); |
|
1175
1b15a4b1a4f7
added comments; fixed a bug; reduced memory usage slightly
clasohm
parents:
1147
diff
changeset
|
190 |
|
67543 | 191 |
(*check productions whose lookahead may depend on lambda NT*) |
192 |
fun examine_prods [] add_lambda nt_dependencies added_tks nt_prods = |
|
193 |
(add_lambda, nt_dependencies, added_tks, nt_prods) |
|
194 |
| examine_prods ((p as (rhs, _, _)) :: ps) add_lambda |
|
195 |
nt_dependencies added_tks nt_prods = |
|
196 |
let val (tk, nts) = lookahead_dependency lambdas rhs nts_empty in |
|
197 |
if nts_member nts l then (*update production's lookahead*) |
|
198 |
let |
|
199 |
val new_lambda = |
|
200 |
is_none tk andalso nts_subset (nts, lambdas); |
|
18 | 201 |
|
67543 | 202 |
val new_tks = |
203 |
tokens_empty |
|
204 |
|> fold tokens_insert (the_list tk) |
|
205 |
|> nts_fold (tokens_union o starts_for_nt) nts |
|
206 |
|> tokens_subtract l_starts; |
|
38712
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
207 |
|
67543 | 208 |
val added_tks' = tokens_merge (added_tks, new_tks); |
330
2fda15dd1e0f
changed the way a grammar is generated to allow the new parser to work;
clasohm
parents:
258
diff
changeset
|
209 |
|
67543 | 210 |
val nt_dependencies' = nts_merge (nt_dependencies, nts); |
38712
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
211 |
|
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
212 |
(*associate production with new starting tokens*) |
67539 | 213 |
fun copy tk nt_prods = |
67543 | 214 |
prods_update (tk, p :: prods_lookup nt_prods tk) nt_prods; |
215 |
||
216 |
val nt_prods' = nt_prods |
|
217 |
|> tokens_fold copy new_tks |
|
67550 | 218 |
|> new_lambda ? copy Lexicon.dummy; |
67543 | 219 |
in |
220 |
examine_prods ps (add_lambda orelse new_lambda) |
|
221 |
nt_dependencies' added_tks' nt_prods' |
|
222 |
end |
|
223 |
else (*skip production*) |
|
224 |
examine_prods ps add_lambda nt_dependencies added_tks nt_prods |
|
225 |
end; |
|
226 |
||
227 |
(*check each NT whose lookahead depends on new lambda NT*) |
|
228 |
fun process_nts nt (added_lambdas, added_starts) = |
|
229 |
let |
|
230 |
val ((old_nts, old_tks), nt_prods) = Array.sub (prods, nt); |
|
231 |
||
232 |
(*existing productions whose lookahead may depend on l*) |
|
233 |
val tk_prods = prods_lookup nt_prods (get_start l_starts); |
|
234 |
||
235 |
(*add_lambda is true if an existing production of the nt |
|
236 |
produces lambda due to the new lambda NT l*) |
|
237 |
val (add_lambda, nt_dependencies, added_tks, nt_prods') = |
|
238 |
examine_prods tk_prods false nts_empty tokens_empty nt_prods; |
|
239 |
||
240 |
val new_nts = nts_merge (old_nts, nt_dependencies); |
|
241 |
val new_tks = tokens_merge (old_tks, added_tks); |
|
242 |
||
243 |
val added_lambdas' = added_lambdas |> add_lambda ? cons nt; |
|
244 |
val _ = Array.update (prods, nt, ((new_nts, new_tks), nt_prods')); |
|
245 |
(*N.B. that because the tks component |
|
246 |
is used to access existing |
|
247 |
productions we have to add new |
|
248 |
tokens at the _end_ of the list*) |
|
249 |
val added_starts' = tokens_add (nt, added_tks) added_starts; |
|
250 |
in (added_lambdas', added_starts') end; |
|
377
ab8917806779
lookaheads are now computed faster (during the grammar is built)
clasohm
parents:
373
diff
changeset
|
251 |
|
67543 | 252 |
val (added_lambdas, added_starts') = |
253 |
nts_fold process_nts dependent ([], added_starts); |
|
254 |
val added_lambdas' = filter_out (nts_member lambdas) added_lambdas; |
|
255 |
in |
|
256 |
propagate_lambda (ls @ added_lambdas') added_starts' |
|
257 |
(fold nts_insert added_lambdas' lambdas) |
|
258 |
end; |
|
259 |
in |
|
260 |
propagate_lambda |
|
261 |
(nts_fold (fn l => not (nts_member lambdas l) ? cons l) lambdas'' []) |
|
262 |
added_starts lambdas'' |
|
263 |
end; |
|
264 |
||
265 |
(*insert production into grammar*) |
|
266 |
val added_starts' = |
|
267 |
if is_some chain then added_starts' (*don't store chain production*) |
|
268 |
else |
|
269 |
let |
|
270 |
(*lookahead tokens of new production and on which |
|
271 |
NTs lookahead depends*) |
|
272 |
val (start_tk, start_nts) = lookahead_dependency lambdas' rhs nts_empty; |
|
273 |
||
274 |
val start_tks = |
|
275 |
tokens_empty |
|
276 |
|> fold tokens_insert (the_list start_tk) |
|
277 |
|> nts_fold (tokens_union o starts_for_nt) start_nts; |
|
278 |
||
279 |
val start_tks' = |
|
280 |
start_tks |
|
67550 | 281 |
|> (if is_some new_lambdas then tokens_insert Lexicon.dummy |
67543 | 282 |
else if tokens_is_empty start_tks then tokens_insert unknown_start |
283 |
else I); |
|
284 |
||
285 |
(*add lhs NT to list of dependent NTs in lookahead*) |
|
286 |
fun add_nts nt initial = |
|
287 |
(if initial then |
|
288 |
let val ((old_nts, old_tks), ps) = Array.sub (prods, nt) in |
|
289 |
if nts_member old_nts lhs then () |
|
290 |
else Array.update (prods, nt, ((nts_insert lhs old_nts, old_tks), ps)) |
|
291 |
end |
|
292 |
else (); false); |
|
293 |
||
294 |
(*add new start tokens to chained NTs' lookahead list; |
|
295 |
also store new production for lhs NT*) |
|
296 |
fun add_tks [] added = added |
|
297 |
| add_tks (nt :: nts) added = |
|
298 |
let |
|
299 |
val ((old_nts, old_tks), nt_prods) = Array.sub (prods, nt); |
|
300 |
||
301 |
val new_tks = tokens_subtract old_tks start_tks; |
|
330
2fda15dd1e0f
changed the way a grammar is generated to allow the new parser to work;
clasohm
parents:
258
diff
changeset
|
302 |
|
67543 | 303 |
(*store new production*) |
304 |
fun store tk (prods, _) = |
|
305 |
let |
|
306 |
val tk_prods = prods_lookup prods tk; |
|
307 |
val tk_prods' = new_prod :: tk_prods; |
|
308 |
val prods' = prods_update (tk, tk_prods') prods; |
|
309 |
in (prods', true) end; |
|
38712
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
310 |
|
67543 | 311 |
val (nt_prods', changed) = (nt_prods, false) |
312 |
|> nt = lhs ? tokens_fold store start_tks'; |
|
313 |
val _ = |
|
314 |
if not changed andalso tokens_is_empty new_tks then () |
|
315 |
else |
|
316 |
Array.update |
|
317 |
(prods, nt, ((old_nts, tokens_merge (old_tks, new_tks)), nt_prods')); |
|
318 |
in add_tks nts (tokens_add (nt, new_tks) added) end; |
|
319 |
val _ = nts_fold add_nts start_nts true; |
|
320 |
in add_tks (chains_all_succs chains' [lhs]) [] end; |
|
38712
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
321 |
|
67543 | 322 |
(*associate productions with new lookaheads*) |
323 |
val _ = |
|
324 |
let |
|
325 |
(*propagate added start tokens*) |
|
326 |
fun add_starts [] = () |
|
327 |
| add_starts ((changed_nt, new_tks) :: starts) = |
|
328 |
let |
|
329 |
(*token under which old productions which |
|
330 |
depend on changed_nt could be stored*) |
|
331 |
val key = |
|
332 |
tokens_find (not o tokens_member new_tks) (starts_for_nt changed_nt) |
|
333 |
|> the_default unknown_start; |
|
334 |
||
335 |
(*copy productions whose lookahead depends on changed_nt; |
|
336 |
if key = SOME unknown_start then tk_prods is used to hold |
|
337 |
the productions not copied*) |
|
338 |
fun update_prods [] result = result |
|
339 |
| update_prods ((p as (rhs, _: string, _: nt)) :: ps) |
|
340 |
(tk_prods, nt_prods) = |
|
341 |
let |
|
342 |
(*lookahead dependency for production*) |
|
343 |
val (tk, depends) = lookahead_dependency lambdas' rhs nts_empty; |
|
344 |
||
345 |
(*test if this production has to be copied*) |
|
346 |
val update = nts_member depends changed_nt; |
|
18 | 347 |
|
67543 | 348 |
(*test if production could already be associated with |
349 |
a member of new_tks*) |
|
350 |
val lambda = |
|
351 |
not (nts_is_unique depends) orelse |
|
352 |
not (nts_is_empty depends) andalso is_some tk |
|
353 |
andalso tokens_member new_tks (the tk); |
|
354 |
||
355 |
(*associate production with new starting tokens*) |
|
356 |
fun copy tk nt_prods = |
|
357 |
let |
|
358 |
val tk_prods = prods_lookup nt_prods tk; |
|
359 |
val tk_prods' = |
|
360 |
if not lambda then p :: tk_prods |
|
361 |
else insert (op =) p tk_prods; |
|
362 |
(*if production depends on lambda NT we |
|
363 |
have to look for duplicates*) |
|
364 |
in prods_update (tk, tk_prods') nt_prods end; |
|
365 |
val result = |
|
366 |
if update then (tk_prods, tokens_fold copy new_tks nt_prods) |
|
367 |
else if key = unknown_start then (p :: tk_prods, nt_prods) |
|
368 |
else (tk_prods, nt_prods); |
|
369 |
in update_prods ps result end; |
|
18 | 370 |
|
67543 | 371 |
(*copy existing productions for new starting tokens*) |
372 |
fun process_nts nt = |
|
373 |
let |
|
374 |
val ((nts, tks), nt_prods) = Array.sub (prods, nt); |
|
375 |
||
376 |
val tk_prods = prods_lookup nt_prods key; |
|
377 |
||
378 |
(*associate productions with new lookahead tokens*) |
|
379 |
val (tk_prods', nt_prods') = update_prods tk_prods ([], nt_prods); |
|
380 |
||
381 |
val nt_prods'' = |
|
382 |
if key = unknown_start then |
|
383 |
prods_update (key, tk_prods') nt_prods' |
|
384 |
else nt_prods'; |
|
385 |
||
386 |
val added_tks = tokens_subtract tks new_tks; |
|
387 |
val tks' = tokens_merge (tks, added_tks); |
|
388 |
val _ = Array.update (prods, nt, ((nts, tks'), nt_prods'')); |
|
389 |
in tokens_add (nt, added_tks) end; |
|
390 |
||
391 |
val ((dependent, _), _) = Array.sub (prods, changed_nt); |
|
392 |
in add_starts (starts @ nts_fold process_nts dependent []) end; |
|
393 |
in add_starts added_starts' end; |
|
394 |
in (chains', lambdas') end; |
|
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
46
diff
changeset
|
395 |
|
18 | 396 |
|
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
46
diff
changeset
|
397 |
(* pretty_gram *) |
18 | 398 |
|
1147
57b5f55bf879
removed 'raw' productions from gram datatype; replaced mk_gram by add_prods;
clasohm
parents:
890
diff
changeset
|
399 |
fun pretty_gram (Gram {tags, prods, chains, ...}) = |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
46
diff
changeset
|
400 |
let |
67531 | 401 |
val print_nt = tags_name tags; |
67518 | 402 |
fun print_pri p = if p < 0 then "" else Symbol.make_sup ("(" ^ string_of_int p ^ ")"); |
1147
57b5f55bf879
removed 'raw' productions from gram datatype; replaced mk_gram by add_prods;
clasohm
parents:
890
diff
changeset
|
403 |
|
67513 | 404 |
fun pretty_symb (Terminal (Lexicon.Token (kind, s, _))) = |
405 |
if kind = Lexicon.Literal then Pretty.quote (Pretty.keyword1 s) else Pretty.str s |
|
406 |
| pretty_symb (Nonterminal (tag, p)) = Pretty.str (print_nt tag ^ print_pri p); |
|
18 | 407 |
|
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
46
diff
changeset
|
408 |
fun pretty_const "" = [] |
67513 | 409 |
| pretty_const c = [Pretty.str ("\<^bold>\<Rightarrow> " ^ quote c)]; |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
46
diff
changeset
|
410 |
|
67513 | 411 |
fun prod_of_chain from = ([Nonterminal (from, ~1)], "", ~1); |
1147
57b5f55bf879
removed 'raw' productions from gram datatype; replaced mk_gram by add_prods;
clasohm
parents:
890
diff
changeset
|
412 |
|
67513 | 413 |
fun pretty_prod (name, tag) = |
67540 | 414 |
(prods_content (#2 (Vector.sub (prods, tag))) @ map prod_of_chain (chains_preds chains tag)) |
67513 | 415 |
|> map (fn (symbs, const, p) => |
416 |
Pretty.block (Pretty.breaks |
|
417 |
(Pretty.str (name ^ print_pri p ^ " =") :: map pretty_symb symbs @ pretty_const const))); |
|
67531 | 418 |
in maps pretty_prod (tags_content tags) end; |
1147
57b5f55bf879
removed 'raw' productions from gram datatype; replaced mk_gram by add_prods;
clasohm
parents:
890
diff
changeset
|
419 |
|
57b5f55bf879
removed 'raw' productions from gram datatype; replaced mk_gram by add_prods;
clasohm
parents:
890
diff
changeset
|
420 |
|
42217 | 421 |
|
67545 | 422 |
(** operations on grammars **) |
1147
57b5f55bf879
removed 'raw' productions from gram datatype; replaced mk_gram by add_prods;
clasohm
parents:
890
diff
changeset
|
423 |
|
38712
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
424 |
val empty_gram = |
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
425 |
Gram |
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
426 |
{nt_count = 0, |
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
427 |
prod_count = 0, |
67531 | 428 |
tags = tags_empty, |
429 |
chains = chains_empty, |
|
67533
f253e5eaf995
clarified types and operations: potentially more efficient add_prods;
wenzelm
parents:
67532
diff
changeset
|
430 |
lambdas = nts_empty, |
f253e5eaf995
clarified types and operations: potentially more efficient add_prods;
wenzelm
parents:
67532
diff
changeset
|
431 |
prods = Vector.fromList [nt_gram_empty]}; |
1147
57b5f55bf879
removed 'raw' productions from gram datatype; replaced mk_gram by add_prods;
clasohm
parents:
890
diff
changeset
|
432 |
|
1438 | 433 |
|
434 |
(*Add productions to a grammar*) |
|
37684 | 435 |
fun extend_gram [] gram = gram |
436 |
| extend_gram xprods (Gram {nt_count, prod_count, tags, chains, lambdas, prods}) = |
|
38712
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
437 |
let |
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
438 |
(*Get tag for existing nonterminal or create a new one*) |
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
439 |
fun get_tag nt_count tags nt = |
67531 | 440 |
(case tags_lookup tags nt of |
38712
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
441 |
SOME tag => (nt_count, tags, tag) |
67531 | 442 |
| NONE => (nt_count + 1, tags_insert (nt, nt_count) tags, nt_count)); |
1438 | 443 |
|
38712
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
444 |
(*Convert symbols to the form used by the parser; |
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
445 |
delimiters and predefined terms are stored as terminals, |
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
446 |
nonterminals are converted to integer tags*) |
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
447 |
fun symb_of [] nt_count tags result = (nt_count, tags, rev result) |
42288
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42282
diff
changeset
|
448 |
| symb_of (Syntax_Ext.Delim s :: ss) nt_count tags result = |
38712
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
449 |
symb_of ss nt_count tags |
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
450 |
(Terminal (Lexicon.Token (Lexicon.Literal, s, Position.no_range)) :: result) |
42288
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42282
diff
changeset
|
451 |
| symb_of (Syntax_Ext.Argument (s, p) :: ss) nt_count tags result = |
38712
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
452 |
let |
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
453 |
val (nt_count', tags', new_symb) = |
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
454 |
(case Lexicon.predef_term s of |
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
455 |
NONE => |
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
456 |
let val (nt_count', tags', s_tag) = get_tag nt_count tags s; |
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
457 |
in (nt_count', tags', Nonterminal (s_tag, p)) end |
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
458 |
| SOME tk => (nt_count, tags, Terminal tk)); |
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
459 |
in symb_of ss nt_count' tags' (new_symb :: result) end |
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
460 |
| symb_of (_ :: ss) nt_count tags result = symb_of ss nt_count tags result; |
1147
57b5f55bf879
removed 'raw' productions from gram datatype; replaced mk_gram by add_prods;
clasohm
parents:
890
diff
changeset
|
461 |
|
38712
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
462 |
(*Convert list of productions by invoking symb_of for each of them*) |
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
463 |
fun prod_of [] nt_count prod_count tags result = |
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
464 |
(nt_count, prod_count, tags, result) |
42288
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42282
diff
changeset
|
465 |
| prod_of (Syntax_Ext.XProd (lhs, xsymbs, const, pri) :: ps) |
38712
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
466 |
nt_count prod_count tags result = |
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
467 |
let |
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
468 |
val (nt_count', tags', lhs_tag) = get_tag nt_count tags lhs; |
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
469 |
val (nt_count'', tags'', prods) = symb_of xsymbs nt_count' tags' []; |
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
470 |
in |
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
471 |
prod_of ps nt_count'' (prod_count + 1) tags'' |
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
472 |
((lhs_tag, (prods, const, pri)) :: result) |
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
473 |
end; |
1147
57b5f55bf879
removed 'raw' productions from gram datatype; replaced mk_gram by add_prods;
clasohm
parents:
890
diff
changeset
|
474 |
|
38712
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
475 |
val (nt_count', prod_count', tags', xprods') = |
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
476 |
prod_of xprods nt_count prod_count tags []; |
1147
57b5f55bf879
removed 'raw' productions from gram datatype; replaced mk_gram by add_prods;
clasohm
parents:
890
diff
changeset
|
477 |
|
38712
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
478 |
(*Copy array containing productions of old grammar; |
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
479 |
this has to be done to preserve the old grammar while being able |
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
480 |
to change the array's content*) |
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
481 |
val prods' = |
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
482 |
let |
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
483 |
fun get_prod i = |
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
484 |
if i < nt_count then Vector.sub (prods, i) |
67533
f253e5eaf995
clarified types and operations: potentially more efficient add_prods;
wenzelm
parents:
67532
diff
changeset
|
485 |
else nt_gram_empty; |
38712
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
486 |
in Array.tabulate (nt_count', get_prod) end; |
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
487 |
|
67543 | 488 |
val (chains', lambdas') = |
489 |
(chains, lambdas) |> fold (add_production prods') xprods'; |
|
38712
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
490 |
in |
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
491 |
Gram |
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
492 |
{nt_count = nt_count', |
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
493 |
prod_count = prod_count', |
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
494 |
tags = tags', |
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
495 |
chains = chains', |
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
496 |
lambdas = lambdas', |
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
497 |
prods = Array.vector prods'} |
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
498 |
end; |
18 | 499 |
|
45632
b23c42b9f78a
prefer Parser.make_gram over Parser.merge_gram, to approximate n-ary merges on theory import;
wenzelm
parents:
44102
diff
changeset
|
500 |
fun make_gram xprods = extend_gram xprods empty_gram; |
1147
57b5f55bf879
removed 'raw' productions from gram datatype; replaced mk_gram by add_prods;
clasohm
parents:
890
diff
changeset
|
501 |
|
18 | 502 |
|
42217 | 503 |
|
42374
b9a6b465da25
clarified pretty_parsetree: suppress literal tokens;
wenzelm
parents:
42288
diff
changeset
|
504 |
(** parser **) |
b9a6b465da25
clarified pretty_parsetree: suppress literal tokens;
wenzelm
parents:
42288
diff
changeset
|
505 |
|
b9a6b465da25
clarified pretty_parsetree: suppress literal tokens;
wenzelm
parents:
42288
diff
changeset
|
506 |
(* parsetree *) |
18 | 507 |
|
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
46
diff
changeset
|
508 |
datatype parsetree = |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
46
diff
changeset
|
509 |
Node of string * parsetree list | |
37683 | 510 |
Tip of Lexicon.token; |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
46
diff
changeset
|
511 |
|
42374
b9a6b465da25
clarified pretty_parsetree: suppress literal tokens;
wenzelm
parents:
42288
diff
changeset
|
512 |
exception PARSETREE of parsetree; |
b9a6b465da25
clarified pretty_parsetree: suppress literal tokens;
wenzelm
parents:
42288
diff
changeset
|
513 |
|
b9a6b465da25
clarified pretty_parsetree: suppress literal tokens;
wenzelm
parents:
42288
diff
changeset
|
514 |
fun pretty_parsetree parsetree = |
b9a6b465da25
clarified pretty_parsetree: suppress literal tokens;
wenzelm
parents:
42288
diff
changeset
|
515 |
let |
b9a6b465da25
clarified pretty_parsetree: suppress literal tokens;
wenzelm
parents:
42288
diff
changeset
|
516 |
fun pretty (Node (c, pts)) = |
b9a6b465da25
clarified pretty_parsetree: suppress literal tokens;
wenzelm
parents:
42288
diff
changeset
|
517 |
[Pretty.enclose "(" ")" (Pretty.breaks (Pretty.quote (Pretty.str c) :: maps pretty pts))] |
b9a6b465da25
clarified pretty_parsetree: suppress literal tokens;
wenzelm
parents:
42288
diff
changeset
|
518 |
| pretty (Tip tok) = |
b9a6b465da25
clarified pretty_parsetree: suppress literal tokens;
wenzelm
parents:
42288
diff
changeset
|
519 |
if Lexicon.valued_token tok then [Pretty.str (Lexicon.str_of_token tok)] else []; |
b9a6b465da25
clarified pretty_parsetree: suppress literal tokens;
wenzelm
parents:
42288
diff
changeset
|
520 |
in (case pretty parsetree of [prt] => prt | _ => raise PARSETREE parsetree) end; |
b9a6b465da25
clarified pretty_parsetree: suppress literal tokens;
wenzelm
parents:
42288
diff
changeset
|
521 |
|
b9a6b465da25
clarified pretty_parsetree: suppress literal tokens;
wenzelm
parents:
42288
diff
changeset
|
522 |
|
b9a6b465da25
clarified pretty_parsetree: suppress literal tokens;
wenzelm
parents:
42288
diff
changeset
|
523 |
(* parser state *) |
42205
22f5cc06c419
direct pretty printing of parsetrees -- prevent diagnostic output from crashing due to undeclared entities;
wenzelm
parents:
41378
diff
changeset
|
524 |
|
18 | 525 |
type state = |
67533
f253e5eaf995
clarified types and operations: potentially more efficient add_prods;
wenzelm
parents:
67532
diff
changeset
|
526 |
nt * int * (*identification and production precedence*) |
38712
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
527 |
parsetree list * (*already parsed nonterminals on rhs*) |
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
528 |
symb list * (*rest of rhs*) |
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
529 |
string * (*name of production*) |
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
530 |
int; (*index for previous state list*) |
18 | 531 |
|
532 |
||
38713 | 533 |
(*Get all rhss with precedence >= min_prec*) |
42217 | 534 |
fun get_RHS min_prec = filter (fn (_, _, prec: int) => prec >= min_prec); |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
46
diff
changeset
|
535 |
|
38713 | 536 |
(*Get all rhss with precedence >= min_prec and < max_prec*) |
537 |
fun get_RHS' min_prec max_prec = |
|
42217 | 538 |
filter (fn (_, _, prec: int) => prec >= min_prec andalso prec < max_prec); |
18 | 539 |
|
330
2fda15dd1e0f
changed the way a grammar is generated to allow the new parser to work;
clasohm
parents:
258
diff
changeset
|
540 |
(*Make states using a list of rhss*) |
62669 | 541 |
fun mk_states i lhs_ID rhss = |
38713 | 542 |
let fun mk_state (rhs, id, prod_prec) = (lhs_ID, prod_prec, [], rhs, id, i); |
543 |
in map mk_state rhss end; |
|
697
40f72ab196f8
changed warning for extremely ambiguous expressions
clasohm
parents:
682
diff
changeset
|
544 |
|
15752 | 545 |
(*Add parse tree to list and eliminate duplicates |
330
2fda15dd1e0f
changed the way a grammar is generated to allow the new parser to work;
clasohm
parents:
258
diff
changeset
|
546 |
saving the maximum precedence*) |
42217 | 547 |
fun conc (t: parsetree list, prec: int) [] = (NONE, [(t, prec)]) |
330
2fda15dd1e0f
changed the way a grammar is generated to allow the new parser to work;
clasohm
parents:
258
diff
changeset
|
548 |
| conc (t, prec) ((t', prec') :: ts) = |
2fda15dd1e0f
changed the way a grammar is generated to allow the new parser to work;
clasohm
parents:
258
diff
changeset
|
549 |
if t = t' then |
38712
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
550 |
(SOME prec', |
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
551 |
if prec' >= prec then (t', prec') :: ts |
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
552 |
else (t, prec) :: ts) |
330
2fda15dd1e0f
changed the way a grammar is generated to allow the new parser to work;
clasohm
parents:
258
diff
changeset
|
553 |
else |
2fda15dd1e0f
changed the way a grammar is generated to allow the new parser to work;
clasohm
parents:
258
diff
changeset
|
554 |
let val (n, ts') = conc (t, prec) ts |
2fda15dd1e0f
changed the way a grammar is generated to allow the new parser to work;
clasohm
parents:
258
diff
changeset
|
555 |
in (n, (t', prec') :: ts') end; |
18 | 556 |
|
330
2fda15dd1e0f
changed the way a grammar is generated to allow the new parser to work;
clasohm
parents:
258
diff
changeset
|
557 |
(*Update entry in used*) |
42226
cb650789f2f0
use standard tables with standard argument order;
wenzelm
parents:
42222
diff
changeset
|
558 |
fun update_trees (A, t) used = |
cb650789f2f0
use standard tables with standard argument order;
wenzelm
parents:
42222
diff
changeset
|
559 |
let |
cb650789f2f0
use standard tables with standard argument order;
wenzelm
parents:
42222
diff
changeset
|
560 |
val (i, ts) = the (Inttab.lookup used A); |
cb650789f2f0
use standard tables with standard argument order;
wenzelm
parents:
42222
diff
changeset
|
561 |
val (n, ts') = conc t ts; |
cb650789f2f0
use standard tables with standard argument order;
wenzelm
parents:
42222
diff
changeset
|
562 |
in (n, Inttab.update (A, (i, ts')) used) end; |
18 | 563 |
|
330
2fda15dd1e0f
changed the way a grammar is generated to allow the new parser to work;
clasohm
parents:
258
diff
changeset
|
564 |
(*Replace entry in used*) |
42226
cb650789f2f0
use standard tables with standard argument order;
wenzelm
parents:
42222
diff
changeset
|
565 |
fun update_prec (A, prec) = |
cb650789f2f0
use standard tables with standard argument order;
wenzelm
parents:
42222
diff
changeset
|
566 |
Inttab.map_entry A (fn (_, ts) => (prec, ts)); |
18 | 567 |
|
42220 | 568 |
fun getS A max_prec NONE Si = |
569 |
filter |
|
570 |
(fn (_, _, _, Nonterminal (B, prec) :: _, _, _) => A = B andalso prec <= max_prec |
|
571 |
| _ => false) Si |
|
572 |
| getS A max_prec (SOME min_prec) Si = |
|
573 |
filter |
|
574 |
(fn (_, _, _, Nonterminal (B, prec) :: _, _, _) => |
|
575 |
A = B andalso prec > min_prec andalso prec <= max_prec |
|
576 |
| _ => false) Si; |
|
18 | 577 |
|
62669 | 578 |
fun get_states Estate j A max_prec = |
33317 | 579 |
filter |
38713 | 580 |
(fn (_, _, _, Nonterminal (B, prec) :: _, _, _) => A = B andalso prec <= max_prec |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
46
diff
changeset
|
581 |
| _ => false) |
62669 | 582 |
(Array.sub (Estate, j)); |
18 | 583 |
|
584 |
||
42219 | 585 |
fun movedot_term c (A, j, ts, Terminal a :: sa, id, i) = |
42282
4fa41e068ff0
report literal tokens according to parsetree head;
wenzelm
parents:
42226
diff
changeset
|
586 |
if Lexicon.valued_token c orelse id <> "" |
4fa41e068ff0
report literal tokens according to parsetree head;
wenzelm
parents:
42226
diff
changeset
|
587 |
then (A, j, Tip c :: ts, sa, id, i) |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
46
diff
changeset
|
588 |
else (A, j, ts, sa, id, i); |
18 | 589 |
|
42219 | 590 |
fun movedot_nonterm tt (A, j, ts, Nonterminal _ :: sa, id, i) = |
42282
4fa41e068ff0
report literal tokens according to parsetree head;
wenzelm
parents:
42226
diff
changeset
|
591 |
(A, j, tt @ ts, sa, id, i); |
18 | 592 |
|
42219 | 593 |
fun movedot_lambda [] _ = [] |
42221 | 594 |
| movedot_lambda ((t, ki) :: ts) (state as (B, j, tss, Nonterminal (A, k) :: sa, id, i)) = |
42282
4fa41e068ff0
report literal tokens according to parsetree head;
wenzelm
parents:
42226
diff
changeset
|
595 |
if k <= ki then (B, j, t @ tss, sa, id, i) :: movedot_lambda ts state |
42221 | 596 |
else movedot_lambda ts state; |
18 | 597 |
|
598 |
||
41378 | 599 |
(*trigger value for warnings*) |
56438 | 600 |
val branching_level = |
64556 | 601 |
Config.int (Config.declare ("syntax_branching_level", \<^here>) (fn _ => Config.Int 600)); |
18 | 602 |
|
1147
57b5f55bf879
removed 'raw' productions from gram datatype; replaced mk_gram by add_prods;
clasohm
parents:
890
diff
changeset
|
603 |
(*get all productions of a NT and NTs chained to it which can |
57b5f55bf879
removed 'raw' productions from gram datatype; replaced mk_gram by add_prods;
clasohm
parents:
890
diff
changeset
|
604 |
be started by specified token*) |
67540 | 605 |
fun prods_for (Gram {prods, chains, ...}) tk nt = |
37683 | 606 |
let |
67540 | 607 |
fun token_prods prods = |
608 |
fold cons (prods_lookup prods tk) #> |
|
67550 | 609 |
fold cons (prods_lookup prods Lexicon.dummy); |
67540 | 610 |
fun nt_prods nt = #2 (Vector.sub (prods, nt)); |
611 |
in fold (token_prods o nt_prods) (chains_all_preds chains [nt]) [] end; |
|
1147
57b5f55bf879
removed 'raw' productions from gram datatype; replaced mk_gram by add_prods;
clasohm
parents:
890
diff
changeset
|
612 |
|
57b5f55bf879
removed 'raw' productions from gram datatype; replaced mk_gram by add_prods;
clasohm
parents:
890
diff
changeset
|
613 |
|
67515 | 614 |
fun PROCESSS gram Estate i c states = |
37683 | 615 |
let |
62669 | 616 |
fun processS _ [] (Si, Sii) = (Si, Sii) |
37683 | 617 |
| processS used (S :: States) (Si, Sii) = |
618 |
(case S of |
|
38713 | 619 |
(_, _, _, Nonterminal (nt, min_prec) :: _, _, _) => |
38712
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
620 |
let (*predictor operation*) |
37683 | 621 |
val (used', new_states) = |
42226
cb650789f2f0
use standard tables with standard argument order;
wenzelm
parents:
42222
diff
changeset
|
622 |
(case Inttab.lookup used nt of |
38713 | 623 |
SOME (used_prec, l) => (*nonterminal has been processed*) |
624 |
if used_prec <= min_prec then |
|
38712
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
625 |
(*wanted precedence has been processed*) |
42219 | 626 |
(used, movedot_lambda l S) |
38712
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
627 |
else (*wanted precedence hasn't been parsed yet*) |
37683 | 628 |
let |
67540 | 629 |
val tk_prods = prods_for gram c nt; |
42217 | 630 |
val States' = |
62669 | 631 |
mk_states i nt (get_RHS' min_prec used_prec tk_prods); |
42219 | 632 |
in (update_prec (nt, min_prec) used, movedot_lambda l S @ States') end |
38712
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
633 |
| NONE => (*nonterminal is parsed for the first time*) |
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
634 |
let |
67540 | 635 |
val tk_prods = prods_for gram c nt; |
62669 | 636 |
val States' = mk_states i nt (get_RHS min_prec tk_prods); |
42226
cb650789f2f0
use standard tables with standard argument order;
wenzelm
parents:
42222
diff
changeset
|
637 |
in (Inttab.update (nt, (min_prec, [])) used, States') end); |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
46
diff
changeset
|
638 |
in |
37683 | 639 |
processS used' (new_states @ States) (S :: Si, Sii) |
15752 | 640 |
end |
38712
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
641 |
| (_, _, _, Terminal a :: _, _, _) => (*scanner operation*) |
37683 | 642 |
processS used States |
67549 | 643 |
(S :: Si, |
644 |
if Lexicon.tokens_match_ord (a, c) = EQUAL then movedot_term c S :: Sii else Sii) |
|
38712
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
645 |
| (A, prec, ts, [], id, j) => (*completer operation*) |
42222
ff50c436b199
accumulate parsetrees in canonical reverse order;
wenzelm
parents:
42221
diff
changeset
|
646 |
let val tt = if id = "" then ts else [Node (id, rev ts)] in |
38712
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
647 |
if j = i then (*lambda production?*) |
37683 | 648 |
let |
42226
cb650789f2f0
use standard tables with standard argument order;
wenzelm
parents:
42222
diff
changeset
|
649 |
val (prec', used') = update_trees (A, (tt, prec)) used; |
42220 | 650 |
val Slist = getS A prec prec' Si; |
651 |
val States' = map (movedot_nonterm tt) Slist; |
|
652 |
in processS used' (States' @ States) (S :: Si, Sii) end |
|
37683 | 653 |
else |
62669 | 654 |
let val Slist = get_states Estate j A prec |
38712
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
655 |
in processS used (map (movedot_nonterm tt) Slist @ States) (S :: Si, Sii) end |
37683 | 656 |
end) |
42226
cb650789f2f0
use standard tables with standard argument order;
wenzelm
parents:
42222
diff
changeset
|
657 |
in processS Inttab.empty states ([], []) end; |
18 | 658 |
|
659 |
||
67515 | 660 |
fun produce gram stateset i indata prev_token = |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
46
diff
changeset
|
661 |
(case Array.sub (stateset, i) of |
25986
26f1e4c172c3
syntax error: reduced spam -- print expected nonterminals instead of terminals;
wenzelm
parents:
24245
diff
changeset
|
662 |
[] => |
26f1e4c172c3
syntax error: reduced spam -- print expected nonterminals instead of terminals;
wenzelm
parents:
24245
diff
changeset
|
663 |
let |
37683 | 664 |
val toks = if Lexicon.is_eof prev_token then indata else prev_token :: indata; |
48992 | 665 |
val pos = Position.here (Lexicon.pos_of_token prev_token); |
27801
0d0adaf0228d
datatype token: maintain range, tuned representation;
wenzelm
parents:
26678
diff
changeset
|
666 |
in |
55624 | 667 |
if null toks then |
668 |
error ("Inner syntax error: unexpected end of input" ^ pos) |
|
669 |
else |
|
670 |
error ("Inner syntax error" ^ pos ^ |
|
671 |
Markup.markup Markup.no_report |
|
672 |
("\n" ^ Pretty.string_of |
|
673 |
(Pretty.block [ |
|
674 |
Pretty.str "at", Pretty.brk 1, |
|
675 |
Pretty.block |
|
676 |
(Pretty.str "\"" :: |
|
677 |
Pretty.breaks (map (Pretty.str o Lexicon.str_of_token) (#1 (split_last toks))) @ |
|
678 |
[Pretty.str "\""])]))) |
|
27801
0d0adaf0228d
datatype token: maintain range, tuned representation;
wenzelm
parents:
26678
diff
changeset
|
679 |
end |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
46
diff
changeset
|
680 |
| s => |
38712
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
681 |
(case indata of |
42218
8e0a4d500e5b
streamlined token list operations, assuming that the order of union does not matter;
wenzelm
parents:
42217
diff
changeset
|
682 |
[] => s |
42217 | 683 |
| c :: cs => |
684 |
let |
|
67515 | 685 |
val (si, sii) = PROCESSS gram stateset i c s; |
42217 | 686 |
val _ = Array.update (stateset, i, si); |
687 |
val _ = Array.update (stateset, i + 1, sii); |
|
67515 | 688 |
in produce gram stateset (i + 1) cs c end)); |
18 | 689 |
|
690 |
||
42217 | 691 |
fun get_trees states = map_filter (fn (_, _, [pt], _, _, _) => SOME pt | _ => NONE) states; |
18 | 692 |
|
67515 | 693 |
fun earley (gram as Gram {tags, ...}) startsymbol indata = |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
46
diff
changeset
|
694 |
let |
37683 | 695 |
val start_tag = |
67531 | 696 |
(case tags_lookup tags startsymbol of |
37683 | 697 |
SOME tag => tag |
40959 | 698 |
| NONE => error ("Inner syntax: bad grammar root symbol " ^ quote startsymbol)); |
37683 | 699 |
val S0 = [(~1, 0, [], [Nonterminal (start_tag, 0), Terminal Lexicon.eof], "", 0)]; |
330
2fda15dd1e0f
changed the way a grammar is generated to allow the new parser to work;
clasohm
parents:
258
diff
changeset
|
700 |
val s = length indata + 1; |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
46
diff
changeset
|
701 |
val Estate = Array.array (s, []); |
38712
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
702 |
val _ = Array.update (Estate, 0, S0); |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
46
diff
changeset
|
703 |
in |
67515 | 704 |
get_trees (produce gram Estate 0 indata Lexicon.eof) |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
46
diff
changeset
|
705 |
end; |
18 | 706 |
|
707 |
||
67515 | 708 |
fun parse gram start toks = |
27801
0d0adaf0228d
datatype token: maintain range, tuned representation;
wenzelm
parents:
26678
diff
changeset
|
709 |
let |
0d0adaf0228d
datatype token: maintain range, tuned representation;
wenzelm
parents:
26678
diff
changeset
|
710 |
val end_pos = |
0d0adaf0228d
datatype token: maintain range, tuned representation;
wenzelm
parents:
26678
diff
changeset
|
711 |
(case try List.last toks of |
0d0adaf0228d
datatype token: maintain range, tuned representation;
wenzelm
parents:
26678
diff
changeset
|
712 |
NONE => Position.none |
67551 | 713 |
| SOME tok => Lexicon.end_pos_of_token tok); |
27801
0d0adaf0228d
datatype token: maintain range, tuned representation;
wenzelm
parents:
26678
diff
changeset
|
714 |
val r = |
67515 | 715 |
(case earley gram start (toks @ [Lexicon.mk_eof end_pos]) of |
37852
a902f158b4fc
eliminated old-style sys_error/SYS_ERROR in favour of exception Fail -- after careful checking that there is no overlap with existing handling of that;
wenzelm
parents:
37684
diff
changeset
|
716 |
[] => raise Fail "Inner syntax: no parse trees" |
27801
0d0adaf0228d
datatype token: maintain range, tuned representation;
wenzelm
parents:
26678
diff
changeset
|
717 |
| pts => pts); |
0d0adaf0228d
datatype token: maintain range, tuned representation;
wenzelm
parents:
26678
diff
changeset
|
718 |
in r end; |
26678 | 719 |
|
720 |
||
721 |
fun guess_infix_lr (Gram gram) c = (*based on educated guess*) |
|
722 |
let |
|
38711
79d3cbfb4730
keep persistent production tables as immutable vectors -- potential performance improvement on modern hardware;
wenzelm
parents:
37852
diff
changeset
|
723 |
fun freeze a = map_range (curry Vector.sub a) (Vector.length a); |
67540 | 724 |
val prods = maps (prods_content o #2) (freeze (#prods gram)); |
37683 | 725 |
fun guess (SOME ([Nonterminal (_, k), |
726 |
Terminal (Lexicon.Token (Lexicon.Literal, s, _)), Nonterminal (_, l)], _, j)) = |
|
26678 | 727 |
if k = j andalso l = j + 1 then SOME (s, true, false, j) |
728 |
else if k = j + 1 then if l = j then SOME (s, false, true, j) |
|
729 |
else if l = j + 1 then SOME (s, false, false, j) |
|
730 |
else NONE |
|
731 |
else NONE |
|
732 |
| guess _ = NONE; |
|
733 |
in guess (find_first (fn (_, s, _) => s = c) prods) end; |
|
18 | 734 |
|
735 |
end; |