Source-to-Source Conversion

I need to translate much source code from an obscure programming language into Java.

The other language is relatively simple, Pascal-like. No structs, no objects. Has if tests and for/while loops similar to Java. No pointers in the C-sense, but has references that I think I can map to Java references. The obscure language has a thousand specialized commands that I am re-implementing in Java as static class method calls.

I am only talking about source-code, not compiling or bytecode generation/manipulation. After my translation tool generates Java source-code, that Java code will become a normal Java project in a regular Java IDE for the usual Java compiler and JVM.

I suspect that a search-and-replace approach will not work well for this source-to-source conversion. At the very least of many possible problems, I need to skip over quoted string literal statements.

I have only recently learned that there may be tools that are aware of programming languages as such, and capable of manipulating source code in a fashion more intelligent than mere search-and-replace.


I have heard of the Antlr project for generating lexers/parsers.

In this tool, I first define a formal BNF-like grammar to describe this obscure programming language. Then Antlr uses that grammar to generate a lexer/parser built in Java source code. I give that lexer/parser a chunk of my obscure language source code to generate an abstract syntax tree representing the identifiers, keywords, operators, and values in that source code. I can then use the tree-parser feature of Antlr 4 to visit the nodes of that AST. As I visit each node, my code uses StringTemplate project to generate Java source code as the translation from obscure language to Java language.

  • Am I correct in my understanding of Antlr? (I am new to this idea of tools that understand and manipulate a programming language.)
  • Is Antlr the best tool for this job?
  • How onerous and enormous a task is defining a grammar?
    • Is my time better spent learning search-and-replace tools such as grep/AWK/sed/Perl given how close my obscure language may be to Java?
    • Should I look for an existing grammar definition for some Pascal-like language that I could then edit to fit my obscure programming language rather than start a grammar from scratch?


I have also heard of the older Yacc tool. From what little I know, Antlr is basically Yacc on steroids.

  • Is there any advantage to Yacc over Antlr or other tools for my purpose of source-to-source conversion?
  • 1
    There is also JavaCC, which is pretty flexible. But note that "all" what these tools do is to generate Java code to parse your particular language. Basically it translates a description of your language into Java. It doesn't translate any code written in such a language to Java. Being able to parse your language is only the first part of the solution. – Maarten Bodewes Jun 21 '17 at 21:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.