X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2FUnion.java;h=db0db7dca1a322625895ee88f6d338a7934623ba;hp=df6c374af8399d98168f3282f5f859580e8eb34b;hb=0516ea34996c86664928ef948013b749876b87ec;hpb=0a0227b9180534d2a431f3d6e08a398bde2244c4 diff --git a/src/edu/berkeley/sbp/Union.java b/src/edu/berkeley/sbp/Union.java index df6c374..db0db7d 100644 --- a/src/edu/berkeley/sbp/Union.java +++ b/src/edu/berkeley/sbp/Union.java @@ -18,6 +18,18 @@ public class Union extends Element implements Iterable { void reachable(HashSet h) { for(Sequence s : alternatives) s.reachable(h); } + Topology toAtom() { + if (alternatives.size()==0) throw new RuntimeException("cannot build an Atom from a Union with no productions"); + Topology ret = null; + for(Sequence s : this) { + Topology a = s.toAtom(); + if (ret==null) ret = a.dup(); + else ret = ret.union(a.dup()); + } + if (ret==null) throw new RuntimeException("confusion on " + this); + return ret; + } + /** adds an alternative */ public void add(Sequence s) { alternatives.add(s); } @@ -36,6 +48,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 +65,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) {