Principles of compiler design

4,568 views
4,256 views

Published on

Published in: Technology
1 Comment
3 Likes
Statistics
Notes
No Downloads
Views
Total views
4,568
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
294
Comments
1
Likes
3
Embeds 0
No embeds

No notes for slide

Principles of compiler design

  1. 1. MODULE CODE:055CS603 MODULE NAME: PRINCIPLES OF COMPILER DESIGN PRESENTED BY: A.ALMA CANDIDA , B.Sc .,MCA., January 21, 2012 A.Alma Candida B.Sc.,MCA., DMI St.Eugene University
  2. 2. TOPICS TO BE COVERED <ul><li>DEFINITION OF COMPLIER </li></ul><ul><li>LANGUAGE PROCESSING SYSTEMS </li></ul><ul><li>PHASES OF COMPILER </li></ul>January 21, 2012 A.Alma Candida B.Sc.,MCA., DMI St.Eugene University
  3. 3. COMPILER <ul><li>Translates one language to another language. </li></ul><ul><li>Role - report any errors in the source program. </li></ul>January 21, 2012 A.Alma Candida B.Sc.,MCA., DMI St.Eugene University Compiler Source Program Target Program
  4. 4. Interpreter <ul><li>Which convert from one language to another. </li></ul><ul><li>Convert line by line. </li></ul><ul><li>Error will be displayed at the time of conversion. </li></ul>January 21, 2012 A.Alma Candida B.Sc.,MCA., DMI St.Eugene University
  5. 5. Language Processing Systems <ul><ul><ul><ul><li>Preprocessor </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Compiler </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Assembler </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Linker </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Loader </li></ul></ul></ul></ul>January 21, 2012 A.Alma Candida B.Sc.,MCA., DMI St.Eugene University
  6. 6. Steps for Language Processing System January 21, 2012 A.Alma Candida B.Sc.,MCA., DMI St.Eugene University
  7. 7. Pre Processor <ul><li>modules are stored in separate files. </li></ul><ul><li>source program is collected by a separate program. </li></ul>January 21, 2012 A.Alma Candida B.Sc.,MCA., DMI St.Eugene University
  8. 8. Compiler <ul><li>Source program is fed to a compiler. </li></ul><ul><li>Output- assembly-language program . </li></ul><ul><li>Assembly language is easier to produce as output. </li></ul><ul><li>easier to debug. </li></ul>January 21, 2012 A.Alma Candida B.Sc.,MCA., DMI St.Eugene University
  9. 9. Assembler <ul><li>Assembly language into relocatable machine code. </li></ul><ul><li>Large programs are compiled into small modules. </li></ul><ul><li>The relocatable machine code have to be linked together with other relocatable object files and library files . </li></ul><ul><li>This code actually runs on the machine. </li></ul>January 21, 2012 A.Alma Candida B.Sc.,MCA., DMI St.Eugene University
  10. 10. Linker <ul><li>Resolves external memory addresses, </li></ul><ul><li>The code in one file may refer to a location in another file. </li></ul>January 21, 2012 A.Alma Candida B.Sc.,MCA., DMI St.Eugene University
  11. 11. Loader <ul><li>Puts together all of the executable object files into memory for execution. </li></ul>January 21, 2012 A.Alma Candida B.Sc.,MCA., DMI St.Eugene University
  12. 12. Phases Of Compiler <ul><li>There are six different phases available in the compiler </li></ul><ul><ul><ul><ul><ul><li>Lexical Analysis </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>Syntax Analysis </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>Semantic Analysis </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>Intermediate Code Generation </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>Code Optimization </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>Code Generation </li></ul></ul></ul></ul></ul>January 21, 2012 A.Alma Candida B.Sc.,MCA., DMI St.Eugene University
  13. 13. Symbol-Table Management <ul><li>Data structure containing a record for each variable name, with fields for the attributes of the name. </li></ul><ul><li>Designed to allow the compiler </li></ul><ul><li>Find the record for each name quickly and to store or retrieve data from that record quickly. </li></ul>January 21, 2012 A.Alma Candida B.Sc.,MCA., DMI St.Eugene University
  14. 14. Lexical Analysis <ul><li>This is also called as Scanning . </li></ul><ul><li>It collects sequences of characters into meaningful units called tokens. </li></ul><ul><li>The text is read and divided into tokens. </li></ul><ul><li>Lexemes </li></ul><ul><li>Reads the stream of characters </li></ul><ul><li>Groups the characters into meaningful sequences called lexemes . </li></ul><ul><li>The output will be in the form of tokens. </li></ul>January 21, 2012 A.Alma Candida B.Sc.,MCA., DMI St.Eugene University
  15. 15. Lexical Analysis (cont..) <ul><li>General Form: </li></ul><ul><li>(token-name, attribute-value) </li></ul><ul><li>token-name -> an abstract symbol </li></ul><ul><li>attribute-value -> entry in the symbol table for this token. </li></ul>January 21, 2012 A.Alma Candida B.Sc.,MCA., DMI St.Eugene University
  16. 16. Example of Lexical Analysis <ul><li>position = initial + rate * 60 </li></ul><ul><li><id,1><=> <id,2><+><id,3><*><60> </li></ul><ul><li>Position - mapped into a token (id, 1) </li></ul><ul><ul><ul><ul><li>id ->identifier and 1 points to the symbol- table </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Assignment symbol = mapped into the token (=). </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Initial - mapped into the token (id, 2), </li></ul></ul></ul></ul><ul><ul><ul><ul><li>+ - mapped into the token (+). </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Rate mapped into the token (id, 3) </li></ul></ul></ul></ul><ul><ul><ul><ul><li>* mapped into the token (*) . </li></ul></ul></ul></ul><ul><ul><ul><ul><li>60 mapped into the token (60) </li></ul></ul></ul></ul>January 21, 2012 A.Alma Candida B.Sc.,MCA., DMI St.Eugene University
  17. 17. Syntax Analysis <ul><li>This is also called as Parsing . </li></ul><ul><li>Arrange tokens form of Tree called Syntax Tree . </li></ul><ul><li>Interior node ->operation </li></ul><ul><li>Child node ->Arguments of the operations. </li></ul><ul><li>Output -> Tree. </li></ul>January 21, 2012 A.Alma Candida B.Sc.,MCA., DMI St.Eugene University
  18. 18. Syntax Analysis(cont..) <ul><li>Consider the example </li></ul><ul><ul><ul><li>position = initial + rate * 60 </li></ul></ul></ul><ul><ul><ul><li>The Syntax tree is in the form of </li></ul></ul></ul>January 21, 2012 A.Alma Candida B.Sc.,MCA., DMI St.Eugene University
  19. 19. January 21, 2012 A.Alma Candida B.Sc.,MCA., DMI St.Eugene University
  20. 20. Semantic Analysis <ul><li>Convert from one data type to another. </li></ul><ul><li>Example: </li></ul><ul><ul><ul><li>position=initial + rate*60 </li></ul></ul></ul><ul><ul><li>position, initial, and rate have been declared to be floating-point numbers, and that the lexeme 60 by itself forms an integer. </li></ul></ul><ul><ul><li>the integer may be converted into a floating-point number. </li></ul></ul><ul><ul><li>the output -> has an extra node for the operator inttofloat , which explicitly converts its integer argument into a floating-point number. </li></ul></ul>January 21, 2012 A.Alma Candida B.Sc.,MCA., DMI St.Eugene University
  21. 21. Intermediate Code Generation <ul><li>translated to a simple machine-independent intermediate language. </li></ul><ul><li>Convert into low level language </li></ul><ul><li>The output -> three-address code sequence. </li></ul><ul><li>Example: </li></ul><ul><li>tl = inttofloat (60) </li></ul><ul><li>t2 = id3 * tl </li></ul><ul><li>t3 = id2 + t2 </li></ul><ul><li>id1 = t3 </li></ul>January 21, 2012 A.Alma Candida B.Sc.,MCA., DMI St.Eugene University
  22. 22. Code Optimization <ul><li>Attempts to improve the intermediate code so that better target code will result. </li></ul><ul><li>Its is in the form of shorter sequence. </li></ul><ul><ul><ul><li>t1=id3*60.0 </li></ul></ul></ul><ul><ul><ul><li>id1=id2+t1 </li></ul></ul></ul>January 21, 2012 A.Alma Candida B.Sc.,MCA., DMI St.Eugene University
  23. 23. CODE GENERATION <ul><li>Input an intermediate representation of the source program and maps it into the target language. </li></ul><ul><li>Target language is machine code, registers or memory locations are selected for each of the variables used by the program. </li></ul><ul><li>Registers R1 and R2, the intermediate translated into the machine code </li></ul><ul><li>LDF R2, id3 </li></ul><ul><li>MULF R2, R2, #60.0 </li></ul><ul><li>LDF Rl, id2 </li></ul><ul><li>ADDF Rl, Rl, R2 </li></ul><ul><li>STF idl, Rl </li></ul>January 21, 2012 A.Alma Candida B.Sc.,MCA., DMI St.Eugene University
  24. 24. PHASES OF THE COMPILER January 21, 2012 A.Alma Candida B.Sc.,MCA., DMI St.Eugene University
  25. 25. January 21, 2012 A.Alma Candida B.Sc.,MCA., DMI St.Eugene University

×