Question

I basically want to create my own language. But instead of compiling a high level language into a low level language, I just want to translate my language to another high level language.

I fist looked at Learning to write a compiler but it seems that those tutorials assume that you want to compile a high level language to assembler, but I just want to translate a language to another.

Because I also know Haskell I looked at Scheme in 48hours but it seems that this is more a scheme interpreter written in Haskell.

I searched a bit more and I found Source To Source compiler, but I don't want to add any language features I just want to change the syntax.

  1. Where would I start if I want to translate a language into another one?

  2. Is every compiler tutorial helpful for the task that I want to solve?

  3. Are there tools that could help me?

  4. Is this a task that ANTLR is designed to solve?
Was it helpful?

Solution

Compiling into a high level language is not any different from compiling into a low level one.

It is best done by a chain of small transforms: you have your source language AST, a target language AST, and you can build a number of slightly different intermediate languages in between, slowly morphing one language into another.

There is a nice tutorial on compiling Scheme to C.

But be warned that it's pretty hard to translate a high level language into idiomatic code in another high level language. Your generated code, no matter how "high level" it is, is not going to be very readable.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top