From: adam Date: Mon, 30 Jan 2006 08:08:34 +0000 (-0500) Subject: checkpoint X-Git-Tag: tag_for_25-Mar~302 X-Git-Url: http://git.megacz.com/?p=sbp.git;a=commitdiff_plain;h=1a249057cbfd2180910e46672eafee3af46ae470 checkpoint darcs-hash:20060130080834-5007d-af3730908af7a65135bb39ef9bea12b4209b1ae8.gz --- diff --git a/TODO b/TODO index 3f7ca10..d9f9879 100644 --- a/TODO +++ b/TODO @@ -3,9 +3,6 @@ Immediately - Fix the metagrammar (really?) - - Repeat, Sequence, Tree - - simplify Forest (considerably) - - decent/better error messages - fix the location stuff, it's broken diff --git a/src/edu/berkeley/sbp/Atom.java b/src/edu/berkeley/sbp/Atom.java index cb7a379..a5f7e87 100644 --- a/src/edu/berkeley/sbp/Atom.java +++ b/src/edu/berkeley/sbp/Atom.java @@ -13,8 +13,6 @@ public abstract class Atom extends Element implements Topology { protected abstract Topology top(); public abstract String toString(); - public Topology toAtom() { return this; } - // Topology Thunks ////////////////////////////////////////////////////////////////////////////// public Topology unwrap() { return top().unwrap(); } @@ -34,7 +32,7 @@ public abstract class Atom extends Element implements Topology { public static class Infer extends Atom { private final Element e; public Infer(Element e) { this.e = e; } - public Topology top() { return (Topology)e.toAtom(); } + public Topology top() { return (Topology)toAtom(e); } public String toString() { return e.toString(); /* FIXME should be toAtom() */ } } @@ -44,5 +42,14 @@ public abstract class Atom extends Element implements Topology { public Topology top() { return ((Topology)a.top()).complement(); } public String toString() { return "~"+a; } } + + static Topology toAtom(Element e) { + if (e instanceof Atom) return (Atom)e; + if (e instanceof Sequence) return ((Sequence)e).toAtom(); + Topology ret = null; + for(Sequence s : (Union)e) + ret = ret==null ? toAtom(s) : ret.union(s.toAtom()); + return ret; + } } diff --git a/src/edu/berkeley/sbp/Element.java b/src/edu/berkeley/sbp/Element.java index f88ce87..7be02af 100644 --- a/src/edu/berkeley/sbp/Element.java +++ b/src/edu/berkeley/sbp/Element.java @@ -11,8 +11,6 @@ import java.lang.ref.*; public abstract class Element { /** if this element always matches exactly one token, return a topology covering exactly those possible tokens, otherwise null */ - abstract Topology toAtom(); - public Topology toAtom0() { return toAtom(); } Forest epsilonForm() { throw new Error("no epsilon form: " + this); } final boolean possiblyEpsilon(Walk.Cache cache) { Boolean ret = cache==null ? null : cache.possiblyEpsilon.get(this); diff --git a/src/edu/berkeley/sbp/ParseFailed.java b/src/edu/berkeley/sbp/ParseFailed.java index eb11b11..0fd9226 100644 --- a/src/edu/berkeley/sbp/ParseFailed.java +++ b/src/edu/berkeley/sbp/ParseFailed.java @@ -22,11 +22,11 @@ public class ParseFailed extends RuntimeException { if (touched.contains(n)) return; touched.add(n); for(Position p : n.state) { - if (((p.isFirst() || p.isLast()) && !force) || p.owner().name==null) { + if (((p.isFirst() || p.isLast()) && !force)/* || p.owner().name==null*/) { for(Node n2 : n.parents()) complain(n2, errors, force | p.isFirst()); } else { - String seqname = p.owner().name; + String seqname = p.owner()/*.name*/+""; HashSet hs = errors.get(seqname); if (hs==null) errors.put(seqname, hs = new HashSet()); hs.add(p.element()+""); diff --git a/src/edu/berkeley/sbp/Sequence.java b/src/edu/berkeley/sbp/Sequence.java index 2eddfc4..3ac811e 100644 --- a/src/edu/berkeley/sbp/Sequence.java +++ b/src/edu/berkeley/sbp/Sequence.java @@ -46,13 +46,11 @@ public abstract class Sequence extends Element implements Iterable { //////////////////////////////////////////////////////////////////////////////// public Element noFollow = null; - public String name = null; - public void setName(String name) { this.name = name; } - public final Topology noFollow() { return noFollow==null ? null : noFollow.toAtom(); } + public final Topology noFollow() { return noFollow==null ? null : Atom.toAtom(noFollow); } Topology toAtom() { if (elements.length!=1) throw new RuntimeException("cannot invoke toAtom() on a Sequence with " + elements.length + " elements: " + this); - return elements[0].toAtom(); + return Atom.toAtom(elements[0]); } protected final Element[] elements; diff --git a/src/edu/berkeley/sbp/Union.java b/src/edu/berkeley/sbp/Union.java index 87aefda..4de0bb4 100644 --- a/src/edu/berkeley/sbp/Union.java +++ b/src/edu/berkeley/sbp/Union.java @@ -10,34 +10,23 @@ 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(); } public boolean contains(Sequence s) { return alternatives.contains(s); } - 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; - else ret = ret.union(a); - } - if (ret==null) throw new RuntimeException("confusion on " + this); - return ret; - } - /** adds an alternative */ public void add(Sequence s) { alternatives.add(s); for(Sequence n : s.needs) add(n); for(Sequence n : s.hates) add(n); - if (/*!synthetic &&*/ shortForm!=null - //&& Character.isUpperCase(shortForm.charAt(0)) - ) - s.setName(toString()); } /** diff --git a/src/edu/berkeley/sbp/misc/MetaGrammar.java b/src/edu/berkeley/sbp/misc/MetaGrammar.java index c563ce5..0493cb1 100644 --- a/src/edu/berkeley/sbp/misc/MetaGrammar.java +++ b/src/edu/berkeley/sbp/misc/MetaGrammar.java @@ -150,6 +150,7 @@ public class MetaGrammar extends StringWalker { else if ("psy".equals(head)) return (PreSequence)walk(tree, 0); else if ("->".equals(head)) { PreSequence p = (PreSequence)walk(tree, 0); p.noFollow = (Element)walk(tree, 1); return p; } else if ("/".equals(head)) return ((PreSequence)walk(tree, 0)).sparse((Element)walk(tree, 1)); + else if (" /".equals(head)) return ((PreSequence)walk(tree, 0)).sparse((Element)walk(tree, 1)); else if ("~".equals(head)) return new Hack(new Atom.Invert(new Atom.Infer((Element)walk(tree, 0)))); else if ("ps".equals(head)) return new PreSequence((Object[])walk(tree,0), null); else if (":".equals(head)) { diff --git a/tests/meta.g b/tests/meta.g index 0d2be6f..be8e435 100644 --- a/tests/meta.g +++ b/tests/meta.g @@ -1,6 +1,6 @@ s = ws grammar:Grammar ws Grammar = NonTerminal +/ ws -NonTerminal = Word !wp ^"=" !wp RHS +NonTerminal = Word ^"=" RHS /ws RHS = (Sequence +/ (!ws "|" !ws)) +/ (!ws ">" !ws) @@ -8,12 +8,12 @@ Elements = e*/ws PreSequence = ps:: Elements | (Quoted|Word) ^"::" PreSequence /ws - > PreSequence !wp ^"/" !ws e - | PreSequence ^"->" e /ws + > PreSequence ^"/" e /ws + | PreSequence ^"->" e /ws Sequence = psx:: PreSequence - | Sequence !ws ^"&" !ws Elements - | Sequence !ws ^"&~" !ws Elements + | Sequence ^"&" Elements /ws + | Sequence ^"&~" Elements /ws ec = ~[\-\]\\] | escaped