fix major bug: create YieldSet2 which does not consider needs/hates
[sbp.git] / src / edu / berkeley / sbp / Walk.java
index 0b19cd4..e2ac662 100644 (file)
@@ -39,13 +39,8 @@ abstract class Walk<T> {
         else if (e instanceof Sequence) return walkSequence((Sequence)e);
         else if (e instanceof Union) {
             T ret = bottom(e);
-            for(Sequence s : (Union)e) {
+            for(Sequence s : (Union)e)
                 ret = union((Union)e, ret, walk(s));
-
-                // FIXME
-                for(Sequence ss : s.needs()) ret = union((Union)e, ret, walk(ss));
-                for(Sequence ss : s.hates()) ret = union((Union)e, ret, walk(ss));
-            }
             return ret;
         } else {
             throw new Error("unknown element of class " + e.getClass().getName() + ": " + e);
@@ -62,6 +57,27 @@ abstract class Walk<T> {
             c.atoms.put(e, c.atoms.get(e)==null ? r : c.atoms.get(e).union(r));
             return super.walkAtom(r);
         }
+        protected HashSet<SequenceOrElement> walk2(SequenceOrElement e) {
+            HashSet<SequenceOrElement> ret = super.walk2(e);
+            if (e instanceof Union)
+                for(Sequence s : (Union)e) {
+                    for(Sequence ss : s.needs()) ret = union((Union)e, ret, walk(ss));
+                    for(Sequence ss : s.hates()) ret = union((Union)e, ret, walk(ss));
+                }
+            return ret;
+        }
+    }
+
+    static class YieldSet2 extends Walk<HashSet<SequenceOrElement>> {
+        private final SequenceOrElement e;
+        public final HashSet<SequenceOrElement> walk() { return walk(e); }
+        public YieldSet2(SequenceOrElement e, Cache c)  { super(c); this.e = e; }
+        public HashSet<SequenceOrElement> bottom(SequenceOrElement e)     { return acc; }
+        public HashSet<SequenceOrElement> walkSequence(Sequence seq) { return bottom(seq); }
+        public HashSet<SequenceOrElement> walkAtom(Atom r) {
+            c.atoms.put(e, c.atoms.get(e)==null ? r : c.atoms.get(e).union(r));
+            return super.walkAtom(r);
+        }
     }
 
     static class EquivalentTo extends Walk<HashSet<Sequence>> {
@@ -149,6 +165,7 @@ abstract class Walk<T> {
         public Topology<Tok> walkAtom(Atom r)          { cs = cs.union(r.getTokenTopology()); return cs; }
     }
 
+    // feature: intersect with "first" set of all positive conjuncts
     static class First<Tok extends Input> extends WalkTokenSet<Tok> {
         public First(Topology<Tok> cs, Walk.Cache cache) { super(cs, cache); }
         public Topology<Tok> walkSequence(Sequence seq) {
@@ -236,6 +253,7 @@ abstract class Walk<T> {
         public HashMap<SequenceOrElement,Boolean> eof = new HashMap<SequenceOrElement,Boolean>();
         public HashMap<SequenceOrElement,Topology> follow = new HashMap<SequenceOrElement,Topology>();
         public HashMapBag<SequenceOrElement,SequenceOrElement>  ys = new HashMapBag<SequenceOrElement,SequenceOrElement>();
+        public HashMapBag<SequenceOrElement,SequenceOrElement>  ys2 = new HashMapBag<SequenceOrElement,SequenceOrElement>();
         public HashMap<SequenceOrElement,Topology> atoms = new HashMap<SequenceOrElement,Topology>();
         public <Tok extends Input> Topology<Tok> first(SequenceOrElement e, Topology<Tok> empty) {
             return new Walk.First<Tok>(empty, this).walk(e);