0

I want to translate a simple programming language to another programming language. I only need that the output be syntactically valid so the code-generation part can me ditched.

Say, a subset of Python to C. I feel like I can cheat a bit and just 'copy-paste' most of the code, but say I wanted to have the list/string splicing of Python translated to C then it becomes non-trivial (for me, at the moment).

I made it all the way to a top-down recursive descent parser (actual grammar pending). What do I do next? I have a copy of the dragon book (2nd ed) but skimming ahead of where I am now, I don't see how I can do this and at the same time ditch code generation. I'm not sure if it's possible to ditch, but I don't need to produce an executable nor extend the program for other output languages to need intermediate code.

I'm stuck. The program is being written in C.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
user56833
  • 109
  • 3
  • 1
    If you only need the output to be syntactically valid, you can generate "main () { }" and be done. The point is, it is never that easy. You have to translate the concepts in the source code into equivalent concepts in the target language. It isn't always obvious how to do that, and often the translation at one point depends on information from "far away" in the code. I think "I fell like I can cheat a bit" is likely not to work. Suggest you read a serious compiler book before you go much further. – Ira Baxter Aug 28 '13 at 20:10
  • This question popped up again, with no further answers. OP might be interested in this discussion about building (esp. "sloppy") language translators: http://stackoverflow.com/questions/3455456/what-kinds-of-patterns-could-i-enforce-on-the-code-to-make-it-easier-to-translat/3460977#3460977 – Ira Baxter May 12 '14 at 18:50
  • I've used LLVM before to achieve this. If you have an LLVM front-end for your language (or are able to write one) you could write an LLVM pass to generate source code for a different language. – silverclaw Jun 22 '15 at 15:12

1 Answers1

0

Several Python-to-C compilers have already been developed. For example, there is a subset of Python called RPython that can be translated to C automatically.

Community
  • 1
  • 1
Anderson Green
  • 30,230
  • 67
  • 195
  • 328