5e1f5347f1bf5ba7d29cd1ed1aacda3c8e36c932
[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 sequence(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 sequence((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> sequence(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
68     // Boolean //////////////////////////////////////////////////////////////////////////////
69
70     static class PossiblyEpsilon extends Walk<Boolean> {
71         public Boolean walkAtom(Atom r) { return false; }
72         public Boolean sequence(Sequence s, Boolean a, Boolean b)  { return new Boolean(a && b); }
73         public Boolean union(Union u, Boolean a, Boolean b)     { return new Boolean(a || b); }
74         public Boolean bottom(SequenceOrElement e)    { return (e instanceof Union) ? false : true; }
75         private HashMap<SequenceOrElement,Boolean> hm = new HashMap<SequenceOrElement,Boolean>();
76         protected Boolean walk(SequenceOrElement e) {
77             if (hm.get(e) != null) return hm.get(e);
78             hm.put(e, false);
79             Boolean ret = walk2(e);
80             hm.put(e, ret);
81             return ret;
82         }
83     }
84
85
86     // Input-Set //////////////////////////////////////////////////////////////////////////////
87
88     static abstract class WalkTokenSet<Tok extends Input> extends Walk<Topology<Tok>> {
89         public Topology<Tok> cs;
90         public WalkTokenSet(Topology<Tok> cs)          { this.cs = cs; }
91         public WalkTokenSet(Topology<Tok> cs, Cache c) { super(c); this.cs = cs; }
92         public Topology<Tok> bottom(SequenceOrElement e)         { return cs; }
93         public Topology<Tok> walkAtom(Atom r)          { cs = cs.union(r.getTokenTopology()); return cs; }
94     }
95
96     static class First<Tok extends Input> extends WalkTokenSet<Tok> {
97         public First(Topology<Tok> cs, Walk.Cache cache) { super(cs, cache); }
98         public Topology<Tok> sequence(Sequence seq) {
99             for(Position p = seq.firstp(); p!=null && !p.isLast(); p = p.next()) {
100                 walk(p.element());
101                 if (!c.possiblyEpsilon(p.element())) break;
102             }
103             return cs;
104         }
105     }
106
107     static class Follow<Tok extends Input> extends WalkTokenSet<Tok> {
108         private final SequenceOrElement me;
109         private final HashSet<SequenceOrElement> all;
110         private boolean eof = false;
111         public boolean includesEof() { return eof; }
112         public Follow(Topology<Tok> cs, SequenceOrElement me, HashSet<SequenceOrElement> all, Cache c)  {
113           super(cs, c); this.me = me; this.all = all; }
114         public Topology<Tok> bottom(SequenceOrElement e)                       { return cs; }
115         public Topology<Tok> sequence(Sequence seq)                  { return cs; }
116         public Topology<Tok> walkAtom(Atom r) { return walk((SequenceOrElement)r); }
117         public Topology<Tok> walk(SequenceOrElement e) {
118             if (acc.contains(e)) return bottom(e);
119             acc.add(e);
120
121             if (c != null) {
122                 Topology<Tok> cached = (Topology<Tok>)c.follow.get(e);
123                 if (cached != null) {
124                     cs = cs.union(cached);
125                     eof |= c.eof.get(e);
126                     return cs;
127                 }
128             }
129
130             Topology<Tok> cso = cs;
131             boolean eofo = eof;
132             eof = c.eof.get(e) != null && c.eof.get(e).booleanValue();
133             cs = cso.empty();
134
135             for(SequenceOrElement x : all) {
136                 boolean matched = false;
137                 if (!(x instanceof Sequence)) continue;
138                 Sequence a = (Sequence)x;
139                 Position mp = null;
140                 for(Position pos = a.firstp(); pos != null && !pos.isLast(); pos = pos.next()) {
141                     if (matched) cs = cs.union(c.first(pos.element(), cs.empty()));
142                     if (pos.isLast()) { matched = (matched && c.possiblyEpsilon(pos.element())); continue; }
143                     boolean good = false;
144                     if (e instanceof Atom) {
145                         Topology top = c.atoms.get(pos.element());
146                         if (top==null) continue;
147                         if (!(top.containsAll(((Atom)e)))) continue;
148                     } else {
149                         if (c.ys.contains(pos.element(),e)) good = true;
150                     }
151                     if (good) {
152                         mp = pos;
153                         matched = true;
154                     }
155                 }
156                 if (matched) walk(a);
157             }
158
159             if (e instanceof Sequence) {
160                 Sequence s = (Sequence)e;
161                 if (s.follow != null) cs = cs.intersect(s.follow.getTokenTopology());
162             }
163
164             if (c != null && e==me) {
165                 c.follow.put(e, cs);
166                 c.eof.put(e, eof);
167             }
168
169             cso = cso.union(cs);
170             cs = cso;
171             eofo |= eof;
172             eof = eofo;
173
174             return cs;
175         }
176     }
177
178     static class Cache {
179         public final HashMap<SequenceOrElement,Boolean> possiblyEpsilon = new HashMap<SequenceOrElement,Boolean>();
180         public HashMap<SequenceOrElement,Boolean> eof = new HashMap<SequenceOrElement,Boolean>();
181         public HashMap<SequenceOrElement,Topology> follow = new HashMap<SequenceOrElement,Topology>();
182         public HashMapBag<SequenceOrElement,SequenceOrElement>  ys = new HashMapBag<SequenceOrElement,SequenceOrElement>();
183         public HashMap<SequenceOrElement,Topology> atoms = new HashMap<SequenceOrElement,Topology>();
184         public <Tok extends Input> Topology<Tok> first(SequenceOrElement e, Topology<Tok> empty) {
185             return new Walk.First<Tok>(empty, this).walk(e);
186         }
187         final boolean possiblyEpsilon(SequenceOrElement e) {
188             Walk.Cache cache = this;
189             Boolean ret = possiblyEpsilon.get(e);
190             if (ret != null) return ret.booleanValue();
191             ret = new Walk.PossiblyEpsilon().walk(e) ? Boolean.TRUE : Boolean.FALSE;
192             possiblyEpsilon.put(e, ret);
193             return ret;
194         }
195     }
196 }