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