1 package edu.berkeley.sbp;
2 import edu.berkeley.sbp.util.*;
3 import edu.berkeley.sbp.*;
4 import edu.berkeley.sbp.*;
5 import edu.berkeley.sbp.Sequence.Position;
8 import java.lang.reflect.*;
9 import java.lang.ref.*;
11 /** a traversal of the grammar performed by mapping from Elements to a lattice and computing the resulting LUB */
12 abstract class Walk<T> {
13 protected HashSet<Element> acc = new HashSet<Element>();
14 protected abstract T bottom(Element e);
16 protected final Cache c;
17 public Walk() { this(null); }
18 public Walk(Cache c) { this.c = c; }
20 public T sequence(Sequence s) {
22 for(Position p = s.firstp(); p!=null && !p.isLast(); p = p.next())
23 ret = sequence(s, ret, walk(p.element()));
27 public T walkAtom(Atom r) { return walk(r); }
28 public T union(Union u, T a, T b) { return bottom(u); }
29 public T sequence(Sequence s, T a, T b) { return bottom(s); }
30 protected T walk(Element e) {
31 if (acc.contains(e)) return bottom(e);
36 protected T walk2(Element e) {
37 if (e instanceof Atom) return walkAtom((Atom)e);
38 else if (e instanceof Sequence) return sequence((Sequence)e);
39 else if (e instanceof Union) {
41 for(Sequence s : (Union)e) ret = union((Union)e, ret, walk(s));
44 throw new Error("unknown element of class " + e.getClass().getName() + ": " + e);
48 static class YieldSet extends Walk<HashSet<Element>> {
49 private final Element e;
50 public final HashSet<Element> walk() { return walk(e); }
51 public YieldSet(Element e, Cache c) { super(c); this.e = e; }
52 public HashSet<Element> bottom(Element e) { return acc; }
53 public HashSet<Element> sequence(Sequence seq) { return bottom(seq); }
54 public HashSet<Element> walkAtom(Atom r) {
55 c.atoms.put(e, c.atoms.get(e)==null ? r : c.atoms.get(e).union(r));
56 return super.walkAtom(r);
61 // Boolean //////////////////////////////////////////////////////////////////////////////
63 static class PossiblyEpsilon extends Walk<Boolean> {
64 public Boolean walkAtom(Atom r) { return false; }
65 public Boolean sequence(Sequence s, Boolean a, Boolean b) { return new Boolean(a && b); }
66 public Boolean union(Union u, Boolean a, Boolean b) { return new Boolean(a || b); }
67 public Boolean bottom(Element e) { return (e instanceof Union) ? false : true; }
68 private HashMap<Element,Boolean> hm = new HashMap<Element,Boolean>();
69 protected Boolean walk(Element e) {
70 if (hm.get(e) != null) return hm.get(e);
72 Boolean ret = walk2(e);
79 // Token-Set //////////////////////////////////////////////////////////////////////////////
81 static abstract class WalkTokenSet<Tok extends Token> extends Walk<Topology<Tok>> {
82 public Topology<Tok> cs;
83 public WalkTokenSet(Topology<Tok> cs) { this.cs = cs; }
84 public WalkTokenSet(Topology<Tok> cs, Cache c) { super(c); this.cs = cs; }
85 public Topology<Tok> bottom(Element e) { return cs; }
86 public Topology<Tok> walkAtom(Atom r) { cs = cs.union(r); return cs; }
89 class First<Tok extends Token> extends WalkTokenSet<Tok> {
90 public First(Topology<Tok> cs, Walk.Cache cache) { super(cs, cache); }
91 public Topology<Tok> sequence(Sequence seq) {
92 for(Position p = seq.firstp(); p!=null && !p.isLast(); p = p.next()) {
94 if (!p.element().possiblyEpsilon(c)) break;
100 static class Follow<Tok extends Token> extends WalkTokenSet<Tok> {
101 private final Element me;
102 private final HashSet<Element> all;
103 private boolean eof = false;
104 public boolean includesEof() { return eof; }
105 public Follow(Topology<Tok> cs, Element me, HashSet<Element> all, Cache c) { super(cs, c); this.me = me; this.all = all; }
106 public Topology<Tok> bottom(Element e) { return cs; }
107 public Topology<Tok> sequence(Sequence seq) { return cs; }
108 public Topology<Tok> walkAtom(Atom r) { return walk((Element)r); }
109 public Topology<Tok> walk(Element e) {
110 if (acc.contains(e)) return bottom(e);
114 Topology<Tok> cached = (Topology<Tok>)c.follow.get(e);
115 if (cached != null) {
116 cs = cs.union(cached);
122 Topology<Tok> cso = cs;
124 eof = c.eof.get(e) != null && c.eof.get(e).booleanValue();
127 for(Element x : all) {
128 boolean matched = false;
129 if (!(x instanceof Sequence)) continue;
130 Sequence a = (Sequence)x;
132 for(Position pos = a.firstp(); pos != null && !pos.isLast(); pos = pos.next()) {
133 if (matched) cs = cs.union(new First<Tok>(cs.empty(), c).walk(pos.element()));
134 if (pos.isLast()) { matched = (matched && pos.element().possiblyEpsilon(c)); continue; }
135 boolean good = false;
136 if (e instanceof Atom) {
137 Topology top = c.atoms.get(pos.element());
138 if (top==null) continue;
139 if (!(top.containsAll(((Atom)e)))) continue;
141 if (c.ys.get(pos.element()).contains(e)) good = true;
148 if (matched) walk(a);
151 if (e instanceof Sequence) {
152 Sequence s = (Sequence)e;
153 if (s.noFollow() != null) cs = cs.minus(s.noFollow());
156 if (c != null && e==me) {
171 public final HashMap<Element,Boolean> possiblyEpsilon = new HashMap<Element,Boolean>();
172 public HashMap<Element,Boolean> eof = new HashMap<Element,Boolean>();
173 public HashMap<Element,Topology> follow = new HashMap<Element,Topology>();
174 public HashMap<Element,HashSet<Element>> ys = new HashMap<Element,HashSet<Element>>();
175 public HashMap<Element,Topology> atoms = new HashMap<Element,Topology>();