You are here

Code generators--Part I

Amr M. Kamel's blog - Thu, 16/08/2007 - 5:33pm
Many people really don't know the difference between interpreted and compiled programming languages. I hope this post may help to the distinguish between those two types. I will start explaining the difference between the compiler and the interpreter, other hybrid models, and finally I will conclude this post by a comparison between both.

Compiler:
A compiler is a program which translates high-level programming language code e.g. C/C++ into some other form "usually Native/Machine code" called the target.

Compilation process involves the following steps:
  1. Scanner (Lexical analyzer) reads your source code character by character, and collect related characters into valid language constructs e.g. variables, reserved words ...etc. These constructs are called Tokens.
  2. Parser (Syntax analyzer) takes the Tokens and checks whether there syntax are correct "defined by Programming Language", and produce parse tree presentation.
  3. Semantical analyzer performs various tests to check whether your code is semantically correct e.g. assignment between variables ...etc. It produces Intermediate representation (IR).
  4. The Intermediate representation (IR) is optimized by Optimizer to improve the output.
  5. and the final step, the optimized Intermediate representation is fed to the Code generator that produces the desired machine code, the output is called Object code/file.
The below animated diagram shows various phases of the compilation process, you might be wondering about the extra step "Linker", didn't I tell you about it!! oh forgive me :) I will tell you why we need this step but after you take a look at the diagram ;) "you may even guess what it does"

The purpose of using a Linker is to link the external resources that you have referenced in your code, mmm like external libraries, other source code files ...etc. The Linker takes the Object code/files and link them to the resources it needs and then produce one big executable file "the one you run e.g. exe, bin ...etc". There are two methods of linking either static or dynamic linking each one of them has its own pros and cons, but this will be the topic of another post..

I am a little bit sleepy, now I have covered how compiler works, my next post will explain how interpreters work and the comparison I promised you between compilers and interpreters. Stay tuned ;)