- public Token.Location getLocation() { return location; }
-
- /** add a new node (merging with existing nodes if possible)
- * @param parent the parent of the new node
- * @param result the SPPF result corresponding to the new node
- * @param state the state that the new node is in
- * @param fromEmptyReduction true iff this node is being created as a result of a reduction of length zero (see GRMLR paper)
- * @param start the earliest part of the input contributing to this node (used to make merging decisions)
- */
- public boolean newNode(Node parent, Forest pending, State state, boolean fromEmptyReduction) {
- return newNode(parent, pending, state, fromEmptyReduction, null); }
- public boolean newNode(Node parent, Forest pending, State state, boolean fromEmptyReduction, Reduction reduction) {
- int pos = parent==null?0:parent.phase()==null?0:parent.phase().pos;
- if (reduction!=null) {
- if (inhibited.contains(pos, reduction.position.owner())) return false;
- if (reduction.position.owner().needs != null) {
- for(Sequence s : reduction.position.owner().needs) {
- if (!performed.contains(pos, s)) {
- waiting.add(s, new Waiting(parent, pending, state, fromEmptyReduction, reduction));
- return false;
- }
- }
- }
- if ((reduction.position.owner().needed != null && reduction.position.owner().needed.size()>0) ||
- (reduction.position.owner().hated != null && reduction.position.owner().hated.size()>0) ||
- (reduction.position.owner().hates != null && reduction.position.owner().hates.size()>0))
- performed.add(pos, reduction.position.owner());
- }
- Node p = hash.get(code(state, parent==null?null:parent.phase()));
- boolean ret;
- if (reduction!=null) inhibit(reduction, parent==null?0:parent.phase().pos);
- if (p != null) ret = newNode2(p, parent, pending, state, fromEmptyReduction, reduction);
- else ret = newNode3(parent, pending, state, fromEmptyReduction, reduction);
- if (reduction != null) {
- boolean redo = true;
- while(redo) {
- redo = false;
- for(Waiting w : waiting.getAll(reduction.position.owner())) {
- if (w.parent==parent || (parent!=null&&w.parent!=null&&w.parent.phase()==parent.phase())) {
- waiting.remove(reduction.position.owner(), w);
- w.perform();
- redo = true;
- break;
- }
- }
- }
- }
- return ret;
- }
- private boolean newNode2(Node p, Node parent, Forest pending, State state, boolean fromEmptyReduction, Reduction reduction) {
- p.holder.merge(pending);
- if (p.parents().contains(parent)) return true;
- //if (p.fe && p.phase() != parent.phase()) throw new Error("yep yep");
- //if (!p.fe && p.phase() == parent.phase()) throw new Error("yep yep2");
- p.parents().add(parent, true);
- if (p!=parent && !fromEmptyReduction) p.queueReductions(parent);
- return true;
- }
- private boolean newNode3(Node parent, Forest pending, State state, boolean fromEmptyReduction, Reduction reduction) {
- do {
- if (token != null && state.canShift(token)) break;
- if (state.isAccepting()) break;
- if (token==null) break;
- //if (!state.canReduce(token)) return false;
- //if (count > 1) break;
- //if (r.numPop == 0) break;
- //r.reduce(pending, parent, null, Phase.this, null);
- //return;
- } while(false);
-
- Node n = new Node(parent, pending, state, fromEmptyReduction); // ALLOC
- n.queueEmptyReductions();
- if (!fromEmptyReduction) n.queueReductions(parent);
- return true;
- }
-
- public void uninhibit(int p, Sequence s) {
- if (s.hated!=null)
- for(Sequence s2 : s.hated)
- inhibited.remove(p, s2);
- }
-
- public void inhibit(Reduction r, int p) {
- if (r.position.owner().hated == null) return;
- // remember that dead states are still allowed to shift -- just not allowed to reduce
- boolean reset = false;
- for(Sequence seq : r.position.owner().hated) {
- if (performed.contains(p,seq)) {
- uninhibit(p, seq);
- //System.out.println("\nresetting due to " + r.position.owner() + " killing " + seq);
- if (!reset) inhibited.add(p, seq);
- reset = true;
- }
- if (!reset) inhibited.add(p, seq);
- }
- if (reset) {
- resets++;
- throw new Reset();
- }
- }
-
- /** perform all reduction operations */
- public void reduce() {
- try {
- reducing = true;
- if (reducing_list==null || reducing_list.length < hash.size())
- reducing_list = new Phase.Node[hash.size() * 4];
- Collection<Node> hv = hash.values();
- hv.toArray(reducing_list);
- int num = hv.size();
- for(int i=0; i<num; i++) {
- Node n = reducing_list[i];
- n.queueEmptyReductions();
- // INVARIANT: we never "see" a node until its parent-set is complete, modulo merges
- }
- for(int i=0; i<num; i++) {
- Node n = reducing_list[i];
- reducing_list[i] = null;
- n.queueReductions();
- }
- } catch (Reset r) {
- reset();
- reduce();
- }
- }
-
- class Reset extends RuntimeException { }
-
- public void invoke(State st, Forest result, Node n) {
- good |= next.newNode(n, result, st, false);
- }