X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2FWalk.java;h=092d1ffff0fef4022fdeb4e5455969fa578617bf;hp=4f133a4d47865640ef8e73da7597b531631129a7;hb=3ee451bce342d4bb61ad6235ba57bdf817bfdd1a;hpb=6b53048f4413f3c618acc3581d0b4f60a236a9bc diff --git a/src/edu/berkeley/sbp/Walk.java b/src/edu/berkeley/sbp/Walk.java index 4f133a4..092d1ff 100644 --- a/src/edu/berkeley/sbp/Walk.java +++ b/src/edu/berkeley/sbp/Walk.java @@ -1,17 +1,18 @@ +// Copyright 2006 all rights reserved; see LICENSE file for BSD-style license + package edu.berkeley.sbp; -import edu.berkeley.sbp.util.*; -import edu.berkeley.sbp.*; import edu.berkeley.sbp.*; +import edu.berkeley.sbp.util.*; import edu.berkeley.sbp.Sequence.Position; import java.io.*; 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); } @@ -27,31 +28,37 @@ 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) { T ret = bottom(e); - for(Sequence s : (Union)e) ret = union((Union)e, ret, walk(s)); + 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); } } - 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); } @@ -64,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); @@ -76,51 +83,37 @@ abstract class Walk { } - // Token-Set ////////////////////////////////////////////////////////////////////////////// + // Input-Set ////////////////////////////////////////////////////////////////////////////// - static abstract class WalkTokenSet extends Walk> { + static abstract class WalkTokenSet extends 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; } } - class First extends WalkTokenSet { + static class First extends WalkTokenSet { public First(Topology cs, Walk.Cache cache) { super(cs, cache); } public Topology sequence(Sequence seq) { for(Position p = seq.firstp(); p!=null && !p.isLast(); p = p.next()) { walk(p.element()); - if (!p.element().possiblyEpsilon(c)) break; + if (!c.possiblyEpsilon(p.element())) break; } return cs; } } - class Last extends WalkTokenSet { - public Last(Topology cs, Walk.Cache cache) { super(cs, cache); } - public Topology sequence(Sequence seq) { sequence(seq.firstp()); return cs; } - private Topology sequence(Position p) { - if (p==null) return null; - Topology ret = sequence(p.next()); - if (ret!=null) return ret; - if (p.isLast()) return null; - if (p.element().possiblyEpsilon(c)) return null; - if (p.element()==null) return null; - return walk(p.element()); - } - } - - static class Follow extends WalkTokenSet { - private final Element me; - private final HashSet all; + static class Follow extends WalkTokenSet { + 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); @@ -135,26 +128,24 @@ abstract class Walk { Topology cso = cs; boolean eofo = eof; - eof = false; + eof = c.eof.get(e) != null && c.eof.get(e).booleanValue(); cs = cso.empty(); - if (e instanceof Parser.Table.Top) eof = true; - for(Element x : all) { + for(SequenceOrElement x : all) { boolean matched = false; - if (x instanceof Parser.Table.Top) walk(x); // because this symbol might not appear in any other Sequence if (!(x instanceof Sequence)) continue; Sequence a = (Sequence)x; Position mp = null; for(Position pos = a.firstp(); pos != null && !pos.isLast(); pos = pos.next()) { - if (matched) cs = cs.union(new First(cs.empty(), c).walk(pos.element())); - if (pos.isLast()) { matched = (matched && pos.element().possiblyEpsilon(c)); continue; } + if (matched) cs = cs.union(c.first(pos.element(), cs.empty())); + if (pos.isLast()) { matched = (matched && c.possiblyEpsilon(pos.element())); continue; } boolean good = false; if (e instanceof Atom) { Topology top = c.atoms.get(pos.element()); if (top==null) continue; if (!(top.containsAll(((Atom)e)))) continue; } else { - if (c.ys.get(pos.element()).contains(e)) good = true; + if (c.ys.contains(pos.element(),e)) good = true; } if (good) { mp = pos; @@ -166,7 +157,7 @@ abstract class Walk { if (e instanceof Sequence) { Sequence s = (Sequence)e; - if (s.noFollow() != null) cs = cs.minus(s.noFollow()); + if (s.follow != null) cs = cs.intersect(s.follow.getTokenTopology()); } if (c != null && e==me) { @@ -184,10 +175,21 @@ abstract class Walk { } static class Cache { - public final HashMap possiblyEpsilon = new HashMap(); - public HashMap eof = new HashMap(); - public HashMap follow = new HashMap(); - public HashMap> ys = new HashMap>(); - public HashMap atoms = new HashMap(); + 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(SequenceOrElement e) { + Walk.Cache cache = this; + Boolean ret = possiblyEpsilon.get(e); + if (ret != null) return ret.booleanValue(); + ret = new Walk.PossiblyEpsilon().walk(e) ? Boolean.TRUE : Boolean.FALSE; + possiblyEpsilon.put(e, ret); + return ret; + } } }