X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2FWalk.java;h=fe09158dc726b69999b2252490fb2694087d6313;hb=6ff6d681e214e91ca3fa5afdff60a0fb88227404;hp=b58dbc5ae926eb42b743b90965d2b0cab149816a;hpb=ae0cef03f2e46f6ae6438f9a3e60ca36ff1a4643;p=sbp.git diff --git a/src/edu/berkeley/sbp/Walk.java b/src/edu/berkeley/sbp/Walk.java index b58dbc5..fe09158 100644 --- a/src/edu/berkeley/sbp/Walk.java +++ b/src/edu/berkeley/sbp/Walk.java @@ -1,3 +1,5 @@ +// Copyright 2006 all rights reserved; see LICENSE file for BSD-style license + package edu.berkeley.sbp; import edu.berkeley.sbp.*; import edu.berkeley.sbp.util.*; @@ -7,10 +9,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 +28,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 +52,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 +71,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,8 +89,8 @@ 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 walkAtom(Atom r) { cs = cs.union(r); return cs; } + public Topology bottom(SequenceOrElement e) { return cs; } + public Topology walkAtom(Atom r) { cs = cs.union(r.getTokenTopology()); return cs; } } static class First extends WalkTokenSet { @@ -103,15 +105,16 @@ 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 boolean includesEof() { return eof; } + 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 +132,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; @@ -155,7 +158,7 @@ abstract class Walk { if (e instanceof Sequence) { Sequence s = (Sequence)e; - if (s.follow() != null) cs = cs.intersect(s.follow()); + if (s.follow != null) cs = cs.intersect(s.follow.getTokenTopology()); } if (c != null && e==me) { @@ -173,15 +176,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();