X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2FWalk.java;fp=src%2Fedu%2Fberkeley%2Fsbp%2FWalk.java;h=3d2346999e7c73deb178971d888fdf2efd7fa3b2;hp=a82cf522915c115eeb5a8245b73920178c117131;hb=111166986ad83b54d0cae5c03c2304d23e332f29;hpb=75d0fa39d405292f4b831a6d1743f2aeea01ebd4 diff --git a/src/edu/berkeley/sbp/Walk.java b/src/edu/berkeley/sbp/Walk.java index a82cf52..3d23469 100644 --- a/src/edu/berkeley/sbp/Walk.java +++ b/src/edu/berkeley/sbp/Walk.java @@ -7,10 +7,10 @@ import java.util.*; import java.lang.reflect.*; import java.lang.ref.*; -/** a traversal of the grammar performed by mapping from Elements to a lattice and computing the resulting LUB */ +/** a traversal of the grammar performed by mapping from SequenceOrElements to a lattice and computing the resulting LUB */ abstract class Walk { - protected HashSet acc = new HashSet(); - protected abstract T bottom(Element e); + protected HashSet acc = new HashSet(); + protected abstract T bottom(SequenceOrElement e); protected final Cache c; public Walk() { this(null); } @@ -26,13 +26,13 @@ abstract class Walk { public T walkAtom(Atom r) { return walk(r); } public T union(Union u, T a, T b) { return bottom(u); } public T sequence(Sequence s, T a, T b) { return bottom(s); } - protected T walk(Element e) { + protected T walk(SequenceOrElement e) { if (acc.contains(e)) return bottom(e); acc.add(e); return walk2(e); } - protected T walk2(Element e) { + protected T walk2(SequenceOrElement e) { if (e instanceof Atom) return walkAtom((Atom)e); else if (e instanceof Sequence) return sequence((Sequence)e); else if (e instanceof Union) { @@ -50,13 +50,13 @@ abstract class Walk { } } - static class YieldSet extends Walk> { - private final Element e; - public final HashSet walk() { return walk(e); } - public YieldSet(Element e, Cache c) { super(c); this.e = e; } - public HashSet bottom(Element e) { return acc; } - public HashSet sequence(Sequence seq) { return bottom(seq); } - public HashSet walkAtom(Atom r) { + static class YieldSet extends Walk> { + private final SequenceOrElement e; + public final HashSet walk() { return walk(e); } + public YieldSet(SequenceOrElement e, Cache c) { super(c); this.e = e; } + public HashSet bottom(SequenceOrElement e) { return acc; } + public HashSet sequence(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); } @@ -69,9 +69,9 @@ abstract class Walk { public Boolean walkAtom(Atom r) { return false; } public Boolean sequence(Sequence s, Boolean a, Boolean b) { return new Boolean(a && b); } public Boolean union(Union u, Boolean a, Boolean b) { return new Boolean(a || b); } - public Boolean bottom(Element e) { return (e instanceof Union) ? false : true; } - private HashMap hm = new HashMap(); - protected Boolean walk(Element e) { + public Boolean bottom(SequenceOrElement e) { return (e instanceof Union) ? false : true; } + private HashMap hm = new HashMap(); + protected Boolean walk(SequenceOrElement e) { if (hm.get(e) != null) return hm.get(e); hm.put(e, false); Boolean ret = walk2(e); @@ -87,7 +87,7 @@ abstract class Walk { public Topology cs; public WalkTokenSet(Topology cs) { this.cs = cs; } public WalkTokenSet(Topology cs, Cache c) { super(c); this.cs = cs; } - public Topology bottom(Element e) { return cs; } + public Topology bottom(SequenceOrElement e) { return cs; } public Topology walkAtom(Atom r) { cs = cs.union(r.getTokenTopology()); return cs; } } @@ -103,15 +103,15 @@ abstract class Walk { } static class Follow extends WalkTokenSet { - private final Element me; - private final HashSet all; + private final SequenceOrElement me; + private final HashSet all; private boolean eof = false; public boolean includesEof() { return eof; } - public Follow(Topology cs, Element me, HashSet all, Cache c) { super(cs, c); this.me = me; this.all = all; } - public Topology bottom(Element e) { return cs; } + public Follow(Topology cs, SequenceOrElement me, HashSet all, Cache c) { super(cs, c); this.me = me; this.all = all; } + public Topology bottom(SequenceOrElement e) { return cs; } public Topology sequence(Sequence seq) { return cs; } - public Topology walkAtom(Atom r) { return walk((Element)r); } - public Topology walk(Element e) { + public Topology walkAtom(Atom r) { return walk((SequenceOrElement)r); } + public Topology walk(SequenceOrElement e) { if (acc.contains(e)) return bottom(e); acc.add(e); @@ -129,7 +129,7 @@ abstract class Walk { eof = c.eof.get(e) != null && c.eof.get(e).booleanValue(); cs = cso.empty(); - for(Element x : all) { + for(SequenceOrElement x : all) { boolean matched = false; if (!(x instanceof Sequence)) continue; Sequence a = (Sequence)x; @@ -173,15 +173,15 @@ abstract class Walk { } static class Cache { - public final HashMap possiblyEpsilon = new HashMap(); - public HashMap eof = new HashMap(); - public HashMap follow = new HashMap(); - public HashMapBag ys = new HashMapBag(); - public HashMap atoms = new HashMap(); - public Topology first(Element e, Topology empty) { + public final HashMap possiblyEpsilon = new HashMap(); + public HashMap eof = new HashMap(); + public HashMap follow = new HashMap(); + public HashMapBag ys = new HashMapBag(); + public HashMap atoms = new HashMap(); + public Topology first(SequenceOrElement e, Topology empty) { return new Walk.First(empty, this).walk(e); } - final boolean possiblyEpsilon(Element e) { + final boolean possiblyEpsilon(SequenceOrElement e) { Walk.Cache cache = this; Boolean ret = possiblyEpsilon.get(e); if (ret != null) return ret.booleanValue();