| author | wenzelm | 
| Mon, 08 May 2023 23:00:17 +0200 | |
| changeset 77997 | 4660181c83c9 | 
| parent 77896 | a9626bcb0c3b | 
| child 78005 | 006cb47a2700 | 
| 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  | 
| 77997 | 10  | 
val timing: bool Unsynchronized.ref  | 
| 1507 | 11  | 
type gram  | 
12  | 
val empty_gram: gram  | 
|
| 
42288
 
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
 
wenzelm 
parents: 
42282 
diff
changeset
 | 
13  | 
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
 | 
14  | 
val make_gram: Syntax_Ext.xprod list -> gram  | 
| 1507 | 15  | 
val pretty_gram: gram -> Pretty.T list  | 
16  | 
datatype parsetree =  | 
|
17  | 
Node of string * parsetree list |  | 
|
18  | 
Tip of Lexicon.token  | 
|
| 
42374
 
b9a6b465da25
clarified pretty_parsetree: suppress literal tokens;
 
wenzelm 
parents: 
42288 
diff
changeset
 | 
19  | 
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
 | 
20  | 
val pretty_parsetree: parsetree -> Pretty.T  | 
| 45641 | 21  | 
val parse: gram -> string -> Lexicon.token list -> parsetree list  | 
| 41378 | 22  | 
val branching_level: int Config.T  | 
| 15752 | 23  | 
end;  | 
| 1507 | 24  | 
|
| 15752 | 25  | 
structure Parser: PARSER =  | 
| 18 | 26  | 
struct  | 
| 15752 | 27  | 
|
| 18 | 28  | 
(** datatype gram **)  | 
29  | 
||
| 67541 | 30  | 
(* nonterminals *)  | 
31  | 
||
| 67545 | 32  | 
(*production for the NTs are stored in a vector, indexed by the NT tag*)  | 
| 67541 | 33  | 
type nt = int;  | 
34  | 
||
| 67545 | 35  | 
type tags = nt Symtab.table;  | 
36  | 
val tags_empty: tags = Symtab.empty;  | 
|
37  | 
fun tags_content (tags: tags) = sort_by #1 (Symtab.dest tags);  | 
|
38  | 
fun tags_lookup (tags: tags) = Symtab.lookup tags;  | 
|
39  | 
fun tags_insert tag (tags: tags) = Symtab.update_new tag tags;  | 
|
40  | 
fun tags_name (tags: tags) = the o Inttab.lookup (Inttab.make (map swap (Symtab.dest tags)));  | 
|
41  | 
||
| 
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  | 
|
| 77997 | 419  | 
val timing = Unsynchronized.ref true;  | 
420  | 
||
| 
38712
 
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
 
wenzelm 
parents: 
38711 
diff
changeset
 | 
421  | 
val empty_gram =  | 
| 
 
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
 
wenzelm 
parents: 
38711 
diff
changeset
 | 
422  | 
Gram  | 
| 
 
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
 
wenzelm 
parents: 
38711 
diff
changeset
 | 
