X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2FWalk.java;h=e2ac662a0e18da8aefd37b46087809e60c7cf875;hp=a92bcb8eaa6d875f243b3e74d76398e169c552d1;hb=a2008a0c57702f49ed7f8be682e4e29484fded38;hpb=0a6dfdc3fab03203f220faac67d5a71b6fdbeae9;ds=sidebyside diff --git a/src/edu/berkeley/sbp/Walk.java b/src/edu/berkeley/sbp/Walk.java index a92bcb8..e2ac662 100644 --- a/src/edu/berkeley/sbp/Walk.java +++ b/src/edu/berkeley/sbp/Walk.java @@ -39,13 +39,8 @@ abstract class Walk { 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,55 @@ abstract class Walk { c.atoms.put(e, c.atoms.get(e)==null ? r : c.atoms.get(e).union(r)); return super.walkAtom(r); } + protected HashSet walk2(SequenceOrElement e) { + HashSet 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> { + private final SequenceOrElement e; + public final HashSet walk() { return walk(e); } + public YieldSet2(SequenceOrElement e, Cache c) { super(c); this.e = e; } + public HashSet bottom(SequenceOrElement e) { return acc; } + public HashSet walkSequence(Sequence seq) { return bottom(seq); } + public HashSet 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> { + private final Sequence s; + private final HashSet eq = new HashSet(); + public final HashSet walk() { return walk(s); } + public EquivalentTo(Sequence e, Cache c) { + super(c); this.s = e; + } + public HashSet bottom(SequenceOrElement e) { return eq; } + public HashSet walkSequence(Sequence seq) { + eq.add(seq); + Position p = seq.firstp(); + for(; !p.isLast(); p = p.next()) { + if (!p.isLast() && isRightNullable(p.next())) + walk(p.element()); + if (!c.possiblyEpsilon(p.element())) break; + } + return eq; + } + public HashSet walkAtom(Atom r) { + return eq; + } + private boolean isRightNullable(Position p) { + if (p.isLast()) return true; + if (!c.possiblyEpsilon(p.element())) return false; + return isRightNullable(p.next()); + } } @@ -82,6 +126,34 @@ abstract class Walk { } } + static class EpsilonFollowSet extends Walk { + Atom all; + Atom empty; + public EpsilonFollowSet(Atom a, Atom empty, Cache c) { + super(c); + this.all = all; + this.empty = empty; + } + public Atom walkAtom(Atom r) { return all; } + public Atom walkSequence(Sequence s) { + if (s.follow==null) return all; + return s.follow; + } + public Atom sequence(Sequence s, Atom a, Atom b) { + throw new RuntimeException(); + } + public Atom union(Union u, Atom a, Atom b) { + /* + if (a==null) return b; + if (b==null) return a; + */ + if (a==null || b==null) return all; + return (Atom)a.union(b); + } + public Atom bottom(SequenceOrElement e) { + return (e instanceof Union) ? empty : all; + } + } // Input-Set ////////////////////////////////////////////////////////////////////////////// @@ -93,6 +165,7 @@ abstract class Walk { public Topology walkAtom(Atom r) { cs = cs.union(r.getTokenTopology()); return cs; } } + // feature: intersect with "first" set of all positive conjuncts static class First extends WalkTokenSet { public First(Topology cs, Walk.Cache cache) { super(cs, cache); } public Topology walkSequence(Sequence seq) { @@ -180,6 +253,7 @@ abstract class Walk { public HashMap eof = new HashMap(); public HashMap follow = new HashMap(); public HashMapBag ys = new HashMapBag(); + public HashMapBag ys2 = new HashMapBag(); public HashMap atoms = new HashMap(); public Topology first(SequenceOrElement e, Topology empty) { return new Walk.First(empty, this).walk(e);