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;
17 private Phase.Node[] reducing_list = null;
18 public int resets = 0;
21 HashMapBag<Sequence,Phase.Waiting> waiting = new HashMapBag<Sequence,Phase.Waiting>();
22 HashMapBag<Integer,Sequence> performed = new HashMapBag<Integer,Sequence>();
23 HashMapBag<Integer,Sequence> lastperformed = new HashMapBag<Integer,Sequence>();
24 HashMapBag<Integer,Sequence> expected = new HashMapBag<Integer,Sequence>();
27 public Forest.Ref finalResult;
29 /** corresponds to a positions <i>between tokens</i> the input stream; same as Tomita's U_i's */
30 class Phase<Tok> implements Invokable<State, Forest, Phase<Tok>.Node>, IntegerMappable, GraphViz.ToGraphViz, Iterable<Phase.Node> {
32 public Iterator<Phase.Node> iterator() { return hash.iterator(); }
33 public void invoke(State st, Forest result, Node n) {
34 good |= next.newNode(n, result, st, false);
37 /** the token immediately after this phase */
40 private final int pos;
43 private IntPairMap<Phase.Node> hash; /* ALLOC */
44 private IntPairMap<Forest> singularReductions; /* ALLOC */
45 private boolean closed;
47 private Phase next = null;
49 private Input.Location location;
50 public final Parser parser;
52 private Forest forest;
54 public Phase(Phase prev, Parser parser, Phase previous, Tok token, Input.Location location, Forest forest) throws ParseFailed {
58 this.pos = previous==null ? 0 : previous.pos+1;
60 this.location = location;
65 public void reset() throws ParseFailed {
68 lastperformed.clear();
69 lastperformed.addAll(performed);
71 hash = new IntPairMap<Phase.Node>();
72 singularReductions = new IntPairMap<Forest>();
78 if (prev != null) prev.shift(this, forest);
82 public boolean isDone() throws ParseFailed {
83 if (token != null) return false;
84 if (token==null && finalResult==null)
85 throw new ParseFailed(ParseFailed.error(ANSI.red("unexpected end of file\n"), token, hash.values()), getLocation());
89 public Input.Location getLocation() { return location; }
91 /** add a new node (merging with existing nodes if possible)
92 * @param parent the parent of the new node
93 * @param result the SPPF result corresponding to the new node
94 * @param state the state that the new node is in
95 * @param fromEmptyReduction true iff this node is being created as a result of a reduction of length zero (see GRMLR paper)
96 * @param start the earliest part of the input contributing to this node (used to make merging decisions)
98 public boolean newNode(Node parent, Forest pending, State state, boolean fromEmptyReduction) {
99 Node p = hash.get(state, parent==null?null:parent.phase());
100 if (p != null) return newNode2(p, parent, pending, state, fromEmptyReduction);
101 else return newNode3(parent, pending, state, fromEmptyReduction);
103 public void newNode(Node parent, Forest pending, State state, boolean fromEmptyReduction, Position reduction) {
104 int pos = parent==null?0:parent.phase()==null?0:parent.phase().pos;
105 Sequence owner = reduction==null ? null : reduction.owner();
106 if (reduction!=null) {
107 if (owner.hates!=null) {
108 for (Sequence s : performed.getAll(pos))
109 if (owner.hates.contains(s))
111 for (Sequence s : lastperformed.getAll(pos))
112 if (owner.hates.contains(s)) {
113 //System.out.println("now expecting ["+pos+"] => " + s);
114 expected.add(pos, s);
118 if (owner.needs != null)
119 for(Sequence s : owner.needs)
120 if (!performed.contains(pos, s)) {
121 waiting.add(s, new Waiting(parent, pending, state, fromEmptyReduction, reduction));
124 if (!performed.contains(pos, owner)) {
125 performed.add(pos, owner);
126 if (owner.hated != null)
127 for(Sequence seq : owner.hated)
128 if (performed.contains(pos, seq)) {
129 performed.remove(pos, seq);
135 newNode(parent, pending, state, fromEmptyReduction);
136 if (reduction != null) {
140 for(Waiting w : waiting.getAll(owner)) {
141 if (w.parent==parent || (parent!=null&&w.parent!=null&&w.parent.phase()==parent.phase())) {
142 waiting.remove(owner, w);
151 private boolean newNode2(Node p, Node parent, Forest pending, State state, boolean fromEmptyReduction) {
152 //if (p.parents().contains(parent)) return true;
153 p.merge(parent, pending);
154 p.parents().add(parent, true);
155 if (p!=parent && !fromEmptyReduction && reducing) p.performReductions(parent);
158 private boolean newNode3(Node parent, Forest pending, State state, boolean fromEmptyReduction) {
160 if (token != null && state.canShift(token)) break;
161 if (state.isAccepting()) break;
162 if (token==null) break;
163 if (!state.canReduce(token)) return false;
164 //if (count > 1) break;
165 //if (r.numPop == 0) break;
166 //r.reduce(pending, parent, null, Phase.this, null);
170 Node n = new Node(parent, pending, state); // ALLOC
172 n.performEmptyReductions();
173 if (!fromEmptyReduction) n.performReductions(parent);
178 /** perform all reduction operations */
179 public void reduce() throws ParseFailed {
182 if (reducing_list==null || reducing_list.length < hash.size())
183 reducing_list = new Phase.Node[hash.size() * 4];
184 hash.toArray(reducing_list);
185 int num = hash.size();
186 for(int i=0; i<num; i++) {
187 Node n = reducing_list[i];
188 n.performEmptyReductions();
189 // INVARIANT: we never "see" a node until its parent-set is complete, modulo merges
191 for(int i=0; i<num; i++) {
192 Node n = reducing_list[i];
193 reducing_list[i] = null;
194 n.performReductions();
201 for(int i : expected)
202 for(Sequence s : expected.getAll(i))
203 if (!performed.contains(i, s)) {
204 //System.out.println("resetting due to pos="+i+": " + s + " " + System.identityHashCode(s));
215 private boolean reset = false;
216 class Reset extends RuntimeException { }
218 /** perform all shift operations, adding promoted nodes to <tt>next</tt> */
219 public void shift(Phase next, Forest result) throws ParseFailed {
220 // this massively improves GC performance
221 if (prev!=null && parser.helpgc) {
223 prev.singularReductions = null;
229 for(Phase.Node n : hash.values()) {
230 if (token == null && n.state.isAccepting()) {
231 if (finalResult==null) finalResult = new Forest.Ref();
232 for(Object f : n.results())
233 finalResult.merge((Forest)f);
235 if (token == null) continue;
236 n.state.invokeShifts(token, this, result, n);
239 if (!good && token!=null)
240 throw new ParseFailed(ParseFailed.error(ANSI.red("unexpected character ")+" \'"+
241 ANSI.purple(StringUtil.escapify(token+"", "\\\'\r\n"))+
242 "\' encountered at "+
243 ANSI.green(getLocation())+"\n", token, hash.values()),
245 if (token==null && finalResult==null)
246 throw new ParseFailed(ParseFailed.error(ANSI.red("unexpected end of file\n"), token, hash.values()),
255 boolean fromEmptyReduction;
257 public Waiting(Node parent, Forest pending, State state, boolean fromEmptyReduction, Position reduction) {
259 this.parent = parent;
260 this.pending = pending;
262 this.fromEmptyReduction = fromEmptyReduction;
263 this.reduction = reduction;
265 public void perform() {
266 //System.out.println("performing: " + reduction.position);
267 newNode(parent, pending, state, fromEmptyReduction, reduction);
271 // Node /////////////////////////////////////////////////////////////////////////////////
273 /** a node in the GSS */
274 final class Node implements Invokable<Position, Node, Node>, IntegerMappable, GraphViz.ToGraphViz {
275 public FastSet<Node> set = new FastSet<Node>();
277 public GraphViz.Node toGraphViz(GraphViz gv) {
278 if (gv.hasNode(this)) return gv.createNode(this);
279 GraphViz.Node n = gv.createNode(this);
280 n.label = ""+state.toStringx();
281 n.shape = "rectangle";
283 //GraphViz.Node f = pending().toGraphViz(gv);
285 for(Forest result : results()) n.edge(result, "");
286 for(Node parent : parents()) n.edge(parent, "");
287 ((GraphViz.Group)phase().toGraphViz(gv)).add(n);
290 public boolean isTransparent() { return false; }
291 public boolean isHidden() { return false; }
294 //private Forest.Ref holder = new Forest.Ref();
295 private boolean allqueued = false;
297 /** what state this node is in */
298 public final Parser.Table<Tok>.State<Tok> state;
300 /** which Phase this Node belongs to (node that Node is also a non-static inner class of Phase) */
301 public Phase phase() { return Phase.this; }
302 //private Forest pending() { return !Phase.this.closed ? holder : holder.resolve(); }
303 private HashMap<Node,Forest> resultMap = new HashMap<Node,Forest>();
304 public void merge(Node parent, Forest result) {
305 //holder.merge(result);
306 Forest.Ref f = (Forest.Ref)resultMap.get(parent);
307 if (f==null) { f = new Forest.Ref(); resultMap.put(parent, f); }
309 set.add(parent, true);
311 //public Iterable<Node> childrenFor(Forest result) { return resultMap.getAll(result); }
312 public Iterable<Forest> results() { return resultMap.values(); }
313 private Forest pending(Node n) {
314 //return !Phase.this.closed ? holder : holder.resolve();
316 for(Forest f : resultMap)
317 if (resultMap.contains(f, n))
321 return resultMap.get(n);
323 public FastSet<Node> parents() { return set; }
325 public void performReductions() {
326 if (allqueued) return;
328 state.invokeReductions(token, this, this, null);
331 public void performReductions(Node n2) {
332 if (!allqueued) performReductions();
333 else state.invokeReductions(token, this, this, n2);
336 public void performEmptyReductions() { state.invokeReductions(token, this, null, null); }
337 public final void invoke(Position r, Node n, Node n2) {
338 //if (r.owner().lame) return;
339 if (n==null || n2==null || r.pos==0) {
341 if (n==null) n = this;
345 Forest[] holder = new Forest[r.pos];
346 if (r.pos==0) n.finish(r, r.zero(), n.phase(), holder);
347 else n.reduce(r, r.pos-1, n.phase(), holder, null, n.pending(n));
349 Forest[] holder = new Forest[r.pos];
350 if (r.pos<=0) throw new Error("called wrong form of reduce()");
352 n.reduce(r, pos, n.phase(), holder, n2, n.pending(n));
357 public void reduce(Position r, int pos, Phase target, Forest[] holder) {
358 reduce(r, pos, target, holder, null); }
359 public void reduce(Position r, int pos, Phase target, Forest[] holder, Node only) {
360 reduce(r, pos, target, holder, only, this.pending());
363 public void reduce(Position r, int pos, Phase target, Forest[] holder, Node only, Forest pending) {
364 Forest old = holder[pos];
365 holder[pos] = pending;
368 // FIXME: I'm unsure about this -- basically we want to deal with the case where
369 // there are two nodes, each of whose Ref points to the same Forest instance.
370 // Some node in the next phase has both of these as parents. This might happen
371 // since the same reduction can appear in more than one state.
374 holder[pos] = pending(only);
375 System.arraycopy(holder, 0, r.holder, 0, holder.length);
376 for(int i=0; i<r.pos; i++) if (r.holder[i]==null) throw new Error("realbad");
378 if (r.pos==1) rex = singularReductions.get(pending, r);
380 rex = r.rewrite(phase().getLocation());
381 if (r.pos==1) singularReductions.put(pending, r, rex);
383 only.finish(r, rex, target, holder);
385 for(Forest result : results()) {
386 pending = holder[pos] = result;
387 System.arraycopy(holder, 0, r.holder, 0, holder.length);
388 for(int i=0; i<r.pos; i++) if (r.holder[i]==null) throw new Error("realbad");
390 if (rex==null && r.pos==1) rex = singularReductions.get(pending, r);
392 rex = r.rewrite(phase().getLocation());
393 if (r.pos==1) singularReductions.put(pending, r, rex);
395 for(Node child : parents()) {
396 if (pending(child)==result)
397 child.finish(r, rex, target, holder);
403 holder[pos] = pending(only);
404 only.reduce(r, pos-1, target, holder, null, only.pending(only));
406 for(Node child : this.parents()) {
407 holder[pos] = pending(child);
408 child.reduce(r, pos-1, target, holder, null, child.pending(child));
415 public void finish(Position r, Forest result, Phase<Tok> target, Forest[] holder) {
416 Parser.Table<Tok>.State<Tok> state0 = state.gotoSetNonTerminals.get(r.owner());
417 if (result==null) throw new Error();
419 target.newNode(this, result, state0, r.pos<=0, r);
422 private Node(Node parent, Forest pending, State state) {
424 this.merge(parent, pending);
425 Phase start = parent==null ? null : parent.phase();
426 if (parent != null) parents().add(parent, true);
427 if (Phase.this.hash.get(state, start) != null) throw new Error("severe problem!");
428 Phase.this.hash.put(state, start, this);
430 public int toInt() { return idx; }
431 private final int idx = node_idx++;
433 private int node_idx = 0;
435 public int toInt() { return pos+1; }
436 public int size() { return hash==null ? 0 : hash.size(); }
438 // GraphViz //////////////////////////////////////////////////////////////////////////////
440 public GraphViz.Node toGraphViz(GraphViz gv) {
441 if (gv.hasNode(this)) return gv.createNode(this);
442 GraphViz.Group g = gv.createGroup(this);
443 g.label = "Phase " + pos;
448 public boolean isTransparent() { return false; }
449 public boolean isHidden() { return false; }