[antlr-interest] source to source transformations

Robert Soule robert.soule at gmail.com
Wed Mar 11 10:10:39 PDT 2009


Hi,


The high level goal that I am trying to accomplish is to translate one
language to another. For the sake of an example, lets say that
language one (A) looks like ('a'|'b')*, while the second language (B)
looks like 'ASECTION' 'a'* 'BSECTION' 'b'*. I have simple parsers and
tree grammars to print the ASTs of both languages.

I can see a few approaches to doing the translation. First, I could
make multiple passes on the A language input, first to print all the
'a' statements I encounter, then to print all the 'b' statements I
encounter, but this approach doesn't seem so great. It only allows me
to print, not construct a B language AST, so I can't do any analysis.
Also, I have to make multiple passes of the AST, so it is somewhat
slow.

The AST re-write rules don't seem to me to be that sufficiently
powerful to do this type of tree transformations either, by which I
mean, I could not convert an A language AST to a B language AST using
only tree re-writes. If I am mistaken about this, can you point me to
an example of this type of translation?

What I think the right translation approach is to build a B language
AST by hand. So, when I start the translation, I create a B language
stub AST (maybe a program node, with null a section and b section
children). I then would want to hold pointers to both those null
childen, and when I encounter an 'a' or 'b; statement, I add it as a
child to the appropriate parent node.

However, I don't see an easy way to do this with ANTLR. Is there a
recommended approach? In the past, I have used Rats!
(http://www.cs.nyu.edu/rgrimm/xtc/), which provides a
"FactoryFactory". This lets you define part of an AST, with place
holders for adding in
nodes of the children. I was wondering if there is an equivalent
facility in ANTLR?

Any advice you can provide me would be really appreciated.

thank you,
Robert


More information about the antlr-interest mailing list