0b19cd49b2fee38880592f3a350b7874795a875c
[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
45                 // FIXME
46                 for(Sequence ss : s.needs()) ret = union((Union)e, ret, walk(ss));
47                 for(Sequence ss : s.hates()) ret = union((Union)e, ret, walk(ss));
48             }
49             return ret;
50         } else {
51             throw new Error("unknown element of class " + e.getClass().getName() + ": " + e);
52         }
53     }
54
55     static class YieldSet extends Walk<HashSet<SequenceOrElement>> {
56         private final SequenceOrElement e;
57         public final HashSet<SequenceOrElement> walk() { return walk(e); }
58         public YieldSet(SequenceOrElement e, Cache c)  { super(c); this.e = e; }
59         public HashSet<SequenceOrElement> bottom(SequenceOrElement e)     { return acc; }
60         public HashSet<SequenceOrElement> walkSequence(Sequence seq) { return bottom(seq); }
61         public HashSet<SequenceOrElement> walkAtom(Atom r) {
62             c.atoms.put(e, c.atoms.get(e)==null ? r : c.atoms.get(e).union(r));
63             return super.walkAtom(r);
64         }
65     }
66
67     static class EquivalentTo extends Walk<HashSet<Sequence>> {
68         private final Sequence s;
69         private final HashSet<Sequence> eq = new HashSet<Sequence>();
70         public final HashSet<Sequence> walk() { return walk(s); }
71         public EquivalentTo(Sequence e, Cache c)  {
72             super(c); this.s = e;
73         }
74         public HashSet<Sequence> bottom(SequenceOrElement e)     { return eq; }
75         public HashSet<Sequence> walkSequence(Sequence seq) {
76             eq.add(seq);
77             Position p = seq.firstp();
78             for(; !p.isLast(); p = p.next()) {
79                 if (!p.isLast() && isRightNullable(p.next()))
80                     walk(p.element());
81                 if (!c.possiblyEpsilon(p.element())) break;
82             }
83             return eq;
84         }
85         public HashSet<Sequence> walkAtom(Atom r) {
86             return eq;
87         }
88         private boolean isRightNullable(Position p) {
89             if (p.isLast()) return true;
90             if (!c.possiblyEpsilon(p.element())) return false;
91             return isRightNullable(p.next());
92         }
93     }
94
95
96     // Boolean //////////////////////////////////////////////////////////////////////////////
97
98     static class PossiblyEpsilon extends Walk<Boolean> {
99         public Boolean walkAtom(Atom r) { return false; }
100         public Boolean sequence(Sequence s, Boolean a, Boolean b)  { return new Boolean(a && b); }
101         public Boolean union(Union u, Boolean a, Boolean b)     { return new Boolean(a || b); }
102         public Boolean bottom(SequenceOrElement e)    { return (e instanceof Union) ? false : true; }
103         private HashMap<SequenceOrElement,Boolean> hm = new HashMap<SequenceOrElement,Boolean>();
104         protected Boolean walk(SequenceOrElement e) {
105             if (hm.get(e) != null) return hm.get(e);
106             hm.put(e, false);
107             Boolean ret = walk2(e);
108             hm.put(e, ret);
109             return ret;
110         }
111     }
112
113     static class EpsilonFollowSet extends Walk<Atom> {
114         Atom all;
115         Atom empty;
116         public EpsilonFollowSet(Atom a, Atom empty, Cache c) {
117             super(c);
118             this.all = all;
119             this.empty = empty;
120         }
121         public Atom walkAtom(Atom r) { return all; }
122         public Atom walkSequence(Sequence s) {
123             if (s.follow==null) return all;
124             return s.follow;
125         }
126         public Atom sequence(Sequence s, Atom a, Atom b)  {
127             throw new RuntimeException();
128         }
129         public Atom union(Union u, Atom a, Atom b) {
130             /*
131             if (a==null) return b;
132             if (b==null) return a;
133             */
134             if (a==null || b==null) return all;
135             return (Atom)a.union(b);
136         }
137         public Atom bottom(SequenceOrElement e) {
138             return (e instanceof Union) ? empty : all;
139         }
140     }
141
142     // Input-Set //////////////////////////////////////////////////////////////////////////////
143
144     static abstract class WalkTokenSet<Tok extends Input> extends Walk<Topology<Tok>> {
145         public Topology<Tok> cs;
146         public WalkTokenSet(Topology<Tok> cs)          { this.cs = cs; }
147         public WalkTokenSet(Topology<Tok> cs, Cache c) { super(c); this.cs = cs; }
148         public Topology<Tok> bottom(SequenceOrElement e)         { return cs; }
149         public Topology<Tok> walkAtom(Atom r)          { cs = cs.union(r.getTokenTopology()); return cs; }
150     }
151
152     static class First<Tok extends Input> extends WalkTokenSet<Tok> {
153         public First(Topology<Tok> cs, Walk.Cache cache) { super(cs, cache); }
154         public Topology<Tok> walkSequence(Sequence seq) {
155             for(Position p = seq.firstp(); p!=null && !p.isLast(); p = p.next()) {
156                 walk(p.element());
157                 if (!c.possiblyEpsilon(p.element())) break;
158             }
159             return cs;
160         }
161     }
162
163     static class Follow<Tok extends Input> extends WalkTokenSet<Tok> {
164         private final SequenceOrElement me;
165         private final HashSet<SequenceOrElement> all;
166         private boolean eof = false;
167         public boolean includesEof() { return eof; }
168         public Follow(Topology<Tok> cs, SequenceOrElement me, HashSet<SequenceOrElement> all, Cache c)  {
169           super(cs, c); this.me = me; this.all = all; }
170         public Topology<Tok> bottom(SequenceOrElement e)                       { return cs; }
171         public Topology<Tok> walkSequence(Sequence seq)                  { return cs; }
172         public Topology<Tok> walkAtom(Atom r) { return walk((SequenceOrElement)r); }
173         public Topology<Tok> walk(SequenceOrElement e) {
174             if (acc.contains(e)) return bottom(e);
175             acc.add(e);
176
177             if (c != null) {
178                 Topology<Tok> cached = (Topology<Tok>)c.follow.get(e);
179                 if (cached != null) {
180                     cs = cs.union(cached);
181                     eof |= c.eof.get(e);
182                     return cs;
183                 }
184             }
185
186             Topology<Tok> cso = cs;
187             boolean eofo = eof;
188             eof = c.eof.get(e) != null && c.eof.get(e).booleanValue();
189             cs = cso.empty();
190
191             for(SequenceOrElement x : all) {
192                 boolean matched = false;
193                 if (!(x instanceof Sequence)) continue;
194                 Sequence a = (Sequence)x;
195                 Position mp = null;
196                 for(Position pos = a.firstp(); pos != null && !pos.isLast(); pos = pos.next()) {
197                     if (matched) cs = cs.union(c.first(pos.element(), cs.empty()));
198                     if (pos.isLast()) { matched = (matched && c.possiblyEpsilon(pos.element())); continue; }
199                     boolean good = false;
200                     if (e instanceof Atom) {
201                         Topology top = c.atoms.get(pos.element());
202                         if (top==null) continue;
203                         if (!(top.containsAll(((Atom)e)))) continue;
204                     } else {
205                         if (c.ys.contains(pos.element(),e)) good = true;
206                     }
207                     if (good) {
208                         mp = pos;
209                         matched = true;
210                     }
211                 }
212                 if (matched) walk(a);
213             }
214
215             if (e instanceof Sequence) {
216                 Sequence s = (Sequence)e;
217                 if (s.follow != null) cs = cs.intersect(s.follow.getTokenTopology());
218             }
219
220             if (c != null && e==me) {
221                 c.follow.put(e, cs);
222                 c.eof.put(e, eof);
223             }
224
225             cso = cso.union(cs);
226             cs = cso;
227             eofo |= eof;
228             eof = eofo;
229
230             return cs;
231         }
232     }
233
234     static class Cache {
235         public final HashMap<SequenceOrElement,Boolean> possiblyEpsilon = new HashMap<SequenceOrElement,Boolean>();
236         public HashMap<SequenceOrElement,Boolean> eof = new HashMap<SequenceOrElement,Boolean>();
237         public HashMap<SequenceOrElement,Topology> follow = new HashMap<SequenceOrElement,Topology>();
238         public HashMapBag<SequenceOrElement,SequenceOrElement>  ys = new HashMapBag<SequenceOrElement,SequenceOrElement>();
239         public HashMap<SequenceOrElement,Topology> atoms = new HashMap<SequenceOrElement,Topology>();
240         public <Tok extends Input> Topology<Tok> first(SequenceOrElement e, Topology<Tok> empty) {
241             return new Walk.First<Tok>(empty, this).walk(e);
242         }
243         final boolean possiblyEpsilon(SequenceOrElement e) {
244             Walk.Cache cache = this;
245             Boolean ret = possiblyEpsilon.get(e);
246             if (ret != null) return ret.booleanValue();
247             ret = new Walk.PossiblyEpsilon().walk(e) ? Boolean.TRUE : Boolean.FALSE;
248             possiblyEpsilon.put(e, ret);
249             return ret;
250         }
251     }
252 }