X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2FUnion.java;h=4de0bb4969c58512982b7977fda60c9c6eaeac05;hp=df6c374af8399d98168f3282f5f859580e8eb34b;hb=1a249057cbfd2180910e46672eafee3af46ae470;hpb=0a0227b9180534d2a431f3d6e08a398bde2244c4 diff --git a/src/edu/berkeley/sbp/Union.java b/src/edu/berkeley/sbp/Union.java index df6c374..4de0bb4 100644 --- a/src/edu/berkeley/sbp/Union.java +++ b/src/edu/berkeley/sbp/Union.java @@ -10,16 +10,24 @@ import java.lang.ref.*; /** an element which can produce one of several alternatives */ public class Union extends Element implements Iterable { + /** display form for the Union (ie not including the RHS) */ final String shortForm; + + /** this is just a hint to use when printing out the grammar in visual form */ final boolean synthetic; + + /** the actual alternatives */ private final List alternatives = new ArrayList(); public Iterator iterator() { return alternatives.iterator(); } - - void reachable(HashSet h) { for(Sequence s : alternatives) s.reachable(h); } + public boolean contains(Sequence s) { return alternatives.contains(s); } /** adds an alternative */ - public void add(Sequence s) { alternatives.add(s); } + public void add(Sequence s) { + alternatives.add(s); + for(Sequence n : s.needs) add(n); + for(Sequence n : s.hates) add(n); + } /** * Since every cycle in a non-degenerate grammar contains at @@ -36,6 +44,9 @@ public class Union extends Element implements Iterable { this.synthetic = synthetic; } + public static Union epsilon = new Union("()"); + static { epsilon.add(Sequence.empty); } + private Forest.Ref epsilonForm = null; Forest epsilonForm() { if (epsilonForm != null) return epsilonForm; @@ -50,7 +61,7 @@ public class Union extends Element implements Iterable { public String toString() { return shortForm; } private static String pad(int i,String s) { return s.length() >= i ? s : pad(i-1,s)+" "; } - void toString(StringBuffer sb) { + public void toString(StringBuffer sb) { if (synthetic) return; boolean first = true; if (alternatives.size()==0) {