[antlr-interest] starting with language translation

Andy Tripp antlr at jazillian.com
Thu Mar 13 08:14:23 PDT 2008


Benzo,

Yes, those 4 steps are fine. You'll use ANTLR to lex and parse your java 
input,
the data structure you'll use is called an AST (Abstract Syntax Tree).

I would start with the latest V2 version of antlr (2.7.7 I think it is) 
rather than V3.
Run antlr on the java.g file that comes with antlr.
Antlr will generate the lexer and parser as Java source files which you 
then compile.
Then call the method on your parser that returns the AST and print out 
the AST
and study its structure and figure out how that maps to actionscript.

If the actionscript is very similar, you may be able to simply take some 
code that
pretty-prints the AST (prints it as normal-looking Java code, as opposed 
to a tree
thing), and modify that. This would be easier than doing fancy 
transformations on the AST.
There are at least two ways to print the AST. See
http://www.antlr2.org/article/1170602723163/treewalkers.html for a 
comparison.
Or skip that whole document and just try either the treewalker approach:
http://jazillian.com/articles/java.tree.g
...or the "by hand" approach:
http://jazillian.com/articles/JavaEmitter.java

Someone else did something similar with translating Java to Python, and 
he ended up
being able to just alter the JavaEmitter to print out python instead of 
Java.

Oh, and one more thing...buy and read the ANTLR book!

Good Luck!
Andy



benzo wrote:
> hi
> this is my first post - thus "hello world!"
>
> I'm interested in antlr since a long time and I finally like to make
> an attempt with it.
> What I'd like to achieve is to write a translator in form of "java to
> actionscript".
>
> >From what I understand there are basically 4 layers/modules/steps involved:
> 1. parse of the source language (java)
> 2. persist/serialize the structure
> 4. apply the translation rules
> 5. generate the  the target language (actionscript)
>
> some questions to these points:
>
> 1. are there any existing java language parser (java grammar), maybe
> even with some (basic) documentation (tutorials, etc.)
> 2. what do you suggest: writing the structure down to xml? persist in memory?
> 4. are there any good examples for doing that the right (clean) way?
> 5. what is your personal favorite way doing that? stringtemplate? are
> there simpler methods?
>
> thanks in advance for your answers.
>
> //benzo
>
>   



More information about the antlr-interest mailing list