1 // Copyright 2006 all rights reserved; see LICENSE file for BSD-style license
3 package edu.berkeley.sbp;
4 import edu.berkeley.sbp.*;
5 import edu.berkeley.sbp.util.*;
6 import edu.berkeley.sbp.Sequence.Position;
9 import java.lang.reflect.*;
10 import java.lang.ref.*;
12 /** a traversal of the grammar performed by mapping from SequenceOrElements to a lattice and computing the resulting LUB */
13 abstract class Walk<T> {
14 protected HashSet<SequenceOrElement> acc = new HashSet<SequenceOrElement>();
15 protected abstract T bottom(SequenceOrElement e);
17 protected final Cache c;
18 public Walk() { this(null); }
19 public Walk(Cache c) { this.c = c; }
21 public T walkSequence(Sequence s) {
23 for(Position p = s.firstp(); p!=null && !p.isLast(); p = p.next())
24 ret = sequence(s, ret, walk(p.element()));
28 public T walkAtom(Atom r) { return walk(r); }
29 public T union(Union u, T a, T b) { return bottom(u); }
30 public T sequence(Sequence s, T a, T b) { return bottom(s); }
31 protected T walk(SequenceOrElement e) {
32 if (acc.contains(e)) return bottom(e);
37 protected T walk2(SequenceOrElement e) {
38 if (e instanceof Atom) return walkAtom((Atom)e);
39 else if (e instanceof Sequence) return walkSequence((Sequence)e);
40 else if (e instanceof Union) {
42 for(Sequence s : (Union)e)
43 ret = union((Union)e, ret, walk(s));
46 throw new Error("unknown element of class " + e.getClass().getName() + ": " + e);
50 static class YieldSet extends Walk<HashSet<SequenceOrElement>> {
51 private final SequenceOrElement e;
52 public final HashSet<SequenceOrElement> walk() { return walk(e); }
53 public YieldSet(SequenceOrElement e, Cache c) { super(c); this.e = e; }
54 public HashSet<SequenceOrElement> bottom(SequenceOrElement e) { return acc; }
55 public HashSet<SequenceOrElement> walkSequence(Sequence seq) { return bottom(seq); }
56 public HashSet<SequenceOrElement> walkAtom(Atom r) {
57 c.atoms.put(e, c.atoms.get(e)==null ? r : c.atoms.get(e).union(r));
58 return super.walkAtom(r);
60 protected HashSet<SequenceOrElement> walk2(SequenceOrElement e) {
61 HashSet<SequenceOrElement> ret = super.walk2(e);
62 if (e instanceof Union)
63 for(Sequence s : (Union)e) {
64 for(Sequence ss : s.needs()) ret = union((Union)e, ret, walk(ss));
65 for(Sequence ss : s.hates()) ret = union((Union)e, ret, walk(ss));
71 static class YieldSet2 extends Walk<HashSet<SequenceOrElement>> {
72 private final SequenceOrElement e;
73 public final HashSet<SequenceOrElement> walk() { return walk(e); }
74 public YieldSet2(SequenceOrElement e, Cache c) { super(c); this.e = e; }
75 public HashSet<SequenceOrElement> bottom(SequenceOrElement e) { return acc; }
76 public HashSet<SequenceOrElement> walkSequence(Sequence seq) { return bottom(seq); }
77 public HashSet<SequenceOrElement> walkAtom(Atom r) {
78 c.atoms.put(e, c.atoms.get(e)==null ? r : c.atoms.get(e).union(r));
79 return super.walkAtom(r);
83 static class EquivalentTo extends Walk<HashSet<Sequence>> {
84 private final Sequence s;
85 private final HashSet<Sequence> eq = new HashSet<Sequence>();
86 public final HashSet<Sequence> walk() { return walk(s); }
87 public EquivalentTo(Sequence e, Cache c) {
90 public HashSet<Sequence> bottom(SequenceOrElement e) { return eq; }
91 public HashSet<Sequence> walkSequence(Sequence seq) {
93 Position p = seq.firstp();
94 for(; !p.isLast(); p = p.next()) {
95 if (!p.isLast() && isRightNullable(p.next()))
97 if (!c.possiblyEpsilon(p.element())) break;
101 public HashSet<Sequence> walkAtom(Atom r) {
104 private boolean isRightNullable(Position p) {
105 if (p.isLast()) return true;
106 if (!c.possiblyEpsilon(p.element())) return false;
107 return isRightNullable(p.next());
112 // Boolean //////////////////////////////////////////////////////////////////////////////
114 static class PossiblyEpsilon extends Walk<Boolean> {
115 public Boolean walkAtom(Atom r) { return false; }
116 public Boolean sequence(Sequence s, Boolean a, Boolean b) { return new Boolean(a && b); }
117 public Boolean union(Union u, Boolean a, Boolean b) { return new Boolean(a || b); }
118 public Boolean bottom(SequenceOrElement e) { return (e instanceof Union) ? false : true; }
119 private HashMap<SequenceOrElement,Boolean> hm = new HashMap<SequenceOrElement,Boolean>();
120 protected Boolean walk(SequenceOrElement e) {
121 if (hm.get(e) != null) return hm.get(e);
123 Boolean ret = walk2(e);
129 static class EpsilonFollowSet extends Walk<Atom> {
132 public EpsilonFollowSet(Atom a, Atom empty, Cache c) {
137 public Atom walkAtom(Atom r) { return all; }
138 public Atom walkSequence(Sequence s) {
139 if (s.follow==null) return all;
142 public Atom sequence(Sequence s, Atom a, Atom b) {
143 throw new RuntimeException();
145 public Atom union(Union u, Atom a, Atom b) {
147 if (a==null) return b;
148 if (b==null) return a;
150 if (a==null || b==null) return all;
151 return (Atom)a.union(b);
153 public Atom bottom(SequenceOrElement e) {
154 return (e instanceof Union) ? empty : all;
158 // Input-Set //////////////////////////////////////////////////////////////////////////////
160 static abstract class WalkTokenSet<Tok extends Input> extends Walk<Topology<Tok>> {
161 public Topology<Tok> cs;
162 public WalkTokenSet(Topology<Tok> cs) { this.cs = cs; }
163 public WalkTokenSet(Topology<Tok> cs, Cache c) { super(c); this.cs = cs; }
164 public Topology<Tok> bottom(SequenceOrElement e) { return cs; }
165 public Topology<Tok> walkAtom(Atom r) { cs = cs.union(r.getTokenTopology()); return cs; }
168 // feature: intersect with "first" set of all positive conjuncts
169 static class First<Tok extends Input> extends WalkTokenSet<Tok> {
170 public First(Topology<Tok> cs, Walk.Cache cache) { super(cs, cache); }
171 public Topology<Tok> walkSequence(Sequence seq) {
172 for(Position p = seq.firstp(); p!=null && !p.isLast(); p = p.next()) {
174 if (!c.possiblyEpsilon(p.element())) break;
180 static class Follow<Tok extends Input> extends WalkTokenSet<Tok> {
181 private final SequenceOrElement me;
182 private final HashSet<SequenceOrElement> all;
183 private boolean eof = false;
184 public boolean includesEof() { return eof; }
185 public Follow(Topology<Tok> cs, SequenceOrElement me, HashSet<SequenceOrElement> all, Cache c) {
186 super(cs, c); this.me = me; this.all = all; }
187 public Topology<Tok> bottom(SequenceOrElement e) { return cs; }
188 public Topology<Tok> walkSequence(Sequence seq) { return cs; }
189 public Topology<Tok> walkAtom(Atom r) { return walk((SequenceOrElement)r); }
190 public Topology<Tok> walk(SequenceOrElement e) {
191 if (acc.contains(e)) return bottom(e);
195 Topology<Tok> cached = (Topology<Tok>)c.follow.get(e);
196 if (cached != null) {
197 cs = cs.union(cached);
203 Topology<Tok> cso = cs;
205 eof = c.eof.get(e) != null && c.eof.get(e).booleanValue();
208 for(SequenceOrElement x : all) {
209 boolean matched = false;
210 if (!(x instanceof Sequence)) continue;
211 Sequence a = (Sequence)x;
213 for(Position pos = a.firstp(); pos != null && !pos.isLast(); pos = pos.next()) {
214 if (matched) cs = cs.union(c.first(pos.element(), cs.empty()));
215 if (pos.isLast()) { matched = (matched && c.possiblyEpsilon(pos.element())); continue; }
216 boolean good = false;
217 if (e instanceof Atom) {
218 Topology top = c.atoms.get(pos.element());
219 if (top==null) continue;
220 if (!(top.containsAll(((Atom)e)))) continue;
222 if (c.ys.contains(pos.element(),e)) good = true;
229 if (matched) walk(a);
232 if (e instanceof Sequence) {
233 Sequence s = (Sequence)e;
234 if (s.follow != null) cs = cs.intersect(s.follow.getTokenTopology());
237 if (c != null && e==me) {
252 public final HashMap<SequenceOrElement,Boolean> possiblyEpsilon = new HashMap<SequenceOrElement,Boolean>();
253 public HashMap<SequenceOrElement,Boolean> eof = new HashMap<SequenceOrElement,Boolean>();
254 public HashMap<SequenceOrElement,Topology> follow = new HashMap<SequenceOrElement,Topology>();
255 public HashMapBag<SequenceOrElement,SequenceOrElement> ys = new HashMapBag<SequenceOrElement,SequenceOrElement>();
256 public HashMapBag<SequenceOrElement,SequenceOrElement> ys2 = new HashMapBag<SequenceOrElement,SequenceOrElement>();
257 public HashMap<SequenceOrElement,Topology> atoms = new HashMap<SequenceOrElement,Topology>();
258 public <Tok extends Input> Topology<Tok> first(SequenceOrElement e, Topology<Tok> empty) {
259 return new Walk.First<Tok>(empty, this).walk(e);
261 final boolean possiblyEpsilon(SequenceOrElement e) {
262 Walk.Cache cache = this;
263 Boolean ret = possiblyEpsilon.get(e);
264 if (ret != null) return ret.booleanValue();
265 ret = new Walk.PossiblyEpsilon().walk(e) ? Boolean.TRUE : Boolean.FALSE;
266 possiblyEpsilon.put(e, ret);