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 */
13 public static int count = 0;
14 public static int shifts = 0;
15 public static int reductions = 0;
19 private Phase.Node[] reducing_list = null;
20 public int resets = 0;
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>();
30 public Forest.Ref finalResult;
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> {
35 public Iterator<Phase.Node> iterator() { return hash.iterator(); }
36 public void invoke(State st, Forest result, Node n) {
38 good |= next.newNode(n, result, st, false);
41 /** the token immediately after this phase */
44 private final int pos;
47 private IntPairMap<Phase.Node> hash; /* ALLOC */
48 private boolean closed;
50 private Phase next = null;
52 private Input.Location location;
53 public final Parser parser;
55 private Forest forest;
57 public Phase(Phase prev, Parser parser, Phase previous, Tok token, Input.Location location, Forest forest) throws ParseFailed {
61 this.pos = previous==null ? 0 : previous.pos+1;
63 this.location = location;
68 public void reset() throws ParseFailed {
71 lastperformed.clear();
72 lastperformed.addAll(performed);
74 hash = new IntPairMap<Phase.Node>();
80 if (prev != null) prev.shift(this, forest);
84 public boolean isDone() throws ParseFailed {
85 if (token != null) return false;
86 if (token==null && finalResult==null)
87 throw new ParseFailed(ParseFailed.error(ANSI.red("unexpected end of file\n"), token, hash.values()), getLocation());
91 public Input.Location getLocation() { return location; }
93 /** add a new node (merging with existing nodes if possible)
94 * @param parent the parent of the new node
95 * @param result the SPPF result corresponding to the new node
96 * @param state the state that the new node is in
97 * @param fromEmptyReduction true iff this node is being created as a result of a reduction of length zero (see GRMLR paper)
98 * @param start the earliest part of the input contributing to this node (used to make merging decisions)
100 public boolean newNode(Node parent, Forest pending, State state, boolean fromEmptyReduction) {
101 Node p = hash.get(state, parent==null?null:parent.phase());
102 if (p != null) return newNode2(p, parent, pending, state, fromEmptyReduction);
103 else return newNode3(parent, pending, state, fromEmptyReduction);
105 public void newNode(Node parent, Forest pending, State state, boolean fromEmptyReduction, Position reduction) {
106 int pos = parent==null?0:parent.phase()==null?0:parent.phase().pos;
107 Sequence owner = reduction==null ? null : reduction.owner();
108 if (reduction!=null) {
109 if (owner.hates!=null) {
110 for (Sequence s : performed.getAll(pos))
111 if (owner.hates.contains(s))
113 for (Sequence s : lastperformed.getAll(pos))
114 if (owner.hates.contains(s)) {
115 //System.out.println("now expecting ["+pos+"] => " + s);
116 expected.add(pos, s);
120 if (owner.needs != null)
121 for(Sequence s : owner.needs)
122 if (!performed.contains(pos, s)) {
123 waiting.add(s, new Waiting(parent, pending, state, fromEmptyReduction, reduction));
126 if (!performed.contains(pos, owner)) {
127 performed.add(pos, owner);
128 if (owner.hated != null)
129 for(Sequence seq : owner.hated)
130 if (performed.contains(pos, seq)) {
131 performed.remove(pos, seq);
137 newNode(parent, pending, state, fromEmptyReduction);
138 if (reduction != null) {
142 for(Waiting w : waiting.getAll(owner)) {
143 if (w.parent==parent || (parent!=null&&w.parent!=null&&w.parent.phase()==parent.phase())) {
144 waiting.remove(owner, w);
154 private boolean newNode2(Node p, Node parent, Forest pending, State state, boolean fromEmptyReduction) {
155 if (p.merge(parent, pending)) return true;
156 p.parents().add(parent, true);
157 if (p!=parent && !fromEmptyReduction && reducing) p.performReductions(parent);
161 private boolean newNode3(Node parent, Forest pending, State state, boolean fromEmptyReduction) {
163 if (token != null && state.canShift(token)) break;
164 if (state.isAccepting()) break;
165 if (token==null) break;
166 if (!state.canReduce(token)) return false;
167 //if (count > 1) break;
168 //if (r.numPop == 0) break;
169 //r.reduce(pending, parent, null, Phase.this, null);
173 Node n = new Node(parent, pending, state); // ALLOC
175 n.performEmptyReductions();
176 if (!fromEmptyReduction) n.performReductions(parent);
181 /** perform all reduction operations */
182 public void reduce() throws ParseFailed {
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();
204 for(int i : expected)
205 for(Sequence s : expected.getAll(i))
206 if (!performed.contains(i, s)) {
207 //System.out.println("resetting due to pos="+i+": " + s + " " + System.identityHashCode(s));
218 private boolean reset = false;
219 class Reset extends RuntimeException { }
221 /** perform all shift operations, adding promoted nodes to <tt>next</tt> */
222 public void shift(Phase next, Forest result) throws ParseFailed {
223 // this massively improves GC performance
224 if (prev!=null && parser.helpgc) {
231 for(Phase.Node n : hash.values()) {
232 if (token == null && n.state.isAccepting()) {
233 if (finalResult==null) finalResult = new Forest.Ref();
234 for(Object f : n.results())
235 finalResult.merge((Forest)f);
237 if (token == null) continue;
238 n.state.invokeShifts(token, this, result, n);
241 if (!good && token!=null)
242 throw new ParseFailed(ParseFailed.error(ANSI.red("unexpected character ")+" \'"+
243 ANSI.purple(StringUtil.escapify(token+"", "\\\'\r\n"))+
244 "\' encountered at "+
245 ANSI.green(getLocation())+"\n", token, hash.values()),
247 if (token==null && finalResult==null)
248 throw new ParseFailed(ParseFailed.error(ANSI.red("unexpected end of file\n"), token, hash.values()),
257 boolean fromEmptyReduction;
259 public Waiting(Node parent, Forest pending, State state, boolean fromEmptyReduction, Position reduction) {
261 this.parent = parent;
262 this.pending = pending;
264 this.fromEmptyReduction = fromEmptyReduction;
265 this.reduction = reduction;
267 public void perform() {
268 //System.out.println("performing: " + reduction.position);
269 newNode(parent, pending, state, fromEmptyReduction, reduction);
273 // Node /////////////////////////////////////////////////////////////////////////////////
275 /** a node in the GSS */
276 final class Node implements Invokable<Position, Node, Node>, IntegerMappable, GraphViz.ToGraphViz {
277 public FastSet<Node> set = new FastSet<Node>();
280 private boolean allqueued = false;
282 /** what state this node is in */
283 public final Parser.Table<Tok>.State<Tok> state;
285 /** which Phase this Node belongs to (node that Node is also a non-static inner class of Phase) */
286 public Phase phase() { return Phase.this; }
288 private HashSet<Forest.Ref> resultMap = new HashSet<Forest.Ref>();
289 public Iterable<Forest.Ref> results() { return resultMap; }
290 public FastSet<Node> parents() { return set; }
291 public boolean merge(Node parent, Forest result) {
292 // FIXME: inefficient!
293 for(Forest.Ref f : results()) {
294 if (f.parents.contains(parent) /* UGLY: */ && f.parents.size()==1) {
299 Forest.Ref f = new Forest.Ref();
300 f.parents.add(parent);
303 set.add(parent, true);
307 public void performReductions() {
308 if (allqueued) return;
310 state.invokeReductions(token, this, this, null);
313 public void performReductions(Node n2) {
314 if (!allqueued) performReductions();
315 else state.invokeReductions(token, this, this, n2);
318 public void performEmptyReductions() { state.invokeReductions(token, this, null, null); }
319 public final void invoke(Position r, Node n, Node n2) {
321 if (n==null || n2==null || r.pos==0) {
323 if (n==null) n = this;
327 Forest[] holder = new Forest[r.pos];
328 if (r.pos==0) n.finish(r, r.zero(), n.phase());
329 else n.reduce(r, r.pos-1, n.phase(), null);
331 if (r.pos<=0) throw new Error("called wrong form of reduce()");
333 n.reduce(r, pos, n.phase(), n2);
337 public void reduce(Position r, int pos, Phase target, Node only) {
338 Forest[] holder = r.holder;
339 Forest old = holder[pos];
341 for(Forest result : results())
342 for(Node child : ((Forest.Ref<?>)result).parents) {
343 if (only != null && child!=only) continue;
344 holder[pos] = result;
345 if (pos==0) child.finish(r, r.rewrite(phase().getLocation()), target);
346 else child.reduce(r, pos-1, target, null);
352 public void finish(Position r, Forest result, Phase<Tok> target) {
353 Parser.Table<Tok>.State<Tok> state0 = state.gotoSetNonTerminals.get(r.owner());
354 if (result==null) throw new Error();
356 target.newNode(this, result, state0, r.pos<=0, r);
359 private Node(Node parent, Forest pending, State state) {
361 this.merge(parent, pending);
362 Phase start = parent==null ? null : parent.phase();
363 if (parent != null) parents().add(parent, true);
364 if (Phase.this.hash.get(state, start) != null) throw new Error("severe problem!");
365 Phase.this.hash.put(state, start, this);
367 public int toInt() { return idx; }
368 private final int idx = node_idx++;
370 // GraphViz //////////////////////////////////////////////////////////////////////////////
372 public GraphViz.Node toGraphViz(GraphViz gv) {
373 if (gv.hasNode(this)) return gv.createNode(this);
374 GraphViz.Node n = gv.createNode(this);
375 n.label = ""+state.toStringx();
376 n.shape = "rectangle";
378 for(Forest result : results()) n.edge(result, "");
379 for(Node parent : parents()) n.edge(parent, "");
380 ((GraphViz.Group)phase().toGraphViz(gv)).add(n);
383 public boolean isTransparent() { return false; }
384 public boolean isHidden() { return false; }
387 private int node_idx = 0;
389 public int toInt() { return pos+1; }
390 public int size() { return hash==null ? 0 : hash.size(); }
392 // GraphViz //////////////////////////////////////////////////////////////////////////////
394 public GraphViz.Node toGraphViz(GraphViz gv) {
395 if (gv.hasNode(this)) return gv.createNode(this);
396 GraphViz.Group g = gv.createGroup(this);
397 g.label = "Phase " + pos;
402 public boolean isTransparent() { return false; }
403 public boolean isHidden() { return false; }