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