From 6b24a8ff6410a47d7290c2b42587fd8a8c5341e8 Mon Sep 17 00:00:00 2001 From: adam Date: Mon, 30 Jan 2006 04:01:45 -0500 Subject: [PATCH] checkpoint darcs-hash:20060130090145-5007d-95c3d7a938654dd7eea6a510468a1b48b07d1fb3.gz --- src/edu/berkeley/sbp/Ambiguous.java | 11 +++++++---- src/edu/berkeley/sbp/Element.java | 4 ++-- src/edu/berkeley/sbp/Forest.java | 15 +++++++++++---- src/edu/berkeley/sbp/Union.java | 6 +----- src/edu/berkeley/sbp/util/PrintableTree.java | 1 + 5 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/edu/berkeley/sbp/Ambiguous.java b/src/edu/berkeley/sbp/Ambiguous.java index cb55ffa..1f7cc38 100644 --- a/src/edu/berkeley/sbp/Ambiguous.java +++ b/src/edu/berkeley/sbp/Ambiguous.java @@ -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(); } } diff --git a/src/edu/berkeley/sbp/Element.java b/src/edu/berkeley/sbp/Element.java index 33bfd8d..0d7fb84 100644 --- a/src/edu/berkeley/sbp/Element.java +++ b/src/edu/berkeley/sbp/Element.java @@ -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 null */ - 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"); } } diff --git a/src/edu/berkeley/sbp/Forest.java b/src/edu/berkeley/sbp/Forest.java index a8c44f0..ceb67eb 100644 --- a/src/edu/berkeley/sbp/Forest.java +++ b/src/edu/berkeley/sbp/Forest.java @@ -11,19 +11,26 @@ public abstract class Forest /*extends PrintableTree>*/ impl /** assume that this forest contains exactly one tree and return it; otherwise throw an exception */ public final Tree expand1() throws Ambiguous, ParseFailed { - Iterator> it = expand(true).iterator(); - if (!it.hasNext()) throw new ParseFailed(); - return it.next(); + try { + Iterator> 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> expand(boolean toss) { final HashSetTreeConsumer ret = new HashSetTreeConsumer(); visit(new TreeMaker2(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 { public void addTree(Tree t); } diff --git a/src/edu/berkeley/sbp/Union.java b/src/edu/berkeley/sbp/Union.java index 70a3e45..dce9cbe 100644 --- a/src/edu/berkeley/sbp/Union.java +++ b/src/edu/berkeley/sbp/Union.java @@ -25,13 +25,8 @@ public class Union extends Element implements Iterable { 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 alternatives = new ArrayList(); public Iterator iterator() { return alternatives.iterator(); } @@ -74,6 +69,7 @@ public class Union extends Element implements Iterable { 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()); diff --git a/src/edu/berkeley/sbp/util/PrintableTree.java b/src/edu/berkeley/sbp/util/PrintableTree.java index 180d2ea..9d37a05 100644 --- a/src/edu/berkeley/sbp/util/PrintableTree.java +++ b/src/edu/berkeley/sbp/util/PrintableTree.java @@ -17,6 +17,7 @@ public abstract class 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; -- 1.7.10.4