e2ac662a0e18da8aefd37b46087809e60c7cf875
[sbp.git] / src / edu / berkeley / sbp / Walk.java
1 // Copyright 2006 all rights reserved; see LICENSE file for BSD-style license
2
3 package edu.berkeley.sbp;
4 import edu.berkeley.sbp.*;
5 import edu.berkeley.sbp.util.*;
6 import edu.berkeley.sbp.Sequence.Position;
7 import java.io.*;
8 import java.util.*;
9 import java.lang.reflect.*;
10 import java.lang.ref.*;
11
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);
16
17     protected final Cache c;
18     public Walk() { this(null); }
19     public Walk(Cache c) { this.c = c; }
20
21     public  T walkSequence(Sequence s) {
22         T ret = bottom(s);
23         for(Position p = s.firstp(); p!=null && !p.isLast(); p = p.next())
24             ret = sequence(s, ret, walk(p.element()));
25         return ret;
26     }
27     
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);
33         acc.add(e);
34         return walk2(e);
35     }
36
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) {
41             T ret = bottom(e);
42             for(Sequence s : (Union)e)
43                 ret = union((Union)e, ret, walk(s));
44             return ret;
45         } else {
46             throw new Error("unknown element of class " + e.getClass().getName() + ": " + e);
47         }
48     }
49
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);
59         }
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));
66                 }
67             return ret;
68         }
69     }
70
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);
80         }
81     }
82
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)  {
88             super(c); this.s = e;
89         }
90         public HashSet<Sequence> bottom(SequenceOrElement e)     { return eq; }
91         public HashSet<Sequence> walkSequence(Sequence seq) {
92             eq.add(seq);
93             Position p = seq.firstp();
94             for(; !p.isLast(); p = p.next()) {
95                 if (!p.isLast() && isRightNullable(p.next()))
96                     walk(p.element());
97                 if (!c.possiblyEpsilon(p.element())) break;
98             }
99             return eq;
100         }
101         public HashSet<Sequence> walkAtom(Atom r) {
102             return eq;
103         }
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());
108         }
109     }
110
111
112     // Boolean //////////////////////////////////////////////////////////////////////////////
113
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);
122             hm.put(e, false);
123             Boolean ret = walk2(e);
124             hm.put(e, ret);
125             return ret;
126         }
127     }
128
129     static class EpsilonFollowSet extends Walk<Atom> {
130         Atom all;
131         Atom empty;
132         public EpsilonFollowSet(Atom a, Atom empty, Cache c) {
133             super(c);
134             this.all = all;
135             this.empty = empty;
136         }
137         public Atom walkAtom(Atom r) { return all; }
138         public Atom walkSequence(Sequence s) {
139             if (s.follow==null) return all;
140             return s.follow;
141         }
142         public Atom sequence(Sequence s, Atom a, Atom b)  {
143             throw new RuntimeException();
144         }
145         public Atom union(Union u, Atom a, Atom b) {
146             /*
147             if (a==null) return b;
148             if (b==null) return a;
149             */
150             if (a==null || b==null) return all;
151             return (Atom)a.union(b);
152         }
153         public Atom bottom(SequenceOrElement e) {
154             return (e instanceof Union) ? empty : all;
155         }
156     }
157
158     // Input-Set //////////////////////////////////////////////////////////////////////////////
159
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; }
166     }
167
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()) {
173                 walk(p.element());
174                 if (!c.possiblyEpsilon(p.element())) break;
175             }
176             return cs;
177         }
178     }
179
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);
192             acc.add(e);
193
194             if (c != null) {
195                 Topology<Tok> cached = (Topology<Tok>)c.follow.get(e);
196                 if (cached != null) {
197                     cs = cs.union(cached);
198                     eof |= c.eof.get(e);
199                     return cs;
200                 }
201             }
202
203             Topology<Tok> cso = cs;
204             boolean eofo = eof;
205             eof = c.eof.get(e) != null && c.eof.get(e).booleanValue();
206             cs = cso.empty();
207
208             for(SequenceOrElement x : all) {
209                 boolean matched = false;
210                 if (!(x instanceof Sequence)) continue;
211                 Sequence a = (Sequence)x;
212                 Position mp = null;
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;
221                     } else {
222                         if (c.ys.contains(pos.element(),e)) good = true;
223                     }
224                     if (good) {
225                         mp = pos;
226                         matched = true;
227                     }
228                 }
229                 if (matched) walk(a);
230             }
231
232             if (e instanceof Sequence) {
233                 Sequence s = (Sequence)e;
234                 if (s.follow != null) cs = cs.intersect(s.follow.getTokenTopology());
235             }
236
237             if (c != null && e==me) {
238                 c.follow.put(e, cs);
239                 c.eof.put(e, eof);
240             }
241
242             cso = cso.union(cs);
243             cs = cso;
244             eofo |= eof;
245             eof = eofo;
246
247             return cs;
248         }
249     }
250
251     static class Cache {
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);
260         }
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);
267             return ret;
268         }
269     }
270 }