vast improvement in error reporting
[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         /** perform all reduction operations */
200         public void reduce() throws ParseFailed {
201             try {
202                 reducing = true;
203                 if (reducing_list==null || reducing_list.length < hash.size())
204                     reducing_list = new Phase.Node[hash.size() * 4];
205                 hash.toArray(reducing_list);
206                 int num = hash.size();
207                 for(int i=0; i<num; i++) {
208                     Node n = reducing_list[i];
209                     n.performEmptyReductions();
210                     // INVARIANT: we never "see" a node until its parent-set is complete, modulo merges
211                 }
212                 for(int i=0; i<num; i++) {
213                     Node n = reducing_list[i];
214                     reducing_list[i] = null;
215                     n.performReductions();
216                 }
217                 if (reset) {
218                     reset = false;
219                     resets++;
220                     throw new Reset();
221                 }                
222                 for(int i : expected)
223                     for(Sequence s : expected.getAll(i))
224                         if (!performed.contains(i, s)) {
225                             //System.out.println("resetting due to pos="+i+": " + s + " " + System.identityHashCode(s));
226                             resets++;
227                             throw new Reset();
228                         }
229             } catch (Reset r) {
230                 reset();
231                 reduce();
232             }
233             count = 0;
234         }
235
236         private boolean reset = false;
237         class Reset extends RuntimeException { }
238
239         /** perform all shift operations, adding promoted nodes to <tt>next</tt> */
240         public void shift(Phase next, Forest result) throws ParseFailed {
241             // this massively improves GC performance
242             if (prev!=null && parser.helpgc) {
243                 prev.hash = null;
244             }
245             this.next = next;
246             closed = true;
247             Forest res = null;
248             boolean ok = false;
249             for(Phase.Node n : hash.values()) {
250                 if (token == null && n.state.isAccepting()) {
251                     if (finalResult==null) finalResult = new Forest.Many();
252                     for(Object f : n.results())
253                         finalResult.merge((Forest)f);
254                 }
255                 if (token == null) continue;
256                 n.state.invokeShifts(token, this, result, n);
257             }
258
259             if (!good && token!=null)
260                 ParseFailed.error("unexpected character",
261                                   getLocation(),
262                                   token,
263                                   hash.values(),
264                                   getRegion(),
265                                   input,
266                                   GSS.this);
267
268             if (token==null && finalResult==null)
269                 ParseFailed.error("unexpected end of file",
270                                   getLocation(),
271                                   token,
272                                   hash.values(),
273                                   getLocation().createRegion(getLocation()),
274                                   input,
275                                   GSS.this);
276         }
277
278
279         class Waiting {
280             Node parent;
281             Forest pending;
282             State state;
283             boolean fromEmptyReduction;
284             Position reduction;
285             public Waiting(Node parent, Forest pending, State state, boolean fromEmptyReduction, Position reduction) {
286                 waits++;
287                 this.parent = parent;
288                 this.pending = pending;
289                 this.state = state;
290                 this.fromEmptyReduction = fromEmptyReduction;
291                 this.reduction = reduction;
292             }
293             public void perform() {
294                 //System.out.println("performing: " + reduction.position);
295                 newNode(parent, pending, state, fromEmptyReduction, reduction);
296             }
297         }
298        
299         // Node /////////////////////////////////////////////////////////////////////////////////
300
301         /** a node in the GSS */
302         final class Node implements Invokable<Position, Node, Node>, IntegerMappable, GraphViz.ToGraphViz {
303             public FastSet<Node> set = new FastSet<Node>();
304
305            
306             private boolean allqueued = false;
307
308             /** what state this node is in */
309             public final Parser.Table<Tok>.State<Tok> state;
310
311             /** which Phase this Node belongs to (node that Node is also a non-static inner class of Phase) */
312             public  Phase phase() { return Phase.this; }
313
314             private HashSet<Forest.Many> resultMap = new HashSet<Forest.Many>();
315             public Iterable<Forest.Many> results() { return resultMap; }
316             public FastSet<Node> parents() { return set; }
317             public boolean merge(Node parent, Forest result) {
318                 // FIXME: inefficient!
319                 for(Forest.Many f : results()) {
320                     if (f.parents.contains(parent) /* UGLY: */ && f.parents.size()==1) {
321                         f.merge(result);
322                         return true;
323                     }
324                 }
325                 Forest.Many f = new Forest.Many();
326                 f.parents.add(parent);
327                 f.merge(result);
328                 resultMap.add(f);
329                 set.add(parent, true);
330                 return false;
331             }
332
333             public void performReductions() {
334                 if (allqueued) return;
335                 allqueued = true;
336                 state.invokeReductions(token, this, this, null);
337             }
338
339             public void performReductions(Node n2) {
340                 if (!allqueued) performReductions();
341                 else            state.invokeReductions(token, this, this, n2);
342             }
343
344             public void performEmptyReductions() { state.invokeReductions(token, this, null, null); }
345             public final void invoke(Position r, Node n, Node n2) {
346                 reductions++;
347                 if (n==null || n2==null || r.pos==0) {
348                     if (r.pos==0) {
349                         if (n==null) n = this;
350                         else return;
351                     }
352                     if (n==null) return;
353                     Forest[] holder = new Forest[r.pos];
354                     if (r.pos==0) n.finish(r, r.zero(n.phase().getLocation().createRegion(n.phase().getLocation())), n.phase());
355                     else          n.reduce(r, r.pos-1,  n.phase(), null);
356                 } else {
357                     if (r.pos<=0) throw new Error("called wrong form of reduce()");
358                     int pos = r.pos-1;
359                     n.reduce(r, pos, n.phase(), n2);
360                 }
361             }
362
363             public void reduce(Position r, int pos, Phase target, Node only) {
364                 Forest[] holder = r.holder;
365                 Forest old = holder[pos];
366
367                 HashSet<Forest> rr = new HashSet<Forest>();
368                 for(Forest result : results()) rr.add(result);
369                 for(Forest result : rr)
370                     for(Node child : ((Forest.Many<?>)result).parents) {
371                         if (only != null && child!=only) continue;
372                         holder[pos] = result;
373                         if (pos==0) child.finish(r, r.rewrite(child.phase().getLocation().createRegion(target.getLocation())), target);
374                         else        child.reduce(r, pos-1, target, null);
375                     }
376
377                 holder[pos] = old;
378             }
379
380             public void finish(Position r, Forest result, Phase<Tok> target) {
381                 Parser.Table<Tok>.State<Tok> state0 = state.gotoSetNonTerminals.get(r.owner());
382                 if (result==null) throw new Error();
383                 if (state0!=null)
384                     target.newNode(this, result, state0, r.pos<=0, r);
385             }
386
387             private Node(Node parent, Forest pending, State state) {
388                 this.state = state;
389                 this.merge(parent, pending);
390                 Phase start = parent==null ? null : parent.phase();
391                 if (parent != null) parents().add(parent, true);
392                 if (Phase.this.hash.get(state, start) != null) throw new Error("severe problem!");
393                 Phase.this.hash.put(state, start, this);
394             }
395             public int toInt() { return idx; }
396             private final int idx = node_idx++;
397
398             // GraphViz //////////////////////////////////////////////////////////////////////////////
399
400             public GraphViz.Node toGraphViz(GraphViz gv) {
401                 if (gv.hasNode(this)) return gv.createNode(this);
402                 GraphViz.Node n = gv.createNode(this);
403                 n.label = ""+state.toStringx();
404                 n.shape = "rectangle";
405                 n.fill = "green";
406                 for(Forest result : results()) n.edge(result, "");
407                 for(Node parent : parents()) n.edge(parent, "");
408                 ((GraphViz.Group)phase().toGraphViz(gv)).add(n);
409                 return n;
410             }
411             public boolean isTransparent() { return false; }
412             public boolean isHidden() { return false; }
413
414         }
415         private int node_idx = 0;
416
417         public int toInt() { return pos+1; }
418         public int size() { return hash==null ? 0 : hash.size(); }
419
420         // GraphViz //////////////////////////////////////////////////////////////////////////////
421
422         public GraphViz.Node toGraphViz(GraphViz gv) {
423             if (gv.hasNode(this)) return gv.createNode(this);
424             GraphViz.Group g = gv.createGroup(this);
425             g.label = "Phase " + pos;
426             g.color = "gray";
427             g.cluster = true;
428             return g;
429         }
430         public boolean isTransparent() { return false; }
431         public boolean isHidden() { return false; }
432
433     }
434 }