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.*;
public HashSet<Element> bottom(Element e) { return acc; }
public HashSet<Element> sequence(Sequence seq) { return bottom(seq); }
public HashSet<Element> walkAtom(Atom r) {
- c.atoms.put(e, c.atoms.get(e)==null ? r.dup() : c.atoms.get(e).union(r.dup()));
+ c.atoms.put(e, c.atoms.get(e)==null ? r : c.atoms.get(e).union(r));
return super.walkAtom(r);
}
}
}
- // Token-Set //////////////////////////////////////////////////////////////////////////////
+ // Input-Set //////////////////////////////////////////////////////////////////////////////
- static abstract class WalkTokenSet<Tok extends Token> extends Walk<Topology<Tok>> {
+ static abstract class WalkTokenSet<Tok extends Input> extends Walk<Topology<Tok>> {
public Topology<Tok> cs;
public WalkTokenSet(Topology<Tok> cs) { this.cs = cs; }
public WalkTokenSet(Topology<Tok> cs, Cache c) { super(c); this.cs = cs; }
public Topology<Tok> bottom(Element e) { return cs; }
- public Topology<Tok> walkAtom(Atom r) { cs.add(r.dup()); return cs; }
+ public Topology<Tok> walkAtom(Atom r) { cs = cs.union(r); return cs; }
}
- class First<Tok extends Token> extends WalkTokenSet<Tok> {
+ static class First<Tok extends Input> extends WalkTokenSet<Tok> {
public First(Topology<Tok> cs, Walk.Cache cache) { super(cs, cache); }
public Topology<Tok> 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<Tok extends Token> extends WalkTokenSet<Tok> {
- public Last(Topology<Tok> cs, Walk.Cache cache) { super(cs, cache); }
- public Topology<Tok> sequence(Sequence seq) { sequence(seq.firstp()); return cs; }
- private Topology<Tok> sequence(Position p) {
- if (p==null) return null;
- Topology<Tok> 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<Tok extends Token> extends WalkTokenSet<Tok> {
+ static class Follow<Tok extends Input> extends WalkTokenSet<Tok> {
private final Element me;
private final HashSet<Element> all;
private boolean eof = false;
if (c != null) {
Topology<Tok> cached = (Topology<Tok>)c.follow.get(e);
if (cached != null) {
- cs.add(cached);
+ cs = cs.union(cached);
eof |= c.eof.get(e);
return cs;
}
Topology<Tok> cso = cs;
boolean eofo = eof;
- eof = false;
- cs = cso.fresh();
+ 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) {
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.add(new First<Tok>(cs.fresh(), 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).dup()))) 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;
if (e instanceof Sequence) {
Sequence s = (Sequence)e;
- if (s.noFollow() != null) cs.remove(s.noFollow().dup());
+ if (s.follow() != null) cs = cs.intersect(s.follow());
}
if (c != null && e==me) {
- c.follow.put(e, cs.dup());
+ c.follow.put(e, cs);
c.eof.put(e, eof);
}
- cso.add(cs);
+ cso = cso.union(cs);
cs = cso;
eofo |= eof;
eof = eofo;
public final HashMap<Element,Boolean> possiblyEpsilon = new HashMap<Element,Boolean>();
public HashMap<Element,Boolean> eof = new HashMap<Element,Boolean>();
public HashMap<Element,Topology> follow = new HashMap<Element,Topology>();
- public HashMap<Element,HashSet<Element>> ys = new HashMap<Element,HashSet<Element>>();
+ public HashMapBag<Element,Element> ys = new HashMapBag<Element,Element>();
public HashMap<Element,Topology> atoms = new HashMap<Element,Topology>();
+ public <Tok extends Input> Topology<Tok> first(Element e, Topology<Tok> empty) {
+ return new Walk.First<Tok>(empty, this).walk(e);
+ }
+ final boolean possiblyEpsilon(Element 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;
+ }
}
}