checkpoint
authoradam <adam@megacz.com>
Mon, 30 Jan 2006 09:01:45 +0000 (04:01 -0500)
committeradam <adam@megacz.com>
Mon, 30 Jan 2006 09:01:45 +0000 (04:01 -0500)
darcs-hash:20060130090145-5007d-95c3d7a938654dd7eea6a510468a1b48b07d1fb3.gz

src/edu/berkeley/sbp/Ambiguous.java
src/edu/berkeley/sbp/Element.java
src/edu/berkeley/sbp/Forest.java
src/edu/berkeley/sbp/Union.java
src/edu/berkeley/sbp/util/PrintableTree.java

index cb55ffa..1f7cc38 100644 (file)
@@ -6,14 +6,17 @@ import java.io.*;
 import java.util.*;
 
 /** if ambiguity checking is enabled, this exception is thrown to signal that the parse was ambiguous */
-public class Ambiguous extends RuntimeException {
+public class Ambiguous extends Exception {
     public final Forest<?> ambiguity;
     public Ambiguous(Forest<?> ambiguity) { this.ambiguity = ambiguity; }
     public String toString() {
+        // FEATURE: more legible printout desperately needed
         StringBuffer sb = new StringBuffer();
-        sb.append("unresolved ambiguity "/*"at " + ambiguity.getLocation() + ":"*/);
-        for(Tree<?> result : ambiguity.expand(false))
-            sb.append("\n\n" + result.toPrettyString());
+        sb.append("unresolved ambiguity ");
+        for(Tree<?> result : ambiguity.expand(false)) {
+            sb.append("\n\n");
+            result.toPrettyString(sb);
+        }
         return sb.toString();
     }
 }
index 33bfd8d..0d7fb84 100644 (file)
@@ -12,7 +12,7 @@ public abstract class Element {
 
     abstract StringBuffer toString(StringBuffer sb);
 
-    /** if this element always matches exactly one token, return a topology covering exactly those possible tokens, otherwise <tt>null</tt> */
-    Forest epsilonForm() { return null; }
+    /** returns the Forest resulting from matching this element against the empty string */
+    Forest epsilonForm() { throw new Error("element " + this + " has no epsilon form"); }
 
 }
index a8c44f0..ceb67eb 100644 (file)
@@ -11,19 +11,26 @@ public abstract class Forest<T> /*extends PrintableTree<Forest.MyBody<T>>*/ impl
 
     /** assume that this forest contains exactly one tree and return it; otherwise throw an exception */
     public final Tree<T> expand1() throws Ambiguous, ParseFailed {
-        Iterator<Tree<T>> it = expand(true).iterator();
-        if (!it.hasNext()) throw new ParseFailed();
-        return it.next();
+        try {
+            Iterator<Tree<T>> it = expand(true).iterator();
+            if (!it.hasNext()) throw new ParseFailed();
+            return it.next();
+        } catch (InnerAmbiguous ia) { throw new Ambiguous(ia.f); }
     }
 
     /** expand this forest into a set of trees */
     public HashSet<Tree<T>> expand(boolean toss) {
         final HashSetTreeConsumer<T> ret = new HashSetTreeConsumer<T>();
         visit(new TreeMaker2<T>(toss, ret), null, null);
-        if (toss && ret.size() > 1) throw new Ambiguous(this);
+        if (toss && ret.size() > 1) throw new InnerAmbiguous(this);
         return ret;
     }
 
+    private static class InnerAmbiguous extends RuntimeException {
+        public final Forest<?> f;
+        public InnerAmbiguous(Forest<?> f) { this.f = f; }
+    }
+
     public static interface TreeConsumer<T> {
         public void addTree(Tree<T> t);
     }
index 70a3e45..dce9cbe 100644 (file)
@@ -25,13 +25,8 @@ public class Union extends Element implements Iterable<Sequence> {
         this.synthetic = synthetic;
     }
 
-    /** 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<Sequence> alternatives = new ArrayList<Sequence>();
 
     public Iterator<Sequence> iterator() { return alternatives.iterator(); }
@@ -74,6 +69,7 @@ public class Union extends Element implements Iterable<Sequence> {
         if (alternatives.size()==0) {
             sb.append(StringUtil.pad(15, shortForm) + " = ");
         } else for(Sequence s : this) {
+            // FIXME: what to do here about printing out negated sequences?
             sb.append(StringUtil.pad(15, first ? shortForm : "") + (first ? " = " : "  | "));
             first = false;
             sb.append(s.toString());
index 180d2ea..9d37a05 100644 (file)
@@ -17,6 +17,7 @@ public abstract class PrintableTree<T extends PrintableTree> implements Iterable
 
     private boolean basic() { return toString().length() < MAXCHARS; }
     public String toPrettyString() { return toPrettyString("\n"); }
+    public StringBuffer toPrettyString(StringBuffer sb) { sb.append(this); return sb; }
     private String toPrettyString(String nl) {
         String str = toString();
         if (str.length() < MAXCHARS) return str;