1 // Copyright 2006 all rights reserved; see LICENSE file for BSD-style license
3 package edu.berkeley.sbp;
4 import edu.berkeley.sbp.*;
5 import edu.berkeley.sbp.Sequence.Position;
6 import edu.berkeley.sbp.util.*;
9 import java.lang.reflect.*;
13 * An efficient representation of a collection of trees (Tomita's
14 * shared packed parse forest).
17 public abstract class Forest<NodeType> implements GraphViz.ToGraphViz {
19 /** assume that this forest contains exactly one tree and return it; otherwise throw an exception */
20 public abstract Tree<NodeType> expand1() throws Ambiguous;
22 /** expand this forest into a set of trees */
23 public void expand(HashSet<Tree<NodeType>> ht) { expand(ht, new HashSet<Forest<NodeType>>(), null); }
25 static <NodeType> Forest<NodeType> create(Input.Region loc, NodeType head, Forest<NodeType>[] children, boolean lift) {
26 if (loc == null) throw new Error();
27 return new One<NodeType>(loc, head, children, lift);
30 /** create a new forest */
31 public static <NodeType> Forest<NodeType> create(Input.Region loc, NodeType head, Forest<NodeType>[] children) {
32 return Forest.create(loc, head, children, false); }
34 // Package-Private //////////////////////////////////////////////////////////////////////////////
36 abstract void expand(HashSet<Tree<NodeType>> ht, HashSet<Forest<NodeType>> ignore, Tree<NodeType> bogus);
37 abstract void gather(HashSet<Forest<NodeType>> ignore);
38 abstract void edges(GraphViz.Node n);
39 boolean ambiguous() { return false; }
41 abstract Input.Region getRegion();
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 lift;
55 Input.Region getRegion() { return location; }
57 private One(Input.Region loc, NodeType head, Forest<NodeType>[] children, boolean lift) {
60 this.children = children==null ? emptyForestArray : new Forest[children.length];
61 if (children != null) System.arraycopy(children, 0, this.children, 0, children.length);
62 if (children != null) for(int i=0; i<children.length; i++) if (children[i]==null) throw new Error(i+"");
66 public Tree<NodeType> expand1() throws Ambiguous {
67 Tree<NodeType>[] ret = new Tree[children.length];
68 for(int i=0; i<children.length; i++) ret[i] = children[i].expand1();
69 return new Tree<NodeType>(location, head, ret, lift);
72 void gather(HashSet<Forest<NodeType>> hf) {
74 for(Forest<NodeType> f : children) f.gather(hf);
76 void expand(HashSet<Tree<NodeType>> ht, HashSet<Forest<NodeType>> ignore, Tree<NodeType> bogus) {
77 if (ignore.contains(this)) { ht.add(bogus); return; }
78 expand(0, new Tree[children.length], ht, ignore, bogus);
80 private void expand(final int i, Tree<NodeType>[] ta, HashSet<Tree<NodeType>> ht, HashSet<Forest<NodeType>> ignore, Tree<NodeType> bogus) {
81 if (i==children.length) {
82 ht.add(new Tree<NodeType>(location, head, ta, lift));
84 HashSet<Tree<NodeType>> ht2 = new HashSet<Tree<NodeType>>();
85 children[i].expand(ht2, ignore, bogus);
86 for(Tree<NodeType> tc : ht2) {
88 expand(i+1, ta, ht, ignore, bogus);
94 // GraphViz, ToInt //////////////////////////////////////////////////////////////////////////////
96 public boolean isTransparent() { return false; }
97 public boolean isHidden() { return false; }
98 public GraphViz.Node toGraphViz(GraphViz gv) {
99 if (gv.hasNode(this)) return gv.createNode(this);
100 GraphViz.Node n = gv.createNode(this);
101 n.label = headToString()==null?"":headToString();
106 boolean edges = false; // FIXME ??
107 public void edges(GraphViz.Node n) {
110 for(int i=0; i<children.length; i++) {
111 if (i==children.length-1 && lift && !children[i].ambiguous()) {
112 children[i].edges(n);
114 n.edge(children[i], null);
119 protected String headToString() { return head==null?null:head.toString(); }
120 protected String headToJava() { return "null"; }
121 protected String left() { return "{"; }
122 protected String right() { return "}"; }
123 protected boolean ignoreSingleton() { return false; }
127 // Many //////////////////////////////////////////////////////////////////////////////
129 /** An "ambiguity node"; this is immutable once it has been "looked at" */
130 static class Many<NodeType> extends Forest<NodeType> {
132 HashSet<GSS.Phase.Node> parents = new HashSet<GSS.Phase.Node>();
133 private FastSet<Forest<NodeType>> hp = new FastSet<Forest<NodeType>>();
134 private boolean touched = false;
138 Input.Region getRegion() { return hp.iterator().next().getRegion(); } // all should be identical
140 public Tree<NodeType> expand1() throws Ambiguous {
143 HashSet<Forest<NodeType>> hf0 = new HashSet<Forest<NodeType>>();
144 Iterator<Forest<NodeType>> ih = hp.iterator();
145 ih.next().gather(hf0);
146 for(Forest<NodeType> f : hp) {
147 HashSet<Forest<NodeType>> hf1 = new HashSet<Forest<NodeType>>();
151 HashSet<Tree<NodeType>> ht = new HashSet<Tree<NodeType>>();
152 expand(ht, hf0, new Tree(null, "*"));
153 throw new Ambiguous((Forest<?>)this,
154 (HashSet<Tree<?>>)(Object)ht);
156 return hp.iterator().next().expand1();
159 void gather(HashSet<Forest<NodeType>> ht) {
162 // FIXME: do something more sensible here
163 if (ht.contains(this)) {
164 System.err.println("WARNING: grammar produced a circular forest\n" + this);
165 //throw new Error("grammar produced a circular forest:\n" + this);
170 for(Forest<NodeType> f : hp) f.gather(ht);
173 private void touched() {
177 FastSet<Forest<NodeType>> f2 = new FastSet<Forest<NodeType>>();
179 if (f instanceof Forest.One) f2.add(f);
180 else for(Forest ff : ((Forest.Many<NodeType>)f))
185 public boolean contains(Forest f) {
187 return hp.contains(f);
189 public void merge(Forest p) {
190 if (touched) throw new RuntimeException("attempt to merge() on a Forest.Many that has already been examined");
191 if (p==this) throw new RuntimeException("attempt to merge() a Forest.Many to itself!");
194 boolean ambiguous() {
196 if (hp.size()==0) return false;
197 if (hp.size()==1) return hp.iterator().next().ambiguous();
201 void expand(HashSet<Tree<NodeType>> ht, HashSet<Forest<NodeType>> ignore, Tree<NodeType> bogus) {
203 if (ignore.contains(this)) { ht.add(bogus); return; }
204 for (Forest<NodeType> f : hp) f.expand(ht, ignore, bogus);
208 // GraphViz, ToInt //////////////////////////////////////////////////////////////////////////////
210 public boolean isTransparent() { return hp.size()==1; }
211 public boolean isHidden() { return hp.size()==0; }
212 public void edges(GraphViz.Node n) {
213 if (hp.size()==1) { hp.iterator().next().edges(n); return; }
214 for(Forest f : hp) f.edges(n);
216 public GraphViz.Node toGraphViz(GraphViz gv) {
217 if (hp.size()==1) return hp.iterator().next().toGraphViz(gv);
218 if (gv.hasNode(this)) return gv.createNode(this);
219 GraphViz.Node n = gv.createNode(this);
222 for(Forest f : hp) n.edge(f, null);
227 // Statics //////////////////////////////////////////////////////////////////////////////
229 private static Tree[] tree_hint = new Tree[0];
230 private static String[] string_hint = new String[0];
231 private static final Forest[] emptyForestArray = new Forest[0];
233 protected String headToString() { return null; }
234 protected String headToJava() { return "null"; }
235 protected String left() { return "<?"; }
236 protected String right() { return "?>"; }
237 protected boolean ignoreSingleton() { return true; }