author | wenzelm |
Fri, 27 Sep 2024 22:14:40 +0200 | |
changeset 80979 | e38c65002f44 |
parent 80978 | 5e2b1588c5cb |
child 80980 | 54f5d6e1c317 |
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 |
|
78009
f906f7f83dae
performance tuning: cached non-persistent Parser.gram reduces heap size by approx. 1-4%;
wenzelm
parents:
78007
diff
changeset
|
12 |
val make_gram: Syntax_Ext.xprod list -> Syntax_Ext.xprod list -> gram option -> gram |
1507 | 13 |
val pretty_gram: gram -> Pretty.T list |
14 |
datatype parsetree = |
|
15 |
Node of string * parsetree list | |
|
16 |
Tip of Lexicon.token |
|
80978
5e2b1588c5cb
minor performance tuning: proper table for parsetree list;
wenzelm
parents:
80977
diff
changeset
|
17 |
val parsetree_ord: parsetree ord |
80950 | 18 |
val pretty_parsetree: parsetree -> Pretty.T list |
45641 | 19 |
val parse: gram -> string -> Lexicon.token list -> parsetree list |
15752 | 20 |
end; |
1507 | 21 |
|
15752 | 22 |
structure Parser: PARSER = |
18 | 23 |
struct |
15752 | 24 |
|
18 | 25 |
(** datatype gram **) |
26 |
||
67541 | 27 |
(* nonterminals *) |
28 |
||
67545 | 29 |
(*production for the NTs are stored in a vector, indexed by the NT tag*) |
67541 | 30 |
type nt = int; |
31 |
||
67545 | 32 |
type tags = nt Symtab.table; |
33 |
val tags_empty: tags = Symtab.empty; |
|
80959
4749c9b0ba73
eliminated redundant nt_count: rely on Symtab.size;
wenzelm
parents:
80958
diff
changeset
|
34 |
fun tags_size (tags: tags) = Symtab.size tags; |
67545 | 35 |
fun tags_content (tags: tags) = sort_by #1 (Symtab.dest tags); |
36 |
fun tags_lookup (tags: tags) = Symtab.lookup tags; |
|
37 |
fun tags_insert tag (tags: tags) = Symtab.update_new tag tags; |
|
78006 | 38 |
fun tags_name (tags: tags) = |
39 |
the o Inttab.lookup (Inttab.build (Symtab.fold (Inttab.update_new o swap) tags)); |
|
67545 | 40 |
|
79079 | 41 |
type nts = Bitset.T; |
42 |
val nts_empty: nts = Bitset.empty; |
|
43 |
val nts_merge: nts * nts -> nts = Bitset.merge; |
|
44 |
fun nts_insert nt : nts -> nts = Bitset.insert nt; |
|
45 |
fun nts_member (nts: nts) = Bitset.member nts; |
|
46 |
fun nts_fold f (nts: nts) = Bitset.fold f nts; |
|
47 |
fun nts_subset (nts1: nts, nts2: nts) = Bitset.forall (nts_member nts2) nts1; |
|
48 |
fun nts_is_empty (nts: nts) = Bitset.is_empty nts; |
|
49 |
fun nts_is_unique (nts: nts) = Bitset.is_unique nts; |
|
67541 | 50 |
|
51 |
||
67539 | 52 |
(* tokens *) |
53 |
||
80979 | 54 |
structure Tokens = Set(type key = Lexicon.token val ord = Lexicon.token_type_ord); |
67539 | 55 |
|
77823 | 56 |
fun tokens_find P tokens = Tokens.get_first (fn tok => if P tok then SOME tok else NONE) tokens; |
57 |
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
|
58 |
|
1147
57b5f55bf879
removed 'raw' productions from gram datatype; replaced mk_gram by add_prods;
clasohm
parents:
890
diff
changeset
|
59 |
|
67545 | 60 |
(* productions *) |
61 |
||
38712
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
62 |
datatype symb = |
67545 | 63 |
Terminal of Lexicon.token | |
80972 | 64 |
Nonterminal of nt * int | (*(tag, precedence)*) |
80969
667f5072ed2d
pro-forma support for markup blocks, without any change of result yet;
wenzelm
parents:
80968
diff
changeset
|
65 |
Markup of Markup.T list * symb list; |
38712
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
66 |
|
80972 | 67 |
type prod = symb list * string * int; (*rhs, name, precedence*) |
80970 | 68 |
|
69 |
fun make_chain_prod from : prod = ([Nonterminal (from, ~1)], "", ~1); |
|
70 |
||
71 |
fun dest_chain_prod (([Nonterminal (from, ~1)], _, ~1): prod) = SOME from |
|
72 |
| dest_chain_prod _ = NONE; |
|
73 |
||
80972 | 74 |
val no_prec = ~1; |
75 |
||
80978
5e2b1588c5cb
minor performance tuning: proper table for parsetree list;
wenzelm
parents:
80977
diff
changeset
|
76 |
fun upto_prec min max p = |
5e2b1588c5cb
minor performance tuning: proper table for parsetree list;
wenzelm
parents:
80977
diff
changeset
|
77 |
(min = no_prec orelse p >= min) andalso (max = no_prec orelse p <= max); |
5e2b1588c5cb
minor performance tuning: proper table for parsetree list;
wenzelm
parents:
80977
diff
changeset
|
78 |
|
5e2b1588c5cb
minor performance tuning: proper table for parsetree list;
wenzelm
parents:
80977
diff
changeset
|
79 |
fun until_prec min max p = |
80972 | 80 |
(min = no_prec orelse p >= min) andalso (max = no_prec orelse p < max); |
81 |
||
82 |
||
77823 | 83 |
structure Prods = Table(Tokens.Key); |
80970 | 84 |
type prods = prod list Prods.table; |
67540 | 85 |
|
77823 | 86 |
val prods_empty: prods = Prods.empty; |
87 |
fun prods_lookup (prods: prods) = Prods.lookup_list prods; |
|
88 |
fun prods_update entry : prods -> prods = Prods.update entry; |
|
89 |
fun prods_content (prods: prods) = distinct (op =) (maps #2 (Prods.dest prods)); |
|
67540 | 90 |
|
77823 | 91 |
type nt_gram = (nts * Tokens.T) * prods; (*dependent_nts, start_tokens, prods*) |
67545 | 92 |
(*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
|
93 |
|
77823 | 94 |
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
|
95 |
|
67531 | 96 |
type chains = unit Int_Graph.T; |
97 |
fun chains_preds (chains: chains) = Int_Graph.immediate_preds chains; |
|
98 |
fun chains_all_preds (chains: chains) = Int_Graph.all_preds chains; |
|
99 |
fun chains_all_succs (chains: chains) = Int_Graph.all_succs chains; |
|
100 |
val chains_empty: chains = Int_Graph.empty; |
|
101 |
fun chains_member (chains: chains) = Int_Graph.is_edge chains; |
|
102 |
fun chains_declare nt : chains -> chains = Int_Graph.default_node (nt, ()); |
|
80968 | 103 |
|
104 |
fun chains_declares (Terminal _) = I |
|
105 |
| chains_declares (Nonterminal (nt, _)) = chains_declare nt |
|
80969
667f5072ed2d
pro-forma support for markup blocks, without any change of result yet;
wenzelm
parents:
80968
diff
changeset
|
106 |
| chains_declares (Markup (_, body)) = fold chains_declares body; |
80968 | 107 |
|
67531 | 108 |
fun chains_insert (from, to) = |
109 |
chains_declare from #> chains_declare to #> Int_Graph.add_edge (from, to); |
|
110 |
||
1147
57b5f55bf879
removed 'raw' productions from gram datatype; replaced mk_gram by add_prods;
clasohm
parents:
890
diff
changeset
|
111 |
datatype gram = |
38712
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
112 |
Gram of |
80959
4749c9b0ba73
eliminated redundant nt_count: rely on Symtab.size;
wenzelm
parents:
80958
diff
changeset
|
113 |
{tags: tags, |
67531 | 114 |
chains: chains, |
67533
f253e5eaf995
clarified types and operations: potentially more efficient add_prods;
wenzelm
parents:
67532
diff
changeset
|
115 |
lambdas: nts, |
38712
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
116 |
prods: nt_gram Vector.vector}; |
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
117 |
(*"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
|
118 |
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
|
119 |
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
|
120 |
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
|
121 |
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
|
122 |
|
38712
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
123 |
(*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
|
124 |
known yet are associated with this token*) |
38713 | 125 |
val unknown_start = Lexicon.eof; |
38712
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
126 |
|
67539 | 127 |
fun get_start tks = |
77823 | 128 |
(case Tokens.min tks of |
77818 | 129 |
SOME tk => tk |
67539 | 130 |
| NONE => unknown_start); |
67516 | 131 |
|
80970 | 132 |
fun add_production array_prods (lhs, new_prod as (rhs, _, _) : prod) (chains, lambdas) = |
67543 | 133 |
let |
80970 | 134 |
val chain = dest_chain_prod new_prod; |
135 |
val (new_chain, chains') = |
|
136 |
(case chain of |
|
137 |
SOME from => |
|
67543 | 138 |
if chains_member chains (from, lhs) |
80970 | 139 |
then (false, chains) |
140 |
else (true, chains_insert (from, lhs) chains) |
|
141 |
| NONE => (false, chains |> chains_declare lhs |> fold chains_declares rhs)); |
|
1147
57b5f55bf879
removed 'raw' productions from gram datatype; replaced mk_gram by add_prods;
clasohm
parents:
890
diff
changeset
|
142 |
|
67543 | 143 |
(*propagate new chain in lookahead and lambdas; |
144 |
added_starts is used later to associate existing |
|
145 |
productions with new starting tokens*) |
|
146 |
val (added_starts, lambdas') = |
|
147 |
if not new_chain then ([], lambdas) |
|
148 |
else |
|
149 |
let (*lookahead of chain's source*) |
|
78007 | 150 |
val ((_, from_tks), _) = Array.nth array_prods (the chain); |
1175
1b15a4b1a4f7
added comments; fixed a bug; reduced memory usage slightly
clasohm
parents:
1147
diff
changeset
|
151 |
|
67543 | 152 |
(*copy from's lookahead to chain's destinations*) |
153 |
fun copy_lookahead to = |
|
154 |
let |
|
78007 | 155 |
val ((to_nts, to_tks), ps) = Array.nth array_prods to; |
1147
57b5f55bf879
removed 'raw' productions from gram datatype; replaced mk_gram by add_prods;
clasohm
parents:
890
diff
changeset
|
156 |
|
77823 | 157 |
val new_tks = Tokens.subtract to_tks from_tks; (*added lookahead tokens*) |
158 |
val to_tks' = Tokens.merge (to_tks, new_tks); |
|
78007 | 159 |
val _ = Array.upd array_prods to ((to_nts, to_tks'), ps); |
67543 | 160 |
in tokens_add (to, new_tks) end; |
1175
1b15a4b1a4f7
added comments; fixed a bug; reduced memory usage slightly
clasohm
parents:
1147
diff
changeset
|
161 |
|
67543 | 162 |
val tos = chains_all_succs chains' [lhs]; |
163 |
in |
|
164 |
(fold copy_lookahead tos [], |
|
165 |
lambdas |> nts_member lambdas lhs ? fold nts_insert tos) |
|
166 |
end; |
|
38712
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
167 |
|
67543 | 168 |
(*test if new production can produce lambda |
169 |
(rhs must either be empty or only consist of lambda NTs)*) |
|
80969
667f5072ed2d
pro-forma support for markup blocks, without any change of result yet;
wenzelm
parents:
80968
diff
changeset
|
170 |
fun lambda_symb (Nonterminal (id, _)) = nts_member lambdas' id |
667f5072ed2d
pro-forma support for markup blocks, without any change of result yet;
wenzelm
parents:
80968
diff
changeset
|
171 |
| lambda_symb (Terminal _) = false |
667f5072ed2d
pro-forma support for markup blocks, without any change of result yet;
wenzelm
parents:
80968
diff
changeset
|
172 |
| lambda_symb (Markup (_, body)) = forall lambda_symb body; |
67543 | 173 |
val new_lambdas = |
80969
667f5072ed2d
pro-forma support for markup blocks, without any change of result yet;
wenzelm
parents:
80968
diff
changeset
|
174 |
if forall lambda_symb rhs |
67543 | 175 |
then SOME (filter_out (nts_member lambdas') (chains_all_succs chains' [lhs])) |
176 |
else NONE; |
|
177 |
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
|
178 |
|
67543 | 179 |
(*list optional terminal and all nonterminals on which the lookahead |
180 |
of a production depends*) |
|
181 |
fun lookahead_dependency _ [] nts = (NONE, nts) |
|
182 |
| lookahead_dependency _ (Terminal tk :: _) nts = (SOME tk, nts) |
|
183 |
| lookahead_dependency lambdas (Nonterminal (nt, _) :: symbs) nts = |
|
184 |
if nts_member lambdas nt then |
|
185 |
lookahead_dependency lambdas symbs (nts_insert nt nts) |
|
80969
667f5072ed2d
pro-forma support for markup blocks, without any change of result yet;
wenzelm
parents:
80968
diff
changeset
|
186 |
else (NONE, nts_insert nt nts) |
667f5072ed2d
pro-forma support for markup blocks, without any change of result yet;
wenzelm
parents:
80968
diff
changeset
|
187 |
| lookahead_dependency lambdas (Markup (_, body) :: symbs) nts = |
667f5072ed2d
pro-forma support for markup blocks, without any change of result yet;
wenzelm
parents:
80968
diff
changeset
|
188 |
lookahead_dependency lambdas (body @ symbs) nts; |
330
2fda15dd1e0f
changed the way a grammar is generated to allow the new parser to work;
clasohm
parents:
258
diff
changeset
|
189 |
|
67543 | 190 |
(*get all known starting tokens for a nonterminal*) |
78007 | 191 |
fun starts_for_nt nt = snd (fst (Array.nth array_prods nt)); |
38712
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
192 |
|
67543 | 193 |
(*update prods, lookaheads, and lambdas according to new lambda NTs*) |
194 |
val (added_starts', lambdas') = |
|
195 |
let |
|
196 |
(*propagate added lambda NT*) |
|
197 |
fun propagate_lambda [] added_starts lambdas = (added_starts, lambdas) |
|
198 |
| propagate_lambda (l :: ls) added_starts lambdas = |
|
199 |
let |
|
200 |
(*get lookahead for lambda NT*) |
|
78007 | 201 |
val ((dependent, l_starts), _) = Array.nth array_prods l; |
1175
1b15a4b1a4f7
added comments; fixed a bug; reduced memory usage slightly
clasohm
parents:
1147
diff
changeset
|
202 |
|
67543 | 203 |
(*check productions whose lookahead may depend on lambda NT*) |
204 |
fun examine_prods [] add_lambda nt_dependencies added_tks nt_prods = |
|
205 |
(add_lambda, nt_dependencies, added_tks, nt_prods) |
|
206 |
| examine_prods ((p as (rhs, _, _)) :: ps) add_lambda |
|
207 |
nt_dependencies added_tks nt_prods = |
|
208 |
let val (tk, nts) = lookahead_dependency lambdas rhs nts_empty in |
|
209 |
if nts_member nts l then (*update production's lookahead*) |
|
210 |
let |
|
211 |
val new_lambda = |
|
212 |
is_none tk andalso nts_subset (nts, lambdas); |
|
18 | 213 |
|
67543 | 214 |
val new_tks = |
77823 | 215 |
Tokens.empty |
216 |
|> fold Tokens.insert (the_list tk) |
|
217 |
|> nts_fold (curry Tokens.merge o starts_for_nt) nts |
|
218 |
|> Tokens.subtract l_starts; |
|
38712
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
219 |
|
77823 | 220 |
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
|
221 |
|
67543 | 222 |
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
|
223 |
|
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
224 |
(*associate production with new starting tokens*) |
67539 | 225 |
fun copy tk nt_prods = |
67543 | 226 |
prods_update (tk, p :: prods_lookup nt_prods tk) nt_prods; |
227 |
||
228 |
val nt_prods' = nt_prods |
|
77823 | 229 |
|> Tokens.fold copy new_tks |
67550 | 230 |
|> new_lambda ? copy Lexicon.dummy; |
67543 | 231 |
in |
232 |
examine_prods ps (add_lambda orelse new_lambda) |
|
233 |
nt_dependencies' added_tks' nt_prods' |
|
234 |
end |
|
235 |
else (*skip production*) |
|
236 |
examine_prods ps add_lambda nt_dependencies added_tks nt_prods |
|
237 |
end; |
|
238 |
||
239 |
(*check each NT whose lookahead depends on new lambda NT*) |
|
240 |
fun process_nts nt (added_lambdas, added_starts) = |
|
241 |
let |
|
78007 | 242 |
val ((old_nts, old_tks), nt_prods) = Array.nth array_prods nt; |
67543 | 243 |
|
244 |
(*existing productions whose lookahead may depend on l*) |
|
245 |
val tk_prods = prods_lookup nt_prods (get_start l_starts); |
|
246 |
||
247 |
(*add_lambda is true if an existing production of the nt |
|
248 |
produces lambda due to the new lambda NT l*) |
|
249 |
val (add_lambda, nt_dependencies, added_tks, nt_prods') = |
|
77823 | 250 |
examine_prods tk_prods false nts_empty Tokens.empty nt_prods; |
67543 | 251 |
|
252 |
val new_nts = nts_merge (old_nts, nt_dependencies); |
|
77823 | 253 |
val new_tks = Tokens.merge (old_tks, added_tks); |
67543 | 254 |
|
255 |
val added_lambdas' = added_lambdas |> add_lambda ? cons nt; |
|
78007 | 256 |
val _ = Array.upd array_prods nt ((new_nts, new_tks), nt_prods'); |
67543 | 257 |
(*N.B. that because the tks component |
258 |
is used to access existing |
|
259 |
productions we have to add new |
|
260 |
tokens at the _end_ of the list*) |
|
261 |
val added_starts' = tokens_add (nt, added_tks) added_starts; |
|
262 |
in (added_lambdas', added_starts') end; |
|
377
ab8917806779
lookaheads are now computed faster (during the grammar is built)
clasohm
parents:
373
diff
changeset
|
263 |
|
67543 | 264 |
val (added_lambdas, added_starts') = |
265 |
nts_fold process_nts dependent ([], added_starts); |
|
266 |
val added_lambdas' = filter_out (nts_member lambdas) added_lambdas; |
|
267 |
in |
|
268 |
propagate_lambda (ls @ added_lambdas') added_starts' |
|
269 |
(fold nts_insert added_lambdas' lambdas) |
|
270 |
end; |
|
271 |
in |
|
272 |
propagate_lambda |
|
273 |
(nts_fold (fn l => not (nts_member lambdas l) ? cons l) lambdas'' []) |
|
274 |
added_starts lambdas'' |
|
275 |
end; |
|
276 |
||
277 |
(*insert production into grammar*) |
|
278 |
val added_starts' = |
|
279 |
if is_some chain then added_starts' (*don't store chain production*) |
|
280 |
else |
|
281 |
let |
|
282 |
(*lookahead tokens of new production and on which |
|
283 |
NTs lookahead depends*) |
|
284 |
val (start_tk, start_nts) = lookahead_dependency lambdas' rhs nts_empty; |
|
285 |
||
286 |
val start_tks = |
|
77823 | 287 |
Tokens.empty |
288 |
|> fold Tokens.insert (the_list start_tk) |
|
289 |
|> nts_fold (curry Tokens.merge o starts_for_nt) start_nts; |
|
67543 | 290 |
|
291 |
val start_tks' = |
|
292 |
start_tks |
|
77823 | 293 |
|> (if is_some new_lambdas then Tokens.insert Lexicon.dummy |
294 |
else if Tokens.is_empty start_tks then Tokens.insert unknown_start |
|
67543 | 295 |
else I); |
296 |
||
297 |
(*add lhs NT to list of dependent NTs in lookahead*) |
|
298 |
fun add_nts nt initial = |
|
299 |
(if initial then |
|
78007 | 300 |
let val ((old_nts, old_tks), ps) = Array.nth array_prods nt in |
67543 | 301 |
if nts_member old_nts lhs then () |
78007 | 302 |
else Array.upd array_prods nt ((nts_insert lhs old_nts, old_tks), ps) |
67543 | 303 |
end |
304 |
else (); false); |
|
305 |
||
306 |
(*add new start tokens to chained NTs' lookahead list; |
|
307 |
also store new production for lhs NT*) |
|
308 |
fun add_tks [] added = added |
|
309 |
| add_tks (nt :: nts) added = |
|
310 |
let |
|
78007 | 311 |
val ((old_nts, old_tks), nt_prods) = Array.nth array_prods nt; |
67543 | 312 |
|
77823 | 313 |
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
|
314 |
|
67543 | 315 |
(*store new production*) |
316 |
fun store tk (prods, _) = |
|
317 |
let |
|
318 |
val tk_prods = prods_lookup prods tk; |
|
319 |
val tk_prods' = new_prod :: tk_prods; |
|
320 |
val prods' = prods_update (tk, tk_prods') prods; |
|
321 |
in (prods', true) end; |
|
38712
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
322 |
|
67543 | 323 |
val (nt_prods', changed) = (nt_prods, false) |
77823 | 324 |
|> nt = lhs ? Tokens.fold store start_tks'; |
67543 | 325 |
val _ = |
77823 | 326 |
if not changed andalso Tokens.is_empty new_tks then () |
78007 | 327 |
else Array.upd array_prods nt ((old_nts, Tokens.merge (old_tks, new_tks)), nt_prods'); |
67543 | 328 |
in add_tks nts (tokens_add (nt, new_tks) added) end; |
329 |
val _ = nts_fold add_nts start_nts true; |
|
330 |
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
|
331 |
|
67543 | 332 |
(*associate productions with new lookaheads*) |
333 |
val _ = |
|
334 |
let |
|
335 |
(*propagate added start tokens*) |
|
336 |
fun add_starts [] = () |
|
337 |
| add_starts ((changed_nt, new_tks) :: starts) = |
|
338 |
let |
|
339 |
(*token under which old productions which |
|
340 |
depend on changed_nt could be stored*) |
|
341 |
val key = |
|
77823 | 342 |
tokens_find (not o Tokens.member new_tks) (starts_for_nt changed_nt) |
67543 | 343 |
|> the_default unknown_start; |
344 |
||
345 |
(*copy productions whose lookahead depends on changed_nt; |
|
346 |
if key = SOME unknown_start then tk_prods is used to hold |
|
347 |
the productions not copied*) |
|
348 |
fun update_prods [] result = result |
|
349 |
| update_prods ((p as (rhs, _: string, _: nt)) :: ps) |
|
350 |
(tk_prods, nt_prods) = |
|
351 |
let |
|
352 |
(*lookahead dependency for production*) |
|
353 |
val (tk, depends) = lookahead_dependency lambdas' rhs nts_empty; |
|
354 |
||
355 |
(*test if this production has to be copied*) |
|
356 |
val update = nts_member depends changed_nt; |
|
18 | 357 |
|
67543 | 358 |
(*test if production could already be associated with |
359 |
a member of new_tks*) |
|
360 |
val lambda = |
|
361 |
not (nts_is_unique depends) orelse |
|
362 |
not (nts_is_empty depends) andalso is_some tk |
|
77823 | 363 |
andalso Tokens.member new_tks (the tk); |
67543 | 364 |
|
365 |
(*associate production with new starting tokens*) |
|
366 |
fun copy tk nt_prods = |
|
367 |
let |
|
368 |
val tk_prods = prods_lookup nt_prods tk; |
|
369 |
val tk_prods' = |
|
370 |
if not lambda then p :: tk_prods |
|
371 |
else insert (op =) p tk_prods; |
|
372 |
(*if production depends on lambda NT we |
|
373 |
have to look for duplicates*) |
|
374 |
in prods_update (tk, tk_prods') nt_prods end; |
|
375 |
val result = |
|
77823 | 376 |
if update then (tk_prods, Tokens.fold copy new_tks nt_prods) |
67543 | 377 |
else if key = unknown_start then (p :: tk_prods, nt_prods) |
378 |
else (tk_prods, nt_prods); |
|
379 |
in update_prods ps result end; |
|
18 | 380 |
|
67543 | 381 |
(*copy existing productions for new starting tokens*) |
382 |
fun process_nts nt = |
|
383 |
let |
|
78007 | 384 |
val ((nts, tks), nt_prods) = Array.nth array_prods nt; |
67543 | 385 |
|
386 |
val tk_prods = prods_lookup nt_prods key; |
|
387 |
||
388 |
(*associate productions with new lookahead tokens*) |
|
389 |
val (tk_prods', nt_prods') = update_prods tk_prods ([], nt_prods); |
|
390 |
||
391 |
val nt_prods'' = |
|
392 |
if key = unknown_start then |
|
393 |
prods_update (key, tk_prods') nt_prods' |
|
394 |
else nt_prods'; |
|
395 |
||
77823 | 396 |
val added_tks = Tokens.subtract tks new_tks; |
397 |
val tks' = Tokens.merge (tks, added_tks); |
|
78007 | 398 |
val _ = Array.upd array_prods nt ((nts, tks'), nt_prods''); |
67543 | 399 |
in tokens_add (nt, added_tks) end; |
400 |
||
78007 | 401 |
val ((dependent, _), _) = Array.nth array_prods changed_nt; |
67543 | 402 |
in add_starts (starts @ nts_fold process_nts dependent []) end; |
403 |
in add_starts added_starts' end; |
|
404 |
in (chains', lambdas') end; |
|
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
46
diff
changeset
|
405 |
|
18 | 406 |
|
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
46
diff
changeset
|
407 |
(* pretty_gram *) |
18 | 408 |
|
1147
57b5f55bf879
removed 'raw' productions from gram datatype; replaced mk_gram by add_prods;
clasohm
parents:
890
diff
changeset
|
409 |
fun pretty_gram (Gram {tags, prods, chains, ...}) = |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
46
diff
changeset
|
410 |
let |
67531 | 411 |
val print_nt = tags_name tags; |
67518 | 412 |
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
|
413 |
|
67552 | 414 |
fun pretty_symb (Terminal tok) = |
415 |
if Lexicon.is_literal tok |
|
80969
667f5072ed2d
pro-forma support for markup blocks, without any change of result yet;
wenzelm
parents:
80968
diff
changeset
|
416 |
then [Pretty.quote (Pretty.keyword1 (Lexicon.str_of_token tok))] |
667f5072ed2d
pro-forma support for markup blocks, without any change of result yet;
wenzelm
parents:
80968
diff
changeset
|
417 |
else [Pretty.str (Lexicon.str_of_token tok)] |
667f5072ed2d
pro-forma support for markup blocks, without any change of result yet;
wenzelm
parents:
80968
diff
changeset
|
418 |
| pretty_symb (Nonterminal (tag, p)) = [Pretty.str (print_nt tag ^ print_pri p)] |
667f5072ed2d
pro-forma support for markup blocks, without any change of result yet;
wenzelm
parents:
80968
diff
changeset
|
419 |
| pretty_symb (Markup (_, body)) = maps pretty_symb body; |
18 | 420 |
|
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
46
diff
changeset
|
421 |
fun pretty_const "" = [] |
67513 | 422 |
| 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
|
423 |
|
67513 | 424 |
fun pretty_prod (name, tag) = |
80970 | 425 |
(prods_content (#2 (Vector.nth prods tag)) @ map make_chain_prod (chains_preds chains tag)) |
67513 | 426 |
|> map (fn (symbs, const, p) => |
427 |
Pretty.block (Pretty.breaks |
|
80969
667f5072ed2d
pro-forma support for markup blocks, without any change of result yet;
wenzelm
parents:
80968
diff
changeset
|
428 |
(Pretty.str (name ^ print_pri p ^ " =") :: maps pretty_symb symbs @ pretty_const const))); |
67531 | 429 |
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
|
430 |
|
57b5f55bf879
removed 'raw' productions from gram datatype; replaced mk_gram by add_prods;
clasohm
parents:
890
diff
changeset
|
431 |
|
42217 | 432 |
|
67545 | 433 |
(** operations on grammars **) |
1147
57b5f55bf879
removed 'raw' productions from gram datatype; replaced mk_gram by add_prods;
clasohm
parents:
890
diff
changeset
|
434 |
|
38712
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
435 |
val empty_gram = |
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
436 |
Gram |
80959
4749c9b0ba73
eliminated redundant nt_count: rely on Symtab.size;
wenzelm
parents:
80958
diff
changeset
|
437 |
{tags = tags_empty, |
67531 | 438 |
chains = chains_empty, |
67533
f253e5eaf995
clarified types and operations: potentially more efficient add_prods;
wenzelm
parents:
67532
diff
changeset
|
439 |
lambdas = nts_empty, |
f253e5eaf995
clarified types and operations: potentially more efficient add_prods;
wenzelm
parents:
67532
diff
changeset
|
440 |
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
|
441 |
|
80960 | 442 |
local |
443 |
||
444 |
fun make_tag s tags = |
|
445 |
(case tags_lookup tags s of |
|
446 |
SOME tag => (tag, tags) |
|
447 |
| NONE => |
|
448 |
let val tag = tags_size tags |
|
449 |
in (tag, tags_insert (s, tag) tags) end); |
|
450 |
||
451 |
fun make_arg (s, p) tags = |
|
452 |
(case Lexicon.get_terminal s of |
|
453 |
NONE => |
|
454 |
let val (tag, tags') = make_tag s tags; |
|
455 |
in (Nonterminal (tag, p), tags') end |
|
456 |
| SOME tok => (Terminal tok, tags)); |
|
457 |
||
80959
4749c9b0ba73
eliminated redundant nt_count: rely on Symtab.size;
wenzelm
parents:
80958
diff
changeset
|
458 |
fun extend_gram xprods gram = |
78009
f906f7f83dae
performance tuning: cached non-persistent Parser.gram reduces heap size by approx. 1-4%;
wenzelm
parents:
78007
diff
changeset
|
459 |
let |
80965
f74aecc6ef9c
tuned, following make_symbs in src/Pure/Syntax/printer.ML;
wenzelm
parents:
80964
diff
changeset
|
460 |
fun make_symbs (Syntax_Ext.Delim s :: xsyms) tags = |
80969
667f5072ed2d
pro-forma support for markup blocks, without any change of result yet;
wenzelm
parents:
80968
diff
changeset
|
461 |
let val (syms, xsyms', tags') = make_symbs xsyms tags |
667f5072ed2d
pro-forma support for markup blocks, without any change of result yet;
wenzelm
parents:
80968
diff
changeset
|
462 |
in (Terminal (Lexicon.literal s) :: syms, xsyms', tags') end |
80965
f74aecc6ef9c
tuned, following make_symbs in src/Pure/Syntax/printer.ML;
wenzelm
parents:
80964
diff
changeset
|
463 |
| make_symbs (Syntax_Ext.Argument a :: xsyms) tags = |
f74aecc6ef9c
tuned, following make_symbs in src/Pure/Syntax/printer.ML;
wenzelm
parents:
80964
diff
changeset
|
464 |
let |
f74aecc6ef9c
tuned, following make_symbs in src/Pure/Syntax/printer.ML;
wenzelm
parents:
80964
diff
changeset
|
465 |
val (arg, tags') = make_arg a tags; |
80969
667f5072ed2d
pro-forma support for markup blocks, without any change of result yet;
wenzelm
parents:
80968
diff
changeset
|
466 |
val (syms, xsyms', tags'') = make_symbs xsyms tags'; |
667f5072ed2d
pro-forma support for markup blocks, without any change of result yet;
wenzelm
parents:
80968
diff
changeset
|
467 |
in (arg :: syms, xsyms', tags'') end |
667f5072ed2d
pro-forma support for markup blocks, without any change of result yet;
wenzelm
parents:
80968
diff
changeset
|
468 |
| make_symbs (Syntax_Ext.Bg {markup, ...} :: xsyms) tags = |
667f5072ed2d
pro-forma support for markup blocks, without any change of result yet;
wenzelm
parents:
80968
diff
changeset
|
469 |
let |
667f5072ed2d
pro-forma support for markup blocks, without any change of result yet;
wenzelm
parents:
80968
diff
changeset
|
470 |
val (bsyms, xsyms', tags') = make_symbs xsyms tags; |
667f5072ed2d
pro-forma support for markup blocks, without any change of result yet;
wenzelm
parents:
80968
diff
changeset
|
471 |
val (syms, xsyms'', tags'') = make_symbs xsyms' tags'; |
667f5072ed2d
pro-forma support for markup blocks, without any change of result yet;
wenzelm
parents:
80968
diff
changeset
|
472 |
val syms' = if null markup then bsyms @ syms else Markup (markup, bsyms) :: syms; |
667f5072ed2d
pro-forma support for markup blocks, without any change of result yet;
wenzelm
parents:
80968
diff
changeset
|
473 |
in (syms', xsyms'', tags'') end |
667f5072ed2d
pro-forma support for markup blocks, without any change of result yet;
wenzelm
parents:
80968
diff
changeset
|
474 |
| make_symbs (Syntax_Ext.En :: xsyms) tags = ([], xsyms, tags) |
667f5072ed2d
pro-forma support for markup blocks, without any change of result yet;
wenzelm
parents:
80968
diff
changeset
|
475 |
| make_symbs (Syntax_Ext.Space _ :: xsyms) tags = make_symbs xsyms tags |
667f5072ed2d
pro-forma support for markup blocks, without any change of result yet;
wenzelm
parents:
80968
diff
changeset
|
476 |
| make_symbs (Syntax_Ext.Brk _ :: xsyms) tags = make_symbs xsyms tags |
667f5072ed2d
pro-forma support for markup blocks, without any change of result yet;
wenzelm
parents:
80968
diff
changeset
|
477 |
| make_symbs [] tags = ([], [], tags); |
1438 | 478 |
|
80960 | 479 |
fun make_prod (Syntax_Ext.XProd (lhs, xsymbs, const, pri)) (result, tags) = |
480 |
let |
|
481 |
val (tag, tags') = make_tag lhs tags; |
|
80969
667f5072ed2d
pro-forma support for markup blocks, without any change of result yet;
wenzelm
parents:
80968
diff
changeset
|
482 |
val (symbs, xsymbs', tags'') = make_symbs xsymbs tags'; |
667f5072ed2d
pro-forma support for markup blocks, without any change of result yet;
wenzelm
parents:
80968
diff
changeset
|
483 |
val _ = if null xsymbs' then () else raise Fail "Unbalanced blocks in grammar production"; |
80960 | 484 |
in ((tag, (symbs, const, pri)) :: result, tags'') end; |
1147
57b5f55bf879
removed 'raw' productions from gram datatype; replaced mk_gram by add_prods;
clasohm
parents:
890
diff
changeset
|
485 |
|
80959
4749c9b0ba73
eliminated redundant nt_count: rely on Symtab.size;
wenzelm
parents:
80958
diff
changeset
|
486 |
|
4749c9b0ba73
eliminated redundant nt_count: rely on Symtab.size;
wenzelm
parents:
80958
diff
changeset
|
487 |
val Gram {tags, chains, lambdas, prods} = gram; |
4749c9b0ba73
eliminated redundant nt_count: rely on Symtab.size;
wenzelm
parents:
80958
diff
changeset
|
488 |
|
80960 | 489 |
val (new_prods, tags') = fold make_prod xprods ([], tags); |
1147
57b5f55bf879
removed 'raw' productions from gram datatype; replaced mk_gram by add_prods;
clasohm
parents:
890
diff
changeset
|
490 |
|
78009
f906f7f83dae
performance tuning: cached non-persistent Parser.gram reduces heap size by approx. 1-4%;
wenzelm
parents:
78007
diff
changeset
|
491 |
val array_prods' = |
80959
4749c9b0ba73
eliminated redundant nt_count: rely on Symtab.size;
wenzelm
parents:
80958
diff
changeset
|
492 |
Array.tabulate (tags_size tags', fn i => |
4749c9b0ba73
eliminated redundant nt_count: rely on Symtab.size;
wenzelm
parents:
80958
diff
changeset
|
493 |
if i < Vector.length prods then Vector.nth prods i |
78009
f906f7f83dae
performance tuning: cached non-persistent Parser.gram reduces heap size by approx. 1-4%;
wenzelm
parents:
78007
diff
changeset
|
494 |
else nt_gram_empty); |
38712
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
495 |
|
78009
f906f7f83dae
performance tuning: cached non-persistent Parser.gram reduces heap size by approx. 1-4%;
wenzelm
parents:
78007
diff
changeset
|
496 |
val (chains', lambdas') = |
80957 | 497 |
(chains, lambdas) |> fold (add_production array_prods') new_prods; |
78009
f906f7f83dae
performance tuning: cached non-persistent Parser.gram reduces heap size by approx. 1-4%;
wenzelm
parents:
78007
diff
changeset
|
498 |
in |
f906f7f83dae
performance tuning: cached non-persistent Parser.gram reduces heap size by approx. 1-4%;
wenzelm
parents:
78007
diff
changeset
|
499 |
Gram |
80959
4749c9b0ba73
eliminated redundant nt_count: rely on Symtab.size;
wenzelm
parents:
80958
diff
changeset
|
500 |
{tags = tags', |
78009
f906f7f83dae
performance tuning: cached non-persistent Parser.gram reduces heap size by approx. 1-4%;
wenzelm
parents:
78007
diff
changeset
|
501 |
chains = chains', |
f906f7f83dae
performance tuning: cached non-persistent Parser.gram reduces heap size by approx. 1-4%;
wenzelm
parents:
78007
diff
changeset
|
502 |
lambdas = lambdas', |
f906f7f83dae
performance tuning: cached non-persistent Parser.gram reduces heap size by approx. 1-4%;
wenzelm
parents:
78007
diff
changeset
|
503 |
prods = Array.vector array_prods'} |
f906f7f83dae
performance tuning: cached non-persistent Parser.gram reduces heap size by approx. 1-4%;
wenzelm
parents:
78007
diff
changeset
|
504 |
end; |
18 | 505 |
|
80960 | 506 |
in |
507 |
||
78009
f906f7f83dae
performance tuning: cached non-persistent Parser.gram reduces heap size by approx. 1-4%;
wenzelm
parents:
78007
diff
changeset
|
508 |
fun make_gram [] _ (SOME gram) = gram |
f906f7f83dae
performance tuning: cached non-persistent Parser.gram reduces heap size by approx. 1-4%;
wenzelm
parents:
78007
diff
changeset
|
509 |
| make_gram new_xprods _ (SOME gram) = extend_gram new_xprods gram |
f906f7f83dae
performance tuning: cached non-persistent Parser.gram reduces heap size by approx. 1-4%;
wenzelm
parents:
78007
diff
changeset
|
510 |
| make_gram [] [] NONE = empty_gram |
f906f7f83dae
performance tuning: cached non-persistent Parser.gram reduces heap size by approx. 1-4%;
wenzelm
parents:
78007
diff
changeset
|
511 |
| make_gram new_xprods old_xprods NONE = extend_gram (new_xprods @ old_xprods) empty_gram; |
1147
57b5f55bf879
removed 'raw' productions from gram datatype; replaced mk_gram by add_prods;
clasohm
parents:
890
diff
changeset
|
512 |
|
80960 | 513 |
end; |
514 |
||
18 | 515 |
|
42217 | 516 |
|
42374
b9a6b465da25
clarified pretty_parsetree: suppress literal tokens;
wenzelm
parents:
42288
diff
changeset
|
517 |
(** parser **) |
b9a6b465da25
clarified pretty_parsetree: suppress literal tokens;
wenzelm
parents:
42288
diff
changeset
|
518 |
|
b9a6b465da25
clarified pretty_parsetree: suppress literal tokens;
wenzelm
parents:
42288
diff
changeset
|
519 |
(* parsetree *) |
18 | 520 |
|
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
46
diff
changeset
|
521 |
datatype parsetree = |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
46
diff
changeset
|
522 |
Node of string * parsetree list | |
37683 | 523 |
Tip of Lexicon.token; |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
46
diff
changeset
|
524 |
|
80978
5e2b1588c5cb
minor performance tuning: proper table for parsetree list;
wenzelm
parents:
80977
diff
changeset
|
525 |
local |
5e2b1588c5cb
minor performance tuning: proper table for parsetree list;
wenzelm
parents:
80977
diff
changeset
|
526 |
|
5e2b1588c5cb
minor performance tuning: proper table for parsetree list;
wenzelm
parents:
80977
diff
changeset
|
527 |
fun cons_nr (Node _) = 0 |
5e2b1588c5cb
minor performance tuning: proper table for parsetree list;
wenzelm
parents:
80977
diff
changeset
|
528 |
| cons_nr (Tip _) = 1; |
5e2b1588c5cb
minor performance tuning: proper table for parsetree list;
wenzelm
parents:
80977
diff
changeset
|
529 |
|
5e2b1588c5cb
minor performance tuning: proper table for parsetree list;
wenzelm
parents:
80977
diff
changeset
|
530 |
in |
5e2b1588c5cb
minor performance tuning: proper table for parsetree list;
wenzelm
parents:
80977
diff
changeset
|
531 |
|
5e2b1588c5cb
minor performance tuning: proper table for parsetree list;
wenzelm
parents:
80977
diff
changeset
|
532 |
fun parsetree_ord pts = |
5e2b1588c5cb
minor performance tuning: proper table for parsetree list;
wenzelm
parents:
80977
diff
changeset
|
533 |
if pointer_eq pts then EQUAL |
5e2b1588c5cb
minor performance tuning: proper table for parsetree list;
wenzelm
parents:
80977
diff
changeset
|
534 |
else |
5e2b1588c5cb
minor performance tuning: proper table for parsetree list;
wenzelm
parents:
80977
diff
changeset
|
535 |
(case pts of |
5e2b1588c5cb
minor performance tuning: proper table for parsetree list;
wenzelm
parents:
80977
diff
changeset
|
536 |
(Node (s, ts), Node (s', ts')) => |
5e2b1588c5cb
minor performance tuning: proper table for parsetree list;
wenzelm
parents:
80977
diff
changeset
|
537 |
(case fast_string_ord (s, s') of |
5e2b1588c5cb
minor performance tuning: proper table for parsetree list;
wenzelm
parents:
80977
diff
changeset
|
538 |
EQUAL => list_ord parsetree_ord (ts, ts') |
5e2b1588c5cb
minor performance tuning: proper table for parsetree list;
wenzelm
parents:
80977
diff
changeset
|
539 |
| ord => ord) |
5e2b1588c5cb
minor performance tuning: proper table for parsetree list;
wenzelm
parents:
80977
diff
changeset
|
540 |
| (Tip t, Tip t') => Lexicon.token_ord (t, t') |
5e2b1588c5cb
minor performance tuning: proper table for parsetree list;
wenzelm
parents:
80977
diff
changeset
|
541 |
| _ => int_ord (apply2 cons_nr pts)); |
5e2b1588c5cb
minor performance tuning: proper table for parsetree list;
wenzelm
parents:
80977
diff
changeset
|
542 |
|
5e2b1588c5cb
minor performance tuning: proper table for parsetree list;
wenzelm
parents:
80977
diff
changeset
|
543 |
structure Parsetrees = Table(type key = parsetree list val ord = list_ord parsetree_ord); |
5e2b1588c5cb
minor performance tuning: proper table for parsetree list;
wenzelm
parents:
80977
diff
changeset
|
544 |
|
5e2b1588c5cb
minor performance tuning: proper table for parsetree list;
wenzelm
parents:
80977
diff
changeset
|
545 |
end; |
5e2b1588c5cb
minor performance tuning: proper table for parsetree list;
wenzelm
parents:
80977
diff
changeset
|
546 |
|
80950 | 547 |
fun pretty_parsetree (Node (c, pts)) = |
548 |
[Pretty.enclose "(" ")" |
|
549 |
(Pretty.breaks (Pretty.quote (Pretty.str c) :: maps pretty_parsetree pts))] |
|
550 |
| pretty_parsetree (Tip tok) = |
|
551 |
if Lexicon.valued_token tok then [Pretty.str (Lexicon.str_of_token tok)] else []; |
|
42374
b9a6b465da25
clarified pretty_parsetree: suppress literal tokens;
wenzelm
parents:
42288
diff
changeset
|
552 |
|
b9a6b465da25
clarified pretty_parsetree: suppress literal tokens;
wenzelm
parents:
42288
diff
changeset
|
553 |
|
b9a6b465da25
clarified pretty_parsetree: suppress literal tokens;
wenzelm
parents:
42288
diff
changeset
|
554 |
(* parser state *) |
42205
22f5cc06c419
direct pretty printing of parsetrees -- prevent diagnostic output from crashing due to undeclared entities;
wenzelm
parents:
41378
diff
changeset
|
555 |
|
18 | 556 |
type state = |
80930 | 557 |
(nt * int * (*identification and production precedence*) |
558 |
string * (*name of production*) |
|
559 |
int) * (*index for previous state list*) |
|
80943 | 560 |
symb list * (*input: rest of rhs*) |
561 |
parsetree list; (*output (reversed): already parsed nonterminals on rhs*) |
|
18 | 562 |
|
80976 | 563 |
local |
564 |
||
80978
5e2b1588c5cb
minor performance tuning: proper table for parsetree list;
wenzelm
parents:
80977
diff
changeset
|
565 |
fun update_trees (A, (t, p)) used = |
5e2b1588c5cb
minor performance tuning: proper table for parsetree list;
wenzelm
parents:
80977
diff
changeset
|
566 |
let |
5e2b1588c5cb
minor performance tuning: proper table for parsetree list;
wenzelm
parents:
80977
diff
changeset
|
567 |
val (i: int, ts) = the (Inttab.lookup used A); |
5e2b1588c5cb
minor performance tuning: proper table for parsetree list;
wenzelm
parents:
80977
diff
changeset
|
568 |
fun update ts' = Inttab.update (A, (i, ts')); |
5e2b1588c5cb
minor performance tuning: proper table for parsetree list;
wenzelm
parents:
80977
diff
changeset
|
569 |
in |
5e2b1588c5cb
minor performance tuning: proper table for parsetree list;
wenzelm
parents:
80977
diff
changeset
|
570 |
(case Parsetrees.lookup ts t of |
5e2b1588c5cb
minor performance tuning: proper table for parsetree list;
wenzelm
parents:
80977
diff
changeset
|
571 |
NONE => (no_prec, update (Parsetrees.make [(t, p)]) used) |
5e2b1588c5cb
minor performance tuning: proper table for parsetree list;
wenzelm
parents:
80977
diff
changeset
|
572 |
| SOME q => (q, if p > q then update (Parsetrees.update (t, p) ts) used else used)) |
5e2b1588c5cb
minor performance tuning: proper table for parsetree list;
wenzelm
parents:
80977
diff
changeset
|
573 |
end; |
18 | 574 |
|
80978
5e2b1588c5cb
minor performance tuning: proper table for parsetree list;
wenzelm
parents:
80977
diff
changeset
|
575 |
fun update_prec (A, p) used = |
5e2b1588c5cb
minor performance tuning: proper table for parsetree list;
wenzelm
parents:
80977
diff
changeset
|
576 |
Inttab.map_entry A (fn (_, ts) => (p, ts)) used; |
18 | 577 |
|
80969
667f5072ed2d
pro-forma support for markup blocks, without any change of result yet;
wenzelm
parents:
80968
diff
changeset
|
578 |
fun head_nonterm pred : state -> bool = |
80933 | 579 |
let |
80969
667f5072ed2d
pro-forma support for markup blocks, without any change of result yet;
wenzelm
parents:
80968
diff
changeset
|
580 |
fun check (Nonterminal a :: _) = pred a |
667f5072ed2d
pro-forma support for markup blocks, without any change of result yet;
wenzelm
parents:
80968
diff
changeset
|
581 |
| check (Markup (_, []) :: rest) = check rest |
667f5072ed2d
pro-forma support for markup blocks, without any change of result yet;
wenzelm
parents:
80968
diff
changeset
|
582 |
| check (Markup (_, body) :: _) = check body |
667f5072ed2d
pro-forma support for markup blocks, without any change of result yet;
wenzelm
parents:
80968
diff
changeset
|
583 |
| check _ = false; |
667f5072ed2d
pro-forma support for markup blocks, without any change of result yet;
wenzelm
parents:
80968
diff
changeset
|
584 |
in check o #2 end; |
667f5072ed2d
pro-forma support for markup blocks, without any change of result yet;
wenzelm
parents:
80968
diff
changeset
|
585 |
|
80978
5e2b1588c5cb
minor performance tuning: proper table for parsetree list;
wenzelm
parents:
80977
diff
changeset
|
586 |
fun get_states_lambda A min max = |
5e2b1588c5cb
minor performance tuning: proper table for parsetree list;
wenzelm
parents:
80977
diff
changeset
|
587 |
filter (head_nonterm (fn (B, p) => A = B andalso upto_prec min max p)); |
18 | 588 |
|
80944 | 589 |
fun get_states A max_prec = |
80969
667f5072ed2d
pro-forma support for markup blocks, without any change of result yet;
wenzelm
parents:
80968
diff
changeset
|
590 |
filter (head_nonterm (fn (B, p) => A = B andalso p <= max_prec)); |
18 | 591 |
|
80943 | 592 |
fun movedot_nonterm tt (info, Nonterminal _ :: sa, ts) : state = (info, sa, tt @ ts); |
18 | 593 |
|
80947 | 594 |
fun process_states (Gram {prods = gram_prods, chains = gram_chains, ...}) stateset i c states = |
37683 | 595 |
let |
80947 | 596 |
(*get all productions of a NT and NTs chained to it which can |
597 |
be started by specified token*) |
|
80974
d76230f575f2
minor performance tuning (NB: order of prods / states does not matter);
wenzelm
parents:
80973
diff
changeset
|
598 |
fun add_prods i tok nt ok = |
80947 | 599 |
let |
80978
5e2b1588c5cb
minor performance tuning: proper table for parsetree list;
wenzelm
parents:
80977
diff
changeset
|
600 |
fun add (rhs, id, prod_prec) = |
5e2b1588c5cb
minor performance tuning: proper table for parsetree list;
wenzelm
parents:
80977
diff
changeset
|
601 |
if ok prod_prec then cons ((nt, prod_prec, id, i), rhs, []) else I; |
80947 | 602 |
fun token_prods prods = |
80972 | 603 |
fold add (prods_lookup prods tok) #> |
604 |
fold add (prods_lookup prods Lexicon.dummy); |
|
80947 | 605 |
val nt_prods = #2 o Vector.nth gram_prods; |
80974
d76230f575f2
minor performance tuning (NB: order of prods / states does not matter);
wenzelm
parents:
80973
diff
changeset
|
606 |
in fold (token_prods o nt_prods) (chains_all_preds gram_chains [nt]) end; |
80947 | 607 |
|
80933 | 608 |
fun process _ [] (Si, Sii) = (Si, Sii) |
80946 | 609 |
| process used ((S as (info, symbs, ts)) :: States) (Si, Sii) = |
610 |
(case symbs of |
|
80969
667f5072ed2d
pro-forma support for markup blocks, without any change of result yet;
wenzelm
parents:
80968
diff
changeset
|
611 |
Markup (_, body) :: sa => process used ((info, body @ sa, ts) :: States) (Si, Sii) |
667f5072ed2d
pro-forma support for markup blocks, without any change of result yet;
wenzelm
parents:
80968
diff
changeset
|
612 |
| Nonterminal (nt, min_prec) :: sa => |
38712
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
613 |
let (*predictor operation*) |
80973 | 614 |
fun movedot_lambda (t, k) = min_prec <= k ? cons (info, sa, t @ ts) |
615 |
val (used', States') = |
|
42226
cb650789f2f0
use standard tables with standard argument order;
wenzelm
parents:
42222
diff
changeset
|
616 |
(case Inttab.lookup used nt of |
38713 | 617 |
SOME (used_prec, l) => (*nonterminal has been processed*) |
618 |
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
|
619 |
(*wanted precedence has been processed*) |
80978
5e2b1588c5cb
minor performance tuning: proper table for parsetree list;
wenzelm
parents:
80977
diff
changeset
|
620 |
(used, Parsetrees.fold movedot_lambda l States) |
38712
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
621 |
else (*wanted precedence hasn't been parsed yet*) |
37683 | 622 |
let |
80974
d76230f575f2
minor performance tuning (NB: order of prods / states does not matter);
wenzelm
parents:
80973
diff
changeset
|
623 |
val States' = States |> add_prods i c nt (until_prec min_prec used_prec) |
80978
5e2b1588c5cb
minor performance tuning: proper table for parsetree list;
wenzelm
parents:
80977
diff
changeset
|
624 |
|> Parsetrees.fold movedot_lambda l; |
80973 | 625 |
in (update_prec (nt, min_prec) used, States') end |
38712
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents:
38711
diff
changeset
|
626 |
| NONE => (*nonterminal is parsed for the first time*) |
80974
d76230f575f2
minor performance tuning (NB: order of prods / states does not matter);
wenzelm
parents:
80973
diff
changeset
|
627 |
let val States' = States |> add_prods i c nt (until_prec min_prec no_prec); |
80978
5e2b1588c5cb
minor performance tuning: proper table for parsetree list;
wenzelm
parents:
80977
diff
changeset
|
628 |
in (Inttab.update (nt, (min_prec, Parsetrees.empty)) used, States') end); |
80973 | 629 |
in process used' States' (S :: Si, Sii) end |
80946 | 630 |
| Terminal a :: sa => (*scanner operation*) |
80929 | 631 |
let |
80946 | 632 |
val (_, _, id, _) = info; |
80929 | 633 |
val Sii' = |
80979 | 634 |
if Lexicon.token_type_ord (a, c) <> EQUAL then Sii |
80929 | 635 |
else (*move dot*) |
636 |
let val ts' = if Lexicon.valued_token c orelse id <> "" then Tip c :: ts else ts |
|
80943 | 637 |
in (info, sa, ts') :: Sii end; |
80933 | 638 |
in process used States (S :: Si, Sii') end |
80946 | 639 |
| [] => (*completer operation*) |
80928 | 640 |
let |
80978
5e2b1588c5cb
minor performance tuning: proper table for parsetree list;
wenzelm
parents:
80977
diff
changeset
|
641 |
val (A, p, id, j) = info; |
80928 | 642 |
val tt = if id = "" then ts else [Node (id, rev ts)]; |
643 |
val (used', Slist) = |
|
644 |
if j = i then (*lambda production?*) |
|
80978
5e2b1588c5cb
minor performance tuning: proper table for parsetree list;
wenzelm
parents:
80977
diff
changeset
|
645 |
let val (q, used') = update_trees (A, (tt, p)) used |
5e2b1588c5cb
minor performance tuning: proper table for parsetree list;
wenzelm
parents:
80977
diff
changeset
|
646 |
in (used', get_states_lambda A q p Si) end |
5e2b1588c5cb
minor performance tuning: proper table for parsetree list;
wenzelm
parents:
80977
diff
changeset
|
647 |
else (used, get_states A p (Array.nth stateset j)); |
80928 | 648 |
val States' = map (movedot_nonterm tt) Slist; |
80933 | 649 |
in process used' (States' @ States) (S :: Si, Sii) end) |
80961 | 650 |
|
651 |
val (Si, Sii) = process Inttab.empty states ([], []); |
|
652 |
in |
|
653 |
Array.upd stateset i Si; |
|
654 |
Array.upd stateset (i + 1) Sii |
|
655 |
end; |
|
18 | 656 |
|
80964 | 657 |
fun produce gram stateset i prev rest = |
77888 | 658 |
(case Array.nth stateset i of |
25986
26f1e4c172c3
syntax error: reduced spam -- print expected nonterminals instead of terminals;
wenzelm
parents:
24245
diff
changeset
|
659 |
[] => |
26f1e4c172c3
syntax error: reduced spam -- print expected nonterminals instead of terminals;
wenzelm
parents:
24245
diff
changeset
|
660 |
let |
80964 | 661 |
val inp = if Lexicon.is_dummy prev then rest else prev :: rest; |
662 |
val pos = Position.here (Lexicon.pos_of_token prev); |
|
27801
0d0adaf0228d
datatype token: maintain range, tuned representation;
wenzelm
parents:
26678
diff
changeset
|
663 |
in |
80964 | 664 |
if null inp then |
55624 | 665 |
error ("Inner syntax error: unexpected end of input" ^ pos) |
666 |
else |
|
667 |
error ("Inner syntax error" ^ pos ^ |
|
668 |
Markup.markup Markup.no_report |
|
669 |
("\n" ^ Pretty.string_of |
|
670 |
(Pretty.block [ |
|
671 |
Pretty.str "at", Pretty.brk 1, |
|
672 |
Pretty.block |
|
673 |
(Pretty.str "\"" :: |
|
80964 | 674 |
Pretty.breaks (map (Pretty.str o Lexicon.str_of_token) (#1 (split_last inp))) @ |
55624 | 675 |
[Pretty.str "\""])]))) |
27801
0d0adaf0228d
datatype token: maintain range, tuned representation;
wenzelm
parents:
26678
diff
changeset
|
676 |
end |
80931 | 677 |
| states => |
80964 | 678 |
(case rest of |
80931 | 679 |
[] => states |
80964 | 680 |
| c :: cs => (process_states gram stateset i c states; produce gram stateset (i + 1) c cs))); |
18 | 681 |
|
80933 | 682 |
in |
18 | 683 |
|
80933 | 684 |
fun parse (gram as Gram {tags, ...}) start toks = |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
46
diff
changeset
|
685 |
let |
37683 | 686 |
val start_tag = |
80933 | 687 |
(case tags_lookup tags start of |
37683 | 688 |
SOME tag => tag |
80933 | 689 |
| NONE => error ("Inner syntax: bad grammar root symbol " ^ quote start)); |
18 | 690 |
|
27801
0d0adaf0228d
datatype token: maintain range, tuned representation;
wenzelm
parents:
26678
diff
changeset
|
691 |
val end_pos = |
0d0adaf0228d
datatype token: maintain range, tuned representation;
wenzelm
parents:
26678
diff
changeset
|
692 |
(case try List.last toks of |
0d0adaf0228d
datatype token: maintain range, tuned representation;
wenzelm
parents:
26678
diff
changeset
|
693 |
NONE => Position.none |
67551 | 694 |
| SOME tok => Lexicon.end_pos_of_token tok); |
80933 | 695 |
val input = toks @ [Lexicon.mk_eof end_pos]; |
696 |
||
80943 | 697 |
val S0: state = ((~1, 0, "", 0), [Nonterminal (start_tag, 0), Terminal Lexicon.eof], []); |
80933 | 698 |
val stateset = Array.array (length input + 1, []); |
699 |
val _ = Array.upd stateset 0 [S0]; |
|
700 |
||
701 |
val pts = |
|
80964 | 702 |
produce gram stateset 0 Lexicon.dummy input |
80943 | 703 |
|> map_filter (fn (_, _, [pt]) => SOME pt | _ => NONE); |
80933 | 704 |
in if null pts then raise Fail "Inner syntax: no parse trees" else pts end; |
26678 | 705 |
|
18 | 706 |
end; |
80933 | 707 |
|
708 |
end; |