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> /*extends PrintableTree<Forest.MyBody<T>>*/ implements Visitable<Forest.Body<T>> {
12 /** assume that this forest contains exactly one tree and return it; otherwise throw an exception */
13 public final Tree<T> expand1() throws Ambiguous, ParseFailed {
14 Iterator<Tree<T>> it = expand(true).iterator();
15 if (!it.hasNext()) throw new ParseFailed();
19 /** expand this forest into a set of trees */
20 public HashSet<Tree<T>> expand(boolean toss) {
21 final HashSet<Tree<T>> hs = new HashSet<Tree<T>>();
22 TreeMaker<T> ret = new TreeMaker<T>(toss) {
23 public void start(T head, Input.Location loc) { }
24 public void finish(T head, Input.Location loc) { hs.add(new Tree<T>(loc, head, toks.toArray(tree_hint))); }
25 public void child(Tree<T> t) { toks.add(t); }
27 visit(ret, null, null);
28 if (toss && hs.size() > 1) throw new Ambiguous(this);
32 static <T> Forest<T> singleton(Input.Location loc) { return create(loc, null, new Forest[] { }, false, true); }
33 static <T> Forest<T> singleton(Input.Location loc, Forest<T> body) { return create(loc, null, new Forest[] { body }, false, true); }
34 static <T> Forest<T> leaf(Input.Location loc, T tag) { return create(loc, tag, null, false, false); }
35 public static <T> Forest<T> create(Input.Location loc, T tag, Forest<T>[] tokens, boolean unwrap, boolean singleton) {
36 return new MyBody<T>(loc, tag, tokens, unwrap, singleton);
39 // Body //////////////////////////////////////////////////////////////////////////////
41 protected static interface Body<T> {
42 void expand(int i, TreeMaker<T> h);
45 protected static class MyBody<T> extends Forest<T> implements Body<T> /* extends PrintableTree<Forest<T>> implements */ {
47 public <B,C> void visit(Invokable<Forest.Body<T>,B,C> ivbc, B b, C c) {
48 ivbc.invoke(this, b, c);
51 private final Input.Location location;
53 private final Forest<T>[] tokens;
54 private final boolean unwrap;
55 private final boolean singleton;
57 private MyBody(Input.Location loc, T tag, Forest<T>[] tokens, boolean unwrap, boolean singleton) {
60 this.tokens = tokens==null ? emptyForestArray : new Forest[tokens.length];
61 if (tokens != null) System.arraycopy(tokens, 0, this.tokens, 0, tokens.length);
62 if (tokens != null) for(int i=0; i<tokens.length; i++) if (tokens[i]==null) throw new Error(i+"");
64 this.singleton = singleton;
67 public void expand(final int i, final TreeMaker<T> h) {
69 tokens[0].visit(h, null, i);
72 if (i==0) h.start(tag, location);
74 if (i==tokens.length) {
75 h.finish(tag, location);
77 } else if (unwrap && i==tokens.length-1) {
78 if (tokens[i] != null)
79 tokens[i].visit(h, null, 0);
82 tokens[i].visit(new TreeMaker<T>(h.toss) {
83 public void start(T head, Input.Location loc) { }
84 public void child(Tree<T> t) { toks.add(t); }
85 public void finish(T head, Input.Location loc) {
86 int old = h.toks.size();
87 h.child(new Tree<T>(loc, head, toks.toArray(tree_hint)));
89 while(h.toks.size() > old) h.toks.remove(h.toks.size()-1);
95 protected String headToString() { return null; }
96 protected String headToJava() { return null; }
97 protected String left() { return "{"; }
98 protected String right() { return "}"; }
99 protected boolean ignoreSingleton() { return false; }
103 // Ref //////////////////////////////////////////////////////////////////////////////
106 * This class represents a partially complete collection of
107 * forests to be viewed as a forest at some later date; once
108 * viewed, it becomes immutable
110 static class Ref<T> extends Forest<T> {
111 private FastSet<Forest<T>> hp = new FastSet<Forest<T>>();
113 public void merge(Forest p) { if (p!=this) hp.add(p, true); }
114 public <B,C> void visit(Invokable<Forest.Body<T>,B,C> ivbc, B b, C c) {
115 if (hp==null) return;
116 for(Forest<T> f : hp)
119 public Forest resolve() { return this; }
122 public abstract <B,C> void visit(Invokable<Forest.Body<T>,B,C> ivbc, B b, C c);
123 private static abstract class TreeMaker<T> implements Invokable<Forest.Body<T>,Boolean,Integer> {
124 public ArrayList<Tree<T>> toks = new ArrayList<Tree<T>>();
125 private boolean toss;
127 public TreeMaker(boolean toss) { this.toss = toss; }
128 public abstract void start(T head, Input.Location loc);
129 public abstract void finish(T head, Input.Location loc);
130 public abstract void child(Tree<T> t);
131 public void invoke(Forest.Body<T> bod, Boolean o, Integer i) {
133 ArrayList<Tree<T>> toks = this.toks;
134 this.toks = new ArrayList<Tree<T>>();
143 // Statics //////////////////////////////////////////////////////////////////////////////
145 private static Tree[] tree_hint = new Tree[0];
146 private static final Forest[] emptyForestArray = new Forest[0];
148 protected String headToString() { return null; }
149 protected String headToJava() { return null; }
150 protected String left() { return "<?"; }
151 protected String right() { return "?>"; }
152 protected boolean ignoreSingleton() { return true; }