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