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