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