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