Thursday, August 18, 2011

Compilers - 1

Today, let's talk about, one more interesting topic called compilers. The def goes like this

"A compiler is a program, that converts a source program in one language into a target program in another language"

The source program may be in any high level language like C, C++ or Java while the target program may be a native code like byte code or machine code that can be understood by the computer.

If you want to design a compiler in the future, you definitely should know about the Bible of Compiler Design. The book is called "Compilers : Principles, Techniques and Tools" by the authors "Aho, Ullman". You can check out the Amazon link on the left side, if you are interested.

For today let's see the difference between interpreter and compiler.

"An interpreter is a program that simply replaces the code present in the source program with the available machine code"

Examples of interpreter include the famous "Basic" language interpreter. In that, when ever you do a mistake, while typing the code, you will immediately get an underline, even before you compile the code, telling, you are wrong and some IDE's (Integrated Development Environment) provide suggestions also.

If you are familiar with the SQL (Structured Query Language), it is similar to the update command. Compiler also does the same thing, replacing the code. So what's the difference ?

The difference is that a compiler is generally capable of translating the whole code at a time, while the interpreter does this line by line. Also, the interpreter takes care of mainly syntax and logical errors, while the compiler is capable of reporting runtime errors also, along with the former. For rapid error checking, interpreter is the best option, since most of the errors can be corrected before running the program, but this alone cannot save us from errors. That is the reason, why languages like "Visual Basic" or "Visual C#", make use of both compiler and interpreter for efficient writing of code and efficient run time error checking.

Tommorrow, let's see different types of compilers and their examples.

No comments:

Post a Comment