How a Compiler Works ?


Published on

Published in: Education, Technology
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

How a Compiler Works ?

  1. 1. How a Compiler Works ? Submitted By- Hirdesh Vishwdewa
  2. 2. <ul><li>A very question that, we always come across is how a compiler compiles a program? </li></ul>
  3. 3. <ul><li>What are those stages that a compiler follow for the compilation of a simple program ? </li></ul>
  4. 4. <ul><ul><li>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 ). </li></ul></ul>What actually a compiler is? Translation from source to object Source language Compiler (A comp. program) object/ target language
  5. 5. Preprocessing <ul><li>If a language supports preprocessor directives( # ) then it’ll be the first stage that every program follows before compilation. </li></ul><ul><li>Comments, macros, escape sequences are processed in this stage. </li></ul>
  6. 6. Preprocessing <ul><li>Comments- these are used to give some explanation about our coding in a code. </li></ul><ul><li>e.g., main( ) // execution of program starts </li></ul><ul><li>{ // from here ! </li></ul><ul><li>c=a+b; // addition of a & b </li></ul><ul><li>cout<<c; // output </li></ul><ul><li>} </li></ul><ul><li>In this step Comments are removed from the source file. </li></ul><ul><li>This step very much simplifies the source program </li></ul>
  7. 7. Preprocessing <ul><li>2. Macros </li></ul><ul><li>#define PI 3.14 </li></ul><ul><li>main() </li></ul><ul><li>{ </li></ul><ul><li>cout<<“area=“<< PI *r*r; </li></ul><ul><li>} </li></ul><ul><li>In this type of coding the PI will be replace by 3.14 wherever it exist in whole program. </li></ul>
  8. 8. Preprocessing <ul><li>3. Escape sequences </li></ul><ul><li>e.g., </li></ul><ul><li>cout<<“ t hey ”; </li></ul><ul><li>here preprocessor recognizes the character as an escape code, and will replace the escape sequence with a special character. </li></ul>
  9. 9. Various stages for the compilation of a program. <ul><li>Lexical Analysis </li></ul><ul><li>Syntactical Analysis </li></ul><ul><li>Semantic Analysis </li></ul><ul><li>Intermediate Code Generation </li></ul><ul><li>Code Optimization </li></ul><ul><li>Code Generation </li></ul>
  10. 10. Lexical Analysis <ul><li>It is the process of breaking down the source code (file) into keywords , constants , identifiers , operators and other simple tokens . </li></ul><ul><li>A token is the smallest piece of the text that the language defines. </li></ul>
  11. 11. Lexical analysis of a simple program <ul><li>e.g., </li></ul><ul><li>if( age >= 18) </li></ul><ul><li>this code line brakes into tokens as- </li></ul>Now the compiler will use these tokens for further the stages. keyword constant operator ) 18 = > age ( if
  12. 12. Syntactical analysis <ul><li>  Syntactical analysis is the process of combining the tokens into well-formed expressions, statements, and programs. </li></ul><ul><li>In this process compiler checks that if the syntax is correct or not but doesn’t enforce that it make sense. </li></ul>
  13. 13. <ul><li>E.g., </li></ul><ul><li>float x= “ Hello dude ” ++ ; </li></ul><ul><li>this is syntactically valid but doesn’t make sense because float number can’t have string assigned to it & which can not be incremented. </li></ul>Syntactical analysis
  14. 14. Semantic analysis <ul><li>It is the process of examining the types and values of the statements used to make sure they make sense. </li></ul><ul><li>e.g., </li></ul><ul><li>float x= “ Hello dude ” ++ ; </li></ul>float String Error – Type do not match increment
  15. 15. Semantic analysis <ul><li>Another example- </li></ul><ul><li>float x= 5 + 3.0; </li></ul>Integer type 1. 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.
  16. 16. Intermediate code generation <ul><li>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. </li></ul><ul><li>The purpose of this step is to allow the compiler writers to support different target computers and different languages with a minimum of effort. </li></ul>(optional)
  17. 17. Intermediate code generation As in java- Intermediate code generation FRONT END EXISTING CODE BYTE CODES (Intermediate code)
  18. 18. Code optimization <ul><li>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.  </li></ul><ul><li>E.g., </li></ul><ul><li>When accessing arrays, it is more efficient to use pointers, so the code optimizer may detect this case and internally use pointers. </li></ul>
  19. 19. Code Generation <ul><li>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 . </li></ul><ul><li>It contains all the instructions, but not all of the final memory addresses have been determined. </li></ul>
  20. 20. <ul><li>A subsequent program, called a linker is used to combine several different object code files into the final executable program. </li></ul>linker GENERATED OBJECT CODE EXECUTABLE PROGRAM OBJECT CODE OBJECT CODE
  21. 21. Bibliography <ul><li> </li></ul><ul><li> </li></ul>
  22. 22. <ul><li>Thank you… </li></ul>