Tuesday, 9 August 2016

My Experiment with Compiler Writing. Part-1


Well, I have written a small OS kernel(NanoOS) earlier(2008-2010). It was written in C++ and assembly language for Self learning as well as for Fellow colleagues at Redhat, Pune. But never tried a compiler or interpreter, as it was never needed for my Job.

The need

Recently for a small project I had to write a small parser. It is not a language parser though.  It was written for a parsing a logfile of an application. Even it was designed to detect wrong sequence in that application. Well forget about the application. After that project is complete I thought to write a small Interpreter or Compiler for a very small language. As a first thought It came to me that I should write a Compiler for BASIC(Beginners' All purpose Symbolic Instruction Code) but implementing a full blown BASIC compiler is a big task for me(I sincerely believe I am not that intelligent to write a full blown compiler single handedly). So in my so-called Compiler a very very small subset of BASIC language will be implemented, as it was meant for self training and example for who ever wants to study this subject practically.

My approach

I prefer practical way to learn. So i will try to lay down fragments of code in sequence. So that any one can follow and understand.
  1. We will be using C or C++ 
  2. We will start with a very basic tokenizer
  3. Modify the code to have a parser
  4. Code generator.
Why I choose C or C++ is, I write code more easily in these 2 languages. Any other language can be used. Even BASIC can be used to write a BASIC compiler.
But it needs through understanding of the language.

I deliberately avoided Gramars and all those Compiler jargon. If those things are needed then please pickup a Compiler writing book and read. If you have read such a book then you may find this article informative on implementation.

Keep in mind "This is not a tutorial for Compiler construction courses" or class assignment. Secondly Please Don't Expect it to be of any practical use. This is just for my personal pleasure and to share my pleasure with others.

In next instalment I will start with lexing/parsing code.


No comments:

Post a Comment