Welcome to graduate2professional.blogspot.com

Wednesday, May 27, 2009

How does a language compiler work?

=>A compiler for a language generally has several different stages as it
processes the input.

=>They are:
1. Preprocessing
2. Lexical analysis
3. Syntactical analysis
4. Semantical analysis
5. Intermediate code generation
6. Code optimization
7. Code generation


1.Preprocessing:
During the preprocessing stage, comments, macros, and directives are
processed.
Comments are removed from the source file. This greatly simplifies the
later stages.
If the language supports macros, the macros are replaced with the equivalent
text.
The preprocessor may also replace special strings with other characters. In
C and C++, the preprocessor recognizes the \ character as an escape code,
and will replace the escape sequence with a special character. For example
\t is the escape code for a tab, so \t would be replaced at this stage with
a tab character.

2.Lexical analysis:
Lexical analysis is the process of breaking down the source files into
key words, constants, identifiers, operators and other simple tokens. A
token is the smallest piece of text that the language defines.


3.Syntactical analysis:
Syntactical analysis is the process of combining the tokens into
well-formed expressions, statements, and programs. Each language has
specific rules about the structure of a program--called the grammar or
syntax. Just like English grammar, it specifies how things may be put
together. In English, a simple sentence is: subject, verb, predicate

4.Semantic analysis:
Semantic analysis is the process of examining the types and values of the
statements used to make sure they make sense. During the semantic
analysis, the types, values, and other required information about statements
are recorded, checked, and transformed as appropriate to make sure the
program makes sense.

5.Intermediate code generation:
Depending on the compiler, this step may be skipped, and instead the program
may be translated directly into the target language (usually machine object
code). If this step is implemented, the compiler designers also design a
machine independent language of there own that is close to machine language
and easily translated into machine language for any number of different
computers.
The purpose of this step is to allow the compiler writers to support
different target computers and different languages with a minimum of effort.
The part of the compiler which deals with processing the source files,
analyzing the language and generating the intermediate code is called the
front end, while the process of optimizing and converting the intermediate
code into the target language is called the back end.

6. Code optimization
During this process the code generated is analyzed and improved for
efficiency. The compiler analyzes the code to see if improvements can be
made to the intermediate code that couldn't be made earlier. For example,
some languages like Pascal do not allow pointers, while all machine
languages do. When accessing arrays, it is more efficient to use pointers,
so the code optimizer may detect this case and internally use pointers.

7. Code generation
Finally, after the intermediate code has been generated and optimized, the
compiler will generated code for the specific target language. Almost
always this is machine code for a particular target machine.


Also, it us usually not the final machine code, but is instead object code,
which contains all the instructions, but not all of the final memory
addresses have been determined.

A subsequent program, called a linker is used to combine several different object code files into the final executable program.

No comments:

Post a Comment