1 package edu.berkeley.sbp;
2 import edu.berkeley.sbp.*;
3 import edu.berkeley.sbp.Sequence.Position;
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> /*extends PrintableTree<Forest.MyBody<T>>*/
11 implements Visitable<Forest.Body<T>>,
15 private static int master_idx = 0;
16 private final int idx = master_idx++;
17 public int toInt() { return idx; }
19 public abstract void expand(TaskList tl, HashSet<Tree<T>> ht);
20 public abstract void gather(TaskList tl, HashSet<Tree<T>>[] ht, HashSet<Tree<T>> target);
22 public static class TaskList extends ArrayList<TaskList.Task> {
23 public interface Task {
24 public void perform();
26 public class ExpandTask implements Task {
29 public ExpandTask(Forest f, HashSet hs) { this.f = f; this.hs = hs; }
30 public void perform() { f.expand(TaskList.this, hs); }
32 public class GatherTask implements Task {
36 public GatherTask(Forest f, HashSet<Tree<?>>[] ht, HashSet<Tree<?>> hs) { this.f = f; this.hs = hs; this.ht = ht;}
37 public void perform() { f.gather(TaskList.this, ht, hs); }
39 public void expand(Forest f, HashSet hs) {
40 add(new ExpandTask(f, hs));
42 public void gather(Forest f, HashSet[] ht, HashSet hs) {
43 add(new GatherTask(f, ht, hs));
47 if (isEmpty()) return;
48 Task task = get(size()-1);
55 /** assume that this forest contains exactly one tree and return it; otherwise throw an exception */
56 public final Tree<T> expand1() throws Ambiguous, ParseFailed {
58 Iterator<Tree<T>> it = expand(true).iterator();
59 if (!it.hasNext()) throw new ParseFailed();
61 } catch (InnerAmbiguous ia) { throw new Ambiguous(ia.f); }
64 /** expand this forest into a set of trees */
65 public HashSet<Tree<T>> expand(boolean toss) {
67 final HashSetTreeConsumer<T> ret = new HashSetTreeConsumer<T>();
68 visit(new TreeMaker2<T>(toss, ret), null, null);
70 TaskList tl = new TaskList();
71 HashSet<Tree<T>> ret = new HashSet<Tree<T>>();
75 if (toss && ret.size() > 1) throw new InnerAmbiguous(this);
79 private static class InnerAmbiguous extends RuntimeException {
80 public final Forest<?> f;
81 public InnerAmbiguous(Forest<?> f) { this.f = f; }
84 static interface TreeConsumer<T> {
85 public void addTree(Tree<T> t);
87 static class HashSetTreeConsumer<T> extends HashSet<Tree<T>> implements TreeConsumer<T> {
88 public void addTree(Tree<T> t) {
93 static <T> Forest<T> singleton(Input.Location loc, Position p) {
94 return create(loc, null, new Forest[] { }, new Object[0], false, true, p); }
95 static <T> Forest<T> singleton(Input.Location loc, Forest<T> body, Position p) {
96 //return create(loc, null, new Forest[] { body }, false, true, p);
99 static <T> Forest<T> leaf(Input.Location loc, T tag, Position p) { return create(loc, tag, null, null, false, false, p); }
100 public static <T> Forest<T> create(Input.Location loc, T tag, Forest<T>[] tokens, Object[] labels, boolean unwrap, boolean singleton, Position p) {
101 return new MyBody<T>(loc, tag, tokens, labels, unwrap, singleton, p);
103 // Body //////////////////////////////////////////////////////////////////////////////
105 protected static interface Body<T> extends GraphViz.ToGraphViz {
106 void expand(int i, TreeMaker<T> h);
108 public abstract void edges(GraphViz.Node n);
109 public boolean ambiguous() { return false; }
110 protected static class MyBody<T> extends Forest<T> implements Body<T> /* extends PrintableTree<Forest<T>> implements */ {
112 public boolean isTransparent() { return false; }
113 public boolean isHidden() { return false; }
114 public GraphViz.Node toGraphViz(GraphViz gv) {
115 if (gv.hasNode(this)) return gv.createNode(this);
116 GraphViz.Node n = gv.createNode(this);
117 n.label = headToString()==null?"":headToString();
119 n.comment = reduction==null?null:reduction+"";
123 boolean edges = false;
124 public void edges(GraphViz.Node n) {
127 for(int i=0; i<tokens.length; i++) {
128 if (i==tokens.length-1 && unwrap && !tokens[i].ambiguous()) {
131 n.edge(tokens[i], labels==null?null:labels[i]);
136 public <B,C> void visit(Invokable<Forest.Body<T>,B,C> ivbc, B b, C c) {
137 ivbc.invoke(this, b, c);
140 private final Input.Location location;
142 private final Forest<T>[] tokens;
143 private final Object[] labels;
144 private final boolean unwrap;
145 private final boolean singleton;
146 private final Sequence.Position reduction;
148 private MyBody(Input.Location loc, T tag, Forest<T>[] tokens, Object[] labels, boolean unwrap, boolean singleton, Position reduction) {
151 this.tokens = tokens==null ? emptyForestArray : new Forest[tokens.length];
152 if (tokens != null) System.arraycopy(tokens, 0, this.tokens, 0, tokens.length);
153 if (tokens != null) for(int i=0; i<tokens.length; i++) if (tokens[i]==null) throw new Error(i+"");
154 this.unwrap = unwrap;
155 this.singleton = singleton;
156 this.reduction = reduction;
157 this.labels = labels;
159 public void gather(TaskList tl, HashSet<Tree<T>>[] ht, HashSet<Tree<T>> target) {
160 gather(tl, ht, target, new Tree[ht.length], 0);
162 private void gather(TaskList tl, HashSet<Tree<T>>[] ht, HashSet<Tree<T>> target, Tree[] trees, int i) {
164 target.add(new Tree<T>(null, tag, trees));
167 for(Tree<T> tree : ht[i]) {
168 if (unwrap && i==trees.length-1) {
169 // I think this is wrong
170 Tree[] trees2 = new Tree[trees.length - 1 + tree.numChildren()];
171 System.arraycopy(trees, 0, trees2, 0, trees.length-1);
172 for(int j=0; j<tree.numChildren(); j++)
173 trees2[trees.length-1+j] = tree.child(j);
174 target.add(new Tree<T>(null, tag, trees2));
177 gather(tl, ht, target, trees, i+1);
182 public void expand(TaskList tl, HashSet<Tree<T>> ht) {
184 tokens[0].expand(tl, ht);
187 HashSet<Tree<T>>[] children = new HashSet[tokens.length];
188 tl.gather(this, children, ht);
189 for(int i=0; i<children.length; i++) {
190 children[i] = new HashSet<Tree<T>>();
191 tl.expand(tokens[i], children[i]);
195 public void expand(final int i, final TreeMaker<T> h) {
197 tokens[0].visit(h, null, i);
200 if (i==0) h.start(tag, location);
202 if (i==tokens.length) {
203 h.finish(tag, location);
205 } else if (unwrap && i==tokens.length-1) {
206 if (tokens[i] != null)
207 tokens[i].visit(h, null, 0);
210 tokens[i].visit(new TreeMaker<T>(h.toss) {
211 public void start(T head, Input.Location loc) { }
212 public void addTree(Tree<T> t, Object label) { toks.add(t); labs.add(label); }
213 public void finish(T head, Input.Location loc) {
214 int old = h.toks.size();
215 h.addTree(new Tree<T>(loc, head, toks.toArray(tree_hint), labs.toArray(string_hint)), labels==null?null:labels[i]);
217 while(h.toks.size() > old) h.toks.remove(h.toks.size()-1);
218 while(h.labs.size() > old) h.labs.remove(h.labs.size()-1);
224 protected String headToString() { return tag==null?null:tag.toString(); }
225 protected String headToJava() { return "null"; }
226 protected String left() { return "{"; }
227 protected String right() { return "}"; }
228 protected boolean ignoreSingleton() { return false; }
232 // Ref //////////////////////////////////////////////////////////////////////////////
235 * This class represents a partially complete collection of
236 * forests to be viewed as a forest at some later date; once
237 * viewed, it becomes immutable
239 static class Ref<T> extends Forest<T> {
240 public void expand(TaskList tl, HashSet<Tree<T>> ht) {
241 for (Forest<T> f : hp) f.expand(tl, ht);
243 public void gather(TaskList tl, HashSet<Tree<T>>[] ht, HashSet<Tree<T>> target) {
246 public HashSet<GSS.Phase.Node> parents = new HashSet<GSS.Phase.Node>();
247 public boolean contains(Forest f) {
248 return hp.contains(f);
250 public boolean ambiguous() {
251 if (hp.size()==0) return false;
252 if (hp.size()==1) return hp.iterator().next().ambiguous();
255 private FastSet<Forest<T>> hp = new FastSet<Forest<T>>();
258 if (hp.size()==1) return hp.iterator().next().toInt();
259 return super.toInt();
261 public void merge(Forest p) { if (p!=this) hp.add(p, true); }
263 public boolean isTransparent() { return hp.size()==1; }
264 public boolean isHidden() { return hp.size()==0; }
265 public void edges(GraphViz.Node n) {
266 if (hp.size()==1) { hp.iterator().next().edges(n); return; }
267 for(Forest f : hp) f.edges(n);
269 public GraphViz.Node toGraphViz(GraphViz gv) {
270 //if (hp.size()==0) return null;
271 if (hp.size()==1) return hp.iterator().next().toGraphViz(gv);
272 if (gv.hasNode(this)) return gv.createNode(this);
273 GraphViz.Node n = gv.createNode(this);
276 for(Forest f : hp) n.edge(f, null);
280 public <B,C> void visit(Invokable<Forest.Body<T>,B,C> ivbc, B b, C c) {
281 if (hp==null) return;
282 for(Forest<T> f : hp)
285 public Forest resolve() { return this; }
288 public abstract <B,C> void visit(Invokable<Forest.Body<T>,B,C> ivbc, B b, C c);
289 private static class TreeMaker2<T> extends TreeMaker<T> {
290 private TreeConsumer<T> tc;
291 public TreeMaker2(boolean toss, TreeConsumer<T> tc) { super(toss); this.tc = tc; }
292 public void finish(T head, Input.Location loc) { tc.addTree(new Tree<T>(loc, head, toks.toArray(tree_hint), labs.toArray(string_hint)));; }
293 public void start(T head, Input.Location loc) { }
294 public void addTree(Tree<T> t, Object label) { toks.add(t); labs.add(label); }
296 private static abstract class TreeMaker<T> implements Invokable<Forest.Body<T>,Boolean,Integer>/*, TreeConsumer<T>*/ {
297 public ArrayList<Tree<T>> toks = new ArrayList<Tree<T>>();
298 public ArrayList<Object> labs = new ArrayList<Object>();
299 private boolean toss;
301 public TreeMaker(boolean toss) { this.toss = toss; }
302 public abstract void start(T head, Input.Location loc);
303 public abstract void finish(T head, Input.Location loc);
304 public abstract void addTree(Tree<T> t, Object label);
305 public void invoke(Forest.Body<T> bod, Boolean o, Integer i) {
307 ArrayList<Tree<T>> toks = this.toks;
308 this.toks = new ArrayList<Tree<T>>();
309 ArrayList<Object> labs = this.labs;
310 this.labs = new ArrayList<Object>();
320 // Statics //////////////////////////////////////////////////////////////////////////////
322 private static Tree[] tree_hint = new Tree[0];
323 private static String[] string_hint = new String[0];
324 private static final Forest[] emptyForestArray = new Forest[0];
326 protected String headToString() { return null; }
327 protected String headToJava() { return "null"; }
328 protected String left() { return "<?"; }
329 protected String right() { return "?>"; }
330 protected boolean ignoreSingleton() { return true; }