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.
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.
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.
- We will be using C or C++
- We will start with a very basic tokenizer
- Modify the code to have a parser
- Code generator.
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.