questionable hack to reduce maximum stack depth
[sbp.git] / src / edu / berkeley / sbp / GSS.java
1 // Copyright 2006 all rights reserved; see LICENSE file for BSD-style license
2
3 package edu.berkeley.sbp;
4 import edu.berkeley.sbp.*;
5 import edu.berkeley.sbp.util.*;
6 import edu.berkeley.sbp.Parser.Table.*;
7 import edu.berkeley.sbp.Sequence.Position;
8 import java.io.*;
9 import java.util.*;
10 import java.lang.reflect.*;
11
12 /** implements Tomita's Graph Structured Stack */
13 class GSS {
14
15     static int count = 0;
16     static int shifts = 0;
17     static int reductions = 0;
18     int resets = 0;
19     int waits = 0;
20     
21     Input input;
22
23     public GSS(Input input) { this.input = input; }
24
25     private Phase.Node[] reducing_list = null;
26
27     // FIXME: right now, these are the performance bottleneck
28     HashMapBag<Sequence,Phase.Waiting> waiting         = new HashMapBag<Sequence,Phase.Waiting>();
29     HashMapBag<Integer,Sequence>       performed       = new HashMapBag<Integer,Sequence>();
30     HashMapBag<Integer,Sequence>       lastperformed   = new HashMapBag<Integer,Sequence>();
31     HashMapBag<Integer,Sequence>       expected        = new HashMapBag<Integer,Sequence>();
32     
33     /** FIXME */
34     Forest.Many finalResult;
35
36     /** corresponds to a positions <i>between tokens</i> the input stream; same as Tomita's U_i's */
37     class Phase<Tok> implements Invokable<State, Forest, Phase<Tok>.Node>, IntegerMappable, GraphViz.ToGraphViz, Iterable<Phase.Node> {
38
39         public Iterator<Phase.Node> iterator() { return hash.iterator(); }
40         public void invoke(State st, Forest result, Node n) {
41             shifts++;
42             good |= next.newNode(n, result, st, false);
43         }
44
45         /** the token immediately after this phase */
46         final Tok token;
47
48         private final int pos;
49
50         boolean reducing;
51         private IntPairMap<Phase.Node> hash;  /* ALLOC */
52         private boolean closed;
53         private boolean good;
54         private Phase next = null;
55         private Phase prev;
56         private Input.Location location;
57         private Input.Location nextLocation;
58         private Input.Location prevLocation;
59         
60         public final Parser parser;
61
62         private Forest forest;
63
64         public Phase(Phase prev, Parser parser, Phase previous, Tok token, Input.Location location,
65                      Input.Location nextLocation, Forest forest) throws ParseFailed {
66             this.prevLocation = prev==null ? location : prev.getLocation();
67             this.prev = prev;
68             this.forest = forest;
69             this.parser = parser;
70             this.pos = previous==null ? 0 : previous.pos+1;
71             this.token = token;
72             this.location = location;
73             this.nextLocation = nextLocation;
74             performed.clear();
75             reset();
76         }
77
78         public void reset() throws ParseFailed {
79             waiting.clear();
80             expected.clear();
81             lastperformed.clear();
82             lastperformed.addAll(performed);
83             performed.clear();
84             hash = new IntPairMap<Phase.Node>();
85             reset = false;
86             good = false;
87             closed = false;
88             reducing = false;
89             finalResult = null;
90             if (prev != null) prev.shift(this, forest);
91         }
92
93       
94         public boolean isDone() throws ParseFailed {
95             if (token != null) return false;
96             if (token==null && finalResult==null)
97                 ParseFailed.error("unexpected end of file",
98                                   getLocation(),
99                                   token,
100                                   hash.values(),
101                                   getLocation().createRegion(getLocation()),
102                                   input,
103                                   GSS.this);
104             return true;
105         }
106
107         public Input.Location getPrevLocation() { return prevLocation; }
108         public Input.Location getLocation() { return location; }
109         public Input.Region   getRegion() { return getPrevLocation().createRegion(getLocation()); }
110         public Input.Location getNextLocation() { return nextLocation; }
111
112         /** add a new node (merging with existing nodes if possible)
113          *  @param parent             the parent of the new node
114          *  @param result             the SPPF result corresponding to the new node
115          *  @param state              the state that the new node is in
116          *  @param fromEmptyReduction true iff this node is being created as a result of a reduction of length zero (see GRMLR paper)
117          *  @param start              the earliest part of the input contributing to this node (used to make merging decisions)
118          */
119         public boolean newNode(Node parent, Forest pending, State state, boolean fromEmptyReduction) {
120             Node p = hash.get(state, parent==null?null:parent.phase());
121             if (p != null)  return newNode2(p, parent, pending, state, fromEmptyReduction);
122             else            return newNode3(parent, pending, state, fromEmptyReduction);
123         }
124         public void newNode(Node parent, Forest pending, State state, boolean fromEmptyReduction, Position reduction) {
125             int pos = parent==null?0:parent.phase()==null?0:parent.phase().pos;
126             Sequence owner = reduction==null ? null : reduction.owner();
127             if (reduction!=null) {
128                 if (owner.hates!=null) {
129                     for (Sequence s : performed.getAll(pos))
130                         if (owner.hates.contains(s))
131                             return;
132                     for (Sequence s : lastperformed.getAll(pos))
133                         if (owner.hates.contains(s)) {
134                             //System.out.println("now expecting ["+pos+"] => " + s);
135                             expected.add(pos, s);
136                             return;
137                         }
138                 }
139                 if (owner.needs != null)
140                     for(Sequence s : owner.needs)
141                         if (!performed.contains(pos, s)) {
142                             waiting.add(s, new Waiting(parent, pending, state, fromEmptyReduction, reduction));
143                             return;
144                         }
145                 if (!performed.contains(pos, owner)) {
146                     performed.add(pos, owner);
147                     if (owner.hated != null)
148                         for(Sequence seq : owner.hated)
149                             if (performed.contains(pos, seq)) {
150                                 performed.remove(pos, seq);
151                                 reset = true;
152                             }
153                 }
154             }
155             newNode(parent, pending, state, fromEmptyReduction);
156             if (reduction != null) {
157                 boolean redo = true;
158                 while(redo) {
159                     redo = false;
160                     for(Waiting w : waiting.getAll(owner)) {
161                         if (w.parent==parent || (parent!=null&&w.parent!=null&&w.parent.phase()==parent.phase())) {
162                             waiting.remove(owner, w);
163                             w.perform();
164                             redo = true;
165                             break;
166                         }
167                     }
168                 }
169             }
170         }
171
172         private boolean newNode2(Node p, Node parent, Forest pending, State state, boolean fromEmptyReduction) {
173             if (p.merge(parent, pending)) return true;
174             p.parents().add(parent, true);
175             if (p!=parent && !fromEmptyReduction && reducing) p.performReductions(parent);
176             return true;
177         }
178
179         private boolean newNode3(Node parent, Forest pending, State state, boolean fromEmptyReduction) {
180             do {
181                 if (token != null && state.canShift(token)) break;
182                 if (state.isAccepting()) break;
183                 if (token==null) break;
184                 if (!state.canReduce(token)) return false;
185                 //if (count > 1) break;
186                 //if (r.numPop == 0) break;
187                 //r.reduce(pending, parent, null, Phase.this, null);
188                 //return;
189             } while(false);
190
191             Node n = new Node(parent, pending, state);  // ALLOC
192             if (reducing) {
193                 n.performEmptyReductions();
194                 if (!fromEmptyReduction) n.performReductions(parent);
195             }
196             return true;
197         }
198
199         LinkedList<Node> reductionQueue = new LinkedList<Node>();
200
201         /** perform all reduction operations */
202         public void reduce() throws ParseFailed {
203             try {
204                 reducing = true;
205                 if (reducing_list==null || reducing_list.length < hash.size())
206                     reducing_list = new Phase.Node[hash.size() * 4];
207                 hash.toArray(reducing_list);
208                 int num = hash.size();
209                 for(int i=0; i<num; i++) {
210                     Node n = reducing_list[i];
211                     n.performEmptyReductions();
212                     // INVARIANT: we never "see" a node until its parent-set is complete, modulo merges
213                 }
214                 for(int i=0; i<num; i++) {
215                     reductionQueue.add(reducing_list[i]);
216                     reducing_list[i] = null;
217                 }
218                 while(!reductionQueue.isEmpty()) {
219                     reductionQueue.remove().performReductions();
220                 }
221                 if (reset) {
222                     reset = false;
223                     resets++;
224                     throw new Reset();
225                 }                
226                 for(int i : expected)
227                     for(Sequence s : expected.getAll(i))
228                         if (!performed.contains(i, s)) {
229                             //System.out.println("resetting due to pos="+i+": " + s + " " + System.identityHashCode(s));
230                             resets++;
231                             throw new Reset();
232                         }
233             } catch (Reset r) {
234                 reset();
235                 reduce();
236             }
237             count = 0;
238         }
239
240         private boolean reset = false;
241         class Reset extends RuntimeException { }
242
243         /** perform all shift operations, adding promoted nodes to <tt>next</tt> */
244         public void shift(Phase next, Forest result) throws ParseFailed {
245             // this massively improves GC performance
246             if (prev!=null && parser.helpgc) {
247                 prev.hash = null;
248             }
249             this.next = next;
250             closed = true;
251             Forest res = null;
252             boolean ok = false;
253             for(Phase.Node n : hash.values()) {
254                 if (token == null && n.state.isAccepting()) {
255                     if (finalResult==null) finalResult = new Forest.Many();
256                     for(Object f : n.results())
257                         finalResult.merge((Forest)f);
258                 }
259                 if (token == null) continue;
260                 n.state.invokeShifts(token, this, result, n);
261             }
262
263             if (!good && token!=null)
264                 ParseFailed.error("unexpected character",
265                                   getLocation(),
266                                   token,
267                                   hash.values(),
268                                   getRegion(),
269                                   input,
270                                   GSS.this);
271
272             if (token==null && finalResult==null)
273                 ParseFailed.error("unexpected end of file",
274                                   getLocation(),
275                                   token,
276                                   hash.values(),
277                                   getLocation().createRegion(getLocation()),
278                                   input,
279                                   GSS.this);
280         }
281
282
283         class Waiting {
284             Node parent;
285             Forest pending;
286             State state;
287             boolean fromEmptyReduction;
288             Position reduction;
289             public Waiting(Node parent, Forest pending, State state, boolean fromEmptyReduction, Position reduction) {
290                 waits++;
291                 this.parent = parent;
292                 this.pending = pending;
293                 this.state = state;
294                 this.fromEmptyReduction = fromEmptyReduction;
295                 this.reduction = reduction;
296             }
297             public void perform() {
298                 //System.out.println("performing: " + reduction.position);
299                 newNode(parent, pending, state, fromEmptyReduction, reduction);
300             }
301         }
302        
303         // Node /////////////////////////////////////////////////////////////////////////////////
304
305         /** a node in the GSS */
306         final class Node implements Invokable<Position, Node, Node>, IntegerMappable, GraphViz.ToGraphViz {
307             public FastSet<Node> set = new FastSet<Node>();
308             private boolean allqueued = false;
309
310             /** what state this node is in */
311             public final Parser.Table<Tok>.State<Tok> state;
312
313             /** which Phase this Node belongs to (node that Node is also a non-static inner class of Phase) */
314             public  Phase phase() { return Phase.this; }
315
316             private HashSet<Forest.Many> resultMap = new HashSet<Forest.Many>();
317             public Iterable<Forest.Many> results() { return resultMap; }
318             public FastSet<Node> parents() { return set; }
319             public boolean merge(Node parent, Forest result) {
320                 // FIXME: inefficient!
321                 for(Forest.Many f : results()) {
322                     if (f.parents.contains(parent) /* UGLY: */ && f.parents.size()==1) {
323                         f.merge(result);
324                         return true;
325                     }
326                 }
327                 Forest.Many f = new Forest.Many();
328                 f.parents.add(parent);
329                 f.merge(result);
330                 resultMap.add(f);
331                 set.add(parent, true);
332                 return false;
333             }
334
335             public void performReductions() {
336                 if (allqueued) return;
337                 allqueued = true;
338                 state.invokeReductions(token, this, this, null);
339             }
340
341             public void performReductions(Node n2) {
342                 if (!allqueued) reductionQueue.add(this);//performReductions();
343                 else            state.invokeReductions(token, this, this, n2);
344             }
345
346             public void performEmptyReductions() { state.invokeReductions(token, this, null, null); }
347             public final void invoke(Position r, Node n, Node n2) {
348                 reductions++;
349                 if (n==null || n2==null || r.pos==0) {
350                     if (r.pos==0) {
351                         if (n==null) n = this;
352                         else return;
353                     }
354                     if (n==null) return;
355                     Forest[] holder = new Forest[r.pos];
356                     if (r.pos==0) n.finish(r, r.zero(n.phase().getLocation().createRegion(n.phase().getLocation())), n.phase());
357                     else          n.reduce(r, r.pos-1,  n.phase(), null);
358                 } else {
359                     if (r.pos<=0) throw new Error("called wrong form of reduce()");
360                     int pos = r.pos-1;
361                     n.reduce(r, pos, n.phase(), n2);
362                 }
363             }
364
365             public void reduce(Position r, int pos, Phase target, Node only) {
366                 Forest[] holder = r.holder;
367                 Forest old = holder[pos];
368
369                 HashSet<Forest> rr = new HashSet<Forest>();
370                 for(Forest result : results()) rr.add(result);
371                 for(Forest result : rr)
372                     for(Node child : ((Forest.Many<?>)result).parents) {
373                         if (only != null && child!=only) continue;
374                         holder[pos] = result;
375                         if (pos==0) child.finish(r, r.rewrite(child.phase().getLocation().createRegion(target.getLocation())), target);
376                         else        child.reduce(r, pos-1, target, null);
377                     }
378
379                 holder[pos] = old;
380             }
381
382             public void finish(Position r, Forest result, Phase<Tok> target) {
383                 Parser.Table<Tok>.State<Tok> state0 = state.gotoSetNonTerminals.get(r.owner());
384                 if (result==null) throw new Error();
385                 if (state0!=null)
386                     target.newNode(this, result, state0, r.pos<=0, r);
387             }
388
389             private Node(Node parent, Forest pending, State state) {
390                 this.state = state;
391                 this.merge(parent, pending);
392                 Phase start = parent==null ? null : parent.phase();
393                 if (parent != null) parents().add(parent, true);
394                 if (Phase.this.hash.get(state, start) != null) throw new Error("severe problem!");
395                 Phase.this.hash.put(state, start, this);
396             }
397             public int toInt() { return idx; }
398             private final int idx = node_idx++;
399
400             // GraphViz //////////////////////////////////////////////////////////////////////////////
401
402             public GraphViz.Node toGraphViz(GraphViz gv) {
403                 if (gv.hasNode(this)) return gv.createNode(this);
404                 GraphViz.Node n = gv.createNode(this);
405                 n.label = ""+state.toStringx();
406                 n.shape = "rectangle";
407                 n.fill = "green";
408                 for(Forest result : results()) n.edge(result, "");
409                 for(Node parent : parents()) n.edge(parent, "");
410                 ((GraphViz.Group)phase().toGraphViz(gv)).add(n);
411                 return n;
412             }
413             public boolean isTransparent() { return false; }
414             public boolean isHidden() { return false; }
415
416         }
417         private int node_idx = 0;
418
419         public int toInt() { return pos+1; }
420         public int size() { return hash==null ? 0 : hash.size(); }
421
422         // GraphViz //////////////////////////////////////////////////////////////////////////////
423
424         public GraphViz.Node toGraphViz(GraphViz gv) {
425             if (gv.hasNode(this)) return gv.createNode(this);
426             GraphViz.Group g = gv.createGroup(this);
427             g.label = "Phase " + pos;
428             g.color = "gray";
429             g.cluster = true;
430             return g;
431         }
432         public boolean isTransparent() { return false; }
433         public boolean isHidden() { return false; }
434
435     }
436 }