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;
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<Tok> implements Invokable<State, Forest, Phase<Tok>.Node>, IntegerMappable {
29 public void invoke(State st, Forest result, Node n) {
30 good |= next.newNode(n, result, st, false);
33 /** the token immediately after this phase */
36 private final int pos;
39 private IntPairMap<Phase.Node> hash; /* ALLOC */
40 private IntPairMap<Forest> singularReductions; /* ALLOC */
41 private boolean closed;
43 private Phase next = null;
45 private Token.Location location;
46 public final Parser parser;
48 private Forest forest;
50 public Phase(Phase prev, Parser parser, Phase previous, Tok token, Token.Location location, Forest forest) {
54 this.pos = previous==null ? 0 : previous.pos+1;
56 this.location = location;
64 hash = new IntPairMap<Phase.Node>();
65 singularReductions = new IntPairMap<Forest>();
70 if (prev != null) prev.shift(this, forest);
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());
81 public Token.Location getLocation() { return location; }
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)
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);
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));
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);
112 newNode(parent, pending, state, fromEmptyReduction);
113 if (reduction!=null) inhibit(reduction, parent==null?0:parent.phase().pos);
114 if (reduction != null) {
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);
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);
136 private boolean newNode3(Node parent, Forest pending, State state, boolean fromEmptyReduction) {
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);
148 Node n = new Node(parent, pending, state); // ALLOC
150 n.performEmptyReductions();
151 if (!fromEmptyReduction) n.performReductions(parent);
156 public void uninhibit(int p, Sequence s) {
158 for(Sequence s2 : s.hated)
159 inhibited.remove(p, s2);
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)) {
169 //System.out.println("\nresetting due to " + r.owner() + " killing " + seq);
171 inhibited.add(p, seq);
172 //inhibited = new HashMapBag<Integer,Sequence>();
177 inhibited.add(p, seq);
181 /** perform all reduction operations */
182 public void reduce() {
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
194 for(int i=0; i<num; i++) {
195 Node n = reducing_list[i];
196 reducing_list[i] = null;
197 n.performReductions();
205 class Reset extends RuntimeException { }
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
212 prev.singularReductions = null;
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);
223 if (token == null) continue;
224 n.state.invokeShifts(token, this, result, n);
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()),
230 if (token==null && finalResult==null)
231 throw new ParseFailed(ParseFailed.error(ANSI.red("unexpected end of file\n"), token, hash.values()),
236 public class Waiting {
240 boolean fromEmptyReduction;
242 public Waiting(Node parent, Forest pending, State state, boolean fromEmptyReduction, Position reduction) {
244 this.parent = parent;
245 this.pending = pending;
247 this.fromEmptyReduction = fromEmptyReduction;
248 this.reduction = reduction;
250 public void perform() {
251 //System.out.println("performing: " + reduction.position);
252 newNode(parent, pending, state, fromEmptyReduction, reduction);
256 // Node /////////////////////////////////////////////////////////////////////////////////
258 /** a node in the GSS */
259 public final class Node extends FastSet<Node> implements Invokable<Position, Node, Node>, IntegerMappable {
261 private Forest.Ref holder = null;
262 private boolean allqueued = false;
264 /** what state this node is in */
265 public final Parser.Table<Tok>.State<Tok> state;
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; }
273 public void performReductions() {
274 if (allqueued) return;
276 state.invokeReductions(token, this, this, null);
279 public void performReductions(Node n2) {
280 if (!allqueued) performReductions();
281 else state.invokeReductions(token, this, this, n2);
284 public final void invoke(Position r, Node n, Node n2) {
285 if (n==null || n2==null || r.pos==0) {
287 if (n==null) n = this;
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);
295 Forest[] holder = new Forest[r.pos];
296 if (r.pos<=0) throw new Error("called wrong form of reduce()");
298 Forest old = holder[pos];
299 holder[pos] = n.pending();
301 System.arraycopy(holder, 0, r.holder, 0, holder.length);
303 if (r.pos==1) rex = singularReductions.get(this, r);
305 rex = r.rewrite(n.phase().getLocation());
306 if (r.pos==1) singularReductions.put(this, r, rex);
308 n2.finish(r, rex, n.phase(), holder);
310 n2.reduce(r, pos-1, n.phase(), holder);
316 public void reduce(Position r, int pos, Phase target, Forest[] holder) {
317 Forest old = holder[pos];
318 holder[pos] = this.pending();
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");
323 if (r.pos==1) rex = singularReductions.get(this, r);
325 rex = r.rewrite(phase().getLocation());
326 if (r.pos==1) singularReductions.put(this, r, rex);
328 for(Node child : this.parents()) child.finish(r, rex, target, holder);
330 for(Node child : this.parents()) child.reduce(r, pos-1, target, holder);
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();
339 target.newNode(this, result, state0, r.pos<=0, r);
342 public void performEmptyReductions() { state.invokeReductions(token, this, null, null); }
344 private Node(Node parent, Forest pending, 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);
352 public int toInt() { return idx; }
353 private final int idx = node_idx++;
355 private int node_idx = 0;
357 public int toInt() { return pos+1; }
358 public int size() { return hash==null ? 0 : hash.size(); }