423  | 
   {nt_count = 0,
 | 
| 
 
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
 
wenzelm 
parents: 
38711 
diff
changeset
 | 
424  | 
prod_count = 0,  | 
| 67531 | 425  | 
tags = tags_empty,  | 
426  | 
chains = chains_empty,  | 
|
| 
67533
 
f253e5eaf995
clarified types and operations: potentially more efficient add_prods;
 
wenzelm 
parents: 
67532 
diff
changeset
 | 
427  | 
lambdas = nts_empty,  | 
| 
 
f253e5eaf995
clarified types and operations: potentially more efficient add_prods;
 
wenzelm 
parents: 
67532 
diff
changeset
 | 
428  | 
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
 | 
429  | 
|
| 1438 | 430  | 
|
431  | 
(*Add productions to a grammar*)  | 
|
| 77997 | 432  | 
fun extend_gram0 [] gram = gram  | 
433  | 
  | extend_gram0 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
 | 
434  | 
let  | 
| 
 
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
 
wenzelm 
parents: 
38711 
diff
changeset
 | 
435  | 
(*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
 | 
436  | 
fun get_tag nt_count tags nt =  | 
| 67531 | 437  | 
(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
 | 
438  | 
SOME tag => (nt_count, tags, tag)  | 
| 67531 | 439  | 
| NONE => (nt_count + 1, tags_insert (nt, nt_count) tags, nt_count));  | 
| 1438 | 440  | 
|
| 
38712
 
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
 
wenzelm 
parents: 
38711 
diff
changeset
 | 
441  | 
(*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
 | 
442  | 
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
 | 
443  | 
nonterminals are converted to integer tags*)  | 
| 
 
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
 
wenzelm 
parents: 
38711 
diff
changeset
 | 
444  | 
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
 | 
445  | 
| symb_of (Syntax_Ext.Delim s :: ss) nt_count tags result =  | 
| 67552 | 446  | 
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
 | 
447  | 
| 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
 | 
448  | 
let  | 
| 
 
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
 
wenzelm 
parents: 
38711 
diff
changeset
 | 
449  | 
val (nt_count', tags', new_symb) =  | 
| 67556 | 450  | 
(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
 | 
451  | 
NONE =>  | 
| 
 
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
 
wenzelm 
parents: 
38711 
diff
changeset
 | 
452  | 
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
 | 
453  | 
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
 | 
454  | 
| 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
 | 
455  | 
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
 | 
456  | 
| 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
 | 
457  | 
|
| 
38712
 
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
 
wenzelm 
parents: 
38711 
diff
changeset
 | 
458  | 
(*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
 | 
459  | 
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
 | 
460  | 
(nt_count, prod_count, tags, result)  | 
| 
42288
 
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
 
wenzelm 
parents: 
42282 
diff
changeset
 | 
461  | 
| 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
 | 
462  | 
nt_count prod_count tags result =  | 
| 
 
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
 
wenzelm 
parents: 
38711 
diff
changeset
 | 
463  | 
let  | 
| 
 
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
 
wenzelm 
parents: 
38711 
diff
changeset
 | 
464  | 
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
 | 
465  | 
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
 | 
466  | 
in  | 
| 
 
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
 
wenzelm 
parents: 
38711 
diff
changeset
 | 
467  | 
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
 | 
468  | 
((lhs_tag, (prods, const, pri)) :: result)  | 
| 
 
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
 
wenzelm 
parents: 
38711 
diff
changeset
 | 
469  | 
end;  | 
| 
1147
 
57b5f55bf879
removed 'raw' productions from gram datatype; replaced mk_gram by add_prods;
 
clasohm 
parents: 
890 
diff
changeset
 | 
470  | 
|
| 
38712
 
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
 
wenzelm 
parents: 
38711 
diff
changeset
 | 
471  | 
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
 | 
472  | 
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
 | 
473  | 
|
| 
38712
 
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
 
wenzelm 
parents: 
38711 
diff
changeset
 | 
474  | 
(*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
 | 
475  | 
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
 | 
476  | 
to change the array's content*)  | 
| 
 
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
 
wenzelm 
parents: 
38711 
diff
changeset
 | 
477  | 
val prods' =  | 
| 
 
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
 
wenzelm 
parents: 
38711 
diff
changeset
 | 
478  | 
let  | 
| 
 
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
 
wenzelm 
parents: 
38711 
diff
changeset
 | 
479  | 
fun get_prod i =  | 
| 
77846
 
5ba68d3bd741
more operations, following Isabelle/ML conventions;
 
wenzelm 
parents: 
77823 
diff
changeset
 | 
480  | 
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
 | 
481  | 
else nt_gram_empty;  | 
| 
38712
 
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
 
wenzelm 
parents: 
38711 
diff
changeset
 | 
482  | 
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
 | 
483  | 
|
| 67543 | 484  | 
val (chains', lambdas') =  | 
485  | 
(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
 | 
486  | 
in  | 
| 
 
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
 
wenzelm 
parents: 
38711 
diff
changeset
 | 
487  | 
Gram  | 
| 
 
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
 
wenzelm 
parents: 
38711 
diff
changeset
 | 
488  | 
         {nt_count = nt_count',
 | 
| 
 
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
 
wenzelm 
parents: 
38711 
diff
changeset
 | 
489  | 
prod_count = prod_count',  | 
| 
 
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
 
wenzelm 
parents: 
38711 
diff
changeset
 | 
490  | 
tags = tags',  | 
| 
 
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
 
wenzelm 
parents: 
38711 
diff
changeset
 | 
491  | 
chains = chains',  | 
| 
 
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
 
wenzelm 
parents: 
38711 
diff
changeset
 | 
492  | 
lambdas = lambdas',  | 
| 
 
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
 
wenzelm 
parents: 
38711 
diff
changeset
 | 
493  | 
prods = Array.vector prods'}  | 
| 
 
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
 
wenzelm 
parents: 
38711 
diff
changeset
 | 
494  | 
end;  | 
| 18 | 495  | 
|
| 77997 | 496  | 
fun extend_gram xprods gram =  | 
497  | 
if ! timing then  | 
|
498  | 
    Timing.cond_timeit true ("Parser.extend_gram" ^ Position.here (Position.thread_data ()))
 | 
|
499  | 
(fn () => extend_gram0 xprods gram)  | 
|
500  | 
else extend_gram0 xprods gram;  | 
|
501  | 
||
| 
45632
 
b23c42b9f78a
prefer Parser.make_gram over Parser.merge_gram, to approximate n-ary merges on theory import;
 
wenzelm 
parents: 
44102 
diff
changeset
 | 
502  | 
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
 | 
503  | 
|
| 18 | 504  | 
|
| 42217 | 505  | 
|
| 
42374
 
b9a6b465da25
clarified pretty_parsetree: suppress literal tokens;
 
wenzelm 
parents: 
42288 
diff
changeset
 | 
506  | 
(** parser **)  | 
| 
 
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  | 
(* parsetree *)  | 
| 18 | 509  | 
|
| 
237
 
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
 
wenzelm 
parents: 
46 
diff
changeset
 | 
510  | 
datatype parsetree =  | 
| 
 
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
 
wenzelm 
parents: 
46 
diff
changeset
 | 
511  | 
Node of string * parsetree list |  | 
| 37683 | 512  | 
Tip of Lexicon.token;  | 
| 
237
 
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
 
wenzelm 
parents: 
46 
diff
changeset
 | 
513  | 
|
| 
42374
 
b9a6b465da25
clarified pretty_parsetree: suppress literal tokens;
 
wenzelm 
parents: 
42288 
diff
changeset
 | 
514  | 
exception PARSETREE of parsetree;  | 
| 
 
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  | 
fun pretty_parsetree parsetree =  | 
| 
 
b9a6b465da25
clarified pretty_parsetree: suppress literal tokens;
 
wenzelm 
parents: 
42288 
diff
changeset
 | 
517  | 
let  | 
| 
 
b9a6b465da25
clarified pretty_parsetree: suppress literal tokens;
 
wenzelm 
parents: 
42288 
diff
changeset
 | 
518  | 
fun pretty (Node (c, pts)) =  | 
| 
 
b9a6b465da25
clarified pretty_parsetree: suppress literal tokens;
 
wenzelm 
parents: 
42288 
diff
changeset
 | 
519  | 
          [Pretty.enclose "(" ")" (Pretty.breaks (Pretty.quote (Pretty.str c) :: maps pretty pts))]
 | 
| 
 
b9a6b465da25
clarified pretty_parsetree: suppress literal tokens;
 
wenzelm 
parents: 
42288 
diff
changeset
 | 
520  | 
| pretty (Tip tok) =  | 
| 
 
b9a6b465da25
clarified pretty_parsetree: suppress literal tokens;
 
wenzelm 
parents: 
42288 
diff
changeset
 | 
521  | 
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
 | 
522  | 
in (case pretty parsetree of [prt] => prt | _ => raise PARSETREE parsetree) end;  | 
| 
 
b9a6b465da25
clarified pretty_parsetree: suppress literal tokens;
 
wenzelm 
parents: 
42288 
diff
changeset
 | 
523  | 
|
| 
 
b9a6b465da25
clarified pretty_parsetree: suppress literal tokens;
 
wenzelm 
parents: 
42288 
diff
changeset
 | 
524  | 
|
| 
 
b9a6b465da25
clarified pretty_parsetree: suppress literal tokens;
 
wenzelm 
parents: 
42288 
diff
changeset
 | 
525  | 
(* parser state *)  | 
| 
42205
 
22f5cc06c419
direct pretty printing of parsetrees -- prevent diagnostic output from crashing due to undeclared entities;
 
wenzelm 
parents: 
41378 
diff
changeset
 | 
526  | 
|
| 18 | 527  | 
type state =  | 
| 
67533
 
f253e5eaf995
clarified types and operations: potentially more efficient add_prods;
 
wenzelm 
parents: 
67532 
diff
changeset
 | 
528  | 
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
 | 
529  | 
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
 | 
530  | 
symb list * (*rest of rhs*)  | 
| 
 
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
 
wenzelm 
parents: 
38711 
diff
changeset
 | 
531  | 
string * (*name of production*)  | 
| 
 
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
 
wenzelm 
parents: 
38711 
diff
changeset
 | 
532  | 
int; (*index for previous state list*)  | 
| 18 | 533  | 
|
534  | 
||
| 38713 | 535  | 
(*Get all rhss with precedence >= min_prec*)  | 
| 42217 | 536  | 
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
 | 
537  | 
|
| 38713 | 538  | 
(*Get all rhss with precedence >= min_prec and < max_prec*)  | 
539  | 
fun get_RHS' min_prec max_prec =  | 
|
| 42217 | 540  | 
filter (fn (_, _, prec: int) => prec >= min_prec andalso prec < max_prec);  | 
| 18 | 541  | 
|
| 
330
 
2fda15dd1e0f
changed the way a grammar is generated to allow the new parser to work;
 
clasohm 
parents: 
258 
diff
changeset
 | 
542  | 
(*Make states using a list of rhss*)  | 
| 62669 | 543  | 
fun mk_states i lhs_ID rhss =  | 
| 38713 | 544  | 
let fun mk_state (rhs, id, prod_prec) = (lhs_ID, prod_prec, [], rhs, id, i);  | 
545  | 
in map mk_state rhss end;  | 
|
| 
697
 
40f72ab196f8
changed warning for extremely ambiguous expressions
 
clasohm 
parents: 
682 
diff
changeset
 | 
546  | 
|
| 15752 | 547  | 
(*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
 | 
548  | 
saving the maximum precedence*)  | 
| 42217 | 549  | 
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
 | 
550  | 
| 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
 | 
551  | 
if t = t' then  | 
| 
38712
 
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
 
wenzelm 
parents: 
38711 
diff
changeset
 | 
552  | 
(SOME prec',  | 
| 
 
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
 
wenzelm 
parents: 
38711 
diff
changeset
 | 
553  | 
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
 | 
554  | 
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
 | 
555  | 
else  | 
| 
 
2fda15dd1e0f
changed the way a grammar is generated to allow the new parser to work;
 
clasohm 
parents: 
258 
diff
changeset
 | 
556  | 
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
 | 
557  | 
in (n, (t', prec') :: ts') end;  | 
| 18 | 558  | 
|
| 
330
 
2fda15dd1e0f
changed the way a grammar is generated to allow the new parser to work;
 
clasohm 
parents: 
258 
diff
changeset
 | 
559  | 
(*Update entry in used*)  | 
| 
42226
 
cb650789f2f0
use standard tables with standard argument order;
 
wenzelm 
parents: 
42222 
diff
changeset
 | 
560  | 
fun update_trees (A, t) used =  | 
| 
 
cb650789f2f0
use standard tables with standard argument order;
 
wenzelm 
parents: 
42222 
diff
changeset
 | 
561  | 
let  | 
| 
 
cb650789f2f0
use standard tables with standard argument order;
 
wenzelm 
parents: 
42222 
diff
changeset
 | 
562  | 
val (i, ts) = the (Inttab.lookup used A);  | 
| 
 
cb650789f2f0
use standard tables with standard argument order;
 
wenzelm 
parents: 
42222 
diff
changeset
 | 
563  | 
val (n, ts') = conc t ts;  | 
| 
 
cb650789f2f0
use standard tables with standard argument order;
 
wenzelm 
parents: 
42222 
diff
changeset
 | 
564  | 
in (n, Inttab.update (A, (i, ts')) used) end;  | 
| 18 | 565  | 
|
| 
330
 
2fda15dd1e0f
changed the way a grammar is generated to allow the new parser to work;
 
clasohm 
parents: 
258 
diff
changeset
 | 
566  | 
(*Replace entry in used*)  | 
| 
42226
 
cb650789f2f0
use standard tables with standard argument order;
 
wenzelm 
parents: 
42222 
diff
changeset
 | 
567  | 
fun update_prec (A, prec) =  | 
| 
 
cb650789f2f0
use standard tables with standard argument order;
 
wenzelm 
parents: 
42222 
diff
changeset
 | 
568  | 
Inttab.map_entry A (fn (_, ts) => (prec, ts));  | 
| 18 | 569  | 
|
| 42220 | 570  | 
fun getS A max_prec NONE Si =  | 
571  | 
filter  | 
|
572  | 
(fn (_, _, _, Nonterminal (B, prec) :: _, _, _) => A = B andalso prec <= max_prec  | 
|
573  | 
| _ => false) Si  | 
|
574  | 
| getS A max_prec (SOME min_prec) Si =  | 
|
575  | 
filter  | 
|
576  | 
(fn (_, _, _, Nonterminal (B, prec) :: _, _, _) =>  | 
|
577  | 
A = B andalso prec > min_prec andalso prec <= max_prec  | 
|
578  | 
| _ => false) Si;  | 
|
| 18 | 579  | 
|
| 62669 | 580  | 
fun get_states Estate j A max_prec =  | 
| 33317 | 581  | 
filter  | 
| 38713 | 582  | 
(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
 | 
583  | 
| _ => false)  | 
| 77888 | 584  | 
(Array.nth Estate j);  | 
| 18 | 585  | 
|
586  | 
||
| 42219 | 587  | 
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
 | 
588  | 
if Lexicon.valued_token c orelse id <> ""  | 
| 
 
4fa41e068ff0
report literal tokens according to parsetree head;
 
wenzelm 
parents: 
42226 
diff
changeset
 | 
589  | 
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
 | 
590  | 
else (A, j, ts, sa, id, i);  | 
| 18 | 591  | 
|
| 42219 | 592  | 
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
 | 
593  | 
(A, j, tt @ ts, sa, id, i);  | 
| 18 | 594  | 
|
| 42219 | 595  | 
fun movedot_lambda [] _ = []  | 
| 42221 | 596  | 
| 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
 | 
597  | 
if k <= ki then (B, j, t @ tss, sa, id, i) :: movedot_lambda ts state  | 
| 42221 | 598  | 
else movedot_lambda ts state;  | 
| 18 | 599  | 
|
600  | 
||
| 41378 | 601  | 
(*trigger value for warnings*)  | 
| 69575 | 602  | 
val branching_level = Config.declare_int ("syntax_branching_level", \<^here>) (K 600);
 | 
| 18 | 603  | 
|
| 
1147
 
57b5f55bf879
removed 'raw' productions from gram datatype; replaced mk_gram by add_prods;
 
clasohm 
parents: 
890 
diff
changeset
 | 
604  | 
(*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
 | 
605  | 
be started by specified token*)  | 
| 67540 | 606  | 
fun prods_for (Gram {prods, chains, ...}) tk nt =
 | 
| 37683 | 607  | 
let  | 
| 67540 | 608  | 
fun token_prods prods =  | 
609  | 
fold cons (prods_lookup prods tk) #>  | 
|
| 67550 | 610  | 
fold cons (prods_lookup prods Lexicon.dummy);  | 
| 
77846
 
5ba68d3bd741
more operations, following Isabelle/ML conventions;
 
wenzelm 
parents: 
77823 
diff
changeset
 | 
611  | 
fun nt_prods nt = #2 (Vector.nth prods nt);  | 
| 67540 | 612  | 
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
 | 
613  | 
|
| 
 
57b5f55bf879
removed 'raw' productions from gram datatype; replaced mk_gram by add_prods;
 
clasohm 
parents: 
890 
diff
changeset
 | 
614  | 
|
| 67515 | 615  | 
fun PROCESSS gram Estate i c states =  | 
| 37683 | 616  | 
let  | 
| 62669 | 617  | 
fun processS _ [] (Si, Sii) = (Si, Sii)  | 
| 37683 | 618  | 
| processS used (S :: States) (Si, Sii) =  | 
619  | 
(case S of  | 
|
| 38713 | 620  | 
(_, _, _, Nonterminal (nt, min_prec) :: _, _, _) =>  | 
| 
38712
 
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
 
wenzelm 
parents: 
38711 
diff
changeset
 | 
621  | 
let (*predictor operation*)  | 
| 37683 | 622  | 
val (used', new_states) =  | 
| 
42226
 
cb650789f2f0
use standard tables with standard argument order;
 
wenzelm 
parents: 
42222 
diff
changeset
 | 
623  | 
(case Inttab.lookup used nt of  | 
| 38713 | 624  | 
SOME (used_prec, l) => (*nonterminal has been processed*)  | 
625  | 
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
 | 
626  | 
(*wanted precedence has been processed*)  | 
| 42219 | 627  | 
(used, movedot_lambda l S)  | 
| 
38712
 
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
 
wenzelm 
parents: 
38711 
diff
changeset
 | 
628  | 
else (*wanted precedence hasn't been parsed yet*)  | 
| 37683 | 629  | 
let  | 
| 67540 | 630  | 
val tk_prods = prods_for gram c nt;  | 
| 42217 | 631  | 
val States' =  | 
| 62669 | 632  | 
mk_states i nt (get_RHS' min_prec used_prec tk_prods);  | 
| 42219 | 633  | 
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
 | 
634  | 
| 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
 | 
635  | 
let  | 
| 67540 | 636  | 
val tk_prods = prods_for gram c nt;  | 
| 62669 | 637  | 
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
 | 
638  | 
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
 | 
639  | 
in  | 
| 37683 | 640  | 
processS used' (new_states @ States) (S :: Si, Sii)  | 
| 15752 | 641  | 
end  | 
| 
38712
 
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
 
wenzelm 
parents: 
38711 
diff
changeset
 | 
642  | 
| (_, _, _, Terminal a :: _, _, _) => (*scanner operation*)  | 
| 37683 | 643  | 
processS used States  | 
| 67549 | 644  | 
(S :: Si,  | 
645  | 
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
 | 
646  | 
| (A, prec, ts, [], id, j) => (*completer operation*)  | 
| 
42222
 
ff50c436b199
accumulate parsetrees in canonical reverse order;
 
wenzelm 
parents: 
42221 
diff
changeset
 | 
647  | 
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
 | 
648  | 
if j = i then (*lambda production?*)  | 
| 37683 | 649  | 
let  | 
| 
42226
 
cb650789f2f0
use standard tables with standard argument order;
 
wenzelm 
parents: 
42222 
diff
changeset
 | 
650  | 
val (prec', used') = update_trees (A, (tt, prec)) used;  | 
| 42220 | 651  | 
val Slist = getS A prec prec' Si;  | 
652  | 
val States' = map (movedot_nonterm tt) Slist;  | 
|
653  | 
in processS used' (States' @ States) (S :: Si, Sii) end  | 
|
| 37683 | 654  | 
else  | 
| 62669 | 655  | 
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
 | 
656  | 
in processS used (map (movedot_nonterm tt) Slist @ States) (S :: Si, Sii) end  | 
| 37683 | 657  | 
end)  | 
| 
42226
 
cb650789f2f0
use standard tables with standard argument order;
 
wenzelm 
parents: 
42222 
diff
changeset
 | 
658  | 
in processS Inttab.empty states ([], []) end;  | 
| 18 | 659  | 
|
660  | 
||
| 67515 | 661  | 
fun produce gram stateset i indata prev_token =  | 
| 77888 | 662  | 
(case Array.nth stateset i of  | 
| 
25986
 
26f1e4c172c3
syntax error: reduced spam -- print expected nonterminals instead of terminals;
 
wenzelm 
parents: 
24245 
diff
changeset
 | 
663  | 
[] =>  | 
| 
 
26f1e4c172c3
syntax error: reduced spam -- print expected nonterminals instead of terminals;
 
wenzelm 
parents: 
24245 
diff
changeset
 | 
664  | 
let  | 
| 37683 | 665  | 
val toks = if Lexicon.is_eof prev_token then indata else prev_token :: indata;  | 
| 48992 | 666  | 
val pos = Position.here (Lexicon.pos_of_token prev_token);  | 
| 
27801
 
0d0adaf0228d
datatype token: maintain range, tuned representation;
 
wenzelm 
parents: 
26678 
diff
changeset
 | 
667  | 
in  | 
| 55624 | 668  | 
if null toks then  | 
669  | 
          error ("Inner syntax error: unexpected end of input" ^ pos)
 | 
|
670  | 
else  | 
|
671  | 
          error ("Inner syntax error" ^ pos ^
 | 
|
672  | 
Markup.markup Markup.no_report  | 
|
673  | 
              ("\n" ^ Pretty.string_of
 | 
|
674  | 
(Pretty.block [  | 
|
675  | 
Pretty.str "at", Pretty.brk 1,  | 
|
676  | 
Pretty.block  | 
|
677  | 
(Pretty.str "\"" ::  | 
|
678  | 
Pretty.breaks (map (Pretty.str o Lexicon.str_of_token) (#1 (split_last toks))) @  | 
|
679  | 
[Pretty.str "\""])])))  | 
|
| 
27801
 
0d0adaf0228d
datatype token: maintain range, tuned representation;
 
wenzelm 
parents: 
26678 
diff
changeset
 | 
680  | 
end  | 
| 
237
 
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
 
wenzelm 
parents: 
46 
diff
changeset
 | 
681  | 
| s =>  | 
| 
38712
 
f7688fd819a8
some attempts to recover Isabelle/ML style from the depths of time;
 
wenzelm 
parents: 
38711 
diff
changeset
 | 
682  | 
(case indata of  | 
| 
42218
 
8e0a4d500e5b
streamlined token list operations, assuming that the order of union does not matter;
 
wenzelm 
parents: 
42217 
diff
changeset
 | 
683  | 
[] => s  | 
| 42217 | 684  | 
| c :: cs =>  | 
685  | 
let  | 
|
| 67515 | 686  | 
val (si, sii) = PROCESSS gram stateset i c s;  | 
| 77896 | 687  | 
val _ = Array.upd stateset i si;  | 
688  | 
val _ = Array.upd stateset (i + 1) sii;  | 
|
| 67515 | 689  | 
in produce gram stateset (i + 1) cs c end));  | 
| 18 | 690  | 
|
691  | 
||
| 42217 | 692  | 
fun get_trees states = map_filter (fn (_, _, [pt], _, _, _) => SOME pt | _ => NONE) states;  | 
| 18 | 693  | 
|
| 67515 | 694  | 
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
 | 
695  | 
let  | 
| 37683 | 696  | 
val start_tag =  | 
| 67531 | 697  | 
(case tags_lookup tags startsymbol of  | 
| 37683 | 698  | 
SOME tag => tag  | 
| 40959 | 699  | 
      | NONE => error ("Inner syntax: bad grammar root symbol " ^ quote startsymbol));
 | 
| 37683 | 700  | 
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
 | 
701  | 
val s = length indata + 1;  | 
| 
237
 
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
 
wenzelm 
parents: 
46 
diff
changeset
 | 
702  | 
val Estate = Array.array (s, []);  | 
| 77896 | 703  | 
val _ = Array.upd Estate 0 S0;  | 
| 
237
 
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
 
wenzelm 
parents: 
46 
diff
changeset
 | 
704  | 
in  | 
| 67515 | 705  | 
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
 | 
706  | 
end;  | 
| 18 | 707  | 
|
708  | 
||
| 67515 | 709  | 
fun parse gram start toks =  | 
| 
27801
 
0d0adaf0228d
datatype token: maintain range, tuned representation;
 
wenzelm 
parents: 
26678 
diff
changeset
 | 
710  | 
let  | 
| 
 
0d0adaf0228d
datatype token: maintain range, tuned representation;
 
wenzelm 
parents: 
26678 
diff
changeset
 | 
711  | 
val end_pos =  | 
| 
 
0d0adaf0228d
datatype token: maintain range, tuned representation;
 
wenzelm 
parents: 
26678 
diff
changeset
 | 
712  | 
(case try List.last toks of  | 
| 
 
0d0adaf0228d
datatype token: maintain range, tuned representation;
 
wenzelm 
parents: 
26678 
diff
changeset
 | 
713  | 
NONE => Position.none  | 
| 67551 | 714  | 
| SOME tok => Lexicon.end_pos_of_token tok);  | 
| 
27801
 
0d0adaf0228d
datatype token: maintain range, tuned representation;
 
wenzelm 
parents: 
26678 
diff
changeset
 | 
715  | 
val r =  | 
| 67515 | 716  | 
(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
 | 
717  | 
[] => raise Fail "Inner syntax: no parse trees"  | 
| 
27801
 
0d0adaf0228d
datatype token: maintain range, tuned representation;
 
wenzelm 
parents: 
26678 
diff
changeset
 | 
718  | 
| pts => pts);  | 
| 
 
0d0adaf0228d
datatype token: maintain range, tuned representation;
 
wenzelm 
parents: 
26678 
diff
changeset
 | 
719  | 
in r end;  | 
| 26678 | 720  | 
|
| 18 | 721  | 
end;  |