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[] { }, 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, false, false, p); }
52 public static <T> Forest<T> create(Input.Location loc, T tag, Forest<T>[] tokens, boolean unwrap, boolean singleton, Position p) {
53 return new MyBody<T>(loc, tag, tokens, 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) {
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 boolean unwrap;
94 private final boolean singleton;
95 private final Sequence.Position reduction;
97 private MyBody(Input.Location loc, T tag, Forest<T>[] tokens, boolean unwrap, boolean singleton, Position reduction) {
100 this.tokens = tokens==null ? emptyForestArray : new Forest[tokens.length];
101 if (tokens != null) System.arraycopy(tokens, 0, this.tokens, 0, tokens.length);
102 if (tokens != null) for(int i=0; i<tokens.length; i++) if (tokens[i]==null) throw new Error(i+"");
103 this.unwrap = unwrap;
104 this.singleton = singleton;
105 this.reduction = reduction;
108 public void expand(final int i, final TreeMaker<T> h) {
110 tokens[0].visit(h, null, i);
113 if (i==0) h.start(tag, location);
115 if (i==tokens.length) {
116 h.finish(tag, location);
118 } else if (unwrap && i==tokens.length-1) {
119 if (tokens[i] != null)
120 tokens[i].visit(h, null, 0);
123 tokens[i].visit(new TreeMaker<T>(h.toss) {
124 public void start(T head, Input.Location loc) { }
125 public void addTree(Tree<T> t) { toks.add(t); }
126 public void finish(T head, Input.Location loc) {
127 int old = h.toks.size();
128 h.addTree(new Tree<T>(loc, head, toks.toArray(tree_hint)));
130 while(h.toks.size() > old) h.toks.remove(h.toks.size()-1);
136 protected String headToString() { return tag==null?null:tag.toString(); }
137 protected String headToJava() { return null; }
138 protected String left() { return "{"; }
139 protected String right() { return "}"; }
140 protected boolean ignoreSingleton() { return false; }
144 // Ref //////////////////////////////////////////////////////////////////////////////
147 * This class represents a partially complete collection of
148 * forests to be viewed as a forest at some later date; once
149 * viewed, it becomes immutable
151 static class Ref<T> extends Forest<T> {
152 private FastSet<Forest<T>> hp = new FastSet<Forest<T>>();
155 if (hp.size()==1) return hp.iterator().next().toInt();
156 return super.toInt();
158 public void merge(Forest p) { if (p!=this) hp.add(p, true); }
160 public boolean isTransparent() { return hp.size()==1; }
161 public boolean isHidden() { return hp.size()==0; }
162 public void edges(GraphViz.Node n) { for(Forest f : hp) f.edges(n); }
163 public GraphViz.Node toGraphViz(GraphViz gv) {
164 if (hp.size()==0) return null;
165 if (hp.size()==1) return hp.iterator().next().toGraphViz(gv);
166 if (gv.hasNode(this)) return gv.createNode(this);
167 GraphViz.Node n = gv.createNode(this);
170 for(Forest f : hp) n.edge(f);
174 public <B,C> void visit(Invokable<Forest.Body<T>,B,C> ivbc, B b, C c) {
175 if (hp==null) return;
176 for(Forest<T> f : hp)
179 public Forest resolve() { return this; }
182 public abstract <B,C> void visit(Invokable<Forest.Body<T>,B,C> ivbc, B b, C c);
183 private static class TreeMaker2<T> extends TreeMaker<T> {
184 private TreeConsumer<T> tc;
185 public TreeMaker2(boolean toss, TreeConsumer<T> tc) { super(toss); this.tc = tc; }
186 public void finish(T head, Input.Location loc) { tc.addTree(new Tree<T>(loc, head, toks.toArray(tree_hint)));; }
187 public void start(T head, Input.Location loc) { }
188 public void addTree(Tree<T> t) { toks.add(t); }
190 private static abstract class TreeMaker<T> implements Invokable<Forest.Body<T>,Boolean,Integer>, TreeConsumer<T> {
191 public ArrayList<Tree<T>> toks = new ArrayList<Tree<T>>();
192 private boolean toss;
194 public TreeMaker(boolean toss) { this.toss = toss; }
195 public abstract void start(T head, Input.Location loc);
196 public abstract void finish(T head, Input.Location loc);
197 public abstract void addTree(Tree<T> t);
198 public void invoke(Forest.Body<T> bod, Boolean o, Integer i) {
200 ArrayList<Tree<T>> toks = this.toks;
201 this.toks = new ArrayList<Tree<T>>();
210 // Statics //////////////////////////////////////////////////////////////////////////////
212 private static Tree[] tree_hint = new Tree[0];
213 private static final Forest[] emptyForestArray = new Forest[0];
215 protected String headToString() { return null; }
216 protected String headToJava() { return null; }
217 protected String left() { return "<?"; }
218 protected String right() { return "?>"; }
219 protected boolean ignoreSingleton() { return true; }