wenzelm@18
|
1 |
(* Title: Pure/Syntax/parser.ML
|
wenzelm@2186
|
2 |
Author: Carsten Clasohm, Sonia Mahjoub, and Markus Wenzel, TU Muenchen
|
wenzelm@18
|
3 |
|
wenzelm@15979
|
4 |
General context-free parser for the inner syntax of terms, types, etc.
|
wenzelm@18
|
5 |
*)
|
wenzelm@18
|
6 |
|
wenzelm@18
|
7 |
signature PARSER =
|
wenzelm@15752
|
8 |
sig
|
paulson@1507
|
9 |
type gram
|
paulson@1507
|
10 |
val empty_gram: gram
|
wenzelm@43160
|
11 |
val extend_gram: Syntax_Ext.xprod list -> gram -> gram
|
wenzelm@37700
|
12 |
val merge_gram: gram * gram -> gram
|
paulson@1507
|
13 |
val pretty_gram: gram -> Pretty.T list
|
paulson@1507
|
14 |
datatype parsetree =
|
paulson@1507
|
15 |
Node of string * parsetree list |
|
paulson@1507
|
16 |
Tip of Lexicon.token
|
wenzelm@43245
|
17 |
exception PARSETREE of parsetree
|
wenzelm@43086
|
18 |
val pretty_parsetree: parsetree -> Pretty.T
|
wenzelm@39077
|
19 |
val parse: Proof.context -> gram -> string -> Lexicon.token list -> parsetree list
|
haftmann@26678
|
20 |
val guess_infix_lr: gram -> string -> (string * bool * bool * int) option
|
wenzelm@41627
|
21 |
val branching_level: int Config.T
|
wenzelm@15752
|
22 |
end;
|
wenzelm@18
|
23 |
|
wenzelm@15752
|
24 |
structure Parser: PARSER =
|
wenzelm@15752
|
25 |
struct
|
paulson@1507
|
26 |
|
wenzelm@18
|
27 |
(** datatype gram **)
|
wenzelm@18
|
28 |
|
wenzelm@38960
|
29 |
(*production for the NTs are stored in a vector
|
wenzelm@38960
|
30 |
so we can identify NTs by their index*)
|
wenzelm@38960
|
31 |
type nt_tag = int;
|
wenzelm@18
|
32 |
|
wenzelm@38960
|
33 |
datatype symb =
|
wenzelm@38960
|
34 |
Terminal of Lexicon.token
|
wenzelm@38960
|
35 |
| Nonterminal of nt_tag * int; (*(tag, precedence)*)
|
wenzelm@18
|
36 |
|
wenzelm@38960
|
37 |
type nt_gram =
|
wenzelm@38960
|
38 |
((nt_tag list * Lexicon.token list) *
|
wenzelm@38960
|
39 |
(Lexicon.token option * (symb list * string * int) list) list);
|
wenzelm@38960
|
40 |
(*(([dependent_nts], [start_tokens]), [(start_token, [(rhs, name, prio)])])*)
|
wenzelm@38960
|
41 |
(*depent_nts is a list of all NTs whose lookahead depends on this NT's lookahead*)
|
clasohm@330
|
42 |
|
clasohm@1147
|
43 |
datatype gram =
|
wenzelm@38960
|
44 |
Gram of
|
wenzelm@38960
|
45 |
{nt_count: int,
|
wenzelm@38960
|
46 |
prod_count: int,
|
wenzelm@38960
|
47 |
tags: nt_tag Symtab.table,
|
wenzelm@38960
|
48 |
chains: (nt_tag * nt_tag list) list, (*[(to, [from])]*)
|
wenzelm@38960
|
49 |
lambdas: nt_tag list,
|
wenzelm@38960
|
50 |
prods: nt_gram Vector.vector};
|
wenzelm@38960
|
51 |
(*"tags" is used to map NT names (i.e. strings) to tags;
|
wenzelm@38960
|
52 |
chain productions are not stored as normal productions
|
wenzelm@38960
|
53 |
but instead as an entry in "chains";
|
wenzelm@38960
|
54 |
lambda productions are stored as normal productions
|
wenzelm@38960
|
55 |
and also as an entry in "lambdas"*)
|
clasohm@330
|
56 |
|
wenzelm@43101
|
57 |
val union_token = union Lexicon.matching_tokens;
|
wenzelm@43101
|
58 |
val subtract_token = subtract Lexicon.matching_tokens;
|
clasohm@330
|
59 |
|
wenzelm@38960
|
60 |
(*productions for which no starting token is
|
wenzelm@38960
|
61 |
known yet are associated with this token*)
|
wenzelm@38961
|
62 |
val unknown_start = Lexicon.eof;
|
wenzelm@38960
|
63 |
|
wenzelm@38960
|
64 |
(*get all NTs that are connected with a list of NTs*)
|
wenzelm@23909
|
65 |
fun connected_with _ ([]: nt_tag list) relatives = relatives
|
clasohm@1147
|
66 |
| connected_with chains (root :: roots) relatives =
|
wenzelm@38960
|
67 |
let val branches = subtract (op =) relatives (these (AList.lookup (op =) chains root));
|
wenzelm@38960
|
68 |
in connected_with chains (branches @ roots) (branches @ relatives) end;
|
clasohm@330
|
69 |
|
wenzelm@38960
|
70 |
(*convert productions to grammar;
|
wenzelm@38960
|
71 |
N.B. that the chains parameter has the form [(from, [to])];
|
wenzelm@38960
|
72 |
prod_count is of type "int option" and is only updated if it is <> NONE*)
|
clasohm@1147
|
73 |
fun add_prods _ chains lambdas prod_count [] = (chains, lambdas, prod_count)
|
wenzelm@38960
|
74 |
| add_prods prods chains lambdas prod_count ((lhs, new_prod as (rhs, name, pri)) :: ps) =
|
wenzelm@38960
|
75 |
let
|
wenzelm@38960
|
76 |
val chain_from =
|
wenzelm@38960
|
77 |
(case (pri, rhs) of
|
wenzelm@38960
|
78 |
(~1, [Nonterminal (id, ~1)]) => SOME id
|
wenzelm@38960
|
79 |
| _ => NONE);
|
wenzelm@15752
|
80 |
|
wenzelm@38960
|
81 |
(*store chain if it does not already exist*)
|
wenzelm@38960
|
82 |
val (new_chain, chains') =
|
wenzelm@38960
|
83 |
(case chain_from of
|
wenzelm@38960
|
84 |
NONE => (NONE, chains)
|
wenzelm@38960
|
85 |
| SOME from =>
|
wenzelm@38960
|
86 |
let val old_tos = these (AList.lookup (op =) chains from) in
|
wenzelm@38960
|
87 |
if member (op =) old_tos lhs then (NONE, chains)
|
wenzelm@38960
|
88 |
else (SOME from, AList.update (op =) (from, insert (op =) lhs old_tos) chains)
|
wenzelm@38960
|
89 |
end);
|
clasohm@330
|
90 |
|
wenzelm@38960
|
91 |
(*propagate new chain in lookahead and lambda lists;
|
wenzelm@38960
|
92 |
added_starts is used later to associate existing
|
wenzelm@38960
|
93 |
productions with new starting tokens*)
|
wenzelm@38960
|
94 |
val (added_starts, lambdas') =
|
wenzelm@38960
|
95 |
if is_none new_chain then ([], lambdas)
|
wenzelm@38960
|
96 |
else
|
wenzelm@38960
|
97 |
let (*lookahead of chain's source*)
|
wenzelm@38960
|
98 |
val ((from_nts, from_tks), _) = Array.sub (prods, the new_chain);
|
clasohm@330
|
99 |
|
wenzelm@38960
|
100 |
(*copy from's lookahead to chain's destinations*)
|
wenzelm@38960
|
101 |
fun copy_lookahead [] added = added
|
wenzelm@38960
|
102 |
| copy_lookahead (to :: tos) added =
|
wenzelm@38960
|
103 |
let
|
wenzelm@38960
|
104 |
val ((to_nts, to_tks), ps) = Array.sub (prods, to);
|
clasohm@1147
|
105 |
|
wenzelm@38960
|
106 |
val new_tks = subtract (op =) to_tks from_tks; (*added lookahead tokens*)
|
wenzelm@38960
|
107 |
val _ = Array.update (prods, to, ((to_nts, to_tks @ new_tks), ps));
|
clasohm@1147
|
108 |
in
|
wenzelm@38960
|
109 |
copy_lookahead tos (if null new_tks then added else (to, new_tks) :: added)
|
clasohm@1147
|
110 |
end;
|
clasohm@1147
|
111 |
|
wenzelm@38960
|
112 |
val tos = connected_with chains' [lhs] [lhs];
|
wenzelm@38960
|
113 |
in
|
wenzelm@38960
|
114 |
(copy_lookahead tos [],
|
wenzelm@38960
|
115 |
union (op =) (if member (op =) lambdas lhs then tos else []) lambdas)
|
wenzelm@38960
|
116 |
end;
|
wenzelm@38960
|
117 |
|
wenzelm@38960
|
118 |
(*test if new production can produce lambda
|
wenzelm@38960
|
119 |
(rhs must either be empty or only consist of lambda NTs)*)
|
wenzelm@38960
|
120 |
val (new_lambda, lambdas') =
|
wenzelm@38960
|
121 |
if forall
|
wenzelm@38960
|
122 |
(fn Nonterminal (id, _) => member (op =) lambdas' id
|
wenzelm@38960
|
123 |
| Terminal _ => false) rhs
|
wenzelm@43101
|
124 |
then (true, union (op =) (connected_with chains' [lhs] [lhs]) lambdas')
|
wenzelm@38960
|
125 |
else (false, lambdas');
|
wenzelm@38960
|
126 |
|
wenzelm@38960
|
127 |
(*list optional terminal and all nonterminals on which the lookahead
|
wenzelm@38960
|
128 |
of a production depends*)
|
wenzelm@38960
|
129 |
fun lookahead_dependency _ [] nts = (NONE, nts)
|
wenzelm@43100
|
130 |
| lookahead_dependency _ (Terminal tk :: _) nts = (SOME tk, nts)
|
wenzelm@43100
|
131 |
| lookahead_dependency lambdas (Nonterminal (nt, _) :: symbs) nts =
|
wenzelm@38960
|
132 |
if member (op =) lambdas nt then
|
wenzelm@38960
|
133 |
lookahead_dependency lambdas symbs (nt :: nts)
|
wenzelm@38960
|
134 |
else (NONE, nt :: nts);
|
wenzelm@38960
|
135 |
|
wenzelm@38960
|
136 |
(*get all known starting tokens for a nonterminal*)
|
wenzelm@38960
|
137 |
fun starts_for_nt nt = snd (fst (Array.sub (prods, nt)));
|
wenzelm@38960
|
138 |
|
wenzelm@38960
|
139 |
(*update prods, lookaheads, and lambdas according to new lambda NTs*)
|
wenzelm@38960
|
140 |
val (added_starts', lambdas') =
|
wenzelm@38960
|
141 |
let
|
wenzelm@38960
|
142 |
(*propagate added lambda NT*)
|
wenzelm@43100
|
143 |
fun propagate_lambda [] added_starts lambdas = (added_starts, lambdas)
|
wenzelm@38960
|
144 |
| propagate_lambda (l :: ls) added_starts lambdas =
|
wenzelm@38960
|
145 |
let
|
wenzelm@38960
|
146 |
(*get lookahead for lambda NT*)
|
wenzelm@38960
|
147 |
val ((dependent, l_starts), _) = Array.sub (prods, l);
|
wenzelm@38960
|
148 |
|
wenzelm@38960
|
149 |
(*check productions whose lookahead may depend on lambda NT*)
|
wenzelm@38960
|
150 |
fun examine_prods [] add_lambda nt_dependencies added_tks nt_prods =
|
wenzelm@38960
|
151 |
(add_lambda, nt_dependencies, added_tks, nt_prods)
|
wenzelm@38960
|
152 |
| examine_prods ((p as (rhs, _, _)) :: ps) add_lambda
|
wenzelm@38960
|
153 |
nt_dependencies added_tks nt_prods =
|
wenzelm@38960
|
154 |
let val (tk, nts) = lookahead_dependency lambdas rhs [] in
|
wenzelm@38960
|
155 |
if member (op =) nts l then (*update production's lookahead*)
|
wenzelm@38960
|
156 |
let
|
wenzelm@38960
|
157 |
val new_lambda = is_none tk andalso subset (op =) (nts, lambdas);
|
wenzelm@38960
|
158 |
|
wenzelm@43101
|
159 |
val new_tks =
|
wenzelm@43101
|
160 |
(if is_some tk then [the tk] else [])
|
wenzelm@43101
|
161 |
|> fold (union_token o starts_for_nt) nts
|
wenzelm@43101
|
162 |
|> subtract (op =) l_starts;
|
wenzelm@38960
|
163 |
|
wenzelm@43101
|
164 |
val added_tks' = union_token added_tks new_tks;
|
wenzelm@38960
|
165 |
|
wenzelm@38960
|
166 |
val nt_dependencies' = union (op =) nts nt_dependencies;
|
wenzelm@38960
|
167 |
|
wenzelm@38960
|
168 |
(*associate production with new starting tokens*)
|
wenzelm@38960
|
169 |
fun copy ([]: Lexicon.token option list) nt_prods = nt_prods
|
wenzelm@38960
|
170 |
| copy (tk :: tks) nt_prods =
|
wenzelm@38960
|
171 |
let
|
wenzelm@38960
|
172 |
val old_prods = these (AList.lookup (op =) nt_prods tk);
|
wenzelm@38960
|
173 |
val prods' = p :: old_prods;
|
wenzelm@38960
|
174 |
in
|
wenzelm@38960
|
175 |
nt_prods
|
wenzelm@38960
|
176 |
|> AList.update (op =) (tk, prods')
|
wenzelm@38960
|
177 |
|> copy tks
|
wenzelm@38960
|
178 |
end;
|
wenzelm@38960
|
179 |
|
wenzelm@38960
|
180 |
val nt_prods' =
|
wenzelm@38960
|
181 |
let val new_opt_tks = map SOME new_tks in
|
wenzelm@38960
|
182 |
copy
|
wenzelm@38960
|
183 |
((if new_lambda then [NONE] else []) @ new_opt_tks) nt_prods
|
wenzelm@38960
|
184 |
end;
|
wenzelm@38960
|
185 |
in
|
wenzelm@38960
|
186 |
examine_prods ps (add_lambda orelse new_lambda)
|
wenzelm@38960
|
187 |
nt_dependencies' added_tks' nt_prods'
|
wenzelm@38960
|
188 |
end
|
wenzelm@38960
|
189 |
else (*skip production*)
|
wenzelm@38960
|
190 |
examine_prods ps add_lambda nt_dependencies added_tks nt_prods
|
wenzelm@38960
|
191 |
end;
|
wenzelm@38960
|
192 |
|
wenzelm@38960
|
193 |
(*check each NT whose lookahead depends on new lambda NT*)
|
wenzelm@38960
|
194 |
fun process_nts [] added_lambdas added_starts =
|
wenzelm@38960
|
195 |
(added_lambdas, added_starts)
|
wenzelm@38960
|
196 |
| process_nts (nt :: nts) added_lambdas added_starts =
|
wenzelm@38960
|
197 |
let
|
wenzelm@38960
|
198 |
val (lookahead as (old_nts, old_tks), nt_prods) = Array.sub (prods, nt);
|
wenzelm@38960
|
199 |
|
wenzelm@38960
|
200 |
(*existing productions whose lookahead may depend on l*)
|
wenzelm@38960
|
201 |
val tk_prods =
|
wenzelm@38960
|
202 |
these
|
wenzelm@38960
|
203 |
(AList.lookup (op =) nt_prods
|
wenzelm@38961
|
204 |
(SOME (hd l_starts handle Empty => unknown_start)));
|
wenzelm@38960
|
205 |
|
wenzelm@38960
|
206 |
(*add_lambda is true if an existing production of the nt
|
wenzelm@38960
|
207 |
produces lambda due to the new lambda NT l*)
|
wenzelm@38960
|
208 |
val (add_lambda, nt_dependencies, added_tks, nt_prods') =
|
wenzelm@38960
|
209 |
examine_prods tk_prods false [] [] nt_prods;
|
wenzelm@38960
|
210 |
|
wenzelm@38960
|
211 |
val added_nts = subtract (op =) old_nts nt_dependencies;
|
wenzelm@38960
|
212 |
|
wenzelm@38960
|
213 |
val added_lambdas' =
|
wenzelm@38960
|
214 |
if add_lambda then nt :: added_lambdas
|
wenzelm@38960
|
215 |
else added_lambdas;
|
wenzelm@38960
|
216 |
val _ =
|
wenzelm@38960
|
217 |
Array.update
|
wenzelm@38960
|
218 |
(prods, nt, ((added_nts @ old_nts, old_tks @ added_tks), nt_prods'));
|
wenzelm@38960
|
219 |
(*N.B. that because the tks component
|
wenzelm@38960
|
220 |
is used to access existing
|
wenzelm@38960
|
221 |
productions we have to add new
|
wenzelm@38960
|
222 |
tokens at the _end_ of the list*)
|
wenzelm@38960
|
223 |
in
|
wenzelm@38960
|
224 |
if null added_tks then
|
wenzelm@38960
|
225 |
process_nts nts added_lambdas' added_starts
|
wenzelm@38960
|
226 |
else
|
wenzelm@38960
|
227 |
process_nts nts added_lambdas' ((nt, added_tks) :: added_starts)
|
wenzelm@38960
|
228 |
end;
|
wenzelm@38960
|
229 |
|
wenzelm@38960
|
230 |
val (added_lambdas, added_starts') = process_nts dependent [] added_starts;
|
wenzelm@38960
|
231 |
val added_lambdas' = subtract (op =) lambdas added_lambdas;
|
wenzelm@38960
|
232 |
in
|
wenzelm@38960
|
233 |
propagate_lambda (ls @ added_lambdas') added_starts' (added_lambdas' @ lambdas)
|
wenzelm@38960
|
234 |
end;
|
wenzelm@38960
|
235 |
in propagate_lambda (subtract (op =) lambdas lambdas') added_starts lambdas' end;
|
wenzelm@38960
|
236 |
|
wenzelm@38960
|
237 |
(*insert production into grammar*)
|
wenzelm@38960
|
238 |
val (added_starts', prod_count') =
|
wenzelm@38960
|
239 |
if is_some chain_from
|
wenzelm@38960
|
240 |
then (added_starts', prod_count) (*don't store chain production*)
|
wenzelm@38960
|
241 |
else
|
wenzelm@38960
|
242 |
let
|
wenzelm@38960
|
243 |
(*lookahead tokens of new production and on which
|
wenzelm@38960
|
244 |
NTs lookahead depends*)
|
wenzelm@38960
|
245 |
val (start_tk, start_nts) = lookahead_dependency lambdas' rhs [];
|
wenzelm@38960
|
246 |
|
wenzelm@38960
|
247 |
val start_tks =
|
wenzelm@43101
|
248 |
(if is_some start_tk then [the start_tk] else [])
|
wenzelm@43101
|
249 |
|> fold (union_token o starts_for_nt) start_nts;
|
wenzelm@38960
|
250 |
|
wenzelm@38960
|
251 |
val opt_starts =
|
wenzelm@38960
|
252 |
(if new_lambda then [NONE]
|
wenzelm@38961
|
253 |
else if null start_tks then [SOME unknown_start]
|
wenzelm@38960
|
254 |
else []) @ map SOME start_tks;
|
wenzelm@38960
|
255 |
|
wenzelm@38960
|
256 |
(*add lhs NT to list of dependent NTs in lookahead*)
|
wenzelm@38960
|
257 |
fun add_nts [] = ()
|
wenzelm@38960
|
258 |
| add_nts (nt :: nts) =
|
wenzelm@43100
|
259 |
let val ((old_nts, old_tks), ps) = Array.sub (prods, nt) in
|
wenzelm@43100
|
260 |
if member (op =) old_nts lhs then ()
|
wenzelm@43100
|
261 |
else Array.update (prods, nt, ((lhs :: old_nts, old_tks), ps))
|
wenzelm@43100
|
262 |
end;
|
wenzelm@38960
|
263 |
|
wenzelm@38960
|
264 |
(*add new start tokens to chained NTs' lookahead list;
|
wenzelm@38960
|
265 |
also store new production for lhs NT*)
|
wenzelm@38960
|
266 |
fun add_tks [] added prod_count = (added, prod_count)
|
wenzelm@38960
|
267 |
| add_tks (nt :: nts) added prod_count =
|
clasohm@1147
|
268 |
let
|
wenzelm@38960
|
269 |
val ((old_nts, old_tks), nt_prods) = Array.sub (prods, nt);
|
clasohm@1147
|
270 |
|
wenzelm@43101
|
271 |
val new_tks = subtract_token old_tks start_tks;
|
clasohm@1147
|
272 |
|
wenzelm@38960
|
273 |
(*store new production*)
|
wenzelm@38960
|
274 |
fun store [] prods is_new =
|
wenzelm@38960
|
275 |
(prods,
|
wenzelm@38960
|
276 |
if is_some prod_count andalso is_new then
|
wenzelm@38960
|
277 |
Option.map (fn x => x + 1) prod_count
|
wenzelm@38960
|
278 |
else prod_count, is_new)
|
wenzelm@38960
|
279 |
| store (tk :: tks) prods is_new =
|
wenzelm@38960
|
280 |
let
|
wenzelm@38960
|
281 |
val tk_prods = these (AList.lookup (op =) prods tk);
|
clasohm@1147
|
282 |
|
wenzelm@38960
|
283 |
(*if prod_count = NONE then we can assume that
|
wenzelm@38960
|
284 |
grammar does not contain new production already*)
|
wenzelm@38960
|
285 |
val (tk_prods', is_new') =
|
wenzelm@38960
|
286 |
if is_some prod_count then
|
wenzelm@38960
|
287 |
if member (op =) tk_prods new_prod then (tk_prods, false)
|
wenzelm@38960
|
288 |
else (new_prod :: tk_prods, true)
|
wenzelm@38960
|
289 |
else (new_prod :: tk_prods, true);
|
clasohm@1147
|
290 |
|
wenzelm@43101
|
291 |
val prods' =
|
wenzelm@43101
|
292 |
if is_new' then
|
wenzelm@43101
|
293 |
AList.update (op =) (tk: Lexicon.token option, tk_prods') prods
|
wenzelm@43101
|
294 |
else prods;
|
wenzelm@38960
|
295 |
in store tks prods' (is_new orelse is_new') end;
|
clasohm@1175
|
296 |
|
wenzelm@38960
|
297 |
val (nt_prods', prod_count', changed) =
|
wenzelm@38960
|
298 |
if nt = lhs
|
wenzelm@38960
|
299 |
then store opt_starts nt_prods false
|
wenzelm@38960
|
300 |
else (nt_prods, prod_count, false);
|
wenzelm@38960
|
301 |
val _ =
|
wenzelm@38960
|
302 |
if not changed andalso null new_tks then ()
|
wenzelm@38960
|
303 |
else Array.update (prods, nt, ((old_nts, old_tks @ new_tks), nt_prods'));
|
wenzelm@38960
|
304 |
in
|
wenzelm@38960
|
305 |
add_tks nts
|
wenzelm@38960
|
306 |
(if null new_tks then added else (nt, new_tks) :: added) prod_count'
|
clasohm@1147
|
307 |
end;
|
wenzelm@38960
|
308 |
val _ = add_nts start_nts;
|
wenzelm@38960
|
309 |
in
|
wenzelm@38960
|
310 |
add_tks (connected_with chains' [lhs] [lhs]) [] prod_count
|
wenzelm@38960
|
311 |
end;
|
clasohm@1147
|
312 |
|
wenzelm@38960
|
313 |
(*associate productions with new lookaheads*)
|
wenzelm@38960
|
314 |
val _ =
|
wenzelm@38960
|
315 |
let
|
wenzelm@38960
|
316 |
(*propagate added start tokens*)
|
wenzelm@38960
|
317 |
fun add_starts [] = ()
|
wenzelm@38960
|
318 |
| add_starts ((changed_nt, new_tks) :: starts) =
|
wenzelm@38960
|
319 |
let
|
wenzelm@38960
|
320 |
(*token under which old productions which
|
wenzelm@38960
|
321 |
depend on changed_nt could be stored*)
|
wenzelm@38960
|
322 |
val key =
|
wenzelm@38960
|
323 |
(case find_first (not o member (op =) new_tks) (starts_for_nt changed_nt) of
|
wenzelm@38961
|
324 |
NONE => SOME unknown_start
|
wenzelm@38960
|
325 |
| t => t);
|
clasohm@1147
|
326 |
|
wenzelm@38960
|
327 |
(*copy productions whose lookahead depends on changed_nt;
|
wenzelm@38961
|
328 |
if key = SOME unknown_start then tk_prods is used to hold
|
wenzelm@38960
|
329 |
the productions not copied*)
|
wenzelm@38960
|
330 |
fun update_prods [] result = result
|
wenzelm@38960
|
331 |
| update_prods ((p as (rhs, _: string, _: nt_tag)) :: ps)
|
wenzelm@38960
|
332 |
(tk_prods, nt_prods) =
|
wenzelm@38960
|
333 |
let
|
wenzelm@38960
|
334 |
(*lookahead dependency for production*)
|
wenzelm@38960
|
335 |
val (tk, depends) = lookahead_dependency lambdas' rhs [];
|
clasohm@1147
|
336 |
|
wenzelm@38960
|
337 |
(*test if this production has to be copied*)
|
wenzelm@38960
|
338 |
val update = member (op =) depends changed_nt;
|
clasohm@1147
|
339 |
|
wenzelm@38960
|
340 |
(*test if production could already be associated with
|
wenzelm@38960
|
341 |
a member of new_tks*)
|
wenzelm@38960
|
342 |
val lambda =
|
wenzelm@38960
|
343 |
length depends > 1 orelse
|
wenzelm@38960
|
344 |
not (null depends) andalso is_some tk
|
wenzelm@38960
|
345 |
andalso member (op =) new_tks (the tk);
|
clasohm@1175
|
346 |
|
wenzelm@38960
|
347 |
(*associate production with new starting tokens*)
|
wenzelm@38960
|
348 |
fun copy ([]: Lexicon.token list) nt_prods = nt_prods
|
wenzelm@38960
|
349 |
| copy (tk :: tks) nt_prods =
|
wenzelm@38960
|
350 |
let
|
wenzelm@38960
|
351 |
val tk_prods = these (AList.lookup (op =) nt_prods (SOME tk));
|
clasohm@1147
|
352 |
|
wenzelm@38960
|
353 |
val tk_prods' =
|
wenzelm@38960
|
354 |
if not lambda then p :: tk_prods
|
wenzelm@38960
|
355 |
else insert (op =) p tk_prods;
|
wenzelm@38960
|
356 |
(*if production depends on lambda NT we
|
wenzelm@38960
|
357 |
have to look for duplicates*)
|
wenzelm@38960
|
358 |
in
|
wenzelm@38960
|
359 |
nt_prods
|
wenzelm@38960
|
360 |
|> AList.update (op =) (SOME tk, tk_prods')
|
wenzelm@38960
|
361 |
|> copy tks
|
wenzelm@38960
|
362 |
end;
|
wenzelm@38960
|
363 |
val result =
|
wenzelm@38960
|
364 |
if update then (tk_prods, copy new_tks nt_prods)
|
wenzelm@38961
|
365 |
else if key = SOME unknown_start then (p :: tk_prods, nt_prods)
|
wenzelm@38960
|
366 |
else (tk_prods, nt_prods);
|
wenzelm@38960
|
367 |
in update_prods ps result end;
|
clasohm@1147
|
368 |
|
wenzelm@38960
|
369 |
(*copy existing productions for new starting tokens*)
|
wenzelm@38960
|
370 |
fun process_nts [] added = added
|
wenzelm@38960
|
371 |
| process_nts (nt :: nts) added =
|
wenzelm@38960
|
372 |
let
|
wenzelm@38960
|
373 |
val (lookahead as (old_nts, old_tks), nt_prods) = Array.sub (prods, nt);
|
clasohm@1147
|
374 |
|
wenzelm@38960
|
375 |
val tk_prods = these (AList.lookup (op =) nt_prods key);
|
clasohm@1147
|
376 |
|
wenzelm@38960
|
377 |
(*associate productions with new lookahead tokens*)
|
wenzelm@38960
|
378 |
val (tk_prods', nt_prods') = update_prods tk_prods ([], nt_prods);
|
clasohm@1147
|
379 |
|
wenzelm@43100
|
380 |
val nt_prods'' =
|
wenzelm@43100
|
381 |
if key = SOME unknown_start then
|
wenzelm@43100
|
382 |
AList.update (op =) (key, tk_prods') nt_prods'
|
wenzelm@43100
|
383 |
else nt_prods';
|
clasohm@1147
|
384 |
|
wenzelm@43101
|
385 |
val added_tks = subtract_token old_tks new_tks;
|
wenzelm@38960
|
386 |
in
|
wenzelm@38960
|
387 |
if null added_tks then
|
wenzelm@43100
|
388 |
(Array.update (prods, nt, (lookahead, nt_prods''));
|
wenzelm@38960
|
389 |
process_nts nts added)
|
wenzelm@38960
|
390 |
else
|
wenzelm@43100
|
391 |
(Array.update (prods, nt, ((old_nts, added_tks @ old_tks), nt_prods''));
|
wenzelm@38960
|
392 |
process_nts nts ((nt, added_tks) :: added))
|
wenzelm@38960
|
393 |
end;
|
clasohm@1175
|
394 |
|
wenzelm@38960
|
395 |
val ((dependent, _), _) = Array.sub (prods, changed_nt);
|
wenzelm@38960
|
396 |
in add_starts (starts @ process_nts dependent []) end;
|
wenzelm@38960
|
397 |
in add_starts added_starts' end;
|
wenzelm@38960
|
398 |
in add_prods prods chains' lambdas' prod_count ps end;
|
wenzelm@18
|
399 |
|
wenzelm@18
|
400 |
|
wenzelm@237
|
401 |
(* pretty_gram *)
|
wenzelm@18
|
402 |
|
clasohm@1147
|
403 |
fun pretty_gram (Gram {tags, prods, chains, ...}) =
|
wenzelm@237
|
404 |
let
|
wenzelm@237
|
405 |
fun pretty_name name = [Pretty.str (name ^ " =")];
|
wenzelm@18
|
406 |
|
wenzelm@26069
|
407 |
val nt_name = the o Inttab.lookup (Inttab.make (map swap (Symtab.dest tags)));
|
clasohm@1147
|
408 |
|
wenzelm@37699
|
409 |
fun pretty_symb (Terminal (Lexicon.Token (Lexicon.Literal, s, _))) = Pretty.quote (Pretty.str s)
|
wenzelm@37699
|
410 |
| pretty_symb (Terminal tok) = Pretty.str (Lexicon.str_of_token tok)
|
wenzelm@28843
|
411 |
| pretty_symb (Nonterminal (tag, p)) =
|
wenzelm@28843
|
412 |
Pretty.str (nt_name tag ^ "[" ^ signed_string_of_int p ^ "]");
|
wenzelm@18
|
413 |
|
wenzelm@237
|
414 |
fun pretty_const "" = []
|
wenzelm@28843
|
415 |
| pretty_const c = [Pretty.str ("=> " ^ quote c)];
|
wenzelm@237
|
416 |
|
wenzelm@28843
|
417 |
fun pretty_pri p = [Pretty.str ("(" ^ signed_string_of_int p ^ ")")];
|
wenzelm@237
|
418 |
|
clasohm@1147
|
419 |
fun pretty_prod name (symbs, const, pri) =
|
wenzelm@237
|
420 |
Pretty.block (Pretty.breaks (pretty_name name @
|
wenzelm@237
|
421 |
map pretty_symb symbs @ pretty_const const @ pretty_pri pri));
|
clasohm@1147
|
422 |
|
clasohm@1147
|
423 |
fun pretty_nt (name, tag) =
|
clasohm@1147
|
424 |
let
|
wenzelm@30189
|
425 |
fun prod_of_chain from = ([Nonterminal (from, ~1)], "", ~1);
|
clasohm@1147
|
426 |
|
clasohm@1147
|
427 |
val nt_prods =
|
wenzelm@43101
|
428 |
fold (union (op =) o snd) (snd (Vector.sub (prods, tag))) [] @
|
wenzelm@43101
|
429 |
map prod_of_chain (these (AList.lookup (op =) chains tag));
|
clasohm@1147
|
430 |
in map (pretty_prod name) nt_prods end;
|
wenzelm@15752
|
431 |
|
wenzelm@25986
|
432 |
in maps pretty_nt (sort_wrt fst (Symtab.dest tags)) end;
|
clasohm@1147
|
433 |
|
clasohm@1147
|
434 |
|
wenzelm@43100
|
435 |
|
clasohm@1438
|
436 |
(** Operations on gramars **)
|
clasohm@1147
|
437 |
|
wenzelm@38960
|
438 |
val empty_gram =
|
wenzelm@38960
|
439 |
Gram
|
wenzelm@38960
|
440 |
{nt_count = 0,
|
wenzelm@38960
|
441 |
prod_count = 0,
|
wenzelm@38960
|
442 |
tags = Symtab.empty, chains = [],
|
wenzelm@38960
|
443 |
lambdas = [],
|
wenzelm@38960
|
444 |
prods = Vector.fromList [(([], []), [])]};
|
clasohm@1147
|
445 |
|
clasohm@1438
|
446 |
|
clasohm@1438
|
447 |
(*Invert list of chain productions*)
|
clasohm@1147
|
448 |
fun inverse_chains [] result = result
|
wenzelm@23909
|
449 |
| inverse_chains ((root, branches: nt_tag list) :: cs) result =
|
wenzelm@38960
|
450 |
let
|
wenzelm@38960
|
451 |
fun add ([]: nt_tag list) result = result
|
clasohm@1147
|
452 |
| add (id :: ids) result =
|
wenzelm@38960
|
453 |
let val old = these (AList.lookup (op =) result id);
|
wenzelm@38960
|
454 |
in add ids (AList.update (op =) (id, root :: old) result) end;
|
wenzelm@38960
|
455 |
in inverse_chains cs (add branches result) end;
|
clasohm@1147
|
456 |
|
clasohm@1438
|
457 |
|
clasohm@1438
|
458 |
(*Add productions to a grammar*)
|
wenzelm@37700
|
459 |
fun extend_gram [] gram = gram
|
wenzelm@37700
|
460 |
| extend_gram xprods (Gram {nt_count, prod_count, tags, chains, lambdas, prods}) =
|
wenzelm@38960
|
461 |
let
|
wenzelm@38960
|
462 |
(*Get tag for existing nonterminal or create a new one*)
|
wenzelm@38960
|
463 |
fun get_tag nt_count tags nt =
|
wenzelm@38960
|
464 |
(case Symtab.lookup tags nt of
|
wenzelm@38960
|
465 |
SOME tag => (nt_count, tags, tag)
|
wenzelm@38960
|
466 |
| NONE => (nt_count + 1, Symtab.update_new (nt, nt_count) tags, nt_count));
|
clasohm@1438
|
467 |
|
wenzelm@38960
|
468 |
(*Convert symbols to the form used by the parser;
|
wenzelm@38960
|
469 |
delimiters and predefined terms are stored as terminals,
|
wenzelm@38960
|
470 |
nonterminals are converted to integer tags*)
|
wenzelm@38960
|
471 |
fun symb_of [] nt_count tags result = (nt_count, tags, rev result)
|
wenzelm@43160
|
472 |
| symb_of (Syntax_Ext.Delim s :: ss) nt_count tags result =
|
wenzelm@38960
|
473 |
symb_of ss nt_count tags
|
wenzelm@38960
|
474 |
(Terminal (Lexicon.Token (Lexicon.Literal, s, Position.no_range)) :: result)
|
wenzelm@43160
|
475 |
| symb_of (Syntax_Ext.Argument (s, p) :: ss) nt_count tags result =
|
wenzelm@38960
|
476 |
let
|
wenzelm@38960
|
477 |
val (nt_count', tags', new_symb) =
|
wenzelm@38960
|
478 |
(case Lexicon.predef_term s of
|
wenzelm@38960
|
479 |
NONE =>
|
wenzelm@38960
|
480 |
let val (nt_count', tags', s_tag) = get_tag nt_count tags s;
|
wenzelm@38960
|
481 |
in (nt_count', tags', Nonterminal (s_tag, p)) end
|
wenzelm@38960
|
482 |
| SOME tk => (nt_count, tags, Terminal tk));
|
wenzelm@38960
|
483 |
in symb_of ss nt_count' tags' (new_symb :: result) end
|
wenzelm@38960
|
484 |
| symb_of (_ :: ss) nt_count tags result = symb_of ss nt_count tags result;
|
clasohm@1147
|
485 |
|
wenzelm@38960
|
486 |
(*Convert list of productions by invoking symb_of for each of them*)
|
wenzelm@38960
|
487 |
fun prod_of [] nt_count prod_count tags result =
|
wenzelm@38960
|
488 |
(nt_count, prod_count, tags, result)
|
wenzelm@43160
|
489 |
| prod_of (Syntax_Ext.XProd (lhs, xsymbs, const, pri) :: ps)
|
wenzelm@38960
|
490 |
nt_count prod_count tags result =
|
wenzelm@38960
|
491 |
let
|
wenzelm@38960
|
492 |
val (nt_count', tags', lhs_tag) = get_tag nt_count tags lhs;
|
wenzelm@38960
|
493 |
val (nt_count'', tags'', prods) = symb_of xsymbs nt_count' tags' [];
|
wenzelm@38960
|
494 |
in
|
wenzelm@38960
|
495 |
prod_of ps nt_count'' (prod_count + 1) tags''
|
wenzelm@38960
|
496 |
((lhs_tag, (prods, const, pri)) :: result)
|
wenzelm@38960
|
497 |
end;
|
clasohm@1147
|
498 |
|
wenzelm@38960
|
499 |
val (nt_count', prod_count', tags', xprods') =
|
wenzelm@38960
|
500 |
prod_of xprods nt_count prod_count tags [];
|
clasohm@1147
|
501 |
|
wenzelm@38960
|
502 |
(*Copy array containing productions of old grammar;
|
wenzelm@38960
|
503 |
this has to be done to preserve the old grammar while being able
|
wenzelm@38960
|
504 |
to change the array's content*)
|
wenzelm@38960
|
505 |
val prods' =
|
wenzelm@38960
|
506 |
let
|
wenzelm@38960
|
507 |
fun get_prod i =
|
wenzelm@38960
|
508 |
if i < nt_count then Vector.sub (prods, i)
|
wenzelm@38960
|
509 |
else (([], []), []);
|
wenzelm@38960
|
510 |
in Array.tabulate (nt_count', get_prod) end;
|
clasohm@1147
|
511 |
|
wenzelm@38960
|
512 |
val fromto_chains = inverse_chains chains [];
|
clasohm@1147
|
513 |
|
wenzelm@38960
|
514 |
(*Add new productions to old ones*)
|
wenzelm@38960
|
515 |
val (fromto_chains', lambdas', _) =
|
wenzelm@38960
|
516 |
add_prods prods' fromto_chains lambdas NONE xprods';
|
clasohm@1147
|
517 |
|
wenzelm@38960
|
518 |
val chains' = inverse_chains fromto_chains' [];
|
wenzelm@38960
|
519 |
in
|
wenzelm@38960
|
520 |
Gram
|
wenzelm@38960
|
521 |
{nt_count = nt_count',
|
wenzelm@38960
|
522 |
prod_count = prod_count',
|
wenzelm@38960
|
523 |
tags = tags',
|
wenzelm@38960
|
524 |
chains = chains',
|
wenzelm@38960
|
525 |
lambdas = lambdas',
|
wenzelm@38960
|
526 |
prods = Array.vector prods'}
|
wenzelm@38960
|
527 |
end;
|
wenzelm@18
|
528 |
|
wenzelm@18
|
529 |
|
clasohm@1438
|
530 |
(*Merge two grammars*)
|
wenzelm@37700
|
531 |
fun merge_gram (gram_a, gram_b) =
|
clasohm@1147
|
532 |
let
|
clasohm@1147
|
533 |
(*find out which grammar is bigger*)
|
wenzelm@38960
|
534 |
val
|
wenzelm@38960
|
535 |
(Gram {nt_count = nt_count1, prod_count = prod_count1, tags = tags1,
|
wenzelm@38960
|
536 |
chains = chains1, lambdas = lambdas1, prods = prods1},
|
wenzelm@38960
|
537 |
Gram {nt_count = nt_count2, prod_count = prod_count2, tags = tags2,
|
wenzelm@38960
|
538 |
chains = chains2, lambdas = lambdas2, prods = prods2}) =
|
wenzelm@38960
|
539 |
let
|
wenzelm@38960
|
540 |
val Gram {prod_count = count_a, ...} = gram_a;
|
wenzelm@38960
|
541 |
val Gram {prod_count = count_b, ...} = gram_b;
|
wenzelm@38960
|
542 |
in
|
wenzelm@38960
|
543 |
if count_a > count_b
|
wenzelm@38960
|
544 |
then (gram_a, gram_b)
|
wenzelm@38960
|
545 |
else (gram_b, gram_a)
|
clasohm@1147
|
546 |
end;
|
clasohm@1147
|
547 |
|
clasohm@1147
|
548 |
(*get existing tag from grammar1 or create a new one*)
|
clasohm@1147
|
549 |
fun get_tag nt_count tags nt =
|
wenzelm@38960
|
550 |
(case Symtab.lookup tags nt of
|
skalberg@15531
|
551 |
SOME tag => (nt_count, tags, tag)
|
wenzelm@38960
|
552 |
| NONE => (nt_count + 1, Symtab.update_new (nt, nt_count) tags, nt_count));
|
clasohm@1147
|
553 |
|
clasohm@1147
|
554 |
val ((nt_count1', tags1'), tag_table) =
|
wenzelm@38960
|
555 |
let
|
wenzelm@38960
|
556 |
val table = Array.array (nt_count2, ~1);
|
clasohm@1147
|
557 |
|
wenzelm@43104
|
558 |
fun the_nt tag =
|
wenzelm@43104
|
559 |
the (Symtab.get_first (fn (n, t) => if t = tag then SOME n else NONE) tags2);
|
wenzelm@38960
|
560 |
fun store_tag nt_count tags ~1 = (nt_count, tags)
|
wenzelm@38960
|
561 |
| store_tag nt_count tags tag =
|
wenzelm@38960
|
562 |
let
|
wenzelm@43104
|
563 |
val (nt_count', tags', tag') = get_tag nt_count tags (the_nt tag);
|
wenzelm@38960
|
564 |
val _ = Array.update (table, tag, tag');
|
wenzelm@38960
|
565 |
in store_tag nt_count' tags' (tag - 1) end;
|
wenzelm@38960
|
566 |
in (store_tag nt_count1 tags1 (nt_count2 - 1), table) end;
|
wenzelm@15752
|
567 |
|
clasohm@1147
|
568 |
(*convert grammar2 tag to grammar1 tag*)
|
clasohm@1147
|
569 |
fun convert_tag tag = Array.sub (tag_table, tag);
|
clasohm@1147
|
570 |
|
clasohm@1147
|
571 |
(*convert chain list to raw productions*)
|
clasohm@1147
|
572 |
fun mk_chain_prods [] result = result
|
clasohm@1147
|
573 |
| mk_chain_prods ((to, froms) :: cs) result =
|
wenzelm@38960
|
574 |
let
|
wenzelm@38960
|
575 |
val to_tag = convert_tag to;
|
clasohm@1147
|
576 |
|
wenzelm@38960
|
577 |
fun make [] result = result
|
wenzelm@38960
|
578 |
| make (from :: froms) result = make froms
|
wenzelm@38960
|
579 |
((to_tag, ([Nonterminal (convert_tag from, ~1)], "", ~1)) :: result);
|
wenzelm@38960
|
580 |
in mk_chain_prods cs (make froms [] @ result) end;
|
wenzelm@15752
|
581 |
|
clasohm@1147
|
582 |
val chain_prods = mk_chain_prods chains2 [];
|
clasohm@1147
|
583 |
|
clasohm@1147
|
584 |
(*convert prods2 array to productions*)
|
clasohm@1147
|
585 |
fun process_nt ~1 result = result
|
clasohm@1147
|
586 |
| process_nt nt result =
|
wenzelm@38960
|
587 |
let
|
wenzelm@43101
|
588 |
val nt_prods = fold (union (op =) o snd) (snd (Vector.sub (prods2, nt))) [];
|
wenzelm@38960
|
589 |
val lhs_tag = convert_tag nt;
|
clasohm@1147
|
590 |
|
wenzelm@38960
|
591 |
(*convert tags in rhs*)
|
wenzelm@38960
|
592 |
fun process_rhs [] result = result
|
wenzelm@38960
|
593 |
| process_rhs (Terminal tk :: rhs) result =
|
wenzelm@38960
|
594 |
process_rhs rhs (result @ [Terminal tk])
|
wenzelm@38960
|
595 |
| process_rhs (Nonterminal (nt, prec) :: rhs) result =
|
wenzelm@38960
|
596 |
process_rhs rhs (result @ [Nonterminal (convert_tag nt, prec)]);
|
clasohm@1147
|
597 |
|
wenzelm@38960
|
598 |
(*convert tags in productions*)
|
wenzelm@38960
|
599 |
fun process_prods [] result = result
|
wenzelm@38960
|
600 |
| process_prods ((rhs, id, prec) :: ps) result =
|
wenzelm@38960
|
601 |
process_prods ps ((lhs_tag, (process_rhs rhs [], id, prec)) :: result);
|
wenzelm@38960
|
602 |
in process_nt (nt - 1) (process_prods nt_prods [] @ result) end;
|
clasohm@1147
|
603 |
|
wenzelm@38960
|
604 |
val raw_prods = chain_prods @ process_nt (nt_count2 - 1) [];
|
clasohm@1147
|
605 |
|
clasohm@1147
|
606 |
val prods1' =
|
wenzelm@38960
|
607 |
let
|
wenzelm@38960
|
608 |
fun get_prod i =
|
wenzelm@38960
|
609 |
if i < nt_count1 then Vector.sub (prods1, i)
|
wenzelm@38960
|
610 |
else (([], []), []);
|
clasohm@1147
|
611 |
in Array.tabulate (nt_count1', get_prod) end;
|
clasohm@1147
|
612 |
|
clasohm@1147
|
613 |
val fromto_chains = inverse_chains chains1 [];
|
clasohm@1147
|
614 |
|
skalberg@15531
|
615 |
val (fromto_chains', lambdas', SOME prod_count1') =
|
skalberg@15531
|
616 |
add_prods prods1' fromto_chains lambdas1 (SOME prod_count1) raw_prods;
|
clasohm@1147
|
617 |
|
clasohm@1147
|
618 |
val chains' = inverse_chains fromto_chains' [];
|
wenzelm@38960
|
619 |
in
|
wenzelm@38960
|
620 |
Gram
|
wenzelm@38960
|
621 |
{nt_count = nt_count1',
|
wenzelm@38960
|
622 |
prod_count = prod_count1',
|
wenzelm@38960
|
623 |
tags = tags1',
|
wenzelm@38960
|
624 |
chains = chains',
|
wenzelm@38960
|
625 |
lambdas = lambdas',
|
wenzelm@38960
|
626 |
prods = Array.vector prods1'}
|
clasohm@1147
|
627 |
end;
|
clasohm@1147
|
628 |
|
wenzelm@18
|
629 |
|
wenzelm@43100
|
630 |
|
wenzelm@43245
|
631 |
(** parser **)
|
wenzelm@43245
|
632 |
|
wenzelm@43245
|
633 |
(* parsetree *)
|
wenzelm@18
|
634 |
|
wenzelm@237
|
635 |
datatype parsetree =
|
wenzelm@237
|
636 |
Node of string * parsetree list |
|
wenzelm@37699
|
637 |
Tip of Lexicon.token;
|
wenzelm@237
|
638 |
|
wenzelm@43245
|
639 |
exception PARSETREE of parsetree;
|
wenzelm@43245
|
640 |
|
wenzelm@43245
|
641 |
fun pretty_parsetree parsetree =
|
wenzelm@43245
|
642 |
let
|
wenzelm@43245
|
643 |
fun pretty (Node (c, pts)) =
|
wenzelm@43245
|
644 |
[Pretty.enclose "(" ")" (Pretty.breaks (Pretty.quote (Pretty.str c) :: maps pretty pts))]
|
wenzelm@43245
|
645 |
| pretty (Tip tok) =
|
wenzelm@43245
|
646 |
if Lexicon.valued_token tok then [Pretty.str (Lexicon.str_of_token tok)] else [];
|
wenzelm@43245
|
647 |
in (case pretty parsetree of [prt] => prt | _ => raise PARSETREE parsetree) end;
|
wenzelm@43245
|
648 |
|
wenzelm@43245
|
649 |
|
wenzelm@43245
|
650 |
(* parser state *)
|
wenzelm@43086
|
651 |
|
wenzelm@18
|
652 |
type state =
|
wenzelm@38960
|
653 |
nt_tag * int * (*identification and production precedence*)
|
wenzelm@38960
|
654 |
parsetree list * (*already parsed nonterminals on rhs*)
|
wenzelm@38960
|
655 |
symb list * (*rest of rhs*)
|
wenzelm@38960
|
656 |
string * (*name of production*)
|
wenzelm@38960
|
657 |
int; (*index for previous state list*)
|
wenzelm@18
|
658 |
|
wenzelm@18
|
659 |
|
wenzelm@38961
|
660 |
(*Get all rhss with precedence >= min_prec*)
|
wenzelm@43100
|
661 |
fun get_RHS min_prec = filter (fn (_, _, prec: int) => prec >= min_prec);
|
wenzelm@18
|
662 |
|
wenzelm@38961
|
663 |
(*Get all rhss with precedence >= min_prec and < max_prec*)
|
wenzelm@38961
|
664 |
fun get_RHS' min_prec max_prec =
|
wenzelm@43100
|
665 |
filter (fn (_, _, prec: int) => prec >= min_prec andalso prec < max_prec);
|
wenzelm@237
|
666 |
|
clasohm@330
|
667 |
(*Make states using a list of rhss*)
|
wenzelm@38961
|
668 |
fun mk_states i min_prec lhs_ID rhss =
|
wenzelm@38961
|
669 |
let fun mk_state (rhs, id, prod_prec) = (lhs_ID, prod_prec, [], rhs, id, i);
|
wenzelm@38961
|
670 |
in map mk_state rhss end;
|
clasohm@697
|
671 |
|
wenzelm@15752
|
672 |
(*Add parse tree to list and eliminate duplicates
|
clasohm@330
|
673 |
saving the maximum precedence*)
|
wenzelm@43100
|
674 |
fun conc (t: parsetree list, prec: int) [] = (NONE, [(t, prec)])
|
clasohm@330
|
675 |
| conc (t, prec) ((t', prec') :: ts) =
|
clasohm@330
|
676 |
if t = t' then
|
wenzelm@38960
|
677 |
(SOME prec',
|
wenzelm@38960
|
678 |
if prec' >= prec then (t', prec') :: ts
|
wenzelm@38960
|
679 |
else (t, prec) :: ts)
|
clasohm@330
|
680 |
else
|
clasohm@330
|
681 |
let val (n, ts') = conc (t, prec) ts
|
clasohm@330
|
682 |
in (n, (t', prec') :: ts') end;
|
wenzelm@18
|
683 |
|
clasohm@330
|
684 |
(*Update entry in used*)
|
wenzelm@43109
|
685 |
fun update_trees (A, t) used =
|
wenzelm@43109
|
686 |
let
|
wenzelm@43109
|
687 |
val (i, ts) = the (Inttab.lookup used A);
|
wenzelm@43109
|
688 |
val (n, ts') = conc t ts;
|
wenzelm@43109
|
689 |
in (n, Inttab.update (A, (i, ts')) used) end;
|
wenzelm@18
|
690 |
|
clasohm@330
|
691 |
(*Replace entry in used*)
|
wenzelm@43109
|
692 |
fun update_prec (A, prec) =
|
wenzelm@43109
|
693 |
Inttab.map_entry A (fn (_, ts) => (prec, ts));
|
wenzelm@18
|
694 |
|
wenzelm@43103
|
695 |
fun getS A max_prec NONE Si =
|
wenzelm@43103
|
696 |
filter
|
wenzelm@43103
|
697 |
(fn (_, _, _, Nonterminal (B, prec) :: _, _, _) => A = B andalso prec <= max_prec
|
wenzelm@43103
|
698 |
| _ => false) Si
|
wenzelm@43103
|
699 |
| getS A max_prec (SOME min_prec) Si =
|
wenzelm@43103
|
700 |
filter
|
wenzelm@43103
|
701 |
(fn (_, _, _, Nonterminal (B, prec) :: _, _, _) =>
|
wenzelm@43103
|
702 |
A = B andalso prec > min_prec andalso prec <= max_prec
|
wenzelm@43103
|
703 |
| _ => false) Si;
|
wenzelm@18
|
704 |
|
wenzelm@38961
|
705 |
fun get_states Estate i ii A max_prec =
|
wenzelm@33325
|
706 |
filter
|
wenzelm@38961
|
707 |
(fn (_, _, _, Nonterminal (B, prec) :: _, _, _) => A = B andalso prec <= max_prec
|
wenzelm@237
|
708 |
| _ => false)
|
wenzelm@237
|
709 |
(Array.sub (Estate, ii));
|
wenzelm@18
|
710 |
|
wenzelm@18
|
711 |
|
wenzelm@43102
|
712 |
fun movedot_term c (A, j, ts, Terminal a :: sa, id, i) =
|
wenzelm@43154
|
713 |
if Lexicon.valued_token c orelse id <> ""
|
wenzelm@43154
|
714 |
then (A, j, Tip c :: ts, sa, id, i)
|
wenzelm@237
|
715 |
else (A, j, ts, sa, id, i);
|
wenzelm@18
|
716 |
|
wenzelm@43102
|
717 |
fun movedot_nonterm tt (A, j, ts, Nonterminal _ :: sa, id, i) =
|
wenzelm@43154
|
718 |
(A, j, tt @ ts, sa, id, i);
|
wenzelm@18
|
719 |
|
wenzelm@43102
|
720 |
fun movedot_lambda [] _ = []
|
wenzelm@43104
|
721 |
| movedot_lambda ((t, ki) :: ts) (state as (B, j, tss, Nonterminal (A, k) :: sa, id, i)) =
|
wenzelm@43154
|
722 |
if k <= ki then (B, j, t @ tss, sa, id, i) :: movedot_lambda ts state
|
wenzelm@43104
|
723 |
else movedot_lambda ts state;
|
wenzelm@18
|
724 |
|
wenzelm@18
|
725 |
|
wenzelm@41627
|
726 |
(*trigger value for warnings*)
|
wenzelm@41627
|
727 |
val branching_level = Config.int (Config.declare "syntax_branching_level" (fn _ => Config.Int 600));
|
wenzelm@18
|
728 |
|
clasohm@1147
|
729 |
(*get all productions of a NT and NTs chained to it which can
|
clasohm@1147
|
730 |
be started by specified token*)
|
clasohm@1147
|
731 |
fun prods_for prods chains include_none tk nts =
|
wenzelm@37699
|
732 |
let
|
wenzelm@38960
|
733 |
fun token_assoc (list, key) =
|
wenzelm@38960
|
734 |
let
|
wenzelm@38960
|
735 |
fun assoc [] result = result
|
wenzelm@38960
|
736 |
| assoc ((keyi, pi) :: pairs) result =
|
wenzelm@38960
|
737 |
if is_some keyi andalso Lexicon.matching_tokens (the keyi, key)
|
wenzelm@38960
|
738 |
orelse include_none andalso is_none keyi then
|
wenzelm@38960
|
739 |
assoc pairs (pi @ result)
|
wenzelm@38960
|
740 |
else assoc pairs result;
|
wenzelm@38960
|
741 |
in assoc list [] end;
|
clasohm@1147
|
742 |
|
wenzelm@38960
|
743 |
fun get_prods [] result = result
|
wenzelm@38960
|
744 |
| get_prods (nt :: nts) result =
|
wenzelm@38959
|
745 |
let val nt_prods = snd (Vector.sub (prods, nt));
|
wenzelm@43100
|
746 |
in get_prods nts (token_assoc (nt_prods, tk) @ result) end;
|
wenzelm@37699
|
747 |
in get_prods (connected_with chains nts nts) [] end;
|
clasohm@1147
|
748 |
|
clasohm@1147
|
749 |
|
kleing@44971
|
750 |
fun PROCESSS ctxt prods chains Estate i c states =
|
wenzelm@37699
|
751 |
let
|
wenzelm@37699
|
752 |
fun all_prods_for nt = prods_for prods chains true c [nt];
|
clasohm@330
|
753 |
|
wenzelm@37699
|
754 |
fun processS used [] (Si, Sii) = (Si, Sii)
|
wenzelm@37699
|
755 |
| processS used (S :: States) (Si, Sii) =
|
wenzelm@37699
|
756 |
(case S of
|
wenzelm@38961
|
757 |
(_, _, _, Nonterminal (nt, min_prec) :: _, _, _) =>
|
wenzelm@38960
|
758 |
let (*predictor operation*)
|
wenzelm@37699
|
759 |
val (used', new_states) =
|
wenzelm@43109
|
760 |
(case Inttab.lookup used nt of
|
wenzelm@38961
|
761 |
SOME (used_prec, l) => (*nonterminal has been processed*)
|
wenzelm@38961
|
762 |
if used_prec <= min_prec then
|
wenzelm@38960
|
763 |
(*wanted precedence has been processed*)
|
wenzelm@43102
|
764 |
(used, movedot_lambda l S)
|
wenzelm@38960
|
765 |
else (*wanted precedence hasn't been parsed yet*)
|
wenzelm@37699
|
766 |
let
|
wenzelm@37699
|
767 |
val tk_prods = all_prods_for nt;
|
wenzelm@43100
|
768 |
val States' =
|
wenzelm@43100
|
769 |
mk_states i min_prec nt (get_RHS' min_prec used_prec tk_prods);
|
wenzelm@43102
|
770 |
in (update_prec (nt, min_prec) used, movedot_lambda l S @ States') end
|
wenzelm@38960
|
771 |
| NONE => (*nonterminal is parsed for the first time*)
|
wenzelm@38960
|
772 |
let
|
wenzelm@38960
|
773 |
val tk_prods = all_prods_for nt;
|
wenzelm@38961
|
774 |
val States' = mk_states i min_prec nt (get_RHS min_prec tk_prods);
|
wenzelm@43109
|
775 |
in (Inttab.update (nt, (min_prec, [])) used, States') end);
|
wenzelm@237
|
776 |
in
|
wenzelm@37699
|
777 |
processS used' (new_states @ States) (S :: Si, Sii)
|
wenzelm@15752
|
778 |
end
|
wenzelm@38960
|
779 |
| (_, _, _, Terminal a :: _, _, _) => (*scanner operation*)
|
wenzelm@37699
|
780 |
processS used States
|
wenzelm@37699
|
781 |
(S :: Si,
|
wenzelm@43102
|
782 |
if Lexicon.matching_tokens (a, c) then movedot_term c S :: Sii else Sii)
|
wenzelm@38960
|
783 |
| (A, prec, ts, [], id, j) => (*completer operation*)
|
wenzelm@43105
|
784 |
let val tt = if id = "" then ts else [Node (id, rev ts)] in
|
wenzelm@38960
|
785 |
if j = i then (*lambda production?*)
|
wenzelm@37699
|
786 |
let
|
wenzelm@43109
|
787 |
val (prec', used') = update_trees (A, (tt, prec)) used;
|
wenzelm@43103
|
788 |
val Slist = getS A prec prec' Si;
|
wenzelm@43103
|
789 |
val States' = map (movedot_nonterm tt) Slist;
|
wenzelm@43103
|
790 |
in processS used' (States' @ States) (S :: Si, Sii) end
|
wenzelm@37699
|
791 |
else
|
wenzelm@38961
|
792 |
let val Slist = get_states Estate i j A prec
|
wenzelm@38960
|
793 |
in processS used (map (movedot_nonterm tt) Slist @ States) (S :: Si, Sii) end
|
wenzelm@37699
|
794 |
end)
|
wenzelm@43109
|
795 |
in processS Inttab.empty states ([], []) end;
|
wenzelm@18
|
796 |
|
wenzelm@18
|
797 |
|
kleing@44971
|
798 |
fun produce ctxt prods tags chains stateset i indata prev_token =
|
wenzelm@25986
|
799 |
(case Array.sub (stateset, i) of
|
wenzelm@25986
|
800 |
[] =>
|
wenzelm@25986
|
801 |
let
|
wenzelm@37699
|
802 |
val toks = if Lexicon.is_eof prev_token then indata else prev_token :: indata;
|
wenzelm@37699
|
803 |
val pos = Position.str_of (Lexicon.pos_of_token prev_token);
|
wenzelm@27801
|
804 |
in
|
wenzelm@27801
|
805 |
if null toks then error ("Inner syntax error: unexpected end of input" ^ pos)
|
wenzelm@27801
|
806 |
else error (Pretty.string_of (Pretty.block
|
wenzelm@27807
|
807 |
(Pretty.str ("Inner syntax error" ^ pos ^ " at \"") ::
|
wenzelm@37699
|
808 |
Pretty.breaks (map (Pretty.str o Lexicon.str_of_token) (#1 (split_last toks))) @
|
wenzelm@27807
|
809 |
[Pretty.str "\""])))
|
wenzelm@27801
|
810 |
end
|
wenzelm@237
|
811 |
| s =>
|
wenzelm@38960
|
812 |
(case indata of
|
wenzelm@43101
|
813 |
[] => s
|
wenzelm@43100
|
814 |
| c :: cs =>
|
wenzelm@43100
|
815 |
let
|
kleing@44971
|
816 |
val (si, sii) = PROCESSS ctxt prods chains stateset i c s;
|
wenzelm@43100
|
817 |
val _ = Array.update (stateset, i, si);
|
wenzelm@43100
|
818 |
val _ = Array.update (stateset, i + 1, sii);
|
kleing@44971
|
819 |
in produce ctxt prods tags chains stateset (i + 1) cs c end));
|
wenzelm@18
|
820 |
|
wenzelm@18
|
821 |
|
wenzelm@43100
|
822 |
fun get_trees states = map_filter (fn (_, _, [pt], _, _, _) => SOME pt | _ => NONE) states;
|
wenzelm@18
|
823 |
|
wenzelm@39077
|
824 |
fun earley ctxt prods tags chains startsymbol indata =
|
wenzelm@237
|
825 |
let
|
wenzelm@37699
|
826 |
val start_tag =
|
wenzelm@37699
|
827 |
(case Symtab.lookup tags startsymbol of
|
wenzelm@37699
|
828 |
SOME tag => tag
|
wenzelm@41207
|
829 |
| NONE => error ("Inner syntax: bad grammar root symbol " ^ quote startsymbol));
|
wenzelm@37699
|
830 |
val S0 = [(~1, 0, [], [Nonterminal (start_tag, 0), Terminal Lexicon.eof], "", 0)];
|
clasohm@330
|
831 |
val s = length indata + 1;
|
wenzelm@237
|
832 |
val Estate = Array.array (s, []);
|
wenzelm@38960
|
833 |
val _ = Array.update (Estate, 0, S0);
|
wenzelm@237
|
834 |
in
|
wenzelm@39077
|
835 |
get_trees
|
kleing@44971
|
836 |
(produce ctxt prods tags chains Estate 0 indata Lexicon.eof)
|
wenzelm@237
|
837 |
end;
|
wenzelm@18
|
838 |
|
wenzelm@18
|
839 |
|
wenzelm@39077
|
840 |
fun parse ctxt (Gram {tags, prods, chains, ...}) start toks =
|
wenzelm@27801
|
841 |
let
|
wenzelm@27801
|
842 |
val end_pos =
|
wenzelm@27801
|
843 |
(case try List.last toks of
|
wenzelm@27801
|
844 |
NONE => Position.none
|
wenzelm@37699
|
845 |
| SOME (Lexicon.Token (_, _, (_, end_pos))) => end_pos);
|
wenzelm@27801
|
846 |
val r =
|
wenzelm@39077
|
847 |
(case earley ctxt prods tags chains start (toks @ [Lexicon.mk_eof end_pos]) of
|
wenzelm@38120
|
848 |
[] => raise Fail "Inner syntax: no parse trees"
|
wenzelm@27801
|
849 |
| pts => pts);
|
wenzelm@27801
|
850 |
in r end;
|
haftmann@26678
|
851 |
|
haftmann@26678
|
852 |
|
haftmann@26678
|
853 |
fun guess_infix_lr (Gram gram) c = (*based on educated guess*)
|
haftmann@26678
|
854 |
let
|
wenzelm@38959
|
855 |
fun freeze a = map_range (curry Vector.sub a) (Vector.length a);
|
wenzelm@27801
|
856 |
val prods = maps snd (maps snd (freeze (#prods gram)));
|
wenzelm@37699
|
857 |
fun guess (SOME ([Nonterminal (_, k),
|
wenzelm@37699
|
858 |
Terminal (Lexicon.Token (Lexicon.Literal, s, _)), Nonterminal (_, l)], _, j)) =
|
haftmann@26678
|
859 |
if k = j andalso l = j + 1 then SOME (s, true, false, j)
|
haftmann@26678
|
860 |
else if k = j + 1 then if l = j then SOME (s, false, true, j)
|
haftmann@26678
|
861 |
else if l = j + 1 then SOME (s, false, false, j)
|
haftmann@26678
|
862 |
else NONE
|
haftmann@26678
|
863 |
else NONE
|
haftmann@26678
|
864 |
| guess _ = NONE;
|
haftmann@26678
|
865 |
in guess (find_first (fn (_, s, _) => s = c) prods) end;
|
wenzelm@18
|
866 |
|
wenzelm@18
|
867 |
end;
|