cwave04 at yahoo dot com
The second part of this tutorial is given here.
- Motivation behind why one should care to design a new language.
- Introduction to a detailed example that we shall use
in the following pages.
- The parts of a compiler.
- Lexical analysis for our example
- Syntax analysis for our example
- How to write a grammar (basic techniques)
- Some theory of syntax analysis
- Semantic analysis
- More on semantic analysis
- And still more on semantic analysis
What you already need to know
This tutorial assumes that you have basic familiarity with C. For
instance, you should know arrays and structure in C. Also, we have
with these languages
would help you follow the tutorial better.
Compiler construction is by no means a lost art, and there are very many
excellent sources that one can learn it from. Two very good sources are:
- The book Compiler Design by Aho, Sethi and Ulman.: It
is the classic textbook of the subject, and it is an excellent
textbook. But unfortunately it is only a textbook, and lacks lively
Modern Compiler Implementation
book (or rather, books, for there are 3 pairs of them, one pair each
for Java, C and ML) by Andrew Appel: These books are real gems, and
provide a very well balanced treatment in terms of theory and practice. They
are full of examples. There is web support available at the author's
What's special about this tutorial?
Most (if not all) of the good sources of compiler design info (including
the two books mentioned above) suffer from two problems. They somehow
always focus themselves on how to make a language like C (with possibly a
bit of object orientedness thrown in for good measure). But this should by no
means be the main aim of a compiler designer. Even if you know how to make
new C like languages within a matter of seconds, you should refrain from
designing one, because if everyone starts designing his/her personal
general purpose language it would defeat the very purpose of
having a general purpose language. It is for designing special
purpose languages that one should devote one's knowledge. And that's where
the fun is mainly! The standard texts (and even some of the standard
compiler courses) are strangely silent about this aspect! This tutorial,
on the other hand, brings this issue to the fore.
The second shortcoming of the standard texts seems to be their reluctance
in teaching how to design a language. They talk a lot about designing the
compiler for a language assuming the language syntax is already
provided. But in real world, you will rarely start with a ready-made
language. You will start with a vague idea about what the language should
do and how. This tutorial tries to provide insight into how you turn this
vague specification into a set of rigid grammar rules.