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>>*/ implements Visitable<Forest.Body<T>>, IntegerMappable, GraphViz.ToGraphViz {
12 private static int master_idx = 0;
13 private final int idx = master_idx++;
14 public int toInt() { return idx; }
16 /** assume that this forest contains exactly one tree and return it; otherwise throw an exception */
17 public final Tree<T> expand1() throws Ambiguous, ParseFailed {
19 Iterator<Tree<T>> it = expand(true).iterator();
20 if (!it.hasNext()) throw new ParseFailed();
22 } catch (InnerAmbiguous ia) { throw new Ambiguous(ia.f); }
25 /** expand this forest into a set of trees */
26 public HashSet<Tree<T>> expand(boolean toss) {
27 final HashSetTreeConsumer<T> ret = new HashSetTreeConsumer<T>();
28 visit(new TreeMaker2<T>(toss, ret), null, null);
29 if (toss && ret.size() > 1) throw new InnerAmbiguous(this);
33 private static class InnerAmbiguous extends RuntimeException {
34 public final Forest<?> f;
35 public InnerAmbiguous(Forest<?> f) { this.f = f; }
38 public static interface TreeConsumer<T> {
39 public void addTree(Tree<T> t);
41 public static class HashSetTreeConsumer<T> extends HashSet<Tree<T>> implements TreeConsumer<T> {
42 public void addTree(Tree<T> t) { super.add(t); }
45 static <T> Forest<T> singleton(Input.Location loc, Position p) {
46 return create(loc, null, new Forest[] { }, new Object[0], false, true, p); }
47 static <T> Forest<T> singleton(Input.Location loc, Forest<T> body, Position p) {
48 //return create(loc, null, new Forest[] { body }, false, true, p);
51 static <T> Forest<T> leaf(Input.Location loc, T tag, Position p) { return create(loc, tag, null, null, false, false, p); }
52 public static <T> Forest<T> create(Input.Location loc, T tag, Forest<T>[] tokens, Object[] labels, boolean unwrap, boolean singleton, Position p) {
53 return new MyBody<T>(loc, tag, tokens, labels, unwrap, singleton, p);
55 // Body //////////////////////////////////////////////////////////////////////////////
57 protected static interface Body<T> extends GraphViz.ToGraphViz {
58 void expand(int i, TreeMaker<T> h);
60 public abstract void edges(GraphViz.Node n);
61 protected static class MyBody<T> extends Forest<T> implements Body<T> /* extends PrintableTree<Forest<T>> implements */ {
63 public boolean isTransparent() { return false; }
64 public boolean isHidden() { return false; }
65 public GraphViz.Node toGraphViz(GraphViz gv) {
66 if (gv.hasNode(this)) return gv.createNode(this);
67 GraphViz.Node n = gv.createNode(this);
68 n.label = headToString()==null?"":headToString();
73 boolean edges = false;
74 public void edges(GraphViz.Node n) {
77 for(int i=0; i<tokens.length; i++) {
78 if (i==tokens.length-1 && unwrap) {
81 n.edge(tokens[i], labels==null?null:labels[i]);
86 public <B,C> void visit(Invokable<Forest.Body<T>,B,C> ivbc, B b, C c) {
87 ivbc.invoke(this, b, c);
90 private final Input.Location location;
92 private final Forest<T>[] tokens;
93 private final Object[] labels;
94 private final boolean unwrap;
95 private final boolean singleton;
96 private final Sequence.Position reduction;
98 private MyBody(Input.Location loc, T tag, Forest<T>[] tokens, Object[] labels, boolean unwrap, boolean singleton, Position reduction) {
101 this.tokens = tokens==null ? emptyForestArray : new Forest[tokens.length];
102 if (tokens != null) System.arraycopy(tokens, 0, this.tokens, 0, tokens.length);
103 if (tokens != null) for(int i=0; i<tokens.length; i++) if (tokens[i]==null) throw new Error(i+"");
104 this.unwrap = unwrap;
105 this.singleton = singleton;
106 this.reduction = reduction;
107 this.labels = labels;
110 public void expand(final int i, final TreeMaker<T> h) {
112 tokens[0].visit(h, null, i);
115 if (i==0) h.start(tag, location);
117 if (i==tokens.length) {
118 h.finish(tag, location);
120 } else if (unwrap && i==tokens.length-1) {
121 if (tokens[i] != null)
122 tokens[i].visit(h, null, 0);
125 tokens[i].visit(new TreeMaker<T>(h.toss) {
126 public void start(T head, Input.Location loc) { }
127 public void addTree(Tree<T> t, Object label) { toks.add(t); labs.add(label); }
128 public void finish(T head, Input.Location loc) {
129 int old = h.toks.size();
130 h.addTree(new Tree<T>(loc, head, toks.toArray(tree_hint), labs.toArray(string_hint)), labels==null?null:labels[i]);
132 while(h.toks.size() > old) h.toks.remove(h.toks.size()-1);
133 while(h.labs.size() > old) h.labs.remove(h.labs.size()-1);
139 protected String headToString() { return tag==null?null:tag.toString(); }
140 protected String headToJava() { return null; }
141 protected String left() { return "{"; }
142 protected String right() { return "}"; }
143 protected boolean ignoreSingleton() { return false; }
147 // Ref //////////////////////////////////////////////////////////////////////////////
150 * This class represents a partially complete collection of
151 * forests to be viewed as a forest at some later date; once
152 * viewed, it becomes immutable
154 static class Ref<T> extends Forest<T> {
155 private FastSet<Forest<T>> hp = new FastSet<Forest<T>>();
158 if (hp.size()==1) return hp.iterator().next().toInt();
159 return super.toInt();
161 public void merge(Forest p) { if (p!=this) hp.add(p, true); }
163 public boolean isTransparent() { return hp.size()==1; }
164 public boolean isHidden() { return hp.size()==0; }
165 public void edges(GraphViz.Node n) { for(Forest f : hp) f.edges(n); }
166 public GraphViz.Node toGraphViz(GraphViz gv) {
167 if (hp.size()==0) return null;
168 if (hp.size()==1) return hp.iterator().next().toGraphViz(gv);
169 if (gv.hasNode(this)) return gv.createNode(this);
170 GraphViz.Node n = gv.createNode(this);
173 for(Forest f : hp) n.edge(f, null);
177 public <B,C> void visit(Invokable<Forest.Body<T>,B,C> ivbc, B b, C c) {
178 if (hp==null) return;
179 for(Forest<T> f : hp)
182 public Forest resolve() { return this; }
185 public abstract <B,C> void visit(Invokable<Forest.Body<T>,B,C> ivbc, B b, C c);
186 private static class TreeMaker2<T> extends TreeMaker<T> {
187 private TreeConsumer<T> tc;
188 public TreeMaker2(boolean toss, TreeConsumer<T> tc) { super(toss); this.tc = tc; }
189 public void finish(T head, Input.Location loc) { tc.addTree(new Tree<T>(loc, head, toks.toArray(tree_hint), labs.toArray(string_hint)));; }
190 public void start(T head, Input.Location loc) { }
191 public void addTree(Tree<T> t, Object label) { toks.add(t); labs.add(label); }
193 private static abstract class TreeMaker<T> implements Invokable<Forest.Body<T>,Boolean,Integer>/*, TreeConsumer<T>*/ {
194 public ArrayList<Tree<T>> toks = new ArrayList<Tree<T>>();
195 public ArrayList<Object> labs = new ArrayList<Object>();
196 private boolean toss;
198 public TreeMaker(boolean toss) { this.toss = toss; }
199 public abstract void start(T head, Input.Location loc);
200 public abstract void finish(T head, Input.Location loc);
201 public abstract void addTree(Tree<T> t, Object label);
202 public void invoke(Forest.Body<T> bod, Boolean o, Integer i) {
204 ArrayList<Tree<T>> toks = this.toks;
205 this.toks = new ArrayList<Tree<T>>();
206 ArrayList<Object> labs = this.labs;
207 this.labs = new ArrayList<Object>();
217 // Statics //////////////////////////////////////////////////////////////////////////////
219 private static Tree[] tree_hint = new Tree[0];
220 private static String[] string_hint = new String[0];
221 private static final Forest[] emptyForestArray = new Forest[0];
223 protected String headToString() { return null; }
224 protected String headToJava() { return null; }
225 protected String left() { return "<?"; }
226 protected String right() { return "?>"; }
227 protected boolean ignoreSingleton() { return true; }