Writing Micro Compiler in OCaml (2014)

https://news.ycombinator.com/rss Hits: 5
Summary

TL;DR Writing micro compiler in OCaml At one point or another every single software developer in the world comes to a realization in his career when the time is ripe and it’s time to write your own super cool programming language. However the subject of creating your own programming language with an compiler is quite a complex one and can’t be tackled without some pre-research. That’s how I’ve started reading Crafting Compiler in C, an aged but really comprehensive book about developing your own compiler for an Ada-like programming language. Second chapter describes writing a really simple micro language targeting pseudo assembly-like output in order to explain the core concepts of developing your own compiler and writing an LL(1) parser. Let’s try rewriting this micro compiler in OCaml, a language better suited for writing compilers that is becoming quite popular due to it’s clean syntax and strict evaluation semantics combined with functional and object-oriented programming styles. If you are not familiar with OCaml try reading Real World OCaml first. Instead of outputting pseudo assembly our micro compiler will output a real nasm source code which will be automatically compiled into a binary executable file. So let’s start by describing simple micro language with an example source code 1 2 3 4 5 6 7 8 begin a := 1; b := a + 1; b := b + 1; write (a,b); read(a,b); write (a+10, b+10); end As you can see from example source code the program starts with begin keyword and ends with an end keyword. It has only integer variables which must be predefined by assignment operation before using in expressions, and it also has two simple functions read and write. read takes a list of variable names separated by comma and reads user input from stdin into those variables write takes a list of expressions and outputs them into stdout Now in order to create an executable from this source code first we need to parse it. Since LL(1) type parser is enough to parse this kind of langua...

First seen: 2025-08-22 22:30

Last seen: 2025-08-23 02:32