questionable commit that eradicates the lame thing
authoradam <adam@megacz.com>
Thu, 20 Jul 2006 08:39:33 +0000 (04:39 -0400)
committeradam <adam@megacz.com>
Thu, 20 Jul 2006 08:39:33 +0000 (04:39 -0400)
darcs-hash:20060720083933-5007d-d2be37a4b87e971eed9a3ebe4a62ff854c7bd5dc.gz

src/edu/berkeley/sbp/Parser.java
src/edu/berkeley/sbp/Sequence.java
src/edu/berkeley/sbp/meta/MetaGrammarBindings.java

index 2344e15..b2445b0 100644 (file)
@@ -268,14 +268,24 @@ public abstract class Parser<Tok, Result> {
                         move.addAll(y, hp);
                     }
                 }
-                for(Element y : move) {
+                OUTER: for(Element y : move) {
                     HashSet<Position> h = move.getAll(y);
                     State<Tok> s = all_states.get(h) == null ? new State<Tok>(h, all_states, all_elements) : all_states.get(h);
                     // if a reduction is "lame", it should wind up in the dead_state after reducing
-                    if (y instanceof Sequence && ((Sequence)y).lame)
-                        ((HashMap)gotoSetNonTerminals).put(y, dead_state);
-                    else
-                        gotoSetNonTerminals.put(y, s);
+                    if (y instanceof Sequence) {
+                        for(Position p : hs) {
+                            if (p.element() != null && (p.element() instanceof Union)) {
+                                Union u = (Union)p.element();
+                                for(Sequence seq : u)
+                                    if (seq.needs.contains((Sequence)y) || seq.hates.contains((Sequence)y)) {
+                                        // FIXME: what if there are two "routes" to get to the sequence?
+                                        ((HashMap)gotoSetNonTerminals).put(y, dead_state);
+                                        continue OUTER;
+                                    }
+                            }
+                        }
+                    }
+                    gotoSetNonTerminals.put(y, s);
                 }
             }
 
index 28f6564..44b30c5 100644 (file)
@@ -18,7 +18,6 @@ public abstract class Sequence extends Element implements Iterable<Element> {
         for(Sequence s : needs) { ret.needs.add(s); s.needed.add(ret); }
         for(Sequence s : hates) { ret.hates.add(s); s.hated.add(ret); }
         ret.follow = follow;
-        ret.lame = lame;
         return ret;
     }
 
