checkpoint
authoradam <adam@megacz.com>
Tue, 11 Apr 2006 20:56:56 +0000 (16:56 -0400)
committeradam <adam@megacz.com>
Tue, 11 Apr 2006 20:56:56 +0000 (16:56 -0400)
darcs-hash:20060411205656-5007d-2a14a7c1cf7a3a936790ffe1f96c099d9d780c6a.gz

Makefile
src/edu/berkeley/sbp/Forest.java
src/edu/berkeley/sbp/GSS.java
src/edu/berkeley/sbp/ParseFailed.java
src/edu/berkeley/sbp/Tree.java
src/edu/berkeley/sbp/misc/MetaGrammar.java
src/edu/berkeley/sbp/util/ANSI.java
src/edu/berkeley/sbp/util/GraphViz.java
tests/regression.tc

index f61196e..874eaca 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -6,6 +6,11 @@ tibdoc: edu.berkeley.sbp.jar
                tests/tibdoc.g \
                tests/input.tibdoc
 
+demo: edu.berkeley.sbp.jar
+       $(java) -cp $< edu.berkeley.sbp.misc.Demo \
+               tests/demo.g \
+               tests/demo.in
+
 regress:
        make boot
        rm edu.berkeley.sbp.jar
@@ -50,7 +55,7 @@ boot: edu.berkeley.sbp.jar
 
 edu.berkeley.sbp.jar: $(shell find src -name \*.java)
        mkdir -p bin
-       javac -cp tests/ArchSimA3.jar:tests/grappa.jar -d bin -sourcepath src $^
+       javac -cp javax.servlet.jar:tests/ArchSimA3.jar:tests/grappa.jar -d bin -sourcepath src $^
        cd bin; jar cf ../$@ .
 
 javadoc:
index 12c8cf1..3790562 100644 (file)
@@ -7,7 +7,10 @@ import java.util.*;
 import java.lang.reflect.*;
 
 /** an efficient representation of a collection of trees (Tomita's shared packed parse forest) */
