eliminate Walk.Last
[sbp.git] / src / edu / berkeley / sbp / GSS.java
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.*;
6 import java.io.*;
7 import java.util.*;
8 import java.lang.reflect.*;
9
10 //////////////////////////////////////////////////////////////////////////////
11 // TODO:
12 //
13 //  - fix public/package/private status
14 //
15
16 //////////////////////////////////////////////////////////////////////////////
17 // Optimizations to add
18 //
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)
24 //
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...
27 //
28
29 /** implements Tomita's Graph Structured Stack */
30 class GSS {
31
32     public GSS() { }
33
34     /** corresponds to a positions <i>between tokens</i> the input stream; same as Tomita's U_i's */
35     public class Phase {
36
37         /** the token immediately after this phase */
38         public  final Token token;
39
40         /** currently this is necessary only for the code() hack -- it doesn't actually correspond to the input */
41         private final int pos;
42
43         /** FIXME */
44         public  Forest.Ref finalResult = null;
45
46         /** all reductions (pending and completed) */
47         private HashSet<Phase.Reduct> reductions = new HashSet<Phase.Reduct>();     /* ALLOC */
48
49         /** all nodes, keyed by the value returned by code() */
50         private HashMap<Long,Phase.Node> hash    = new HashMap<Long,Phase.Node>();  /* ALLOC */
51
52         /** the number of pending reductions */
53         private int pendingReductions = 0;
54         private int totalReductions = 0;
55         private HashSet<Reduct> pendingReduct = new HashSet<Reduct>();
56
57         /** the number of nodes in this phase */
58         private int numNodes = 0;
59
60         boolean closed = false;
61
62         private Token.Location location;
63         public Phase(Phase previous, Token token, Token.Location location) {
64             this.pos = previous==null ? 0 : previous.pos+1;
65             this.token = token;
66             this.location = location;
67         }
68
69         public boolean isDone() { return token == null; }
70
71         private String error = "generic syntax error";
72         public void checkFailure() throws Parser.Failed {
73             if (numNodes <= 0)
74                 throw new Parser.Failed(error, getLocation());
75         }
76
77         public Token.Location getLocation() { return location; }
78
79         /** add a new node (merging with existing nodes if possible)
80          *  @param parent             the parent of the new node
81          *  @param result             the SPPF result corresponding to the new node
82          *  @param state              the state that the new node is in
83          *  @param fromEmptyReduction true iff this node is being created as a result of a reduction of length zero (see GRMLR paper)
84          *  @param start              the earliest part of the input contributing to this node (used to make merging decisions)
85          */
86         public void newNode(Node parent, Forest pending, Parser.Table.State state, boolean fromEmptyReduction, Phase start) {
87             Node p = hash.get(code(state, start));
88             if (p != null)  newNode2(p, parent, pending, state, fromEmptyReduction, start);
89             else            newNode3(parent, pending, state, fromEmptyReduction, start);
90         }
91         private void newNode2(Node p, Node parent, Forest pending, Parser.Table.State state, boolean fromEmptyReduction, Phase start) {
92             p.holder.merge(pending);
93             if (p.parents.contains(parent)) return;
94             p.parents.add(parent, true);
95             if (p!=parent && !fromEmptyReduction) p.queueReductions(parent);
96         }
97         private void newNode3(Node parent, Forest pending, Parser.Table.State state, boolean fromEmptyReduction, Phase start) {
98             do {
99                 if (token != null && state.canShift(token)) break;
100                 if (state.isAccepting()) break;
101                 if (token==null) break;
102                 int count = 0;
103                 Parser.Table.Reduction r = null;
104                 for(Parser.Table.Reduction red : token==null ? state.getEofReductions() : state.getReductions(token)) { r = red; count++; }
105                 //if (count==0) return;     -- BEWARE! this optimization is suspected to cause really nasty heisenbugs
106                 if (count > 1) break;
107                 //if (r.numPop == 0) break;
108                 //r.reduce(pending, parent, null, Phase.this, null);
109                 //return;
110             } while(false);
111
112             Node n = new Node(parent, pending, state, start);  // ALLOC
113             n.queueEmptyReductions();
114             if (!fromEmptyReduction) n.queueReductions();
115         }
116
117         /** perform all reduction operations */
118         public void reduce() {
119             for(Phase.Node n : hash.values()) {
120                 n.queueEmptyReductions();
121                 n.queueReductions();
122             }
123             while(pendingReduct.size()>0)
124                 pendingReduct.iterator().next().go();
125         }
126
127         /** perform all shift operations, adding promoted nodes to <tt>next</tt> */
128         public void shift(Phase next, Forest result) {
129             closed = true;
130             Forest res = null;
131             boolean ok = false;
132             for(Phase.Node n : hash.values()) {
133                 n.holder.resolve();
134                 if (token == null && n.state.isAccepting()) {
135                     ok = true;
136                     if (finalResult==null) finalResult = new Forest.Ref();
137                     finalResult.merge(n.holder);
138                 }
139                 if (!n.holder.valid()) continue;
140                 if (token == null) continue;
141                 for(Parser.Table.State st : n.state.getShifts(token)) {
142                     if (res == null) res = result;
143                     next.newNode(n, res, st, true, this);
144                     ok = true;
145                 }
146             }
147
148             if (!ok && token != null) {
149                 StringBuffer error = new StringBuffer();
150                 error.append("error: unable to shift token \"" + token + "\"\n");
151                 error.append("  before: " +pendingReductions+ "\n");
152                 error.append("  before: " +totalReductions+ "\n");
153                 for(Phase.Node n : hash.values()) {
154                     n.queueReductions();
155                     n.queueEmptyReductions();
156                 }
157                 error.append("  after: " +pendingReductions+ "\n");
158                 error.append("  candidate states:\n");
159                 for(Phase.Node n : hash.values()) {
160                     //for(Sequence.Position p : n.state) error.append("        " + p + "\n");
161                     //error.append("        --\n");
162                     for(Parser.Table.Reduction r : n.state.getReductions(token)) error.append("        " + r + "\n");
163                     //error.append("        ==\n");
164                 }
165                 next.error = error.toString();
166             }
167
168             // this massively improves GC performance
169             reductions = null;
170             hash = null;
171         }
172
173        
174         // GSS Nodes //////////////////////////////////////////////////////////////////////////////
175
176         private HashMap<Parser.Table.Reduction,Forest> pcache = new HashMap<Parser.Table.Reduction,Forest>();
177         /** a node in the GSS */
178         public class Node {
179
180             private Forest.Ref holder = null;
181             private HashMap<Parser.Table.Reduction,Forest> cache = null;
182
183             public  HashMap<Parser.Table.Reduction,Forest> cache() { return cache==null ? (cache = new HashMap<Parser.Table.Reduction,Forest>()) : cache; }
184             public  Forest.Ref holder() { return holder==null ? (holder = new Forest.Ref()) : holder; }
185             public  Forest pending() { return Phase.this.closed ? holder().resolve() : holder; }
186             public  FastSet<Node> parents() { return parents; }
187
188             /** which Phase this Node belongs to (node that Node is also a non-static inner class of Phase) */
189             public final Phase phase = Phase.this;
190
191             /** what state this node is in */
192             public final Parser.Table.State state;
193
194             /** the set of nodes to which there is an edge starting at this node */
195             public final FastSet<Node> parents = new FastSet<Node>();  /* ALLOC */
196
197             /** FIXME */
198             public void queueReductions() {
199                 for(Node n2 : parents)
200                     queueReductions(n2);
201             }
202
203             /** FIXME */
204             public void queueReductions(Node n2) {
205                 new Reduct(this, n2, null);
206                 for(Parser.Table.Reduction r : token==null ? state.getEofReductions() : state.getReductions(token)) {
207
208                     // currently we have this weird problem where we
209                     // have to do an individual reduct for each child
210                     // when the reduction length is one (ie the
211                     // children wind up being children of the newly
212                     // created node rather than part of the popped
213                     // sequence
214
215                     if (r.numPop == 1) new Reduct(this, n2, r);
216                 }
217             }
218
219
220             /** FIXME */
221             public void queueEmptyReductions() {
222                 for(Parser.Table.Reduction r : token==null ? state.getEofReductions() : state.getReductions(token)) {
223                     if (r.numPop==0)
224                         new Reduct(this, null, r);   /* ALLOC */
225                 }
226             }
227
228             private Node(Node parent, Forest pending, Parser.Table.State state, Phase start) {
229                 this.state = state;
230                 if (pending != null) this.holder().merge(pending);
231                 if (parent != null) parents.add(parent, true);
232                 if (Phase.this.hash.get(code(state, start)) != null) throw new Error("severe problem!");
233                 Phase.this.hash.put(code(state, start), this);
234                 Phase.this.numNodes++;
235                 if (parent==null) holder().valid = true; // hack to make sure that the "base" node is always considered valid
236             }
237         }
238
239
240         // Forest / Completed Reductions //////////////////////////////////////////////////////////////////////////////
241
242         /** a pending or completed reduction */
243         class Reduct {
244             
245             /** the node from which the reduction should begin */
246             public Node n = null;
247
248             /** the node on the other end of the edge to be reduced along (either: null, the second node of the reduction,
249              *  or the parent of the result of a length-one reduction)
250              */
251             public Node n2 = null;
252
253             /** true iff the reduction has already been performed */
254             private boolean done = false;
255
256             /** the reduction to be applied */
257             public Parser.Table.Reduction r;
258
259             public Tree<String> result = null;
260
261             public Reduct(Node n, Node n2, Parser.Table.Reduction r) {
262                 this.n = n;
263                 this.n2 = n2;
264                 this.r = r;
265                 if (reductions.contains(this)) { done = true; return; }
266                 reductions.add(this);
267                 pendingReduct.add(this);
268                 pendingReductions++;
269             }
270
271             /** perform the reduction */
272             public void go() {
273                 if (done) return;
274                 done = true;
275                 pendingReduct.remove(this);
276                 pendingReductions--;
277
278                 // FIXME: explain this
279                 if (r==null) {
280                     for(Parser.Table.Reduction r : token==null ? n.state.getEofReductions() : n.state.getReductions(token)) {
281                         if (r.numPop <= 1) continue;
282                         r.reduce(n, n2, Phase.this, null);
283                     }
284                 } else if (r.numPop<=1) {
285                     // UGLY HACK
286                     // The problem here is that a "reduction of length 0/1"
287                     // performed twice with different values of n2 needs
288                     // to only create a *single* new result, but must add
289                     // multiple parents to the node holding that result.
290                     // The current reducer doesn't differentiate between
291                     // the next node of an n-pop reduction and the
292                     // ultimate parent of the last pop, so we need to
293                     // cache instances here as a way of avoiding
294                     // recreating them.
295                     
296                     Forest ret = (r.numPop==0 ? pcache : n.cache()).get(r);
297                     if (ret != null) r.reduce(n, n2, n.phase, ret);
298                     else (r.numPop==0 ? pcache : n.cache()).put(r, r.reduce(n, n2, n.phase, null));
299                     
300                 } else {
301                     r.reduce(n, n2, Phase.this, null);
302                 }
303             }
304
305             // FIXME: this is a PITA
306             public int hashCode() { return n.hashCode() ^ (r==null ? 0 : r.hashCode()) ^ (n2==null ? 0 : n2.hashCode()); }
307             public boolean equals(Object o) {
308                 if (o==null) return false;
309                 if (o==this) return true;
310                 if (!(o instanceof Reduct)) return false;
311                 Reduct other = (Reduct)o;
312                 return equal(r, other.r) && equal(n, other.n) && equal(n2, other.n2);
313             }
314         }
315
316     }
317
318     /** helper method */
319     private static boolean equal(Object a, Object b) {
320         if (a==null && b==null) return true;
321         if (a==null || b==null) return false;
322         return a.equals(b);
323     }
324
325     /** this is something of a hack right now */
326     private static long code(Parser.Table.State state, Phase start) {
327         return (((long)state.idx) << 32) | (start==null ? 0 : start.pos);
328     }
329
330 }