1 package edu.berkeley.sbp;
2 import edu.berkeley.sbp.*;
3 import edu.berkeley.sbp.*;
4 import edu.berkeley.sbp.util.*;
7 import java.lang.reflect.*;
9 /** an efficient representation of a collection of trees (Tomita's shared packed parse forest) */
10 public abstract class Forest<T> {
12 /** assume that this forest contains exactly one tree and return it; otherwise throw an exception */
13 public final Tree<T> expand1() throws Parser.Ambiguous, Parser.Failed {
14 Iterator<Tree<T>> it = expand(true).iterator();
15 if (!it.hasNext()) throw new Parser.Failed();
19 /** expand this forest into a set of trees */
20 public abstract HashSet<Tree<T>> expand(boolean toss);
22 abstract boolean valid();
24 static <T> Forest<T> singleton(Token.Location loc, Sequence creator) { return create(loc, null, new Forest[] { }, creator, false, true); }
25 static <T> Forest<T> singleton(Token.Location loc, Forest<T> body, Sequence creator) { return create(loc, null, new Forest[] { body }, creator, false, true); }
26 static <T> Forest<T> leaf(Token.Location loc, T tag, Sequence creator) { return create(loc, tag, null, creator, false, false); }
27 public static <T> Forest<T> create(Token.Location loc, T tag, Forest<T>[] tokens, Sequence creator, boolean unwrap, boolean singleton) {
28 return new MultiForest<T>(loc, tag, tokens, creator, unwrap, singleton);
31 // Body //////////////////////////////////////////////////////////////////////////////
33 protected static class Body<T> {
35 private final Token.Location location;
37 private final Forest<T>[] tokens;
38 private final Sequence creator;
39 private final boolean unwrap;
40 private final boolean singleton;
42 private Body(Token.Location loc, T tag, Forest<T>[] tokens, Sequence creator, boolean unwrap, boolean singleton) {
45 this.tokens = tokens==null ? emptyForestArray : new Forest[tokens.length];
46 if (tokens != null) System.arraycopy(tokens, 0, this.tokens, 0, tokens.length);
47 if (tokens != null) for(int i=0; i<tokens.length; i++) if (tokens[i]==null) throw new Error(i+"");
48 this.creator = creator;
50 this.singleton = singleton;
53 private HashSet<Tree<T>> expand(boolean toss, ArrayList<Tree<T>> toks, int i, HashSet<Tree<T>> h) {
55 for(Body<T> b : (IterableForest<T>)tokens[0]) b.expand(toss, toks, i, h);
57 } else if (i==tokens.length) {
58 h.add(new Tree<T>(null, tag, toks.toArray(tree_hint)));
60 } else if (unwrap && i==tokens.length-1) {
61 if (tokens[i] != null)
62 for(Body b : (IterableForest<T>)tokens[i])
63 b.expand(toss, toks, 0, h);
66 if (tokens[i]!=null) {
67 HashSet<Tree<T>> exp = tokens[i].expand(toss);
69 for(Tree<T> r : exp) {
70 int old = toks.size();
72 expand(toss, toks, i+1, h);
73 while(toks.size() > old) toks.remove(toks.size()-1);
79 void addTo(HashSet<Body> h) {
80 if (!singleton) h.add(this);
81 else for(Body b : (IterableForest<T>)tokens[0]) b.addTo(h);
83 void addTo(FastSet<Body> h) {
84 if (!singleton) h.add(this, true);
85 else for(Body b : (IterableForest<T>)tokens[0]) b.addTo(h);
88 private boolean kcache = false;
89 private boolean keep = false;
90 public boolean keep() {
93 if (kcache) return keep;
95 for(Forest<T> token : tokens) if (!token.valid()) return keep = false;
96 return keep = creator==null || (creator.needs.size()==0 && creator.hates.size()==0);
99 public boolean keep(Iterable<Body<T>> h) {
100 if (keep()) return true;
101 for(Forest<T> token : tokens) if (!token.valid()) return false;
104 if (creator.hates.contains(b.creator) && b.keep(h)) return false;
105 if (creator.needs.contains(b.creator) && b.keep(h)) needs--;
107 return needs <= -1 * creator.needs.size();
110 private boolean rep = false;
111 public String toString() {
112 if (rep) return "***";
115 StringBuffer ret = new StringBuffer();
116 for(int i=0; i<tokens.length; i++) {
117 String q = tokens[i]==null ? "null" : tokens[i].toString();
118 if (q.length() > 0) {
123 String tail = ret.toString().trim();
124 String head = (tag!=null && !tag.toString().equals("")) ? (tail.length() > 0 ? tag+":" : tag+"") : "";
125 if (tail.length() > 0) tail = "{" + tail + "}";
134 // Ref //////////////////////////////////////////////////////////////////////////////
136 static abstract class IterableForest<T> extends Forest<T> implements Iterable<Forest.Body<T>> {
137 public abstract Iterator<Forest.Body<T>> iterator();
141 * This class represents a partially complete collection of
142 * forests to be viewed as a forest at some later date; once
143 * viewed, it becomes immutable
145 static class Ref<T> extends IterableForest<T> {
146 private FastSet<Forest> hp = new FastSet<Forest>();
147 private Forest res = null;
148 public boolean valid = false;
150 public void merge(Forest p) {
151 //if (p==null) throw new Error("bad evil bad!");
152 if (res != null) throw new Error("already resolved!");
153 if (p==null) throw new Error();
154 if (p!=this) hp.add(p, true);
156 public Iterator<Body<T>> iterator() { return ((IterableForest<T>)resolve()).iterator(); }
157 public HashSet<Tree<T>> expand(boolean toss) { return resolve().expand(toss); }
158 public boolean valid() { return true; /*if (valid) return true; resolve(); return valid;*/ }
159 public String toString() { return resolve().toString(); }
160 public Forest resolve() {
161 if (hp==null) return res;
162 FastSet<Body> nh = new FastSet<Body>();
164 HashSet<Body> results = null;
165 for(Forest<?> p : hp)
166 for(Body<?> b : (IterableForest<?>)p) {
167 if (b.keep() && (b.creator==null || !b.creator.lame)) {
171 results = new HashSet<Body>();
174 if (results != null) {
175 for(Forest<?> p : hp)
176 for(Body<?> b : (IterableForest<?>)p)
178 for(Body b : results) {
179 if (b.keep() && (b.creator==null || !b.creator.lame)) continue;
180 if (b.creator!=null && b.creator.lame) continue;
181 if (!b.keep(results)) continue;
188 for(Forest<?> p : hp)
189 for(Body<?> b : (IterableForest<?>)p)
190 if (b.creator==null || !b.creator.lame)
192 res = new MultiForest(nh, nh.size()>0);
198 // Implementations //////////////////////////////////////////////////////////////////////////////
200 private static class MultiForest<T> extends IterableForest<T> {
201 private final FastSet<Body<T>> results;
202 private boolean valid;
203 public boolean valid() { /*return valid;*/ return true; }
204 private MultiForest(FastSet<Body<T>> results, boolean valid) { this.results = results; this.valid = valid; }
205 public MultiForest(Token.Location loc, T tag, Forest<T>[] tokens, Sequence creator, boolean unwrap, boolean singleton) {
206 this.results = new FastSet<Body<T>>(new Body(loc, tag, tokens, creator, unwrap, singleton));
209 public Iterator<Body<T>> iterator() { return results.iterator(); }
211 public HashSet<Tree<T>> expand(boolean toss) {
212 HashSet<Tree<T>> ret = new HashSet<Tree<T>>();
213 for(Body<T> b : results)
214 ret.addAll(b.expand(toss, new ArrayList<Tree<T>>(), 0, new HashSet<Tree<T>>()));
215 if (toss && ret.size() > 1) throw new Parser.Ambiguous(this);
219 // Display //////////////////////////////////////////////////////////////////////////////
221 private String toString = null;
222 public String toString() {
223 if (toString != null) return toString;
224 StringBuffer ret = new StringBuffer();
225 if (results.size()==1) {
226 for(Forest.Body<T> r : results)
228 return toString = ret.toString();
231 boolean first = true;
232 for(Forest.Body<T> r : results) {
233 if (!first) ret.append(' ');
238 return toString = ret.toString();
242 // Statics //////////////////////////////////////////////////////////////////////////////
244 private static Tree[] tree_hint = new Tree[0];
245 private static Body[] body_hint = new Body[0];
246 private static final Forest[] emptyForestArray = new Forest[0];