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(); }
20 if (alternatives.size()==0) throw new RuntimeException("cannot build an Atom from a Union with no productions");
22 for(Sequence s : this) {
23 Topology a = s.toAtom();
24 if (ret==null) ret = a;
25 else ret = ret.union(a);
27 if (ret==null) throw new RuntimeException("confusion on " + this);
31 /** adds an alternative */
32 public void add(Sequence s) { alternatives.add(s); }
35 * Since every cycle in a non-degenerate grammar contains at
36 * least one Union, every instance of this class must be able to
37 * display itself in both "long form" (list of the long forms of
38 * its alternatives) and "short form" (some abbreviation).
40 * @param shortForm the "short form" display; usually
41 * @param synthetic if true, this Union's "long form" is "obvious" and should not be displayed when printing the grammar
43 public Union(String shortForm) { this(shortForm, false); }
44 public Union(String shortForm, boolean synthetic) {
45 this.shortForm = shortForm;
46 this.synthetic = synthetic;
49 public static Union epsilon = new Union("()");
50 static { epsilon.add(Sequence.empty); }
52 private Forest.Ref epsilonForm = null;
53 Forest epsilonForm() {
54 if (epsilonForm != null) return epsilonForm;
55 epsilonForm = new Forest.Ref();
56 for(Sequence s : this)
57 if (s.possiblyEpsilon(null))
58 epsilonForm.merge(s.epsilonForm());
62 // Display //////////////////////////////////////////////////////////////////////////////
64 public String toString() { return shortForm; }
65 private static String pad(int i,String s) { return s.length() >= i ? s : pad(i-1,s)+" "; }
66 public void toString(StringBuffer sb) {
67 if (synthetic) return;
69 if (alternatives.size()==0) {
70 sb.append(pad(15, shortForm) + "::= ");
71 } else for(Sequence s : this) {
72 sb.append(pad(15, first ? shortForm : "") + (first ? "::= " : " | "));
74 sb.append(s.toString());
80 // SubUnion //////////////////////////////////////////////////////////////////////////////
82 /** FIXME this is kind of a hack */
83 public class Subset extends Union {
84 private final Set<Sequence> exclude;
85 public Subset(String shortForm, Set<Sequence> exclude) { super(shortForm, true); this.exclude = exclude; }
86 public Iterator<Sequence> iterator() {
87 final Iterator<Sequence> it = Union.this.iterator();
88 return new Iterator<Sequence>() {
89 private Sequence next = it.hasNext() ? it.next() : null;
90 public boolean hasNext() { return next != null; }
91 public Sequence next() {
94 next = it.hasNext() ? it.next() : null;
95 } while (next != null && (next instanceof Sequence) && exclude.contains((Sequence)next));
98 public void remove() { throw new Error(); }