how a compiler works ?
TRANSCRIPT
How a Compiler How a Compiler Works ?Works ?
Submitted By-Submitted By-
Hirdesh VishwdewaHirdesh Vishwdewa
A very question that, we always come across is how a compiler compiles a program?
What are those stages that a compiler follow for the compilation of a simple program ?
A compiler is a computer program that translates a computer program written in one computer language (called the source language) into an equivalent program written in another computer language (called the output, object, or target language).
Source language
Compiler
(A comp. program)
What actually a compiler is?
object/ target language
Translation from source to Translation from source to objectobject
Preprocessing If a language supports
preprocessor directives(#) then it’ll be the first stage that every program follows before compilation.
Comments, macros, escape sequences are processed in this stage.
Preprocessing1. Comments-these are used to give
some explanation about our coding in a code.
e.g., main( ) //execution of program starts { //from here ! c=a+b; //addition of a & b cout<<c; //output } In this step Comments are removed from
the source file. This step very much simplifies the source
program
Preprocessing
2. Macros #define PI 3.14 main() { cout<<“area=“<<PI*r*r; } In this type of coding the PI will be replace
by 3.14 wherever it exist in whole program.
Preprocessing
3.Escape sequences e.g., cout<<“\t hey ”; here preprocessor recognizes the \ character as an escape code, and will replace the escape sequence with a special character.
Various stages for the compilation of a program.
1. Lexical Analysis2. Syntactical Analysis3. Semantic Analysis4. Intermediate Code
Generation5. Code Optimization6. Code Generation
Lexical AnalysisIt is the process of breaking
down the source code (file) into keywords, constants, identifiers, operators and other simple tokens.
A token is the smallest piece of the text that the language defines.
Lexical analysis of a simple programe.g., if( age >= 18) this code line brakes into tokens as-
if ( age
> = 18 )
Now the compiler will use these tokens for further the stages.
keyword constant
operator
Syntactical analysis
Syntactical analysis is the process of combining the tokens into well-formed expressions, statements, and programs.
In this process compiler checks that if the syntax is correct or not but doesn’t enforce that it make sense.
E.g.,
float x= “Hello dude” ++;
this is syntactically valid but doesn’t make sense because float number can’t have string assigned to it & which can not be incremented.
Syntactical analysis
Semantic analysisIt is the process of examining the
types and values of the statements used to make sure they make sense.
e.g.,
float x= “Hello dude” ++;
floatfloat StringString
Error – Type do not matchError – Type do not match
incremeincrementnt
Semantic analysisSemantic analysisAnother example-
float x= 5 + 3.0;
Integer Integer typetype1. 5 is an integer while 3.0 be a double
therefore through type conversion concept 5 first converts into double, and the expression would then be translated into double, so the addition could be performed.
2.Then compiler would recognize x as float through another conversion.
Intermediate code generation
Depending on the compiler, this step may be skipped, 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.
(optional)
Intermediate code generation
EXISTINGCODE
As in java-As in java-
BYTE BYTE CODESCODES(Intermedia(Intermediate code)te code)
Intermediate code generation
FRONT ENDFRONT END
Code optimizationCode optimizationDuring 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.
E.g., When accessing arrays, it is more
efficient to use pointers, so the code optimizer may detect this case and internally use pointers.
Code GenerationCode Generation
Finally, after the intermediate code has been generated and optimized, the compiler will generated a code, which is not usually a final machine code instead an Object Code.
It 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.
GENERATEDOBJECT CODE
linkerEXECUTABLE PROGRAM
OBJECT CODE
OBJECT CODE
Bibliography
www.google.comwww.botskool.com
Thank you…