[antlr-interest] Translating from Java to C++
Jean Marc Vanel
JeanMarc.Vanel at mathworks.fr
Sat Feb 3 04:31:32 PST 2007
Yesterday I tried Antlr. The initial motivation was to translate my Java
code into C++. There is a very nice and clean Java 5 grammar with Antlr
3b6 <http://www.antlr.org/grammar/1152141644268/java.g> (other Java
grammars for antlr 2.7.X
<http://www.antlr.org/grammar/1090713067533/index.html> ). I can parse
any Java source tree. But apparently no one has written a translator on
top of the Java 5 grammar . Of course there is some work to do, but
certainly less than the translation from C++ to Java; see comparing c++
and java
<http://www.javacoffeebreak.com/articles/thinkinginjava/comparingc++andj
ava.html> for a list of differences. I don't expect a full translation
at first, I would be satisfied by something translating over 95% of the
syntax, leaving 5% to correct manually.
I don't know yet how to implement the translator:
* use Tree construction (rewrite rules) as explained in
http://www.antlr.org/wiki/display/ANTLR3/Tree+construction
http://www.antlr.org/doc/trees.html#_bb1
* traverse the Abstract Syntax tree in Java and directly output
what I want
* use stringtemplate <http://www.stringtemplate.org/> , which
seems well integrated with Antlr, having the same author .
In fact in first approximation, neglecting the .h files, a C++ class is
very similar to a Java class with some local changes (like replacing
"extends" by ":" ) or less local changes, like:
A a = new A(); ===> A* a = new A();
Is it possible to start from a verbatim recursive template, then adding
rules incrementally, like is often done in XSLT ? In fact I'd be
interested to read about comparison of concepts between Antlr and XSLT.
Or wouldn't it be possible to use the multi-language layer that is
inside Antlr and which allows Antlr to generate parsers in several
languages like Java, C++, C# ?
Among the questions I have is this one. Which is the best way to keep
the grammar definition separate from the different uses of that grammar
? Like reusing the pure Java 5 grammar for 1) a translator to C++ , 2) a
Java code quality checker , 3) a pattern recognizer for design patterns
, etc .
I have to confess that I'm new to Antlr, although I collaborated to
eXist <http://exist-db.org/> , which has an Antlr grammar for XQuery. I
already wrote a non-trivial flex-bison parser and lots of XSLT.
Jean Marc Vanel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20070203/1dd29c97/attachment.html
More information about the antlr-interest
mailing list