add LabelNode
[sbp.git] / src / edu / berkeley / sbp / GSS.java
index db833b9..81e4909 100644 (file)
@@ -18,16 +18,13 @@ class GSS {
     public GSS(Input input, Parser parser) { this.input = input; this.parser = parser;}
     public Input getInput() { return input; }
 
-    /*
-    HashSet<Reduction> finishedReductions = new HashSet<Reduction>();
-    */
     int numNewNodes = 0;
     int numOldNodes = 0;
     int viewPos = 0;
     int numReductions = 0;
 
     /** corresponds to a positions <i>between tokens</i> the input stream; same as Tomita's U_i's */
-    class Phase<Tok> implements Invokable<State, StateNode, Forest>, IntegerMappable, GraphViz.ToGraphViz, Iterable<StateNode> {
+    class Phase<Tok> implements Invokable<State, StateNode, Forest>, IntegerMappable, GraphViz.ToGraphViz /*, Iterable<StateNode>*/ {
 
         // FIXME: right now, these are the performance bottleneck
         private HashMapBag<Integer,Integer>       performed       = new HashMapBag<Integer,Integer>();
@@ -44,7 +41,7 @@ class GSS {
 
         public void invoke(State st, StateNode pred, Forest f) {
             parser.spin();
-            good |= next.newNode(f, null, pred, st, false);
+            good |= next.newNode(f, null, pred, st);
         }
 
         /** the token immediately after this phase */
@@ -61,7 +58,7 @@ class GSS {
 
         public Phase(State startState) throws ParseFailed, IOException {
             this(null, null);
-            newNode(null, null, null, startState, true);
+            newNode(null, null, null, startState);
         }
         public Phase(Phase prev, Forest forest) throws ParseFailed, IOException {
             this.location = input.getLocation();
@@ -131,10 +128,10 @@ class GSS {
                 for(StateNode n : h) n.check();
             }
             numOldNodes = hash.size();
-            for(StateNode n : hash.values()) {
+            for(StateNode n : hash) {
                 if (token == null && n.state().isAccepting()) {
                     if (finalResult==null) finalResult = new Forest.Many();
-                    for(Result r : n)
+                    for(ResultNode r : n)
                         finalResult.merge(r.getForest());
                 }
                 if (token == null) continue;
@@ -179,32 +176,32 @@ class GSS {
                 performed.add(pos, reduction.provides());
             Parser.Table.State state = (Parser.Table.State)pred.state().gotoSetNonTerminals.get(reduction);
             if (state!=null)
-                newNode(f, reduction, pred, state, reduction.numPops()<=0);
+                newNode(f, reduction, pred, state);
         }
 
         /** 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)
          */
-        private boolean newNode(Forest f, Pos reduction, StateNode pred, State state, boolean fromEmptyReduction) {
+        private boolean newNode(Forest f, Pos reduction, StateNode pred, State state) {
             StateNode p = pred==null ? null : hash.get(state, pred.phase());
             if (p != null) {
-                p.addResult(f, reduction, pred);
+                p.addPred(f, reduction, pred);
                 return !state.doomed();
             }
             do {
+                // optimizations
                 if (token != null && state.canShift(token)) break;
                 if (state.isAccepting()) break;
                 if (token==null) break;
                 if (!state.canReduce(token)) return false;
             } while(false);
-            StateNode n = new StateNode(Phase.this, f, reduction, pred, state, fromEmptyReduction);  // ALLOC
+            StateNode n = new StateNode(Phase.this, new ResultNode(f, reduction, pred), state);  // ALLOC
             /** FIXME: this null-result can be used to notice bogus/dead states */
             for(Object s : state.conjunctStates)
-                newNode(null, null, n, (State)s, fromEmptyReduction);
+                newNode(null, null, n, (State)s);
             return !n.state().doomed();
         }
 
@@ -212,7 +209,7 @@ class GSS {
         public int size() { return hash==null ? 0 : hash.size(); }
         public int pos() { return pos; }
         public Tok getToken() { return token; }
-        public Iterator<StateNode> iterator() { return hash.iterator(); }
+        //public Iterator<StateNode> iterator() { return hash.iterator(); }
         public GSS getGSS() { return GSS.this; }
 
         // GraphViz //////////////////////////////////////////////////////////////////////////////
@@ -232,8 +229,10 @@ class GSS {
             FileOutputStream fos = new FileOutputStream(filename);
             PrintWriter p = new PrintWriter(new OutputStreamWriter(fos));
             GraphViz gv = new GraphViz();
+            /*
             for(Object n : this)
                 ((StateNode)n).toGraphViz(gv);
+            */
             gv.dump(p);
             p.flush();
             p.close();