1 package edu.berkeley.sbp;
2 import edu.berkeley.sbp.*;
3 import edu.berkeley.sbp.util.*;
4 import edu.berkeley.sbp.Sequence.Position;
5 import edu.berkeley.sbp.Parser.Table.State;
8 import java.lang.reflect.*;
10 /** implements Tomita's Graph Structured Stack */
15 private Phase.Node[] reducing_list = null;
16 public int resets = 0;
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>();
24 public Forest.Ref finalResult;
26 /** corresponds to a positions <i>between tokens</i> the input stream; same as Tomita's U_i's */
27 public class Phase implements Invokable<State, Forest, GSS.Phase.Node>, IntegerMappable {
29 /** the token immediately after this phase */
32 private final int pos;
35 private IntPairMap<Phase.Node> hash; /* ALLOC */
36 private boolean closed;
38 private Phase next = null;
40 private Token.Location location;
41 public final Parser parser;
43 private Forest forest;
45 public Phase(Phase prev, Parser parser, Phase previous, Token token, Token.Location location, Forest forest) {
49 this.pos = previous==null ? 0 : previous.pos+1;
51 this.location = location;
59 hash = new IntPairMap<Phase.Node>();
64 if (prev != null) prev.shift(this, forest);
68 public boolean isDone() throws ParseFailed {
69 if (token != null) return false;
70 if (token==null && finalResult==null)
71 throw new ParseFailed(ParseFailed.error(ANSI.red("unexpected end of file\n"), token, hash.values()), getLocation());
75 public Token.Location getLocation() { return location; }
77 /** add a new node (merging with existing nodes if possible)
78 * @param parent the parent of the new node
79 * @param result the SPPF result corresponding to the new node
80 * @param state the state that the new node is in
81 * @param fromEmptyReduction true iff this node is being created as a result of a reduction of length zero (see GRMLR paper)
82 * @param start the earliest part of the input contributing to this node (used to make merging decisions)
84 public boolean newNode(Node parent, Forest pending, State state, boolean fromEmptyReduction) {
85 Node p = hash.get(state, parent==null?null:parent.phase());
86 if (p != null) return newNode2(p, parent, pending, state, fromEmptyReduction);
87 else return newNode3(parent, pending, state, fromEmptyReduction);
89 public void newNode(Node parent, Forest pending, State state, boolean fromEmptyReduction, Position reduction) {
90 int pos = parent==null?0:parent.phase()==null?0:parent.phase().pos;
91 Sequence owner = reduction==null ? null : reduction.owner();
92 if (reduction!=null) {
93 if (inhibited.contains(pos, owner)) return;
94 if (owner.needs != null)
95 for(Sequence s : owner.needs)
96 if (!performed.contains(pos, s)) {
97 waiting.add(s, new Waiting(parent, pending, state, fromEmptyReduction, reduction));
100 if ((owner.needed != null && owner.needed.size()>0) ||
101 (owner.hated != null && owner.hated.size()>0) ||
102 (owner.hates != null && owner.hates.size()>0))
103 performed.add(pos, owner);
106 newNode(parent, pending, state, fromEmptyReduction);
107 if (reduction!=null) inhibit(reduction, parent==null?0:parent.phase().pos);
108 if (reduction != null) {
112 for(Waiting w : waiting.getAll(owner)) {
113 if (w.parent==parent || (parent!=null&&w.parent!=null&&w.parent.phase()==parent.phase())) {
114 waiting.remove(owner, w);
123 private boolean newNode2(Node p, Node parent, Forest pending, State state, boolean fromEmptyReduction) {
124 p.holder.merge(pending);
125 if (p.parents().contains(parent)) return true;
126 p.parents().add(parent, true);
127 if (p!=parent && !fromEmptyReduction && reducing) p.performReductions(parent);
130 private boolean newNode3(Node parent, Forest pending, State state, boolean fromEmptyReduction) {
132 if (token != null && state.canShift(token)) break;
133 if (state.isAccepting()) break;
134 if (token==null) break;
135 if (!state.canReduce(token)) return false;
136 //if (count > 1) break;
137 //if (r.numPop == 0) break;
138 //r.reduce(pending, parent, null, Phase.this, null);
142 Node n = new Node(parent, pending, state); // ALLOC
144 n.performEmptyReductions();
145 if (!fromEmptyReduction) n.performReductions(parent);
150 public void uninhibit(int p, Sequence s) {
152 for(Sequence s2 : s.hated)
153 inhibited.remove(p, s2);
156 public void inhibit(Position r, int p) {
157 if (r.owner().hated == null) return;
158 // remember that dead states are still allowed to shift -- just not allowed to reduce
159 boolean reset = false;
160 for(Sequence seq : r.owner().hated) {
161 if (performed.contains(p,seq)) {
163 //System.out.println("\nresetting due to " + r.owner() + " killing " + seq);
165 inhibited.add(p, seq);
166 //inhibited = new HashMapBag<Integer,Sequence>();
171 inhibited.add(p, seq);
175 /** perform all reduction operations */
176 public void reduce() {
179 if (reducing_list==null || reducing_list.length < hash.size())
180 reducing_list = new Phase.Node[hash.size() * 4];
181 hash.toArray(reducing_list);
182 int num = hash.size();
183 for(int i=0; i<num; i++) {
184 Node n = reducing_list[i];
185 n.performEmptyReductions();
186 // INVARIANT: we never "see" a node until its parent-set is complete, modulo merges
188 for(int i=0; i<num; i++) {
189 Node n = reducing_list[i];
190 reducing_list[i] = null;
191 n.performReductions();
199 class Reset extends RuntimeException { }
201 public void invoke(State st, Forest result, Node n) {
202 good |= next.newNode(n, result, st, false);
205 /** perform all shift operations, adding promoted nodes to <tt>next</tt> */
206 public void shift(Phase next, Forest result) throws ParseFailed {
207 // this massively improves GC performance
208 if (prev!=null) prev.hash = null;
213 for(Phase.Node n : hash.values()) {
214 if (token == null && n.state.isAccepting()) {
215 if (finalResult==null) finalResult = new Forest.Ref();
216 finalResult.merge(n.holder);
218 if (token == null) continue;
219 n.state.invokeShifts(token, this, result, n);
222 if (!good && token!=null)
223 throw new ParseFailed(ParseFailed.error(ANSI.red("unexpected character")+" "+ANSI.purple(token)+" encountered at "+ANSI.green(getLocation())+"\n", token, hash.values()),
225 if (token==null && finalResult==null)
226 throw new ParseFailed(ParseFailed.error(ANSI.red("unexpected end of file\n"), token, hash.values()),
231 public class Waiting {
235 boolean fromEmptyReduction;
237 public Waiting(Node parent, Forest pending, State state, boolean fromEmptyReduction, Position reduction) {
239 this.parent = parent;
240 this.pending = pending;
242 this.fromEmptyReduction = fromEmptyReduction;
243 this.reduction = reduction;
245 public void perform() {
246 //System.out.println("performing: " + reduction.position);
247 newNode(parent, pending, state, fromEmptyReduction, reduction);
251 // Node /////////////////////////////////////////////////////////////////////////////////
253 /** a node in the GSS */
254 public final class Node extends FastSet<Node> implements Invokable<Position, Node, Node> {
256 private Forest.Ref holder = null;
257 private boolean allqueued = false;
259 /** what state this node is in */
260 public final State state;
262 /** which Phase this Node belongs to (node that Node is also a non-static inner class of Phase) */
263 public Phase phase() { return Phase.this; }
264 public Forest.Ref holder() { return holder==null ? (holder = new Forest.Ref()) : holder; }
265 public Forest pending() { return Phase.this.closed ? holder().resolve() : holder; }
266 public FastSet<Node> parents() { return this; }
268 public void performReductions() {
269 if (allqueued) return;
271 state.invokeReductions(token, this, this, null);
274 public void performReductions(Node n2) {
275 if (!allqueued) performReductions();
276 else state.invokeReductions(token, this, this, n2);
279 public final void invoke(Position r, Node n, Node n2) {
280 if (n==null || n2==null || r.pos==0) {
282 if (n==null) n = this;
286 Forest[] holder = new Forest[r.pos];
287 if (r.pos==0) n.finish(r, r.zero(), n.phase(), holder);
288 else n.reduce(r, r.pos-1, n.phase(), holder);
290 Forest[] holder = new Forest[r.pos];
291 if (r.pos<=0) throw new Error("called wrong form of reduce()");
293 Forest old = holder[pos];
294 holder[pos] = n.pending();
296 System.arraycopy(holder, 0, r.holder, 0, holder.length);
297 Forest rex = r.rewrite(n.phase().getLocation());
298 n2.finish(r, rex, n.phase(), holder);
300 n2.reduce(r, pos-1, n.phase(), holder);
306 public void reduce(Position r, int pos, GSS.Phase target, Forest[] holder) {
307 Forest old = holder[pos];
308 holder[pos] = this.pending();
310 System.arraycopy(holder, 0, r.holder, 0, holder.length);
311 for(int i=0; i<r.pos; i++) if (r.holder[i]==null) throw new Error("realbad");
312 Forest rex = r.rewrite(target.getLocation());
313 for(GSS.Phase.Node child : this.parents()) child.finish(r, rex, target, holder);
315 for(GSS.Phase.Node child : this.parents()) child.reduce(r, pos-1, target, holder);
320 public void finish(Position r, Forest result, GSS.Phase target, Forest[] holder) {
321 State state0 = state.gotoSetNonTerminals.get(r.owner());
322 if (result==null) throw new Error();
324 target.newNode(this, result, state0, r.pos<=0, r);
327 public void performEmptyReductions() { state.invokeReductions(token, this, null, null); }
329 private Node(Node parent, Forest pending, State state) {
331 this.holder().merge(pending);
332 Phase start = parent==null ? null : parent.phase();
333 if (parent != null) parents().add(parent, true);
334 if (Phase.this.hash.get(state, start) != null) throw new Error("severe problem!");
335 Phase.this.hash.put(state, start, this);
339 public int toInt() { return pos+1; }
340 public int size() { return hash==null ? 0 : hash.size(); }