-
- /** 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) {
- Node p = hash.get(state, parent==null?null:parent.phase());
- if (p != null) return newNode2(p, parent, pending, state, fromEmptyReduction);
- else return newNode3(parent, pending, state, fromEmptyReduction);
- }
- public void newNode(Node parent, Forest pending, State state, boolean fromEmptyReduction, Position reduction) {
- int pos = parent==null?0:parent.phase()==null?0:parent.phase().pos;
- Sequence owner = reduction==null ? null : reduction.owner();
- if (reduction!=null) {
- if (owner.hates!=null) {
- for (Sequence s : performed.getAll(pos))
- if (owner.hates.contains(s))
- return;
- for (Sequence s : lastperformed.getAll(pos))
- if (owner.hates.contains(s)) {
- //System.out.println("now expecting ["+pos+"] => " + s);
- expected.add(pos, s);
- return;
- }
- }
- if (owner.needs != null)
- for(Sequence s : owner.needs)
- if (!performed.contains(pos, s)) {
- waiting.add(s, new Waiting(parent, pending, state, fromEmptyReduction, reduction));
- return;
- }
- if (!performed.contains(pos, owner)) {
- performed.add(pos, owner);
- if (owner.hated != null)
- for(Sequence seq : owner.hated)
- if (performed.contains(pos, seq)) {
- performed.remove(pos, seq);
- reset = true;
- }
- }
- }
- newNode(parent, pending, state, fromEmptyReduction);
- if (reduction != null) {
- boolean redo = true;
- while(redo) {
- redo = false;
- for(Waiting w : waiting.getAll(owner)) {
- if (w.parent==parent || (parent!=null&&w.parent!=null&&w.parent.phase()==parent.phase())) {
- waiting.remove(owner, w);
- w.perform();
- redo = true;
- break;
- }
- }
- }
- }
- }
-
- private boolean newNode2(Node p, Node parent, Forest pending, State state, boolean fromEmptyReduction) {
- if (p.merge(parent, pending)) return true;
- p.parents().add(parent, true);
- if (p!=parent && !fromEmptyReduction && reducing) p.performReductions(parent);
- return true;
- }
-
- private boolean newNode3(Node parent, Forest pending, State state, boolean fromEmptyReduction) {
- 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); // ALLOC
- if (reducing) {
- n.performEmptyReductions();
- if (!fromEmptyReduction) n.performReductions(parent);
- }
- return true;
- }
-
- /** perform all reduction operations */
- public void reduce() throws ParseFailed {
- try {
- reducing = true;
- if (reducing_list==null || reducing_list.length < hash.size())
- reducing_list = new Phase.Node[hash.size() * 4];
- hash.toArray(reducing_list);
- int num = hash.size();
- for(int i=0; i<num; i++) {
- Node n = reducing_list[i];
- n.performEmptyReductions();
- // 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.performReductions();
- }
- if (reset) {
- reset = false;
- resets++;
- throw new Reset();
- }
- for(int i : expected)
- for(Sequence s : expected.getAll(i))
- if (!performed.contains(i, s)) {
- //System.out.println("resetting due to pos="+i+": " + s + " " + System.identityHashCode(s));
- resets++;
- throw new Reset();
- }
- } catch (Reset r) {
- reset();
- reduce();
- }
- count = 0;
- }
-
- private boolean reset = false;
- class Reset extends RuntimeException { }