1 // Copyright 2006-2007 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 public static <NodeType> Forest<NodeType> create(Input.Region region, NodeType head, Forest<NodeType>[] children) {
32 return create(region, head, children, new boolean[children==null ? 0 : children.length]); }
33 public static <NodeType> Forest<NodeType> create(Input.Region region, NodeType head, Forest<NodeType>[] children, boolean[] lifts) {
34 if (region == null) throw new RuntimeException("invoked Forest.create(region=null) -- this should never happen");
35 return new One<NodeType>(region, head, children, lifts);
38 abstract void expand(HashSet<Tree<NodeType>> ht, HashSet<Forest<NodeType>> ignore, Tree<NodeType> bogus);
39 abstract void gather(HashSet<Forest<NodeType>> ignore);
40 abstract void edges(GraphViz.Node n);
41 boolean ambiguous() { return false; }
43 // One //////////////////////////////////////////////////////////////////////////////
45 /** A "single" forest with a head and child subforests */
46 private static class One<NodeType> extends Forest<NodeType> {
48 private final Input.Region location;
49 private final NodeType head;
50 private final Forest<NodeType>[] children;
52 /** if true, the last child's children are considered children of this node */
53 private final boolean[] lifts;
55 public Input.Region getRegion() { return location; }
57 private One(Input.Region loc, NodeType head, Forest<NodeType>[] children, boolean[] lifts) {
60 if (head==null) throw new RuntimeException("invoked Forest.create(,null,,,) -- this should never happen");
61 this.children = children==null ? emptyForestArray : new Forest[children.length];
62 if (children != null) System.arraycopy(children, 0, this.children, 0, children.length);
63 if (children != null) for(int i=0; i<children.length; i++) if (children[i]==null) throw new Error(i+"");
67 public Tree<NodeType> expand1() throws Ambiguous {
68 Tree<NodeType>[] ret = new Tree[children.length];
69 for(int i=0; i<children.length; i++) ret[i] = children[i].expand1();
70 return new Tree<NodeType>(location, head, ret, lifts);
73 void gather(HashSet<Forest<NodeType>> hf) {
75 for(Forest<NodeType> f : children) f.gather(hf);
77 void expand(HashSet<Tree<NodeType>> ht, HashSet<Forest<NodeType>> ignore, Tree<NodeType> bogus) {
78 if (ignore.contains(this)) { ht.add(bogus); return; }
79 expand(0, new Tree[children.length], ht, ignore, bogus);
81 private void expand(final int i, Tree<NodeType>[] ta, HashSet<Tree<NodeType>> ht, HashSet<Forest<NodeType>> ignore,
82 Tree<NodeType> bogus) {
83 if (i==children.length) {
84 ht.add(new Tree<NodeType>(location, head, ta, lifts));
86 HashSet<Tree<NodeType>> ht2 = new HashSet<Tree<NodeType>>();
87 children[i].expand(ht2, ignore, bogus);
88 for(Tree<NodeType> tc : ht2) {
90 expand(i+1, ta, ht, ignore, bogus);
96 // GraphViz, ToInt //////////////////////////////////////////////////////////////////////////////
98 public boolean isTransparent() { return false; }
99 public boolean isHidden() { return false; }
100 public GraphViz.Node toGraphViz(GraphViz gv) {
101 if (gv.hasNode(this)) return gv.createNode(this);
102 GraphViz.Node n = gv.createNode(this);
103 n.label = headToString()==null?"":headToString();
108 boolean edges = false; // FIXME ??
109 public void edges(GraphViz.Node n) {
112 for(int i=0; i<children.length; i++) {
113 if (lifts[i] && !children[i].ambiguous()) {
114 children[i].edges(n);
116 n.edge(children[i], null);
121 protected String headToString() { return head==null?null:head.toString(); }
122 protected String headToJava() { return "null"; }
123 protected String left() { return "{"; }
124 protected String right() { return "}"; }
125 protected boolean ignoreSingleton() { return false; }
129 // Many //////////////////////////////////////////////////////////////////////////////
131 /** An "ambiguity node"; this is immutable once it has been "looked at" */
132 static class Many<NodeType> extends Forest<NodeType> {
134 private FastSet<Forest<NodeType>> hp = new FastSet<Forest<NodeType>>();
135 private boolean touched = false;
139 public Input.Region getRegion() { return hp.iterator().next().getRegion(); } // all should be identical
141 public Tree<NodeType> expand1() throws Ambiguous {
144 HashSet<Forest<NodeType>> hf0 = new HashSet<Forest<NodeType>>();
145 Iterator<Forest<NodeType>> ih = hp.iterator();
146 ih.next().gather(hf0);
147 for(Forest<NodeType> f : hp) {
148 HashSet<Forest<NodeType>> hf1 = new HashSet<Forest<NodeType>>();
152 HashSet<Tree<NodeType>> ht = new HashSet<Tree<NodeType>>();
153 expand(ht, hf0, new Tree(null, "*"));
154 throw new Ambiguous((Forest<?>)this,
155 (HashSet<Tree<?>>)(Object)ht);
157 return hp.iterator().next().expand1();
160 void gather(HashSet<Forest<NodeType>> ht) {
163 // FIXME: do something more sensible here
164 if (ht.contains(this)) {
165 System.err.println("WARNING: grammar produced a circular forest\n" + this);
166 //throw new Error("grammar produced a circular forest:\n" + this);
171 for(Forest<NodeType> f : hp) f.gather(ht);
174 private void touched() {
178 FastSet<Forest<NodeType>> f2 = new FastSet<Forest<NodeType>>();
180 if (f instanceof Forest.One) f2.add(f);
181 else for(Forest ff : ((Forest.Many<NodeType>)f))
186 public boolean contains(Forest f) {
188 return hp.contains(f);
190 public void merge(Forest p) {
191 if (touched) throw new RuntimeException("attempt to merge() on a Forest.Many that has already been examined");
192 if (p==this) throw new RuntimeException("attempt to merge() a Forest.Many to itself!");
195 boolean ambiguous() {
197 if (hp.size()==0) return false;
198 if (hp.size()==1) return hp.iterator().next().ambiguous();
202 void expand(HashSet<Tree<NodeType>> ht, HashSet<Forest<NodeType>> ignore, Tree<NodeType> bogus) {
204 if (ignore.contains(this)) { ht.add(bogus); return; }
205 for (Forest<NodeType> f : hp) f.expand(ht, ignore, bogus);
209 // GraphViz, ToInt //////////////////////////////////////////////////////////////////////////////
211 public boolean isTransparent() { return hp.size()==1; }
212 public boolean isHidden() { return hp.size()==0; }
213 public void edges(GraphViz.Node n) {
214 if (hp.size()==1) { hp.iterator().next().edges(n); return; }
215 for(Forest f : hp) f.edges(n);
217 public GraphViz.Node toGraphViz(GraphViz gv) {
218 if (hp.size()==1) return hp.iterator().next().toGraphViz(gv);
219 if (gv.hasNode(this)) return gv.createNode(this);
220 GraphViz.Node n = gv.createNode(this);
223 for(Forest f : hp) n.edge(f, null);
228 // Statics //////////////////////////////////////////////////////////////////////////////
230 private static Tree[] tree_hint = new Tree[0];
231 private static String[] string_hint = new String[0];
232 private static final Forest[] emptyForestArray = new Forest[0];
234 protected String headToString() { return null; }
235 protected String headToJava() { return "null"; }
236 protected String left() { return "<?"; }
237 protected String right() { return "?>"; }
238 protected boolean ignoreSingleton() { return true; }