checkpoint
[sbp.git] / src / edu / berkeley / sbp / misc / Demo.java
index dd56ea0..92c9194 100644 (file)
@@ -17,26 +17,13 @@ public class Demo {
         Tree<String> res = new CharParser(MetaGrammar.make()).parse(new FileInputStream(s[0])).expand1();
         MetaGrammar.Meta.MetaGrammarFile mgf = m.new MetaGrammarFile(res);
         MetaGrammar.BuildContext bc = new MetaGrammar.BuildContext(mgf);
+
         Union meta = mgf.get("s").build(bc);
-        System.err.println("parsing " + s[1]);
         Tree t = new CharParser(meta).parse(new FileInputStream(s[1])).expand1();
-        System.out.println("tree:\n" + t.toPrettyString());
 
-        Reducer red = (Reducer)t.head();
-        MG.Grammar g = (MG.Grammar)red.reduce(t);
-        System.out.println(g);
-        
-        Context cx = new Context(g,m);
-        Element u = null;
-        for(MG.NonTerminal nt : g.nonterminals) {
-            Union el = (Union)cx.get(nt.name);
-            StringBuffer st = new StringBuffer();
-            el.toString(st);
-            System.err.println(st);
-            if (nt.name.equals("s")) u = el;
-        }
-        System.err.println();
+        Union u = Demo.make(t, "s");
 
+        System.err.println();
         System.err.println("== parsing with parsed grammar =================================================================================");
         t = new CharParser((Union)u).parse(new FileInputStream(s[1])).expand1();
         System.out.println(t.toPrettyString());
@@ -442,6 +429,22 @@ public class Demo {
         return o;
     }
 
+    public static Union make(Tree t, String s) {
+        ReflectiveMeta rm = new ReflectiveMeta();
+        Reducer red = (Reducer)t.head();
+        MG.Grammar g = (MG.Grammar)red.reduce(t);
+        Context cx = new Context(g,rm);
+        Union u = null;
+        for(MG.NonTerminal nt : g.nonterminals) {
+            Union el = (Union)cx.get(nt.name);
+            StringBuffer st = new StringBuffer();
+            el.toString(st);
+            System.err.println(st);
+            if (nt.name.equals(s)) u = el;
+        }
+        return u;
+    }
+
     public static class MG {
         public static @tag("grammar") class Grammar {
             public NonTerminal get(String s) {