2

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?
Community
  • 1
  • 1
Maik Klein
  • 15,548
  • 27
  • 101
  • 197
  • 2
    There's hardly a general answer for this, it depends very much on what you want to translate into what and how similar those two are. Fundamentally there's no difference between compiling to ASM and compiling to any other language though, so a general compiler tutorial should very much help. – deceze Dec 30 '13 at 16:37
  • 1
    One thing you need to distinguish between is how high-level the output *language* is, vs. how high-level the actual output *code* is. Any compiler that produces assembly can be trivially redesigned to output a list of C++ function calls where each call matches one assembly instruction, but this is still low-level output, regardless of any unused capabilities of the host. If you want your compiler to actually rewrite into *high*-level output language features (infix operator expressions? classes? lambdas? types?), that's a very different question. – Alex Celeste Dec 30 '13 at 16:40
  • I don't want to add any language features, I basically just want to change the syntax. – Maik Klein Dec 30 '13 at 16:47

1 Answers1

4

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.

SK-logic
  • 9,605
  • 1
  • 23
  • 35