how a compiler works ?

22
How a Compiler Works How a Compiler Works ? ? Submitted By- Submitted By- Hirdesh Hirdesh Vishwdewa Vishwdewa

Upload: hirdesh-vishwdewa

Post on 06-May-2015

2.128 views

Category:

Education


2 download

TRANSCRIPT

Page 1: How a Compiler Works ?

How a Compiler How a Compiler Works ?Works ?

Submitted By-Submitted By-

Hirdesh VishwdewaHirdesh Vishwdewa

Page 2: How a Compiler Works ?

A very question that, we always come across is how a compiler compiles a program?

Page 3: How a Compiler Works ?

What are those stages that a compiler follow for the compilation of a simple program ?

Page 4: How a Compiler Works ?

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

Page 5: How a Compiler Works ?

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.

Page 6: How a Compiler Works ?

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

Page 7: How a Compiler Works ?

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.

Page 8: How a Compiler Works ?

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.

Page 9: How a Compiler Works ?

Various stages for the compilation of a program.

1. Lexical Analysis2. Syntactical Analysis3. Semantic Analysis4. Intermediate Code

Generation5. Code Optimization6. Code Generation

Page 10: How a Compiler Works ?

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.

Page 11: How a Compiler Works ?

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

Page 12: How a Compiler Works ?

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.

Page 13: How a Compiler Works ?

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

Page 14: How a Compiler Works ?

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

Page 15: How a Compiler Works ?

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.

Page 16: How a Compiler Works ?

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)

Page 17: How a Compiler Works ?

Intermediate code generation

EXISTINGCODE

As in java-As in java-

BYTE BYTE CODESCODES(Intermedia(Intermediate code)te code)

Intermediate code generation

FRONT ENDFRONT END

Page 18: How a Compiler Works ?

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.

Page 19: How a Compiler Works ?

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.

Page 20: How a Compiler Works ?

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

Page 21: How a Compiler Works ?

Bibliography

www.google.comwww.botskool.com

Page 22: How a Compiler Works ?

Thank you…