Note: During high-school, I learned BASIC, C, C++, and x86 assembly – in the process I became very interested in how compilers worked. I spent a lot of my free time developing a BASIC -> x86 compiler and writing a tutorial to document my process. This page is the result of my efforts at the time of graduating from high-school.
Interested in building a compiler? If so, you have come to the right place. Here I am developing a compiler named KoolB and writing a tutorial that details my journey in doing so. If you are interested in compiler technology or want to develop your own “perfect” language, check out my tutorial, where I will guide you through the process.
My compiler tutorial will cover everything you need to build a compiler from beginning to end for both Windows and Linux. We start out with a blank C++ project and start adding things to it. First, we build the skeleton of the compiler. Secondly, we start designing and building a BASIC-like language into the compiler. Finally, we will cover some issues such as an making an IDE (Integrated Development Environment) for your language, distributing your compiler, developing a standard library and GUI objects, and further development.
Status
Abandoned. Unfortunately, I stopped working on this project in 2004 because I got too involved with cool projects in college. If I get time, I may look at picking this back up again, but I doubt that will happen. In the mean time, feel free to read what I have written so far.
History
The Compiler Tutorial is one of my oldest projects and is closely related to my KoolB compiler. In high school, one of my favorite languages, RapidQ Basic, was discontinued, which prompted a lot of discussion on building your own compilers and in particular, a RapidQ clone. My KoolB compiler and this tutorial-style book is a result of my attempt to document the creation of a compiler.
Versions
In high school, I wrote my first version of the Compiler Tutorial, but stopped at chapter 6. One of the reasons was I was doing simultaneous development of KoolB and writing the chapters. I discovered that I didn’t like some of my approach because it was overly messy. So as I entered college, I decided to re-write both KoolB and the Compiler Tutorial using what I had learned from my first (aborted) attempt. However, as you well know, college is not the cake-walk that high school is. I got about 2 and half chapters finished before I got a job as a research assistant, and started taking Differential Equations, and volunteered my life away to work at the Robotics Lab at UCF.
Thus I offer two different versions of my Compiler Tutorial, both in an unfinished state. The first one is aimed a bit more for beginners (as I was a complete beginner writing it) and assumes you are learning C++. The second one is a bit more mature, assuming you know some C++ already, and is better formatted as a book. However, the first one is more complete (6 chapters as opposed to 2 and a half).
Chapters
- Compiler Tutorial chapters 1-2 (new): This is my new compiler tutorial, featuring chapters 1, 2, and part of 3 with an appendix on how to install Dev-C++.
- Chapter 1 (old): Introduction
- Chapter 2 (old): Meet Mr. C++
- Chapter 3 (old): Teaching KoolB to Read
- Chapter 4 (old): Teaching KoolB to Write
- Chapter 5 (old): Compiling Simple Data Types
- Chapter 6 (old): Error Checking & Reporting
Source Code (Downloads)
- Last KoolB Release (15.02): The last KoolB release I made, which should have corresponded to chapter 15 of my tutorial. Alas I never wrote that many chapters, so here it is left standalone.
- Updated KoolB Release (16.0): A correspondent of mine, Mark, graciously updated my last update to KoolB and released a new version that had a bunch of bug-fixes and added features. The Linux support has been removed, but everything else should work pretty well.
- Simply KoolB: Just about the time I was finishing KoolB, I had a crazy idea to go back and make the most basic compiler and IDE I could with under 1,000 lines of code. The result was Simply KoolB, a heavily commented 7 command BASIC -> assembly compiler with IDE. It should be fairly instructional to somebody interested in building a compiler.
What I assume you know
That is an excellent question. I try not to assume to much, but I don’t have the time to teach you everything. So I assume that you know several things:
- Experience in working computers. In order to understand the tutorial, it almost goes without saying that you have to have some experience in operating and using either the Windows operating system or Linux. Preferably, it would be extremely helpful if you knew a bit of the Windows API and C library functions.
- Programming in general. I assume that you have a solid foundation in programming, perferably C or C++. If you know another language other than C or C++, you might have to do some additional reading else where to catch up. See the next section for some resources/tutorials.
- Object oriented programming. I don’t use heavy object oriented concepts like polymorphism or inheritance, but I do use simple objects to separate the program into easy to understand parts.
- Modern PC assembly language. I’m hoping that you have some understanding of assembly language. I’ll do my best to give short overviews of the assembly language, but it would help tremendously if you know some of it beforehand. I know that this is has a reputation of being very difficult, but see the next section for some tutorials that explain it in an easy to understand manner.
- Programming in BASIC. Since we are building a BASIC compiler, its sort of nice to know a bit about it. If you’ve programmed it years ago or have dabbled in it a bit, that should be OK.
Finally, the most important thing you will need to get something from my tutorial is patience, persistence, and willingness to spend time to learn. Building a compiler is difficult and takes time – however, it is very rewarding once you learn it.
Resources
My best recommendation would be to take some time and learn this stuff before attempting to build a compiler. I jumped right into building a compiler before I knew this stuff, and it didn’t take long before I gave up. After about a year of learning other languages and working on other projects, I gave building a compiler another try. And look what happened!
So, I would highly recommend that you check out some of these resources/tutorials before you just jump into my tutorial. They will give you a much better idea of what is going on.
C or C++ programming
- Cplusplus.com is a great place to learn C++ as it takes you through the core of programming C++.
- For those who want more in depth, there is a very good electronic book (that can be printed out too) by Bruce Eckel named Thinking in C++.
Object Oriented Programming
- For those C programmers who don’t know much about objects and so forth, there is a decent discussion of it here: Classes. Constructors and Destructors. Pointers to classes.
Assembly language
- Ahh, this is a tough one to learn. However, you are in luck. John Carter has written an excellent e-book/tutorial on modern PC assembly language.
BASIC programming
- This category isn’t too big of a deal, so if you don’t know BASIC that much, and want to learn some more about it, I suggest you download Rapid-Q and play around with it or read the excellent documentation/tutorials that come with it.