Basics of Compilers

A compiler is a program which takes as input one text file and as output, produces another text file. So, in this sense, it can be said that a compiler is just another program. In this post, we shall look at basics of a compiler and how is it written.

To create a compiler, we need special tools called Lex and Yaac.
Lex is a lexical analyser, which means that it analyses the text and finds out which words have special meanings. For example, in C, the symbols, {,(,),}, all have special meanings. Thus, it is Lex’s job to find out meaningful text from the given input file.

Yaac stands for “Yet Another Compiler Compiler”. It is a parser. It analyses the syntax of the input stream and makes sure that there are no syntactical errors in the input stream.

Now, these tools are available only on Linux Systems. If you want to write a compiler in Windows or Mac OS, then you have to use Flex (replacement for Lex) and Bison (Replacment for Yaac).

These two tools, in combination, can be used to write compilers. A compiler converts a high level language into machine language. It also links various modules of a program together. A compiler also makes severe optimizations of code and tries to make full use of the processor features that it is being run under.

If you see the optimized code of a compiler, it will be difficult to read because it will mainly consist of numbers and seemingly meaningless calculations.

To perform the above functions, a compiler usually has to make two or more passes through the source code to generate address references to code which has not yet been reached.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s