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 boolean closed;
42 private Phase next = null;
44 private Token.Location location;
45 public final Parser parser;
47 private Forest forest;
49 public Phase(Phase prev, Parser parser, Phase previous, Tok token, Token.Location location, Forest forest) {
53 this.pos = previous==null ? 0 : previous.pos+1;
55 this.location = location;
63 hash = new IntPairMap<Phase.Node>();
68 if (prev != null) prev.shift(this, forest);
72 public boolean isDone() throws ParseFailed {
73 if (token != null) return false;
74 if (token==null && finalResult==null)
75 throw new ParseFailed(ParseFailed.error(ANSI.red("unexpected end of file\n"), token, hash.values()), getLocation());
79 public Token.Location getLocation() { return location; }
81 /** add a new node (merging with existing nodes if possible)
82 * @param parent the parent of the new node
83 * @param result the SPPF result corresponding to the new node
84 * @param state the state that the new node is in
85 * @param fromEmptyReduction true iff this node is being created as a result of a reduction of length zero (see GRMLR paper)
86 * @param start the earliest part of the input contributing to this node (used to make merging decisions)
88 public boolean newNode(Node parent, Forest pending, State state, boolean fromEmptyReduction) {
89 Node p = hash.get(state, parent==null?null:parent.phase());
90 if (p != null) return newNode2(p, parent, pending, state, fromEmptyReduction);
91 else return newNode3(parent, pending, state, fromEmptyReduction);
93 public void newNode(Node parent, Forest pending, State state, boolean fromEmptyReduction, Position reduction) {
94 int pos = parent==null?0:parent.phase()==null?0:parent.phase().pos;
95 Sequence owner = reduction==null ? null : reduction.owner();
96 if (reduction!=null) {
97 if (inhibited.contains(pos, owner)) return;
98 if (owner.needs != null)
99 for(Sequence s : owner.needs)
100 if (!performed.contains(pos, s)) {
101 waiting.add(s, new Waiting(parent, pending, state, fromEmptyReduction, reduction));
104 if ((owner.needed != null && owner.needed.size()>0) ||
105 (owner.hated != null && owner.hated.size()>0) ||
106 (owner.hates != null && owner.hates.size()>0))
107 performed.add(pos, owner);
110 newNode(parent, pending, state, fromEmptyReduction);
111 if (reduction!=null) inhibit(reduction, parent==null?0:parent.phase().pos);
112 if (reduction != null) {
116 for(Waiting w : waiting.getAll(owner)) {
117 if (w.parent==parent || (parent!=null&&w.parent!=null&&w.parent.phase()==parent.phase())) {
118 waiting.remove(owner, w);
127 private boolean newNode2(Node p, Node parent, Forest pending, State state, boolean fromEmptyReduction) {
128 p.holder.merge(pending);
129 if (p.parents().contains(parent)) return true;
130 p.parents().add(parent, true);
131 if (p!=parent && !fromEmptyReduction && reducing) p.performReductions(parent);
134 private boolean newNode3(Node parent, Forest pending, State state, boolean fromEmptyReduction) {
136 if (token != null && state.canShift(token)) break;
137 if (state.isAccepting()) break;
138 if (token==null) break;
139 if (!state.canReduce(token)) return false;
140 //if (count > 1) break;
141 //if (r.numPop == 0) break;
142 //r.reduce(pending, parent, null, Phase.this, null);
146 Node n = new Node(parent, pending, state); // ALLOC
148 n.performEmptyReductions();
149 if (!fromEmptyReduction) n.performReductions(parent);
154 public void uninhibit(int p, Sequence s) {
156 for(Sequence s2 : s.hated)
157 inhibited.remove(p, s2);
160 public void inhibit(Position r, int p) {
161 if (r.owner().hated == null) return;
162 // remember that dead states are still allowed to shift -- just not allowed to reduce
163 boolean reset = false;
164 for(Sequence seq : r.owner().hated) {
165 if (performed.contains(p,seq)) {
167 //System.out.println("\nresetting due to " + r.owner() + " killing " + seq);
169 inhibited.add(p, seq);
170 //inhibited = new HashMapBag<Integer,Sequence>();
175 inhibited.add(p, seq);
179 /** perform all reduction operations */
180 public void reduce() {
183 if (reducing_list==null || reducing_list.length < hash.size())
184 reducing_list = new Phase.Node[hash.size() * 4];
185 hash.toArray(reducing_list);
186 int num = hash.size();
187 for(int i=0; i<num; i++) {
188 Node n = reducing_list[i];
189 n.performEmptyReductions();
190 // INVARIANT: we never "see" a node until its parent-set is complete, modulo merges
192 for(int i=0; i<num; i++) {
193 Node n = reducing_list[i];
194 reducing_list[i] = null;
195 n.performReductions();
203 class Reset extends RuntimeException { }
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 Parser.Table<Tok>.State<Tok> 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, 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(Node child : this.parents()) child.finish(r, rex, target, holder);
315 for(Node child : this.parents()) child.reduce(r, pos-1, target, holder);
320 public void finish(Position r, Forest result, Phase<Tok> target, Forest[] holder) {
321 Parser.Table<Tok>.State<Tok> 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(); }