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