checkpoint
authoradam <adam@megacz.com>
Wed, 5 Jul 2006 17:33:25 +0000 (13:33 -0400)
committeradam <adam@megacz.com>
Wed, 5 Jul 2006 17:33:25 +0000 (13:33 -0400)
darcs-hash:20060705173325-5007d-a1a2511272e19187ef9130019478b6262cbacefb.gz

src/edu/berkeley/sbp/misc/Demo.java [new file with mode: 0644]

diff --git a/src/edu/berkeley/sbp/misc/Demo.java b/src/edu/berkeley/sbp/misc/Demo.java
new file mode 100644 (file)
index 0000000..0bafd76
--- /dev/null
@@ -0,0 +1,58 @@
+package edu.berkeley.sbp.misc;
+
+import edu.berkeley.sbp.*;
+import edu.berkeley.sbp.chr.*;
+import edu.berkeley.sbp.misc.*;
+import edu.berkeley.sbp.meta.*;
+import edu.berkeley.sbp.bind.*;
+import edu.berkeley.sbp.util.*;
+import java.util.*;
+import java.io.*;
+
+public class Demo {
+
+    /** our grammar class */
+    public static class Math {
+
+        public static @bind.as("(") Expr parenthesis(Expr e) { return e; }
+        public static class Expr implements Reflection.Show { }
+
+        public static @bind.as("Expr") class Numeric extends Expr {
+            public @bind.arg String numeric;
+        }
+
+        public static class BinOp extends Expr {
+            public @bind.arg Expr left;
+            public @bind.arg Expr right;
+        }
+
+        public static @bind.as("*") class Multiply extends BinOp { }
+        public static @bind.as("/") class Divide   extends BinOp { }
+        public static @bind.as("+") class Add      extends BinOp { }
+        public static @bind.as("-") class Subtract extends BinOp { }
+
+    }
+
+
+    // invoke "java -jar edu.berkeley.sbp.jar edu.berkeley.sbp.misc.Demo tests/demo.g <expr>"
+    public static void main(String[] s) throws Exception {
+
+        Parser metaGrammarParser   = new CharParser(MetaGrammar.make());
+        Tree<String> parsedGrammar = metaGrammarParser.parse(new CharInput(new FileInputStream(s[0]))).expand1();
+        GrammarBindingResolver gbr = new AnnotationGrammarBindingResolver(Math.class);
+        Union   mathGrammar        = MetaGrammar.make(parsedGrammar, "Expr", gbr);
+        Parser  mathParser         = new CharParser(mathGrammar);
+
+        System.out.println("about to parse: \""+s[1]+"\"");
+        Tree tree = mathParser.parse(new CharInput(new StringReader(s[1]))).expand1();
+
+        // below is ugly voodoo which will go away very soon.  ignore it.
+        Tree.TreeFunctor tf = (Tree.TreeFunctor)tree.head();
+        Math.Expr e = (Math.Expr)tf.invoke(tree.children());
+        // above is ugly voodoo which will go away very soon.  ignore it.
+
+        System.out.println("done!");
+        System.out.println(Reflection.show(e));
+    }
+
+}