Debian Codegraph @

lib/mllpt-lib/ml-lpt/ml-antlr/compute-predict.sml

raw
1 (* compute-predict.sml
2 *
3 * COPYRIGHT (c) 2006
4 * John Reppy (http://www.cs.uchicago.edu/~jhr)
5 * Aaron Turon (http://www.cs.uchicago.edu/~adrassi)
6 * All rights reserved.
7 *
8 * Prediction tree computation; given a grammar,
9 * produce a decision tree for each decision point.
10 *)
11
12 structure ComputePredict :
13 sig
14
15 val mkPM : LLKSpec.grammar * GLA.gla -> Predict.predict_maps
16
17 end = struct
18
19 structure P = Predict
20 structure TSet = Token.Set
21 structure NMap = Nonterm.Map
22 structure S = LLKSpec
23
24 val maxK = 5
25
26 val debugPredict = false
27
28 fun debug s = if debugPredict
29 then TextIO.output(TextIO.stdErr, s ^ "\n")
30 else ()
31 fun debugs ss = debug (concat ss)
32
33 fun mapi f l = let
34 fun mapf (_, [], l) = List.rev l
35 | mapf (i, x::r, l) = mapf (i+1, r, f(i, x)::l)
36 in
37 mapf (0, l, [])
38 end
39
40 fun ntToString (S.NT{binding = S.WITHIN prod, ...}) = Prod.toString prod
41 | ntToString nt = Nonterm.name nt
42
43 (* error handling, for lookahead computation failure *)
44 fun doErr (prePath, nt, msg) = let
45 val (prefix, obj) = (case nt
46 of S.NT{binding = S.WITHIN prod, ...} =>
47 ("Error " ^ Err.span2str(Prod.span prod), concat["\n ", Prod.toString prod, "\n"])
48 | _ => ("Error", concat[" '", Nonterm.name nt, "',\n"])
49 (* end case *))
50 in
51 Err.errMsg [
52 prefix, ": lookahead computation failed for",
53 obj, msg, "\n",
54 "The conflicting token sets are:\n ",
55 String.concatWith "\n "
56 (mapi (fn (k, s) => concat[
57 "k = ", Int.toString (k+1), ": ", Token.setToString s
58 ]) prePath),
59 "\n"
60 ]
61 end
62
63 (* compute a decision tree for predicting a production for a nonterminal *)
64 fun compute(gla, nt) = let
65 fun tryToks (prePath, k) (prod, branches) = let
66 fun consNE ((set, prods), ls) =
67 if TSet.isEmpty set
68 then ls
69 else (set, prods)::ls
70 fun intersect ((set, prods), (branches, toks)) = let
71 val isct = TSet.intersection (set, toks)
72 in
73 (consNE((isct, prod::prods),
74 consNE((TSet.difference (set, isct), prods),
75 branches)),
76 TSet.difference (toks, isct))
77 end
78 val toks = GLA.lookK (gla, prod, prePath, k)
79 val (branches', toks') = foldl intersect ([], toks) branches
80 in
81 consNE((toks', [prod]), branches')
82 end
83 fun try (prePath, k, prods) = let
84 fun finBranch (set, [prod]) = (set, P.Pick prod)
85 | finBranch (set, []) = raise Fail "bug: ComputePredict"
86 | finBranch (set, prods) =
87 if List.all Prod.canTry prods then
88 (set, P.Choice (Prod.sortProds prods))
89 else if k = maxK then (
90 doErr (prePath @ [set], nt, String.concat [
91 "with a conflict for the following productions:\n ",
92 String.concatWith "\n " (map Prod.toString prods)
93 ]);
94 raise Err.Abort)
95 else (set, try (prePath @ [set], k+1, prods))
96 val branches = foldl (tryToks(prePath, k)) [] prods
97 in
98 debugs [" trying k = ", Int.toString k];
99 P.ByTok (map finBranch branches)
100 end
101 in
102 debugs [" computing prediction tree for ", Nonterm.qualName nt];
103 try ([], 1, Nonterm.prods nt)
104 before debugs [" done"]
105 end
106
107 fun unionAll sets = foldl TSet.union TSet.empty sets
108
109 (* compute a decision tree for an EBNF decision, e.g., for
110 * (A* B), the tree will predict true if A appears again and
111 * false otherwise.
112 *)
113 fun computeEBNF(gla, nt) = let
114 fun tryToks (k, prePath) = let
115 fun lookProd prod = GLA.lookK (gla, prod, prePath, k)
116 val trueToks = unionAll (map lookProd (Nonterm.prods nt))
117 val falseToks = GLA.lookKFollow (gla, nt, prePath, k)
118 val isct = TSet.intersection (trueToks, falseToks)
119 val trueOnly = TSet.difference (trueToks, isct)
120 val falseOnly = TSet.difference (falseToks, isct)
121 val choices = List.concat [
122 if TSet.isEmpty trueOnly then []
123 else [(trueOnly, Predict.Pick true)],
124 (* No longer include "false" cases *)
125 (* if TSet.isEmpty falseOnly then []
126 else [(falseOnly, Predict.Pick false)], *)
127 if TSet.isEmpty isct then []
128 else (if k <= maxK
129 then [(isct, tryToks (k+1, prePath @ [isct]))]
130 else (doErr (prePath @ [isct], nt,
131 "deciding between the subrule and "
132 ^ "the sequence following it:");
133 raise Err.Abort))]
134 in
135 Predict.ByTok choices
136 end
137 in
138 (*
139 debugs [" EBNF: ", Nonterm.qualName nt, "\n"];
140 *)
141 tryToks (1, [])
142 end
143
144 fun mkPM (grm, gla) = let
145 val LLKSpec.Grammar {sortedTops, nterms, ...} = grm
146 fun doNT (nt, prodMap) =
147 NMap.insert (prodMap, nt, compute(gla, nt))
148 fun doEBNF (nt, ebnfMap) =
149 NMap.insert (ebnfMap, nt, computeEBNF(gla, nt))
150 (* foldr ==> do innermost predictions first *)
151 val prodMapTops = foldr doNT NMap.empty (List.concat sortedTops)
152 val prodMap = foldl doNT prodMapTops (List.filter Nonterm.isSubrule nterms)
153 val ebnfMap = foldl doEBNF NMap.empty (List.filter Nonterm.isEBNF nterms)
154 fun mkFn map nt = valOf (NMap.find (map, nt))
155 in
156 Predict.PMaps {
157 prodPredict = mkFn prodMap,
158 ebnfPredict = mkFn ebnfMap
159 }
160 end
161
162 end