1 package edu.berkeley.sbp;
2 import edu.berkeley.sbp.*;
3 import edu.berkeley.sbp.*;
4 import edu.berkeley.sbp.*;
5 import edu.berkeley.sbp.util.*;
8 import java.lang.reflect.*;
10 //////////////////////////////////////////////////////////////////////////////
13 // - fix public/package/private status
16 //////////////////////////////////////////////////////////////////////////////
17 // Optimizations to add
19 // ** NOTE: not all of these are appropriate for this class -- it is
20 // simply a list of optimizations not implemented. This
21 // class is meant to remain simple and easy to understand;
22 // optimizations which obscure that do not belong here (they
23 // should go into the compiled version instead)
25 // - most of our time is now spent creating and storing Reduct instances
26 // - we should be able to perform Reduct's immediately after creating them...
29 /** implements Tomita's Graph Structured Stack */
34 /** corresponds to a positions <i>between tokens</i> the input stream; same as Tomita's U_i's */
37 /** the token immediately after this phase */
38 public final Token token;
40 /** currently this is necessary only for the code() hack -- it doesn't actually correspond to the input */
41 private final int pos;
44 public Forest.Ref finalResult = null;
46 /** all reductions (pending and completed) */
47 private HashSet<Phase.Reduct> reductions = new HashSet<Phase.Reduct>(); /* ALLOC */
49 /** all nodes, keyed by the value returned by code() */
50 private HashMap<Long,Phase.Node> hash = new HashMap<Long,Phase.Node>(); /* ALLOC */
52 /** the number of pending reductions */
53 private int pendingReductions = 0;
54 private int totalReductions = 0;
55 private HashSet<Reduct> pendingReduct = new HashSet<Reduct>();
57 /** the number of nodes in this phase */
58 private int numNodes = 0;
60 boolean closed = false;
62 public Phase(Phase previous, Token token) {
63 this.pos = previous==null ? 0 : previous.pos+1;
67 public boolean isDone() { return token == null; }
69 private String error = "generic syntax error";
70 public void checkFailure() throws Parser.Failed {
72 throw new Parser.Failed(error, getLocation());
75 public Token.Location getLocation() { return token==null ? null : token.getLocation(); }
77 /** add a new node (merging with existing nodes if possible)
78 * @param parent the parent of the new node
79 * @param result the SPPF result corresponding to the new node
80 * @param state the state that the new node is in
81 * @param fromEmptyReduction true iff this node is being created as a result of a reduction of length zero (see GRMLR paper)
82 * @param start the earliest part of the input contributing to this node (used to make merging decisions)
84 public void newNode(Node parent, Forest pending, Parser.Table.State state, boolean fromEmptyReduction, Phase start) {
85 Node p = hash.get(code(state, start));
86 if (p != null) newNode2(p, parent, pending, state, fromEmptyReduction, start);
87 else newNode3(parent, pending, state, fromEmptyReduction, start);
89 private void newNode2(Node p, Node parent, Forest pending, Parser.Table.State state, boolean fromEmptyReduction, Phase start) {
90 p.holder.merge(pending);
91 if (p.parents.contains(parent)) return;
92 p.parents.add(parent, true);
93 if (p!=parent && !fromEmptyReduction) p.queueReductions(parent);
95 private void newNode3(Node parent, Forest pending, Parser.Table.State state, boolean fromEmptyReduction, Phase start) {
97 if (token != null && state.canShift(token)) break;
98 if (state.isAccepting()) break;
99 if (token==null) break;
101 Parser.Table.Reduction r = null;
102 for(Parser.Table.Reduction red : token==null ? state.getEofReductions() : state.getReductions(token)) { r = red; count++; }
103 //if (count==0) return; -- BEWARE! this optimization is suspected to cause really nasty heisenbugs
104 if (count > 1) break;
105 //if (r.numPop == 0) break;
106 //r.reduce(pending, parent, null, Phase.this, null);
110 Node n = new Node(parent, pending, state, start); // ALLOC
111 n.queueEmptyReductions();
112 if (!fromEmptyReduction) n.queueReductions();
115 /** perform all reduction operations */
116 public void reduce() {
117 for(Phase.Node n : hash.values()) {
118 n.queueEmptyReductions();
121 while(pendingReduct.size()>0)
122 pendingReduct.iterator().next().go();
125 /** perform all shift operations, adding promoted nodes to <tt>next</tt> */
126 public void shift(Phase next) {
130 for(Phase.Node n : hash.values()) {
132 if (token == null && n.state.isAccepting()) {
134 if (finalResult==null) finalResult = new Forest.Ref();
135 finalResult.merge(n.holder);
137 if (!n.holder.valid()) continue;
138 if (token == null) continue;
139 for(Parser.Table.State st : n.state.getShifts(token)) {
140 if (res == null) res = Forest.create(token.getLocation(), token.result(), null, null, false, false);
141 next.newNode(n, res, st, true, this);
146 if (!ok && token != null) {
147 StringBuffer error = new StringBuffer();
148 error.append("error: unable to shift token \"" + token + "\"\n");
149 error.append(" before: " +pendingReductions+ "\n");
150 error.append(" before: " +totalReductions+ "\n");
151 for(Phase.Node n : hash.values()) {
153 n.queueEmptyReductions();
155 error.append(" after: " +pendingReductions+ "\n");
156 error.append(" candidate states:\n");
157 for(Phase.Node n : hash.values()) {
158 //for(Sequence.Position p : n.state) error.append(" " + p + "\n");
159 //error.append(" --\n");
160 for(Parser.Table.Reduction r : n.state.getReductions(token)) error.append(" " + r + "\n");
161 //error.append(" ==\n");
163 next.error = error.toString();
166 // this massively improves GC performance
172 // GSS Nodes //////////////////////////////////////////////////////////////////////////////
174 private HashMap<Parser.Table.Reduction,Forest> pcache = new HashMap<Parser.Table.Reduction,Forest>();
175 /** a node in the GSS */
178 private Forest.Ref holder = null;
179 private HashMap<Parser.Table.Reduction,Forest> cache = null;
181 public HashMap<Parser.Table.Reduction,Forest> cache() { return cache==null ? (cache = new HashMap<Parser.Table.Reduction,Forest>()) : cache; }
182 public Forest.Ref holder() { return holder==null ? (holder = new Forest.Ref()) : holder; }
183 public Forest pending() { return Phase.this.closed ? holder().resolve() : holder; }
184 public FastSet<Node> parents() { return parents; }
186 /** which Phase this Node belongs to (node that Node is also a non-static inner class of Phase) */
187 public final Phase phase = Phase.this;
189 /** what state this node is in */
190 public final Parser.Table.State state;
192 /** the set of nodes to which there is an edge starting at this node */
193 public final FastSet<Node> parents = new FastSet<Node>(); /* ALLOC */
196 public void queueReductions() {
197 for(Node n2 : parents)
202 public void queueReductions(Node n2) {
203 new Reduct(this, n2, null);
204 for(Parser.Table.Reduction r : token==null ? state.getEofReductions() : state.getReductions(token)) {
206 // currently we have this weird problem where we
207 // have to do an individual reduct for each child
208 // when the reduction length is one (ie the
209 // children wind up being children of the newly
210 // created node rather than part of the popped
213 if (r.numPop == 1) new Reduct(this, n2, r);
219 public void queueEmptyReductions() {
220 for(Parser.Table.Reduction r : token==null ? state.getEofReductions() : state.getReductions(token)) {
222 new Reduct(this, null, r); /* ALLOC */
226 private Node(Node parent, Forest pending, Parser.Table.State state, Phase start) {
228 if (pending != null) this.holder().merge(pending);
229 if (parent != null) parents.add(parent, true);
230 if (Phase.this.hash.get(code(state, start)) != null) throw new Error("severe problem!");
231 Phase.this.hash.put(code(state, start), this);
232 Phase.this.numNodes++;
233 if (parent==null) holder().valid = true; // hack to make sure that the "base" node is always considered valid
238 // Forest / Completed Reductions //////////////////////////////////////////////////////////////////////////////
240 /** a pending or completed reduction */
243 /** the node from which the reduction should begin */
244 public Node n = null;
246 /** the node on the other end of the edge to be reduced along (either: null, the second node of the reduction,
247 * or the parent of the result of a length-one reduction)
249 public Node n2 = null;
251 /** true iff the reduction has already been performed */
252 private boolean done = false;
254 /** the reduction to be applied */
255 public Parser.Table.Reduction r;
257 public Tree<String> result = null;
259 public Reduct(Node n, Node n2, Parser.Table.Reduction r) {
263 if (reductions.contains(this)) { done = true; return; }
264 reductions.add(this);
265 pendingReduct.add(this);
269 /** perform the reduction */
273 pendingReduct.remove(this);
276 // FIXME: explain this
278 for(Parser.Table.Reduction r : token==null ? n.state.getEofReductions() : n.state.getReductions(token)) {
279 if (r.numPop <= 1) continue;
280 r.reduce(n, n2, Phase.this, null);
282 } else if (r.numPop<=1) {
284 // The problem here is that a "reduction of length 0/1"
285 // performed twice with different values of n2 needs
286 // to only create a *single* new result, but must add
287 // multiple parents to the node holding that result.
288 // The current reducer doesn't differentiate between
289 // the next node of an n-pop reduction and the
290 // ultimate parent of the last pop, so we need to
291 // cache instances here as a way of avoiding
294 Forest ret = (r.numPop==0 ? pcache : n.cache()).get(r);
295 if (ret != null) r.reduce(n, n2, n.phase, ret);
296 else (r.numPop==0 ? pcache : n.cache()).put(r, r.reduce(n, n2, n.phase, null));
299 r.reduce(n, n2, Phase.this, null);
303 // FIXME: this is a PITA
304 public int hashCode() { return n.hashCode() ^ (r==null ? 0 : r.hashCode()) ^ (n2==null ? 0 : n2.hashCode()); }
305 public boolean equals(Object o) {
306 if (o==null) return false;
307 if (o==this) return true;
308 if (!(o instanceof Reduct)) return false;
309 Reduct other = (Reduct)o;
310 return equal(r, other.r) && equal(n, other.n) && equal(n2, other.n2);
317 private static boolean equal(Object a, Object b) {
318 if (a==null && b==null) return true;
319 if (a==null || b==null) return false;
323 /** this is something of a hack right now */
324 private static long code(Parser.Table.State state, Phase start) {
325 return (((long)state.idx) << 32) | (start==null ? 0 : start.pos);