1 package edu.berkeley.sbp;
2 import edu.berkeley.sbp.util.*;
3 import edu.berkeley.sbp.*;
4 import edu.berkeley.sbp.*;
7 import java.lang.reflect.*;
8 import java.lang.ref.*;
10 /** an element which can produce one of several alternatives */
11 public class Union extends Element implements Iterable<Sequence> {
13 final String shortForm;
14 final boolean synthetic;
15 private final List<Sequence> alternatives = new ArrayList<Sequence>();
17 public Iterator<Sequence> iterator() { return alternatives.iterator(); }
19 void reachable(HashSet<Sequence.Position> h) { for(Sequence s : alternatives) s.reachable(h); }
22 if (alternatives.size()==0) throw new RuntimeException("cannot build an Atom from a Union with no productions");
24 for(Sequence s : this) {
25 Topology a = s.toAtom();
26 if (ret==null) ret = a.dup();
27 else ret = ret.union(a.dup());
29 if (ret==null) throw new RuntimeException("confusion on " + this);
33 /** adds an alternative */
34 public void add(Sequence s) { alternatives.add(s); }
37 * Since every cycle in a non-degenerate grammar contains at
38 * least one Union, every instance of this class must be able to
39 * display itself in both "long form" (list of the long forms of
40 * its alternatives) and "short form" (some abbreviation).
42 * @param shortForm the "short form" display; usually
43 * @param synthetic if true, this Union's "long form" is "obvious" and should not be displayed when printing the grammar
45 public Union(String shortForm) { this(shortForm, false); }
46 public Union(String shortForm, boolean synthetic) {
47 this.shortForm = shortForm;
48 this.synthetic = synthetic;
51 public static Union epsilon = new Union("()");
52 static { epsilon.add(Sequence.empty); }
54 private Forest.Ref epsilonForm = null;
55 Forest epsilonForm() {
56 if (epsilonForm != null) return epsilonForm;
57 epsilonForm = new Forest.Ref();
58 for(Sequence s : this)
59 if (s.possiblyEpsilon(null))
60 epsilonForm.merge(s.epsilonForm());
64 // Display //////////////////////////////////////////////////////////////////////////////
66 public String toString() { return shortForm; }
67 private static String pad(int i,String s) { return s.length() >= i ? s : pad(i-1,s)+" "; }
68 public void toString(StringBuffer sb) {
69 if (synthetic) return;
71 if (alternatives.size()==0) {
72 sb.append(pad(15, shortForm) + "::= ");
73 } else for(Sequence s : this) {
74 sb.append(pad(15, first ? shortForm : "") + (first ? "::= " : " | "));
76 sb.append(s.toString());
82 // SubUnion //////////////////////////////////////////////////////////////////////////////
84 /** FIXME this is kind of a hack */
85 public class Subset extends Union {
86 private final Set<Sequence> exclude;
87 public Subset(String shortForm, Set<Sequence> exclude) { super(shortForm, true); this.exclude = exclude; }
88 public Iterator<Sequence> iterator() {
89 final Iterator<Sequence> it = Union.this.iterator();
90 return new Iterator<Sequence>() {
91 private Sequence next = it.hasNext() ? it.next() : null;
92 public boolean hasNext() { return next != null; }
93 public Sequence next() {
96 next = it.hasNext() ? it.next() : null;
97 } while (next != null && (next instanceof Sequence) && exclude.contains((Sequence)next));
100 public void remove() { throw new Error(); }