1 // Copyright 2006 all rights reserved; see LICENSE file for BSD-style license
3 package edu.berkeley.sbp;
4 import edu.berkeley.sbp.util.*;
10 * An efficient representation of a collection of trees (Tomita's
11 * shared packed parse forest).
14 public abstract class Forest<NodeType> implements GraphViz.ToGraphViz {
16 /** assume that this forest contains exactly one tree and return it; otherwise throw an exception */
17 public abstract Tree<NodeType> expand1() throws Ambiguous;
19 /** expand this forest into a set of trees */
20 public Iterable<Tree<NodeType>> expand() {
21 HashSet<Tree<NodeType>> ht = new HashSet<Tree<NodeType>>();
22 expand(ht, new HashSet<Forest<NodeType>>(), null);
26 /** returns the input Region which this Forest was parsed from */
27 public abstract Input.Region getRegion();
29 // Package-Private //////////////////////////////////////////////////////////////////////////////
31 static <NodeType> Forest<NodeType> create(Input.Region region, NodeType head, Forest<NodeType>[] children, boolean lift) {
32 if (region == null) throw new RuntimeException("invoked Forest.create(region=null) -- this should never happen");
33 return new One<NodeType>(region, head, children, lift);
36 /** create a new forest */
37 public static <NodeType> Forest<NodeType> create(Input.Region region, NodeType head, Forest<NodeType>[] children) {
38 return Forest.create(region, head, children, false); }
40 abstract void expand(HashSet<Tree<NodeType>> ht, HashSet<Forest<NodeType>> ignore, Tree<NodeType> bogus);
41 abstract void gather(HashSet<Forest<NodeType>> ignore);
42 abstract void edges(GraphViz.Node n);
43 boolean ambiguous() { return false; }
45 // One //////////////////////////////////////////////////////////////////////////////
47 /** A "single" forest with a head and child subforests */
48 private static class One<NodeType> extends Forest<NodeType> {
50 private final Input.Region location;
51 private final NodeType head;
52 private final Forest<NodeType>[] children;
54 /** if true, the last child's children are considered children of this node */
55 private final boolean lift;
57 public Input.Region getRegion() { return location; }
59 private One(Input.Region loc, NodeType head, Forest<NodeType>[] children, boolean lift) {
62 if (head==null) throw new RuntimeException("invoked Forest.create(,null,,,) -- this should never happen");
63 this.children = children==null ? emptyForestArray : new Forest[children.length];
64 if (children != null) System.arraycopy(children, 0, this.children, 0, children.length);
65 if (children != null) for(int i=0; i<children.length; i++) if (children[i]==null) throw new Error(i+"");
69 public Tree<NodeType> expand1() throws Ambiguous {
70 Tree<NodeType>[] ret = new Tree[children.length];
71 for(int i=0; i<children.length; i++) ret[i] = children[i].expand1();
72 return new Tree<NodeType>(location, head, ret, lift);
75 void gather(HashSet<Forest<NodeType>> hf) {
77 for(Forest<NodeType> f : children) f.gather(hf);
79 void expand(HashSet<Tree<NodeType>> ht, HashSet<Forest<NodeType>> ignore, Tree<NodeType> bogus) {
80 if (ignore.contains(this)) { ht.add(bogus); return; }
81 expand(0, new Tree[children.length], ht, ignore, bogus);
83 private void expand(final int i, Tree<NodeType>[] ta, HashSet<Tree<NodeType>> ht, HashSet<Forest<NodeType>> ignore,
84 Tree<NodeType> bogus) {
85 if (i==children.length) {
86 ht.add(new Tree<NodeType>(location, head, ta, lift));
88 HashSet<Tree<NodeType>> ht2 = new HashSet<Tree<NodeType>>();
89 children[i].expand(ht2, ignore, bogus);
90 for(Tree<NodeType> tc : ht2) {
92 expand(i+1, ta, ht, ignore, bogus);
98 // GraphViz, ToInt //////////////////////////////////////////////////////////////////////////////
100 public boolean isTransparent() { return false; }
101 public boolean isHidden() { return false; }
102 public GraphViz.Node toGraphViz(GraphViz gv) {
103 if (gv.hasNode(this)) return gv.createNode(this);
104 GraphViz.Node n = gv.createNode(this);
105 n.label = headToString()==null?"":headToString();
110 boolean edges = false; // FIXME ??
111 public void edges(GraphViz.Node n) {
114 for(int i=0; i<children.length; i++) {
115 if (i==children.length-1 && lift && !children[i].ambiguous()) {
116 children[i].edges(n);
118 n.edge(children[i], null);
123 protected String headToString() { return head==null?null:head.toString(); }
124 protected String headToJava() { return "null"; }
125 protected String left() { return "{"; }
126 protected String right() { return "}"; }
127 protected boolean ignoreSingleton() { return false; }
131 // Many //////////////////////////////////////////////////////////////////////////////
133 /** An "ambiguity node"; this is immutable once it has been "looked at" */
134 static class Many<NodeType> extends Forest<NodeType> {
136 private FastSet<Forest<NodeType>> hp = new FastSet<Forest<NodeType>>();
137 private boolean touched = false;
141 public Input.Region getRegion() { return hp.iterator().next().getRegion(); } // all should be identical
143 public Tree<NodeType> expand1() throws Ambiguous {
146 HashSet<Forest<NodeType>> hf0 = new HashSet<Forest<NodeType>>();
147 Iterator<Forest<NodeType>> ih = hp.iterator();
148 ih.next().gather(hf0);
149 for(Forest<NodeType> f : hp) {
150 HashSet<Forest<NodeType>> hf1 = new HashSet<Forest<NodeType>>();
154 HashSet<Tree<NodeType>> ht = new HashSet<Tree<NodeType>>();
155 expand(ht, hf0, new Tree(null, "*"));
156 throw new Ambiguous((Forest<?>)this,
157 (HashSet<Tree<?>>)(Object)ht);
159 return hp.iterator().next().expand1();
162 void gather(HashSet<Forest<NodeType>> ht) {
165 // FIXME: do something more sensible here
166 if (ht.contains(this)) {
167 System.err.println("WARNING: grammar produced a circular forest\n" + this);
168 //throw new Error("grammar produced a circular forest:\n" + this);
173 for(Forest<NodeType> f : hp) f.gather(ht);
176 private void touched() {
180 FastSet<Forest<NodeType>> f2 = new FastSet<Forest<NodeType>>();
182 if (f instanceof Forest.One) f2.add(f);
183 else for(Forest ff : ((Forest.Many<NodeType>)f))
188 public boolean contains(Forest f) {
190 return hp.contains(f);
192 public void merge(Forest p) {
193 if (touched) throw new RuntimeException("attempt to merge() on a Forest.Many that has already been examined");
194 if (p==this) throw new RuntimeException("attempt to merge() a Forest.Many to itself!");
197 boolean ambiguous() {
199 if (hp.size()==0) return false;
200 if (hp.size()==1) return hp.iterator().next().ambiguous();
204 void expand(HashSet<Tree<NodeType>> ht, HashSet<Forest<NodeType>> ignore, Tree<NodeType> bogus) {
206 if (ignore.contains(this)) { ht.add(bogus); return; }
207 for (Forest<NodeType> f : hp) f.expand(ht, ignore, bogus);
211 // GraphViz, ToInt //////////////////////////////////////////////////////////////////////////////
213 public boolean isTransparent() { return hp.size()==1; }
214 public boolean isHidden() { return hp.size()==0; }
215 public void edges(GraphViz.Node n) {
216 if (hp.size()==1) { hp.iterator().next().edges(n); return; }
217 for(Forest f : hp) f.edges(n);
219 public GraphViz.Node toGraphViz(GraphViz gv) {
220 if (hp.size()==1) return hp.iterator().next().toGraphViz(gv);
221 if (gv.hasNode(this)) return gv.createNode(this);
222 GraphViz.Node n = gv.createNode(this);
225 for(Forest f : hp) n.edge(f, null);
230 // Statics //////////////////////////////////////////////////////////////////////////////
232 private static Tree[] tree_hint = new Tree[0];
233 private static String[] string_hint = new String[0];
234 private static final Forest[] emptyForestArray = new Forest[0];
236 protected String headToString() { return null; }
237 protected String headToJava() { return "null"; }
238 protected String left() { return "<?"; }
239 protected String right() { return "?>"; }
240 protected boolean ignoreSingleton() { return true; }