Hello, I am very new to the ANTLR tools so please bear with me. I am trying
to create a tool that would essentially be able to parse Java source,
convert to and AST and then to an XML output. From there I would like to be
able to take the XML data and convert it back into Java source, and
hopefully be able to compile the java to byte code, load and execute it
(although right now I am just focusing on the conversion aspect). The
objective is very similar to the one described in this stackoverflow post:
http://stackoverflow.com/questions/5570701/antlr-converting-java-file-to-xml-and-read-back-xml-to-create-java-object-issue
I would only need to show this functionality on a very fundamental level,
so the source inputs would be very simple. I'm thinking this can be
accomplished using the Java lexer/parsers and the XML lexer within the
ANTLR examples set (http://www.antlr.org/download.html), and following the
XML parsing tutorials, I've been able to create a simple XML parser. Using
these available resources, the source might look something like this:

public static void main(String args[]) {
try{
JavaLexer lex = new JavaLexer(new ANTLRFileStream(args[0]));
CommonTokenStream tokens = new CommonTokenStream(lex);

JavaParser parser = new JavaParser(tokens);
JavaParser.compilationUnit_return r = parser.compilationUnit();
if ( r!=null ) System.out.println(((CommonTree)r.tree).toStringTree());

CommonTreeNodeStream nodes = new CommonTreeNodeStream((Tree)r.tree);
nodes.setTokenStream(tokens);

XMLTreeParser walker = new XMLTreeParser(nodes);
walker.document();

}
catch(Throwable t){
System.out.println("exception: "+t);
         t.printStackTrace();
}

So I am wondering if the conversion can be through this method, or are
there additional steps needed that I am completely missing?
Thanks,
-Robert
_______________________________________________
antlr-dev mailing list
antlr-dev@antlr.org
http://www.antlr.org/mailman/listinfo/antlr-dev

Reply via email to