1 // Copyright 2006 all rights reserved; see LICENSE file for BSD-style license
3 package edu.berkeley.sbp;
4 import edu.berkeley.sbp.*;
5 import edu.berkeley.sbp.util.*;
6 import edu.berkeley.sbp.Parser.Table.*;
7 import edu.berkeley.sbp.Sequence.Position;
10 import java.lang.reflect.*;
12 /** implements Tomita's Graph Structured Stack */
16 static int shifts = 0;
17 static int reductions = 0;
23 public GSS(Input input) { this.input = input; }
25 private Phase.Node[] reducing_list = null;
27 // FIXME: right now, these are the performance bottleneck
28 HashMapBag<Sequence,Phase.Waiting> waiting = new HashMapBag<Sequence,Phase.Waiting>();
29 HashMapBag<Integer,Sequence> performed = new HashMapBag<Integer,Sequence>();
30 HashMapBag<Integer,Sequence> lastperformed = new HashMapBag<Integer,Sequence>();
31 HashMapBag<Integer,Sequence> expected = new HashMapBag<Integer,Sequence>();
34 Forest.Many finalResult;
36 /** corresponds to a positions <i>between tokens</i> the input stream; same as Tomita's U_i's */
37 class Phase<Tok> implements Invokable<State, Forest, Phase<Tok>.Node>, IntegerMappable, GraphViz.ToGraphViz, Iterable<Phase.Node> {
39 public Iterator<Phase.Node> iterator() { return hash.iterator(); }
40 public void invoke(State st, Forest result, Node n) {
42 good |= next.newNode(n, result, st, false);
45 /** the token immediately after this phase */
48 private final int pos;
51 private IntPairMap<Phase.Node> hash; /* ALLOC */
52 private boolean closed;
54 private Phase next = null;
56 private Input.Location location;
57 private Input.Location nextLocation;
58 private Input.Location prevLocation;
60 public final Parser parser;
62 private Forest forest;
64 public Phase(Phase prev, Parser parser, Phase previous, Tok token, Input.Location location,
65 Input.Location nextLocation, Forest forest) throws ParseFailed {
66 this.prevLocation = prev==null ? location : prev.getLocation();
70 this.pos = previous==null ? 0 : previous.pos+1;
72 this.location = location;
73 this.nextLocation = nextLocation;
78 public void reset() throws ParseFailed {
81 lastperformed.clear();
82 lastperformed.addAll(performed);
84 hash = new IntPairMap<Phase.Node>();
90 if (prev != null) prev.shift(this, forest);
94 public boolean isDone() throws ParseFailed {
95 if (token != null) return false;
96 if (token==null && finalResult==null)
97 throw new ParseFailed(ParseFailed.error(("unexpected end of file\n"),
98 token, hash.values()),
99 getLocation().createRegion(getLocation()), input);
103 public Input.Location getPrevLocation() { return prevLocation; }
104 public Input.Location getLocation() { return location; }
105 public Input.Region getRegion() { return getPrevLocation().createRegion(getLocation()); }
106 public Input.Location getNextLocation() { return nextLocation; }
108 /** add a new node (merging with existing nodes if possible)
109 * @param parent the parent of the new node
110 * @param result the SPPF result corresponding to the new node
111 * @param state the state that the new node is in
112 * @param fromEmptyReduction true iff this node is being created as a result of a reduction of length zero (see GRMLR paper)
113 * @param start the earliest part of the input contributing to this node (used to make merging decisions)
115 public boolean newNode(Node parent, Forest pending, State state, boolean fromEmptyReduction) {
116 Node p = hash.get(state, parent==null?null:parent.phase());
117 if (p != null) return newNode2(p, parent, pending, state, fromEmptyReduction);
118 else return newNode3(parent, pending, state, fromEmptyReduction);
120 public void newNode(Node parent, Forest pending, State state, boolean fromEmptyReduction, Position reduction) {
121 int pos = parent==null?0:parent.phase()==null?0:parent.phase().pos;
122 Sequence owner = reduction==null ? null : reduction.owner();
123 if (reduction!=null) {
124 if (owner.hates!=null) {
125 for (Sequence s : performed.getAll(pos))
126 if (owner.hates.contains(s))
128 for (Sequence s : lastperformed.getAll(pos))
129 if (owner.hates.contains(s)) {
130 //System.out.println("now expecting ["+pos+"] => " + s);
131 expected.add(pos, s);
135 if (owner.needs != null)
136 for(Sequence s : owner.needs)
137 if (!performed.contains(pos, s)) {
138 waiting.add(s, new Waiting(parent, pending, state, fromEmptyReduction, reduction));
141 if (!performed.contains(pos, owner)) {
142 performed.add(pos, owner);
143 if (owner.hated != null)
144 for(Sequence seq : owner.hated)
145 if (performed.contains(pos, seq)) {
146 performed.remove(pos, seq);
151 newNode(parent, pending, state, fromEmptyReduction);
152 if (reduction != null) {
156 for(Waiting w : waiting.getAll(owner)) {
157 if (w.parent==parent || (parent!=null&&w.parent!=null&&w.parent.phase()==parent.phase())) {
158 waiting.remove(owner, w);
168 private boolean newNode2(Node p, Node parent, Forest pending, State state, boolean fromEmptyReduction) {
169 if (p.merge(parent, pending)) return true;
170 p.parents().add(parent, true);
171 if (p!=parent && !fromEmptyReduction && reducing) p.performReductions(parent);
175 private boolean newNode3(Node parent, Forest pending, State state, boolean fromEmptyReduction) {
177 if (token != null && state.canShift(token)) break;
178 if (state.isAccepting()) break;
179 if (token==null) break;
180 if (!state.canReduce(token)) return false;
181 //if (count > 1) break;
182 //if (r.numPop == 0) break;
183 //r.reduce(pending, parent, null, Phase.this, null);
187 Node n = new Node(parent, pending, state); // ALLOC
189 n.performEmptyReductions();
190 if (!fromEmptyReduction) n.performReductions(parent);
195 /** perform all reduction operations */
196 public void reduce() throws ParseFailed {
199 if (reducing_list==null || reducing_list.length < hash.size())
200 reducing_list = new Phase.Node[hash.size() * 4];
201 hash.toArray(reducing_list);
202 int num = hash.size();
203 for(int i=0; i<num; i++) {
204 Node n = reducing_list[i];
205 n.performEmptyReductions();
206 // INVARIANT: we never "see" a node until its parent-set is complete, modulo merges
208 for(int i=0; i<num; i++) {
209 Node n = reducing_list[i];
210 reducing_list[i] = null;
211 n.performReductions();
218 for(int i : expected)
219 for(Sequence s : expected.getAll(i))
220 if (!performed.contains(i, s)) {
221 //System.out.println("resetting due to pos="+i+": " + s + " " + System.identityHashCode(s));
232 private boolean reset = false;
233 class Reset extends RuntimeException { }
235 /** perform all shift operations, adding promoted nodes to <tt>next</tt> */
236 public void shift(Phase next, Forest result) throws ParseFailed {
237 // this massively improves GC performance
238 if (prev!=null && parser.helpgc) {
245 for(Phase.Node n : hash.values()) {
246 if (token == null && n.state.isAccepting()) {
247 if (finalResult==null) finalResult = new Forest.Many();
248 for(Object f : n.results())
249 finalResult.merge((Forest)f);
251 if (token == null) continue;
252 n.state.invokeShifts(token, this, result, n);
255 if (!good && token!=null)
256 throw new ParseFailed(ParseFailed.error(("unexpected character ")+" \'"+
257 ANSI.purple(StringUtil.escapify(token+"", "\\\'\r\n"))+
258 "\' encountered at "+
259 ANSI.green(next.getRegion())+"\n", token, hash.values()),
260 next.getRegion(), input);
261 if (token==null && finalResult==null)
262 throw new ParseFailed(ParseFailed.error(("unexpected end of file at "+getLocation()+"\n"), token, hash.values()),
263 getLocation().createRegion(getLocation()), input);
271 boolean fromEmptyReduction;
273 public Waiting(Node parent, Forest pending, State state, boolean fromEmptyReduction, Position reduction) {
275 this.parent = parent;
276 this.pending = pending;
278 this.fromEmptyReduction = fromEmptyReduction;
279 this.reduction = reduction;
281 public void perform() {
282 //System.out.println("performing: " + reduction.position);
283 newNode(parent, pending, state, fromEmptyReduction, reduction);
287 // Node /////////////////////////////////////////////////////////////////////////////////
289 /** a node in the GSS */
290 final class Node implements Invokable<Position, Node, Node>, IntegerMappable, GraphViz.ToGraphViz {
291 public FastSet<Node> set = new FastSet<Node>();
294 private boolean allqueued = false;
296 /** what state this node is in */
297 public final Parser.Table<Tok>.State<Tok> state;
299 /** which Phase this Node belongs to (node that Node is also a non-static inner class of Phase) */
300 public Phase phase() { return Phase.this; }
302 private HashSet<Forest.Many> resultMap = new HashSet<Forest.Many>();
303 public Iterable<Forest.Many> results() { return resultMap; }
304 public FastSet<Node> parents() { return set; }
305 public boolean merge(Node parent, Forest result) {
306 // FIXME: inefficient!
307 for(Forest.Many f : results()) {
308 if (f.parents.contains(parent) /* UGLY: */ && f.parents.size()==1) {
313 Forest.Many f = new Forest.Many();
314 f.parents.add(parent);
317 set.add(parent, true);
321 public void performReductions() {
322 if (allqueued) return;
324 state.invokeReductions(token, this, this, null);
327 public void performReductions(Node n2) {
328 if (!allqueued) performReductions();
329 else state.invokeReductions(token, this, this, n2);
332 public void performEmptyReductions() { state.invokeReductions(token, this, null, null); }
333 public final void invoke(Position r, Node n, Node n2) {
335 if (n==null || n2==null || r.pos==0) {
337 if (n==null) n = this;
341 Forest[] holder = new Forest[r.pos];
342 if (r.pos==0) n.finish(r, r.zero(n.phase().getLocation().createRegion(n.phase().getLocation())), n.phase());
343 else n.reduce(r, r.pos-1, n.phase(), null);
345 if (r.pos<=0) throw new Error("called wrong form of reduce()");
347 n.reduce(r, pos, n.phase(), n2);
351 public void reduce(Position r, int pos, Phase target, Node only) {
352 Forest[] holder = r.holder;
353 Forest old = holder[pos];
355 HashSet<Forest> rr = new HashSet<Forest>();
356 for(Forest result : results()) rr.add(result);
357 for(Forest result : rr)
358 for(Node child : ((Forest.Many<?>)result).parents) {
359 if (only != null && child!=only) continue;
360 holder[pos] = result;
361 if (pos==0) child.finish(r, r.rewrite(child.phase().getLocation().createRegion(target.getLocation())), target);
362 else child.reduce(r, pos-1, target, null);
368 public void finish(Position r, Forest result, Phase<Tok> target) {
369 Parser.Table<Tok>.State<Tok> state0 = state.gotoSetNonTerminals.get(r.owner());
370 if (result==null) throw new Error();
372 target.newNode(this, result, state0, r.pos<=0, r);
375 private Node(Node parent, Forest pending, State state) {
377 this.merge(parent, pending);
378 Phase start = parent==null ? null : parent.phase();
379 if (parent != null) parents().add(parent, true);
380 if (Phase.this.hash.get(state, start) != null) throw new Error("severe problem!");
381 Phase.this.hash.put(state, start, this);
383 public int toInt() { return idx; }
384 private final int idx = node_idx++;
386 // GraphViz //////////////////////////////////////////////////////////////////////////////
388 public GraphViz.Node toGraphViz(GraphViz gv) {
389 if (gv.hasNode(this)) return gv.createNode(this);
390 GraphViz.Node n = gv.createNode(this);
391 n.label = ""+state.toStringx();
392 n.shape = "rectangle";
394 for(Forest result : results()) n.edge(result, "");
395 for(Node parent : parents()) n.edge(parent, "");
396 ((GraphViz.Group)phase().toGraphViz(gv)).add(n);
399 public boolean isTransparent() { return false; }
400 public boolean isHidden() { return false; }
403 private int node_idx = 0;
405 public int toInt() { return pos+1; }
406 public int size() { return hash==null ? 0 : hash.size(); }
408 // GraphViz //////////////////////////////////////////////////////////////////////////////
410 public GraphViz.Node toGraphViz(GraphViz gv) {
411 if (gv.hasNode(this)) return gv.createNode(this);
412 GraphViz.Group g = gv.createGroup(this);
413 g.label = "Phase " + pos;
418 public boolean isTransparent() { return false; }
419 public boolean isHidden() { return false; }