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.StateNode 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 if (children.length == 0)
69 return new Tree<NodeType>(location, head);
71 return expand1ForOneAndMany((Forest<NodeType>)this);
74 void gather(HashSet<Forest<NodeType>> hf) {
76 for(Forest<NodeType> f : children) f.gather(hf);
78 void expand(HashSet<Tree<NodeType>> ht, HashSet<Forest<NodeType>> ignore, Tree<NodeType> bogus) {
79 if (ignore.contains(this)) { ht.add(bogus); return; }
80 expand(0, new Tree[children.length], ht, ignore, bogus);
82 private void expand(final int i, Tree<NodeType>[] ta, HashSet<Tree<NodeType>> ht, HashSet<Forest<NodeType>> ignore,
83 Tree<NodeType> bogus) {
84 if (i==children.length) {
85 ht.add(new Tree<NodeType>(location, head, ta, lifts));
87 HashSet<Tree<NodeType>> ht2 = new HashSet<Tree<NodeType>>();
88 children[i].expand(ht2, ignore, bogus);
89 for(Tree<NodeType> tc : ht2) {
91 expand(i+1, ta, ht, ignore, bogus);
97 // GraphViz, ToInt //////////////////////////////////////////////////////////////////////////////
99 public boolean isTransparent() { return false; }
100 public boolean isHidden() { return false; }
101 public GraphViz.StateNode toGraphViz(GraphViz gv) {
102 if (gv.hasNode(this)) return gv.createNode(this);
103 GraphViz.StateNode n = gv.createNode(this);
104 n.label = headToString()==null?"":headToString();
109 boolean edges = false; // FIXME ??
110 public void edges(GraphViz.StateNode n) {
113 for(int i=0; i<children.length; i++) {
114 if (lifts[i] && !children[i].ambiguous()) {
115 children[i].edges(n);
117 n.edge(children[i], null);
122 protected String headToString() { return head==null?null:head.toString(); }
123 protected String headToJava() { return "null"; }
124 protected String left() { return "{"; }
125 protected String right() { return "}"; }
126 protected boolean ignoreSingleton() { return false; }
130 // Many //////////////////////////////////////////////////////////////////////////////
132 /** An "ambiguity node"; this is immutable once it has been "looked at" */
133 static class Many<NodeType> extends Forest<NodeType> {
135 private FastSet<Forest<NodeType>> hp = new FastSet<Forest<NodeType>>();
136 private boolean touched = false;
140 public Input.Region getRegion() { return hp.iterator().next().getRegion(); } // all should be identical
142 Forest<NodeType> simplify() throws Ambiguous {
145 HashSet<Forest<NodeType>> hf0 = new HashSet<Forest<NodeType>>();
146 Iterator<Forest<NodeType>> ih = hp.iterator();
147 ih.next().gather(hf0);
148 for(Forest<NodeType> f : hp) {
149 HashSet<Forest<NodeType>> hf1 = new HashSet<Forest<NodeType>>();
153 HashSet<Tree<NodeType>> ht = new HashSet<Tree<NodeType>>();
154 expand(ht, hf0, new Tree(null, "*"));
155 throw new Ambiguous((Forest<?>)this,
156 (HashSet<Tree<?>>)(Object)ht);
159 return hp.iterator().next();
162 public Tree<NodeType> expand1() throws Ambiguous {
163 return simplify().expand1();
166 void gather(HashSet<Forest<NodeType>> ht) {
169 // FIXME: do something more sensible here
170 if (ht.contains(this)) {
171 System.err.println("WARNING: grammar produced a circular forest\n" + this);
172 //throw new Error("grammar produced a circular forest:\n" + this);
177 for(Forest<NodeType> f : hp) f.gather(ht);
180 private void touched() {
184 FastSet<Forest<NodeType>> f2 = new FastSet<Forest<NodeType>>();
186 if (f instanceof Forest.One) f2.add(f);
187 else for(Forest ff : ((Forest.Many<NodeType>)f))
192 public boolean contains(Forest f) {
194 return hp.contains(f);
196 public void merge(Forest p) {
197 if (touched) throw new RuntimeException("attempt to merge() on a Forest.Many that has already been examined");
198 if (p==this) throw new RuntimeException("attempt to merge() a Forest.Many to itself!");
201 boolean ambiguous() {
203 if (hp.size()==0) return false;
204 if (hp.size()==1) return hp.iterator().next().ambiguous();
208 void expand(HashSet<Tree<NodeType>> ht, HashSet<Forest<NodeType>> ignore, Tree<NodeType> bogus) {
210 if (ignore.contains(this)) { ht.add(bogus); return; }
211 for (Forest<NodeType> f : hp) f.expand(ht, ignore, bogus);
215 // GraphViz, ToInt //////////////////////////////////////////////////////////////////////////////
217 public boolean isTransparent() { return hp.size()==1; }
218 public boolean isHidden() { return hp.size()==0; }
219 public void edges(GraphViz.StateNode n) {
220 if (hp.size()==1) { hp.iterator().next().edges(n); return; }
221 for(Forest f : hp) f.edges(n);
223 public GraphViz.StateNode toGraphViz(GraphViz gv) {
224 if (hp.size()==1) return hp.iterator().next().toGraphViz(gv);
225 if (gv.hasNode(this)) return gv.createNode(this);
226 GraphViz.StateNode n = gv.createNode(this);
229 for(Forest f : hp) n.edge(f, null);
235 // Statics //////////////////////////////////////////////////////////////////////////////
237 /** Depth-first expansion, handling .One and .Many without recursion. */
238 private static <NodeType> Tree<NodeType> expand1ForOneAndMany(
239 Forest<NodeType> forest) throws Ambiguous {
240 List<Tree<NodeType>> nodes = new ArrayList<Tree<NodeType>>();
241 List<Tree<NodeType>> build = new ArrayList<Tree<NodeType>>();
244 List<Forest.One<NodeType>> path = new ArrayList<Forest.One<NodeType>>();
245 List<Integer> sizes = new ArrayList<Integer>(),
246 poss = new ArrayList<Integer>();
248 // handle the left-most leaf
249 expand1Descend(path, nodes, sizes, poss, forest);
251 for (Forest.One<NodeType> f; path.size() > 0;) {
254 int size = pop(sizes) + 1;
255 int pos = pop(poss) + 1;
256 Forest<NodeType>[] children = f.children;
257 if (pos < children.length) {
258 // down the next branch
260 sizes.add(new Integer(size));
261 poss.add(new Integer(pos));
263 // handle the left-most leaf
264 expand1Descend(path, nodes, sizes, poss, children[pos]);
266 if (path.size() > 0 && peek(path).lifts[peek(poss)]) {
267 // skip assembling this node, lift children
268 sizes.add(pop(sizes) + size - 1);
271 // assemble this node
272 for (int i=size; i > 0; i--)
273 build.add(nodes.remove(nodes.size() - i));
274 nodes.add(new Tree<NodeType>(f.location, f.head, build));
282 /** Descend to the left-most leaf, building the path. */
283 private static <NodeType> void expand1Descend(
284 List<Forest.One<NodeType>> path, List<Tree<NodeType>> nodes,
285 List<Integer> sizes, List<Integer> poss, Forest<NodeType> f)
288 if (f instanceof Forest.Many)
289 f = ((Forest.Many<NodeType>)f).simplify();
290 else if (f instanceof Forest.One) {
291 Forest.One<NodeType> one = (Forest.One<NodeType>)f;
292 if (one.children.length == 0)
299 nodes.add(f.expand1());
304 if (path.size() > 0 && peek(path).lifts[peek(poss)])
305 sizes.add(pop(sizes) - 1); // ignore lifted leafs
307 nodes.add(f.expand1());
310 private static <T> T pop(List<T> list) {
311 return list.remove(list.size() - 1); }
312 private static <T> T peek(List<T> list) {
313 return list.get(list.size() - 1); }
315 private static Tree[] tree_hint = new Tree[0];
316 private static String[] string_hint = new String[0];
317 private static final Forest[] emptyForestArray = new Forest[0];
319 protected String headToString() { return null; }
320 protected String headToJava() { return "null"; }
321 protected String left() { return "<?"; }
322 protected String right() { return "?>"; }
323 protected boolean ignoreSingleton() { return true; }