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,
33 if (region == null) throw new RuntimeException("invoked Forest.create(region=null) -- this should never happen");
34 return new One<NodeType>(region, head, children, lift);
37 static <NodeType> Forest<NodeType> create(Input.Region region, NodeType head, Forest<NodeType>[] children,
38 boolean lift, boolean liftLeft) {
39 if (region == null) throw new RuntimeException("invoked Forest.create(region=null) -- this should never happen");
40 return new One<NodeType>(region, head, children, lift, liftLeft);
43 /** create a new forest */
44 public static <NodeType> Forest<NodeType> create(Input.Region region, NodeType head, Forest<NodeType>[] children) {
45 return Forest.create(region, head, children, false); }
47 abstract void expand(HashSet<Tree<NodeType>> ht, HashSet<Forest<NodeType>> ignore, Tree<NodeType> bogus);
48 abstract void gather(HashSet<Forest<NodeType>> ignore);
49 abstract void edges(GraphViz.Node n);
50 boolean ambiguous() { return false; }
52 // One //////////////////////////////////////////////////////////////////////////////
54 /** A "single" forest with a head and child subforests */
55 private static class One<NodeType> extends Forest<NodeType> {
57 private final Input.Region location;
58 private final NodeType head;
59 private final Forest<NodeType>[] children;
61 /** if true, the last child's children are considered children of this node */
62 private final boolean lift;
63 private final boolean liftLeft;
65 public Input.Region getRegion() { return location; }
67 private One(Input.Region loc, NodeType head, Forest<NodeType>[] children, boolean lift) {
68 this(loc, head, children, lift, false);
70 private One(Input.Region loc, NodeType head, Forest<NodeType>[] children, boolean lift, boolean liftLeft) {
73 if (head==null) throw new RuntimeException("invoked Forest.create(,null,,,) -- this should never happen");
74 this.children = children==null ? emptyForestArray : new Forest[children.length];
75 if (children != null) System.arraycopy(children, 0, this.children, 0, children.length);
76 if (children != null) for(int i=0; i<children.length; i++) if (children[i]==null) throw new Error(i+"");
78 this.liftLeft = liftLeft;
81 public Tree<NodeType> expand1() throws Ambiguous {
82 Tree<NodeType>[] ret = new Tree[children.length];
83 for(int i=0; i<children.length; i++) ret[i] = children[i].expand1();
84 return new Tree<NodeType>(location, head, ret, lift, liftLeft);
87 void gather(HashSet<Forest<NodeType>> hf) {
89 for(Forest<NodeType> f : children) f.gather(hf);
91 void expand(HashSet<Tree<NodeType>> ht, HashSet<Forest<NodeType>> ignore, Tree<NodeType> bogus) {
92 if (ignore.contains(this)) { ht.add(bogus); return; }
93 expand(0, new Tree[children.length], ht, ignore, bogus);
95 private void expand(final int i, Tree<NodeType>[] ta, HashSet<Tree<NodeType>> ht, HashSet<Forest<NodeType>> ignore,
96 Tree<NodeType> bogus) {
97 if (i==children.length) {
98 ht.add(new Tree<NodeType>(location, head, ta, lift, liftLeft));
100 HashSet<Tree<NodeType>> ht2 = new HashSet<Tree<NodeType>>();
101 children[i].expand(ht2, ignore, bogus);
102 for(Tree<NodeType> tc : ht2) {
104 expand(i+1, ta, ht, ignore, bogus);
110 // GraphViz, ToInt //////////////////////////////////////////////////////////////////////////////
112 public boolean isTransparent() { return false; }
113 public boolean isHidden() { return false; }
114 public GraphViz.Node toGraphViz(GraphViz gv) {
115 if (gv.hasNode(this)) return gv.createNode(this);
116 GraphViz.Node n = gv.createNode(this);
117 n.label = headToString()==null?"":headToString();
122 boolean edges = false; // FIXME ??
123 public void edges(GraphViz.Node n) {
126 for(int i=0; i<children.length; i++) {
127 if (i==children.length-1 && lift && !children[i].ambiguous()) {
128 children[i].edges(n);
130 n.edge(children[i], null);
135 protected String headToString() { return head==null?null:head.toString(); }
136 protected String headToJava() { return "null"; }
137 protected String left() { return "{"; }
138 protected String right() { return "}"; }
139 protected boolean ignoreSingleton() { return false; }
143 // Many //////////////////////////////////////////////////////////////////////////////
145 /** An "ambiguity node"; this is immutable once it has been "looked at" */
146 static class Many<NodeType> extends Forest<NodeType> {
148 private FastSet<Forest<NodeType>> hp = new FastSet<Forest<NodeType>>();
149 private boolean touched = false;
153 public Input.Region getRegion() { return hp.iterator().next().getRegion(); } // all should be identical
155 public Tree<NodeType> expand1() throws Ambiguous {
158 HashSet<Forest<NodeType>> hf0 = new HashSet<Forest<NodeType>>();
159 Iterator<Forest<NodeType>> ih = hp.iterator();
160 ih.next().gather(hf0);
161 for(Forest<NodeType> f : hp) {
162 HashSet<Forest<NodeType>> hf1 = new HashSet<Forest<NodeType>>();
166 HashSet<Tree<NodeType>> ht = new HashSet<Tree<NodeType>>();
167 expand(ht, hf0, new Tree(null, "*"));
168 throw new Ambiguous((Forest<?>)this,
169 (HashSet<Tree<?>>)(Object)ht);
171 return hp.iterator().next().expand1();
174 void gather(HashSet<Forest<NodeType>> ht) {
177 // FIXME: do something more sensible here
178 if (ht.contains(this)) {
179 System.err.println("WARNING: grammar produced a circular forest\n" + this);
180 //throw new Error("grammar produced a circular forest:\n" + this);
185 for(Forest<NodeType> f : hp) f.gather(ht);
188 private void touched() {
192 FastSet<Forest<NodeType>> f2 = new FastSet<Forest<NodeType>>();
194 if (f instanceof Forest.One) f2.add(f);
195 else for(Forest ff : ((Forest.Many<NodeType>)f))
200 public boolean contains(Forest f) {
202 return hp.contains(f);
204 public void merge(Forest p) {
205 if (touched) throw new RuntimeException("attempt to merge() on a Forest.Many that has already been examined");
206 if (p==this) throw new RuntimeException("attempt to merge() a Forest.Many to itself!");
209 boolean ambiguous() {
211 if (hp.size()==0) return false;
212 if (hp.size()==1) return hp.iterator().next().ambiguous();
216 void expand(HashSet<Tree<NodeType>> ht, HashSet<Forest<NodeType>> ignore, Tree<NodeType> bogus) {
218 if (ignore.contains(this)) { ht.add(bogus); return; }
219 for (Forest<NodeType> f : hp) f.expand(ht, ignore, bogus);
223 // GraphViz, ToInt //////////////////////////////////////////////////////////////////////////////
225 public boolean isTransparent() { return hp.size()==1; }
226 public boolean isHidden() { return hp.size()==0; }
227 public void edges(GraphViz.Node n) {
228 if (hp.size()==1) { hp.iterator().next().edges(n); return; }
229 for(Forest f : hp) f.edges(n);
231 public GraphViz.Node toGraphViz(GraphViz gv) {
232 if (hp.size()==1) return hp.iterator().next().toGraphViz(gv);
233 if (gv.hasNode(this)) return gv.createNode(this);
234 GraphViz.Node n = gv.createNode(this);
237 for(Forest f : hp) n.edge(f, null);
242 // Statics //////////////////////////////////////////////////////////////////////////////
244 private static Tree[] tree_hint = new Tree[0];
245 private static String[] string_hint = new String[0];
246 private static final Forest[] emptyForestArray = new Forest[0];
248 protected String headToString() { return null; }
249 protected String headToJava() { return "null"; }
250 protected String left() { return "<?"; }
251 protected String right() { return "?>"; }
252 protected boolean ignoreSingleton() { return true; }