@@ -26,7 +25,7 @@ public abstract class Sequence extends Element implements Iterable<Element> {
     public static final Sequence empty = new Sequence.Constant.Empty();
 
     /** after matching the sequence, do not add anything to the output tree */
-    public static Sequence drop(Element[] e, boolean lame) { return new Constant.Drop(e, lame); }
+    public static Sequence drop(Element[] e) { return new Constant.Drop(e); }
 
     /** after matching the sequence, insert a constant into the output tree */
     public static Sequence constant(Element[] e, Object o) { return new Constant(e, o); }
@@ -65,7 +64,6 @@ public abstract class Sequence extends Element implements Iterable<Element> {
     final HashSet<Sequence> hated  = new HashSet<Sequence>();
     final HashSet<Sequence> needs  = new HashSet<Sequence>();
     final HashSet<Sequence> hates  = new HashSet<Sequence>();
-    public boolean           lame  = false;
 
     final Position          firstp;
     Position firstp() { return firstp; }
@@ -184,15 +182,12 @@ public abstract class Sequence extends Element implements Iterable<Element> {
             return (Forest<T>)Forest.create(loc, result, null, false);
         }
         static class Drop extends Constant {
-            Sequence _clone() { return new Drop(elements, lame); }
-            public Drop(Element[] e, boolean lame) {
-                super(e, null);
-                this.lame = lame;
-            }
+            Sequence _clone() { return new Drop(elements); }
+            public Drop(Element[] e) { super(e, null); }
         }
         static class Empty extends Sequence.Constant.Drop {
             Sequence _clone() { return new Empty(); }
-            public Empty() { super(new Element[] { }, false); } }
+            public Empty() { super(new Element[] { }); } }
     }
 
     static class Singleton extends Sequence {
index 196394a..3790227 100644 (file)
@@ -78,14 +78,11 @@ public class MetaGrammarBindings extends AnnotationGrammarBindings {
                 Union u2 = new Union(null, false);
                 if (sequences.length==1) u2 = u;
                 for(int j=0; j<group.length; j++) {
-                    group[j].build(cx, u2, false, cnt);
+                    group[j].build(cx, u2, cnt);
                 }
                 if (sequences.length==1) break;
                 Sequence seq = Sequence.singleton(u2);
-                for(Sequence s : bad2) {
-                    s.lame = true;
-                    seq = seq.not(s);
-                }
+                for(Sequence s : bad2) seq = seq.not(s);
                 u.add(seq);
                 bad2.add(Sequence.singleton(u2));
             }
@@ -143,7 +140,7 @@ public class MetaGrammarBindings extends AnnotationGrammarBindings {
                 if (sequences.length==1) u2 = u;
                 for(int j=0; j<group.length; j++) {
                     Union u3 = new Union(null, false);
-                    group[j].build(cx, u3, false, this);
+                    group[j].build(cx, u3, this);
                     Sequence s = Sequence.unwrap(new Element[] { u3, urep },
                                                  cx.rm.repeatTag(),
                                                  new boolean[] { false, false });
@@ -151,10 +148,7 @@ public class MetaGrammarBindings extends AnnotationGrammarBindings {
                 }
                 if (sequences.length==1) break;
                 Sequence seq = Sequence.singleton(u2);
-                for(Sequence s : bad2) {
-                    s.lame = true;
-                    seq = seq.not(s);
-                }
+                for(Sequence s : bad2) seq = seq.not(s);
                 u.add(seq);
                 bad2.add(Sequence.singleton(u2));
             }
@@ -190,7 +184,6 @@ public class MetaGrammarBindings extends AnnotationGrammarBindings {
         ElementNode[] elements;
         ElementNode follow;
         String tag = null;
-        boolean lame;
         public void append(ElementNode e) {
             ElementNode[] elements = new ElementNode[this.elements.length+1];
             System.arraycopy(this.elements, 0, elements, 0, this.elements.length);
@@ -216,8 +209,8 @@ public class MetaGrammarBindings extends AnnotationGrammarBindings {
             ret.tag = prefix+tag;
             return ret;
         }
-        public Seq and(Seq s) { and.add(s); s.lame = true; return this; }
-        public Seq andnot(Seq s) { not.add(s); s.lame = true; return this; }
+        public Seq and(Seq s) { and.add(s); return this; }
+        public Seq andnot(Seq s) { not.add(s); return this; }
         public Seq separate(ElementNode sep) {
             ElementNode[] elements = new ElementNode[this.elements.length * 2 - 1];
             for(int i=0; i<this.elements.length; i++) {
@@ -228,16 +221,15 @@ public class MetaGrammarBindings extends AnnotationGrammarBindings {
             this.elements = elements;
             return this;
         }
-        public Sequence build(Context cx, Union u, boolean lame, NonTerminalNode cnt) {
-            Sequence ret = build0(cx, lame || this.lame, cnt);
-            for(Seq s : and) { Sequence dork = s.build(cx, u, true, cnt); ret = ret.and(dork); }
-            for(Seq s : not) { Sequence dork = s.build(cx, u, true, cnt); ret = ret.not(dork); }
+        public Sequence build(Context cx, Union u, NonTerminalNode cnt) {
+            Sequence ret = build0(cx, cnt);
+            for(Seq s : and) { Sequence dork = s.build(cx, u, cnt); ret = ret.and(dork); }
+            for(Seq s : not) { Sequence dork = s.build(cx, u, cnt); ret = ret.not(dork); }
             u.add(ret);
-            ret.lame = lame;
             return ret;
         }
-        public Sequence build0(Context cx, boolean lame, NonTerminalNode cnt) {
-            boolean dropAll = lame;
+        public Sequence build0(Context cx, NonTerminalNode cnt) {
+            boolean dropAll = false;
             if (tag!=null && tag.endsWith("()")) dropAll = true;
             boolean[] drops = new boolean[elements.length];
             Element[] els = new Element[elements.length];
@@ -248,7 +240,7 @@ public class MetaGrammarBindings extends AnnotationGrammarBindings {
                     tag = elements[i].getOwnerTag();
             }
             Sequence ret = null;
-            if (dropAll)     ret = Sequence.drop(els, false);
+            if (dropAll)     ret = Sequence.drop(els);
             else {
                 Production prod = new Production(tag, (cnt==null?null:cnt.name), els, drops);
                 ret = cx.rm.createSequence(prod);
@@ -257,14 +249,13 @@ public class MetaGrammarBindings extends AnnotationGrammarBindings {
                     for(int i=0; i<els.length; i++)
                         if (!drops[i])
                             if (idx==-1) idx = i;
-                            else throw new Error("multiple non-dropped elements in sequence: " + Sequence.drop(els, false));
+                            else throw new Error("multiple non-dropped elements in sequence: " + Sequence.drop(els));
                     if (idx != -1) ret = Sequence.singleton(els, idx);
-                    else           ret = Sequence.drop(els, false);
+                    else           ret = Sequence.drop(els);
                 }
             }
             if (this.follow != null)
                 ret.follow = this.follow.toAtom(cx);
-            ret.lame = this.lame;
             return ret;
         }
     }
@@ -337,7 +328,7 @@ public class MetaGrammarBindings extends AnnotationGrammarBindings {
         public @bind.arg Seq body;
         public Element build(Context cx, NonTerminalNode cnt) {
             Union u = new Union(null, false);
-            Sequence s = body.build(cx, u, false, null);
+            Sequence s = body.build(cx, u, null);
             Union u2 = new Union(null, false);
             u2.add(Sequence.singleton(new Element[] {
                 CharAtom.leftBrace,