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) {
66 TaskList tl = new TaskList();
67 HashSet<Tree<T>> ret = new HashSet<Tree<T>>();
71 if (toss && ret.size() > 1) throw new InnerAmbiguous(this);
75 private static class InnerAmbiguous extends RuntimeException {
76 public final Forest<?> f;
77 public InnerAmbiguous(Forest<?> f) { this.f = f; }
80 static <T> Forest<T> singleton(Input.Region loc, Position p) { return create(loc, null, new Forest[] { }, false, p); }
81 static <T> Forest<T> singleton(Input.Region loc, Forest<T> body, Position p) { return body; }
82 static <T> Forest<T> leaf(Input.Region loc, T tag, Position p) { return create(loc, tag, null, false, p); }
83 public static <T> Forest<T> create(Input.Region loc, T tag, Forest<T>[] tokens, boolean unwrap, Position p) {
84 return new MyBody<T>(loc, tag, tokens, unwrap, p);
86 // Body //////////////////////////////////////////////////////////////////////////////
88 public /*protected*/ static interface Body<T> extends GraphViz.ToGraphViz {
89 void expand(int i, TreeBuilder<T> h);
91 public abstract void edges(GraphViz.Node n);
92 public boolean ambiguous() { return false; }
93 public /*protected*/ static class MyBody<T> extends Forest<T> implements Body<T> /* extends PrintableTree<Forest<T>> implements */ {
95 public boolean isTransparent() { return false; }
96 public boolean isHidden() { return false; }
97 public GraphViz.Node toGraphViz(GraphViz gv) {
98 if (gv.hasNode(this)) return gv.createNode(this);
99 GraphViz.Node n = gv.createNode(this);
100 n.label = headToString()==null?"":headToString();
102 n.comment = reduction==null?null:reduction+"";
106 boolean edges = false;
107 public void edges(GraphViz.Node n) {
110 for(int i=0; i<tokens.length; i++) {
111 if (i==tokens.length-1 && unwrap && !tokens[i].ambiguous()) {
114 n.edge(tokens[i], null);
119 public <B,C> void visit(Invokable<Forest.Body<T>,B,C> ivbc, B b, C c) {
120 ivbc.invoke(this, b, c);
123 private final Input.Region location;
125 private final Forest<T>[] tokens;
126 private final boolean unwrap;
127 private final Sequence.Position reduction;
129 private MyBody(Input.Region loc, T tag, Forest<T>[] tokens, boolean unwrap, Position reduction) {
132 this.tokens = tokens==null ? emptyForestArray : new Forest[tokens.length];
133 if (tokens != null) System.arraycopy(tokens, 0, this.tokens, 0, tokens.length);
134 if (tokens != null) for(int i=0; i<tokens.length; i++) if (tokens[i]==null) throw new Error(i+"");
135 this.unwrap = unwrap;
136 this.reduction = reduction;
138 public void gather(TaskList tl, HashSet<Tree<T>>[] ht, HashSet<Tree<T>> target) {
139 gather(tl, ht, target, new Tree[ht.length], 0);
141 private void gather(TaskList tl, HashSet<Tree<T>>[] ht, HashSet<Tree<T>> target, Tree[] trees, int i) {
143 target.add(new Tree<T>(location, tag, trees));
146 for(Tree<T> tree : ht[i]) {
147 if (unwrap && i==trees.length-1) {
148 // I think this is wrong
149 Tree[] trees2 = new Tree[trees.length - 1 + tree.numChildren()];
150 System.arraycopy(trees, 0, trees2, 0, trees.length-1);
151 for(int j=0; j<tree.numChildren(); j++)
152 trees2[trees.length-1+j] = tree.child(j);
153 target.add(new Tree<T>(location, tag, trees2));
156 gather(tl, ht, target, trees, i+1);
161 public void expand(TaskList tl, HashSet<Tree<T>> ht) {
162 HashSet<Tree<T>>[] children = new HashSet[tokens.length];
163 tl.gather(this, children, ht);
164 for(int i=0; i<children.length; i++) {
165 children[i] = new HashSet<Tree<T>>();
166 tl.expand(tokens[i], children[i]);
170 public void expand(final int i, final TreeBuilder<T> h) {
171 if (i==0) h.start(tag, location);
173 if (i==tokens.length) {
174 h.finish(tag, location);
176 } else if (unwrap && i==tokens.length-1) {
177 if (tokens[i] != null)
178 tokens[i].visit(h, null, 0);
181 tokens[i].visit(new TreeBuilder<T>(h.toss) {
182 public void start(T head, Input.Region loc) { }
183 public void addTree(Tree<T> t) { toks.add(t); }
184 public void finish(T head, Input.Region loc) {
185 int old = h.toks.size();
186 h.addTree(new Tree<T>(loc, head, toks.toArray(tree_hint)));
188 while(h.toks.size() > old) h.toks.remove(h.toks.size()-1);
194 protected String headToString() { return tag==null?null:tag.toString(); }
195 protected String headToJava() { return "null"; }
196 protected String left() { return "{"; }
197 protected String right() { return "}"; }
198 protected boolean ignoreSingleton() { return false; }
202 // Ref //////////////////////////////////////////////////////////////////////////////
205 * This class represents a partially complete collection of
206 * forests to be viewed as a forest at some later date; once
207 * viewed, it becomes immutable
209 static class Ref<T> extends Forest<T> {
210 public void expand(TaskList tl, HashSet<Tree<T>> ht) {
211 for (Forest<T> f : hp) f.expand(tl, ht);
213 public void gather(TaskList tl, HashSet<Tree<T>>[] ht, HashSet<Tree<T>> target) {
216 public HashSet<GSS.Phase.Node> parents = new HashSet<GSS.Phase.Node>();
217 public boolean contains(Forest f) {
218 return hp.contains(f);
220 public boolean ambiguous() {
221 if (hp.size()==0) return false;
222 if (hp.size()==1) return hp.iterator().next().ambiguous();
225 private FastSet<Forest<T>> hp = new FastSet<Forest<T>>();
228 if (hp.size()==1) return hp.iterator().next().toInt();
229 return super.toInt();
231 public void merge(Forest p) { if (p!=this) hp.add(p, true); }
233 public boolean isTransparent() { return hp.size()==1; }
234 public boolean isHidden() { return hp.size()==0; }
235 public void edges(GraphViz.Node n) {
236 if (hp.size()==1) { hp.iterator().next().edges(n); return; }
237 for(Forest f : hp) f.edges(n);
239 public GraphViz.Node toGraphViz(GraphViz gv) {
240 //if (hp.size()==0) return null;
241 if (hp.size()==1) return hp.iterator().next().toGraphViz(gv);
242 if (gv.hasNode(this)) return gv.createNode(this);
243 GraphViz.Node n = gv.createNode(this);
246 for(Forest f : hp) n.edge(f, null);
250 public <B,C> void visit(Invokable<Forest.Body<T>,B,C> ivbc, B b, C c) {
251 if (hp==null) return;
252 for(Forest<T> f : hp)
255 public Forest resolve() { return this; }
258 public abstract <B,C> void visit(Invokable<Forest.Body<T>,B,C> ivbc, B b, C c);
260 // Statics //////////////////////////////////////////////////////////////////////////////
262 private static Tree[] tree_hint = new Tree[0];
263 private static String[] string_hint = new String[0];
264 private static final Forest[] emptyForestArray = new Forest[0];
266 protected String headToString() { return null; }
267 protected String headToJava() { return "null"; }
268 protected String left() { return "<?"; }
269 protected String right() { return "?>"; }
270 protected boolean ignoreSingleton() { return true; }