Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a Compiler: Learn OCaml or Stick With Java/C/C++

I have to create a new language and a compiler for that language for a class I'm taking this semester - this means the deadline is in about 3.5 months. I have read that OCaml is one of the better languages to write a compiler in, but I have no previous experience with it. My question is thus as follows: would it be a better idea for me to try to create the compiler with languages I already know well (Java, C, C++), or devote some time to learning OCaml and use it instead?

like image 427
John Roberts Avatar asked Jan 31 '13 18:01

John Roberts


1 Answers

It depends of how you evaluate the interest of each choice.

If your aim is to finish your course project as quickly as possible, sticking to a tool you already know well is probably the best choice. The language is important and I do believe that ML languages are most adapted to write compilers, but on a short scale being comfortable with the language, tools, error messages, and common idioms will probably trumps language-related productivity benefits. Or at least among garbage-collected languages; you can be efficient with C but you have to know the language very well (and in that case probably wouldn't lump it among C++ and Java).

If you are interested in building compilers, and more generally language tools, or study programming languages after the end of your course, learning a statically typed functional language is certainly a worthwhile investment, and a compiler course is an excellent excuse to do that.

Edit: some pointers

rgrinberg's helpful comment reminds me to point you to MinCaml as one possible source of inspiration.

My first thought was the "Compiler Implementation in {C,Java,ML}" books by Andrew Appel but, as the Amazon comments put it, this book is too advanced to be used as an introduction either to compilers or to ML.

Andrej's Bauer Programming Language Zoo is also a great resource, but it is more oriented towards discovery of a wide variety of programming languages by interpreter implementation than compilation per se (though some of the zoo animals are compiled to abstract machines).

like image 61
gasche Avatar answered Oct 12 '22 22:10

gasche