b36b293094276d048676d096e3258779394df973
[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                 throw new ParseFailed(ParseFailed.error(("unexpected end of file\n"),
98                                                         token, hash.values()),
99                                       getLocation().createRegion(getLocation()), input);
100             return true;
101         }
102
103         public Input.Location getPrevLocation() { return prevLocation; }
104         public Input.Location getLocation() { return location; }
105         public Input.Region   getRegion() { return getPrevLocation().createRegion(getLocation()); }
106         public Input.Location getNextLocation() { return nextLocation; }
107
108         /** add a new node (merging with existing nodes if possible)
109          *  @param parent             the parent of the new node
110          *  @param result             the SPPF result corresponding to the new node
111          *  @param state              the state that the new node is in
112          *  @param fromEmptyReduction true iff this node is being created as a result of a reduction of length zero (see GRMLR paper)
113          *  @param start              the earliest part of the input contributing to this node (used to make merging decisions)
114          */
115         public boolean newNode(Node parent, Forest pending, State state, boolean fromEmptyReduction) {
116             Node p = hash.get(state, parent==null?null:parent.phase());
117             if (p != null)  return newNode2(p, parent, pending, state, fromEmptyReduction);
118             else            return newNode3(parent, pending, state, fromEmptyReduction);
119         }
120         public void newNode(Node parent, Forest pending, State state, boolean fromEmptyReduction, Position reduction) {
121             int pos = parent==null?0:parent.phase()==null?0:parent.phase().pos;
122             Sequence owner = reduction==null ? null : reduction.owner();
123             if (reduction!=null) {
124                 if (owner.hates!=null) {
125                     for (Sequence s : performed.getAll(pos))
126                         if (owner.hates.contains(s))
127                             return;
128                     for (Sequence s : lastperformed.getAll(pos))
129                         if (owner.hates.contains(s)) {
130                             //System.out.println("now expecting ["+pos+"] => " + s);
131                             expected.add(pos, s);
132                             return;
133                         }
134                 }
135                 if (owner.needs != null)
136                     for(Sequence s : owner.needs)
137                         if (!performed.contains(pos, s)) {
138                             waiting.add(s, new Waiting(parent, pending, state, fromEmptyReduction, reduction));
139                             return;
140                         }
141                 if (!performed.contains(pos, owner)) {
142                     performed.add(pos, owner);
143                     if (owner.hated != null)
144                         for(Sequence seq : owner.hated)
145                             if (performed.contains(pos, seq)) {
146                                 performed.remove(pos, seq);
147                                 reset = true;
148                             }
149                 }
150             }
151             newNode(parent, pending, state, fromEmptyReduction);
152             if (reduction != null) {
153                 boolean redo = true;
154                 while(redo) {
155                     redo = false;
156                     for(Waiting w : waiting.getAll(owner)) {
157                         if (w.parent==parent || (parent!=null&&w.parent!=null&&w.parent.phase()==parent.phase())) {
158                             waiting.remove(owner, w);
159                             w.perform();
160                             redo = true;
161                             break;
162                         }
163                     }
164                 }
165             }
166         }
167
168         private boolean newNode2(Node p, Node parent, Forest pending, State state, boolean fromEmptyReduction) {
169             if (p.merge(parent, pending)) return true;
170             p.parents().add(parent, true);
171             if (p!=parent && !fromEmptyReduction && reducing) p.performReductions(parent);
172             return true;
173         }
174
175         private boolean newNode3(Node parent, Forest pending, State state, boolean fromEmptyReduction) {
176             do {
177                 if (token != null && state.canShift(token)) break;
178                 if (state.isAccepting()) break;
179                 if (token==null) break;
180                 if (!state.canReduce(token)) return false;
181                 //if (count > 1) break;
182                 //if (r.numPop == 0) break;
183                 //r.reduce(pending, parent, null, Phase.this, null);
184                 //return;
185             } while(false);
186
187             Node n = new Node(parent, pending, state);  // ALLOC
188             if (reducing) {
189                 n.performEmptyReductions();
190                 if (!fromEmptyReduction) n.performReductions(parent);
191             }
192             return true;
193         }
194
195         /** perform all reduction operations */
196         public void reduce() throws ParseFailed {
197             try {
198                 reducing = true;
199                 if (reducing_list==null || reducing_list.length < hash.size())
200                     reducing_list = new Phase.Node[hash.size() * 4];
201                 hash.toArray(reducing_list);
202                 int num = hash.size();
203                 for(int i=0; i<num; i++) {
204                     Node n = reducing_list[i];
205                     n.performEmptyReductions();
206                     // INVARIANT: we never "see" a node until its parent-set is complete, modulo merges
207                 }
208                 for(int i=0; i<num; i++) {
209                     Node n = reducing_list[i];
210                     reducing_list[i] = null;
211                     n.performReductions();
212                 }
213                 if (reset) {
214                     reset = false;
215                     resets++;
216                     throw new Reset();
217                 }                
218                 for(int i : expected)
219                     for(Sequence s : expected.getAll(i))
220                         if (!performed.contains(i, s)) {
221                             //System.out.println("resetting due to pos="+i+": " + s + " " + System.identityHashCode(s));
222                             resets++;
223                             throw new Reset();
224                         }
225             } catch (Reset r) {
226                 reset();
227                 reduce();
228             }
229             count = 0;
230         }
231
232         private boolean reset = false;
233         class Reset extends RuntimeException { }
234
235         /** perform all shift operations, adding promoted nodes to <tt>next</tt> */
236         public void shift(Phase next, Forest result) throws ParseFailed {
237             // this massively improves GC performance
238             if (prev!=null && parser.helpgc) {
239                 prev.hash = null;
240             }
241             this.next = next;
242             closed = true;
243             Forest res = null;
244             boolean ok = false;
245             for(Phase.Node n : hash.values()) {
246                 if (token == null && n.state.isAccepting()) {
247                     if (finalResult==null) finalResult = new Forest.Many();
248                     for(Object f : n.results())
249                         finalResult.merge((Forest)f);
250                 }
251                 if (token == null) continue;
252                 n.state.invokeShifts(token, this, result, n);
253             }
254
255             if (!good && token!=null)
256                 throw new ParseFailed(ParseFailed.error(("unexpected character ")+" \'"+
257                                                         ANSI.purple(StringUtil.escapify(token+"", "\\\'\r\n"))+
258                                                         "\' encountered at "+
259                                                         ANSI.green(next.getRegion())+"\n", token, hash.values()),
260                                         next.getRegion(), input);
261             if (token==null && finalResult==null)
262                 throw new ParseFailed(ParseFailed.error(("unexpected end of file at "+getLocation()+"\n"), token, hash.values()),
263                                       getLocation().createRegion(getLocation()), input);
264         }
265
266
267         class Waiting {
268             Node parent;
269             Forest pending;
270             State state;
271             boolean fromEmptyReduction;
272             Position reduction;
273             public Waiting(Node parent, Forest pending, State state, boolean fromEmptyReduction, Position reduction) {
274                 waits++;
275                 this.parent = parent;
276                 this.pending = pending;
277                 this.state = state;
278                 this.fromEmptyReduction = fromEmptyReduction;
279                 this.reduction = reduction;
280             }
281             public void perform() {
282                 //System.out.println("performing: " + reduction.position);
283                 newNode(parent, pending, state, fromEmptyReduction, reduction);
284             }
285         }
286        
287         // Node /////////////////////////////////////////////////////////////////////////////////
288
289         /** a node in the GSS */
290         final class Node implements Invokable<Position, Node, Node>, IntegerMappable, GraphViz.ToGraphViz {
291             public FastSet<Node> set = new FastSet<Node>();
292
293            
294             private boolean allqueued = false;
295
296             /** what state this node is in */
297             public final Parser.Table<Tok>.State<Tok> state;
298
299             /** which Phase this Node belongs to (node that Node is also a non-static inner class of Phase) */
300             public  Phase phase() { return Phase.this; }
301
302             private HashSet<Forest.Many> resultMap = new HashSet<Forest.Many>();
303             public Iterable<Forest.Many> results() { return resultMap; }
304             public FastSet<Node> parents() { return set; }
305             public boolean merge(Node parent, Forest result) {
306                 // FIXME: inefficient!
307                 for(Forest.Many f : results()) {
308                     if (f.parents.contains(parent) /* UGLY: */ && f.parents.size()==1) {
309                         f.merge(result);
310                         return true;
311                     }
312                 }
313                 Forest.Many f = new Forest.Many();
314                 f.parents.add(parent);
315                 f.merge(result);
316                 resultMap.add(f);
317                 set.add(parent, true);
318                 return false;
319             }
320
321             public void performReductions() {
322                 if (allqueued) return;
323                 allqueued = true;
324                 state.invokeReductions(token, this, this, null);
325             }
326
327             public void performReductions(Node n2) {
328                 if (!allqueued) performReductions();
329                 else            state.invokeReductions(token, this, this, n2);
330             }
331
332             public void performEmptyReductions() { state.invokeReductions(token, this, null, null); }
333             public final void invoke(Position r, Node n, Node n2) {
334                 reductions++;
335                 if (n==null || n2==null || r.pos==0) {
336                     if (r.pos==0) {
337                         if (n==null) n = this;
338                         else return;
339                     }
340                     if (n==null) return;
341                     Forest[] holder = new Forest[r.pos];
342                     if (r.pos==0) n.finish(r, r.zero(n.phase().getLocation().createRegion(n.phase().getLocation())), n.phase());
343                     else          n.reduce(r, r.pos-1,  n.phase(), null);
344                 } else {
345                     if (r.pos<=0) throw new Error("called wrong form of reduce()");
346                     int pos = r.pos-1;
347                     n.reduce(r, pos, n.phase(), n2);
348                 }
349             }
350
351             public void reduce(Position r, int pos, Phase target, Node only) {
352                 Forest[] holder = r.holder;
353                 Forest old = holder[pos];
354
355                 HashSet<Forest> rr = new HashSet<Forest>();
356                 for(Forest result : results()) rr.add(result);
357                 for(Forest result : rr)
358                     for(Node child : ((Forest.Many<?>)result).parents) {
359                         if (only != null && child!=only) continue;
360                         holder[pos] = result;
361                         if (pos==0) child.finish(r, r.rewrite(child.phase().getLocation().createRegion(target.getLocation())), target);
362                         else        child.reduce(r, pos-1, target, null);
363                     }
364
365                 holder[pos] = old;
366             }
367
368             public void finish(Position r, Forest result, Phase<Tok> target) {
369                 Parser.Table<Tok>.State<Tok> state0 = state.gotoSetNonTerminals.get(r.owner());
370                 if (result==null) throw new Error();
371                 if (state0!=null)
372                     target.newNode(this, result, state0, r.pos<=0, r);
373             }
374
375             private Node(Node parent, Forest pending, State state) {
376                 this.state = state;
377                 this.merge(parent, pending);
378                 Phase start = parent==null ? null : parent.phase();
379                 if (parent != null) parents().add(parent, true);
380                 if (Phase.this.hash.get(state, start) != null) throw new Error("severe problem!");
381                 Phase.this.hash.put(state, start, this);
382             }
383             public int toInt() { return idx; }
384             private final int idx = node_idx++;
385
386             // GraphViz //////////////////////////////////////////////////////////////////////////////
387
388             public GraphViz.Node toGraphViz(GraphViz gv) {
389                 if (gv.hasNode(this)) return gv.createNode(this);
390                 GraphViz.Node n = gv.createNode(this);
391                 n.label = ""+state.toStringx();
392                 n.shape = "rectangle";
393                 n.fill = "green";
394                 for(Forest result : results()) n.edge(result, "");
395                 for(Node parent : parents()) n.edge(parent, "");
396                 ((GraphViz.Group)phase().toGraphViz(gv)).add(n);
397                 return n;
398             }
399             public boolean isTransparent() { return false; }
400             public boolean isHidden() { return false; }
401
402         }
403         private int node_idx = 0;
404
405         public int toInt() { return pos+1; }
406         public int size() { return hash==null ? 0 : hash.size(); }
407
408         // GraphViz //////////////////////////////////////////////////////////////////////////////
409
410         public GraphViz.Node toGraphViz(GraphViz gv) {
411             if (gv.hasNode(this)) return gv.createNode(this);
412             GraphViz.Group g = gv.createGroup(this);
413             g.label = "Phase " + pos;
414             g.color = "gray";
415             g.cluster = true;
416             return g;
417         }
418         public boolean isTransparent() { return false; }
419         public boolean isHidden() { return false; }
420
421     }
422 }