-public abstract class Forest<T> /*extends PrintableTree<Forest.MyBody<T>>*/ implements Visitable<Forest.Body<T>>, IntegerMappable, GraphViz.ToGraphViz {
+public abstract class Forest<T> /*extends PrintableTree<Forest.MyBody<T>>*/
+    implements Visitable<Forest.Body<T>>,
+               IntegerMappable,
+               GraphViz.ToGraphViz {
 
     private static int master_idx = 0;
     private final int idx = master_idx++;
index 7d3758f..2154731 100644 (file)
@@ -10,6 +10,7 @@ import java.lang.reflect.*;
 /** implements Tomita's Graph Structured Stack */
 class GSS {
 
+    public static int count = 0;
     public GSS() { }
 
     private Phase.Node[] reducing_list = null;
@@ -66,6 +67,7 @@ class GSS {
             singularReductions = new IntPairMap<Forest>();
             expectedInhibit.clear();
             expectedInhibit.addAll(inhibited);
+            reset = false;
             good = false;
             closed = false;
             reducing = false;
@@ -113,7 +115,7 @@ class GSS {
             }
             if (!owner.lame)
                 newNode(parent, pending, state, fromEmptyReduction);
-            if (reduction!=null) inhibit(reduction, parent==null?0:parent.phase().pos);
+            if (reduction!=null) uninhibit(reduction, parent==null?0:parent.phase().pos);
             if (reduction != null) {
                 boolean redo = true;
                 while(redo) {
@@ -156,19 +158,19 @@ class GSS {
             return true;
         }
 
-        public void uninhibit(int p, Sequence s) {
+        public void inhibit(int p, Sequence s) {
             if (s.hated!=null)
                 for(Sequence s2 : s.hated)
                     inhibited.remove(p, s2);
         }
 
-        public void inhibit(Position r, int p) {
+        public void uninhibit(Position r, int p) {
             if (r.owner().hated == null) return;
             // remember that dead states are still allowed to shift -- just not allowed to reduce
             boolean reset = false;
             for(Sequence seq : r.owner().hated) {
                 if (performed.contains(p,seq)) {
-                    uninhibit(p, seq);
+                    inhibit(p, seq);
                     //System.out.println("\nresetting due to " + r.owner() + " killing " + seq);
                     //inhibited.clear();
                     inhibited.add(p, seq);
@@ -209,8 +211,10 @@ class GSS {
                 reset();
                 reduce();
             }
+            count = 0;
         }
 
+        private boolean reset = false;
         class Reset extends RuntimeException { }
 
         /** perform all shift operations, adding promoted nodes to <tt>next</tt> */
index 619bf65..d1e60ae 100644 (file)
@@ -37,7 +37,7 @@ public class ParseFailed extends Exception {
 
     public static String el(Object e) {
         String s = e.toString();
-        if (s.length()==0 || s.charAt(0)!='\"' || s.charAt(s.length()-1)!='\"') return ANSI.yellow(s);
+        if (s.length()==0 || s.charAt(0)!='\"' || s.charAt(s.length()-1)!='\"') return /*ANSI.yellow(s)*/s;
         s = s.substring(1);
         s = s.substring(0, s.length()-1);
         StringBuffer ret = new StringBuffer();
@@ -45,13 +45,14 @@ public class ParseFailed extends Exception {
             if (s.charAt(i)=='\\' && i<s.length()-1) ret.append(s.charAt(++i));
             else ret.append(s);
         }
-        return ANSI.purple(ret.toString());
+        return /*ANSI.purple(ret.toString())*/ret.toString();
     }
     public static String error(String message, Object token, Iterable<Node> nodes) {
         String lookAhead = token==null ? "<EOF>" : token.toString();
         StringBuffer ret = new StringBuffer();
         ret.append("\n  ");
         ret.append(message);
+        /*
         HashMap<String,HashSet<String>> errors = new HashMap<String,HashSet<String>>();
         for(Node n : nodes) {
             //System.out.println(n.state);
@@ -72,6 +73,7 @@ public class ParseFailed extends Exception {
                 ret.append("\n");
             }
         }
+        */
         return ret.toString();
     }
 
index 220d1dd..cdd3b8b 100644 (file)
@@ -7,7 +7,10 @@ import java.util.*;
 import java.lang.reflect.*;
 
 /** a tree (or node in a tree); see jargon.txt for details */
-public class Tree<T> extends PrintableTree<Tree<T>> implements Iterable<Tree<T>> {
+public class Tree<T>
+    extends PrintableTree<Tree<T>>
+    implements Iterable<Tree<T>>,
+               GraphViz.ToGraphViz {
 
     final T           head;
           Tree<T>[]   children;
@@ -43,4 +46,15 @@ public class Tree<T> extends PrintableTree<Tree<T>> implements Iterable<Tree<T>>
     protected String left()   { return "{"; }
     protected String right()  { return "}"; }
     protected boolean ignoreSingleton() { return false; }
+
+    public GraphViz.Node toGraphViz(GraphViz gv) {
+        if (gv.hasNode(this)) return gv.createNode(this);
+        GraphViz.Node n = gv.createNode(this);
+        n.label = head()==null ? "" : head().toString();
+        //n.color = "red";
+        for(Tree t : this) n.edge(t, null);
+        return n;
+    }
+    public boolean isTransparent() { return false; }
+    public boolean isHidden() { return false; }
 }
index 7dda407..d73586b 100644 (file)
@@ -530,6 +530,19 @@ public class MetaGrammar extends StringWalker {
 
 
 
+
+
+
+
+
+
+
+
+
+
+
+
+
         // DO NOT EDIT STUFF BELOW: IT IS AUTOMATICALLY GENERATED
 new edu.berkeley.sbp.Tree(null, "grammar", new edu.berkeley.sbp.Tree[] { new edu.berkeley.sbp.Tree(null, null, new edu.berkeley.sbp.Tree[] { new edu.berkeley.sbp.Tree(null, "=", new edu.berkeley.sbp.Tree[] { new edu.berkeley.sbp.Tree(null, null, new edu.berkeley.sbp.Tree[] { new edu.berkeley.sbp.Tree(null, "s", new edu.berkeley.sbp.Tree[] { })}),
         new edu.berkeley.sbp.Tree(null, null, new edu.berkeley.sbp.Tree[] { new edu.berkeley.sbp.Tree(null, null, new edu.berkeley.sbp.Tree[] { new edu.berkeley.sbp.Tree(null, "psx", new edu.berkeley.sbp.Tree[] { new edu.berkeley.sbp.Tree(null, "ps", new edu.berkeley.sbp.Tree[] { new edu.berkeley.sbp.Tree(null, null, new edu.berkeley.sbp.Tree[] { new edu.berkeley.sbp.Tree(null, "!", new edu.berkeley.sbp.Tree[] { new edu.berkeley.sbp.Tree(null, "nonTerminal", new edu.berkeley.sbp.Tree[] { new edu.berkeley.sbp.Tree(null, null, new edu.berkeley.sbp.Tree[] { new edu.berkeley.sbp.Tree(null, "w", new edu.berkeley.sbp.Tree[] { }),
@@ -1194,3 +1207,16 @@ new edu.berkeley.sbp.Tree(null, "grammar", new edu.berkeley.sbp.Tree[] { new edu
 
 
 
+
+
+
+
+
+
+
+
+
+
+
+
+
index 750f90d..d0109df 100644 (file)
@@ -1,10 +1,17 @@
 package edu.berkeley.sbp.util;
 public class ANSI {
-    public static String black(Object o) { return "\033[30m"+o+"\033[0m"; }
-    public static String red(Object o) { return "\033[31m"+o+"\033[0m"; }
-    public static String green(Object o) { return "\033[32m"+o+"\033[0m"; }
-    public static String yellow(Object o) { return "\033[33m"+o+"\033[0m"; }
-    public static String blue(Object o) { return "\033[34m"+o+"\033[0m"; }
-    public static String purple(Object o) { return "\033[35m"+o+"\033[0m"; }
-    public static String cyan(Object o) { return "\033[36m"+o+"\033[0m"; }
+    //public static String black(Object o) { return "\033[30m"+o+"\033[0m"; }
+    public static String black(Object o) { return o+""; }
+    //public static String red(Object o) { return "\033[31m"+o+"\033[0m"; }
+    public static String red(Object o) { return o+""; }
+    //public static String green(Object o) { return "\033[32m"+o+"\033[0m"; }
+    public static String green(Object o) { return o+""; }
+    //public static String yellow(Object o) { return "\033[33m"+o+"\033[0m"; }
+    public static String yellow(Object o) { return o+""; }
+    //public static String blue(Object o) { return "\033[34m"+o+"\033[0m"; }
+    public static String blue(Object o) { return o+""; }
+    //public static String purple(Object o) { return "\033[35m"+o+"\033[0m"; }
+    public static String purple(Object o) { return o+""; }
+    //public static String cyan(Object o) { return "\033[36m"+o+"\033[0m"; }
+    public static String cyan(Object o) { return o+""; }
 }
index 86d0d50..ff8f46c 100644 (file)
@@ -113,6 +113,10 @@ public class GraphViz {
         public boolean isHidden();
     }
 
+    public void show() throws IOException {
+        Runtime.getRuntime().exec(new String[] { "dot", "-Tsvg" });
+    }
+
     public void dump(PrintWriter pw) {
         IdentityHashMap<Node,Node> done = new IdentityHashMap<Node,Node>();
         pw.println("digraph G { rankdir=LR; \n");
@@ -131,6 +135,7 @@ public class GraphViz {
         }
         for(Node n : ihm.values()) n.edges(pw);
         pw.println("}\n");
+        pw.flush();
     }
 
 }
index faba28d..77af771 100644 (file)
@@ -265,65 +265,65 @@ testcase {
   q    = q:: [a-z]++
 }
 
-//testcase {
-//
-//    input "
-//
-//
-//
-// while x>0
-//    while y>0
-//       foo()
-//          bar()
-//
-//
-// while x>0
-//    while y>0
-//          foo()
-//        bar()
-//
-//
-//
-//";
-//  output "smt:{while:{>:{{x} {0}} while:{>:{{y} {0}} sbb:{{f o o} {b a r}}}} while:{>:{{x} {0}} sbb:{while:{>:{{y} {0}} {f o o}} {b a r}}}}";
-//
-//indent  !  = ww
-//outdent !  = " "  outdent " "
-//           | " "  (~[]*)  "\n"
-//
-//w        !  = " " | "\n" | "\r"
-//ws       !  = w*
-//ww       !  = sp*
-//sp       !  = " "
-//any      !  = ~[]*
-//
-//s           = ws statement ws statement ws => smt
-//
-//block       = "\n" indent  blockBody
-//           &~ "\n" outdent ~[\ ] ~[]*
-//
-//blockBody   = statement
-//            > statement ws blockBody => "sbb"
-//
-//statement   = call
-//            | ^"while" expr block /ws
-//
-//expr        = ident
-//            | call
-//            | expr ^">" expr   /ws
-//            | num
-//
-//call        = expr "()"        /ws
-//
-//num         = [0-9]++
-//
-//ident       = [a-z]++ &~ keyword
-//keyword     = "if" | "then" | "else" | "while"
-//
-//
-//
-//}
-//
+testcase {
+
+    input "
+
+
+
+ while x>0
+    while y>0
+       foo()
+          bar()
+
+
+ while x>0
+    while y>0
+          foo()
+        bar()
+
+
+
+";
+  output "smt:{while:{>:{{x} {0}} while:{>:{{y} {0}} sbb:{{f o o} {b a r}}}} while:{>:{{x} {0}} sbb:{while:{>:{{y} {0}} {f o o}} {b a r}}}}";
+
+indent    = ww
+outdent   = " "  outdent " "
+          | " "  (~[]*)  "\n"
+
+w          = " " | "\n" | "\r"
+ws         = w*
+ww         = sp*
+sp         = " "
+any        = ~[]*
+
+s          = smt:: !ws statement !ws statement !ws
+
+block       =        !"\n" !indent  blockBody
+           &~        !"\n" !outdent !(~[\ ]) !(~[]*)
+
+blockBody   =       statement
+            > sbb:: statement ws blockBody
+
+statement   = call
+            | ^"while" expr block /ws
+
+expr        = ident
+            | call
+            | expr ^">" expr   /ws
+            | num
+
+call        = expr "()"        /ws
+
+num         = [0-9]++
+
+ident       = [a-z]++ &~ keyword
+keyword     = "if" | "then" | "else" | "while"
+
+
+
+}
+
 
 
 testcase {