1

I'm planning to create something that would do automated translation from Java to C# (and in reverse afterwards). What I need is something that you could use to translate Java source code into C# source code.

I ran across ANTLR, but I'm not exactly sure of how go about using it for my task. I know ANTLR has a strong support for both Java and C#, and there are already existing grammars for both of them, the lexing/parsing process, then the AST creation, and then finally the tree walker. Theoretically, I get it, but when it comes to actually putting it to work, I stall hardcore.

Can someone provide a generic step-by-step of "how to" that can be used for any language, because I can't find any decent tutorials of how to even use ANTLR?

Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
Alexey
  • 3,607
  • 8
  • 34
  • 54
  • 2
    *"can't find any decent tutorials of how to even use ANTLR?"*, I can't believe you searched real hard. Using your favorite search engine (be it Google, Bing or Yahoo) with the keywords "ANTLR tutorial", you must have seen the excellent tutorials of Scott Stanchfield. Also browse through the answers already given in the [ANTLR tag here on SO](http://stackoverflow.com/questions/tagged/antlr?sort=faq&pagesize=50): there are many that contain copy-paste ready answers for you to try. – Bart Kiers Jan 30 '12 at 20:06
  • 1
    There is also Terry Parr's (the inventor of ANTLR) book: http://pragprog.com/book/tpdsl/language-implementation-patterns for probably the "ultimate" tutorial on ANTLR. – Ira Baxter Jan 30 '12 at 20:12
  • 2
    You should also visit my SO answer regarding building such a translator: "How to translate between programming languages" http://stackoverflow.com/a/3460977/120163 – Ira Baxter Jan 30 '12 at 20:14
  • @IraBaxter, while *Language Implementation Patterns* is a (IMHO) good book on how to (quickly) build various types of interpreters/translators, it's not a good introduction to ANTLR. For an in-depth tutorial on ANTLR, see his other book: [The Definitive ANTLR Reference](http://pragprog.com/book/tpantlr/the-definitive-antlr-reference) – Bart Kiers Jan 30 '12 at 21:09
  • @BartKiers: He wrote 2? Great! Glad you identified the better one for tutorial purposes. I agree, OP didn't look very hard. – Ira Baxter Jan 30 '12 at 21:10
  • I'm really skeptical of books when it comes to such matters. Online tutorials seem to help me much more. The Scott Stanchfield are totally baws. @IraBaxter I guess you are right about the lack of research. I did not exactly understand what I was looking for ;) – Alexey Jan 30 '12 at 21:14
  • But how does ANTLR tackle the problem of different libraries? – Alexey Jan 30 '12 at 21:22
  • *"But how does ANTLR tackle the problem of different libraries?"* not sure what you mean by that... – Bart Kiers Jan 30 '12 at 21:26
  • You mean that ANTLR generates parsers in different target languages? But that has nothing to do with the translation between two arbitrary languages... – Bart Kiers Jan 30 '12 at 21:35
  • It said that ANTLR has tree walkers, which means it turns an AST into source code from another language, right? So would it not need libraries? (Stream readers and writers for example). – Alexey Jan 30 '12 at 21:40
  • @Alexey: A tree walker simply visits every node in the AST. What it does depends on what you tell it to do; it is not specifically a "translator" to another language. You might tell the tree walker node visitor to generate code that matches the tree node; if you do so (that's actually a lot of work if done accurately), you will get what is called a pure "syntax-directed translation", which is usually pretty horrible because it doesn't account for information "far away" in the tree. Of course, you can fix that by making the translation more sophisticated, ... and it goes on. – Ira Baxter Jan 30 '12 at 22:58
  • 1
    @Alexey: At the risk of offending you, from the nature of the discussion so far, I don't think you have the understanding or the background to use ANTLR to build a langauge translator. Suggest a compiler class first, and then revisit the topic. Language translations are *hard* to do right. – Ira Baxter Jan 30 '12 at 23:00
  • @Alexey: You've also completely ignored the problem of replacing the Java libraries with equivelent libraries for your generated C# code. The translator effort will be dwarfed by the effort to replace the libraries. The reason people code in Java is not that it is a great language; it is because it has great libraries, so you don't have to invent a lot of code when building an application. You can't skip these. – Ira Baxter Jan 30 '12 at 23:02
  • 2
    And no, you didn't offend me at all. I am always open for any type of criticism ;) (I sometimes even prefer that, because it points out flaws in my programming and/or researching skills) – Alexey Jan 31 '12 at 14:58
  • @Alexey: Notice he didn't say he had a working translator, just that it didn't have any syntax errors. That doesn't mean Python has the libraries he needs, or his code works. Best of luck :-} – Ira Baxter Jan 31 '12 at 23:40
  • @IraBaxter But as you said, doesn't the sophistication rely on how well your node visitor is? And he said he made something that would translate Java to Python, which it did. It isn't perfect but you can continuously improve it as you go on. – Alexey Feb 01 '12 at 15:13
  • @Alexy: Yes, if you hid vast amounts of machinery in each node visitor, it can generate correct and good code. The problem is you've reduced the miracle of compiling to the miracle of great node visitors, without any suggestion as to what they have to do. The compiler guys have been thinking about this for 50 years. You ignore their understanding and knowledge at your peril. – Ira Baxter Feb 01 '12 at 16:02
  • @Alexy: ... I should add that that what is normally done before generating code is to build a variety of derived data structures (symbol tables, control and data flow graphs, etc.) [some of which might/must be done by AST node visitors) so that each code generation element (maybe an AST visitor, but often not) doesn't have to be giant and magic. You need to know what these additional structures are, how they are built and used. Thus the suggestion for the compiler class. – Ira Baxter Feb 02 '12 at 14:41

1 Answers1

0

Once you made yourself familiar with the basics of ANTLR you will need these: openjdk.java.net/projects/compiler-grammar/antlrworks/Java.g + http://antlrcsharp.codeplex.com/

Team Pannous
  • 1,074
  • 1
  • 8
  • 11