import org.antlr.runtime.*; import org.antlr.runtime.tree.*; import org.antlr.stringtemplate.*; import java.io.*; public class Test { public static void main(String[] args) throws Exception { // load the group file ByteCode.stg, put in templates var FileReader groupFileR = new FileReader("Python.stg"); StringTemplateGroup templates = new StringTemplateGroup(groupFileR); groupFileR.close(); // PARSE INPUT AND BUILD AST ANTLRInputStream input = new ANTLRInputStream(System.in); CMinusLexer lexer = new CMinusLexer(input); // create lexer // create a buffer of tokens pulled from the lexer CommonTokenStream tokens = new CommonTokenStream(lexer); CMinusParser parser = new CMinusParser(tokens); // create parser CMinusParser.program_return r = parser.program(); // parse rule prog // WALK TREE // get the tree from the return structure for rule prog CommonTree t = (CommonTree)r.getTree(); System.out.println(t.toStringTree()); // create a stream of tree nodes from AST built by parser CommonTreeNodeStream nodes = new CommonTreeNodeStream(t); // tell it where it can find the token objects nodes.setTokenStream(tokens); CMinusTree walker = new CMinusTree(nodes); // create the tree Walker walker.setTemplateLib(templates); // where to find templates // invoke rule prog, passing in information from parser CMinusTree.program_return r2 = walker.program(); // EMIT BYTE CODES // get template from return values struct StringTemplate output = (StringTemplate)r2.getTemplate(); System.out.println(output.toString()); // render full template } }