introduction to compiling
play

Introduction to Compiling Chapter 1 1 Compiler Construction - PowerPoint PPT Presentation

Introduction to Compiling Chapter 1 1 Compiler Construction Introduction to Compiling To Do Read Chapter 1 of the Dragon book Begin work on Assignment 1 2 Compiler Construction Introduction to Compiling What is a compiler? A


  1. Introduction to Compiling Chapter 1 1 Compiler Construction Introduction to Compiling

  2. To Do • Read Chapter 1 of the Dragon book • Begin work on Assignment 1 2 Compiler Construction Introduction to Compiling

  3. What is a compiler? • A program that reads a program written in one language (source) and translates it into an equivalent program in another language (target) • Error messages are generated if necessary Source program Target program Compiler Error messages 3 Compiler Construction Introduction to Compiling

  4. Analysis-Synthesis Model Compilation has two parts • Analysis: source code − → intermediate representation • Synthesis: intermediate representation − → target code = position + position = initial + rate * 60; initial * rate 60 4 Compiler Construction Introduction to Compiling

  5. Analysis = • The intermediate representation is usually in the form of a tree position + • This tree can be traversed initial * • Analysis of various types can be performed during this traversal rate 60 Type checking Identifier Offset Type position int 0 initial float 4 rate int 8 5 Compiler Construction Introduction to Compiling

  6. Other Tools that Perform Analysis • Structure editors • Pretty printers • Static checkers • Interpreters 6 Compiler Construction Introduction to Compiling

  7. Compiler Context The compiler interacts Source code with other programs in Preprocessor a complete language processing system Embellished source code Compiler Target assembly program Assembler Relocatable machine code Library and relocatable Loader/Linker object files Absolute machine code 7 Compiler Construction Introduction to Compiling

  8. Types of Analysis • Lexical analysis • Syntax analysis • Semantic analysis 8 Compiler Construction Introduction to Compiling

  9. Lexical Analysis • Also called scanning • Input stream: position = initial + rate * 60; • Token stream: 1. Identifier token position 2. Assignment token = 3. Identifier token initial 4. Plus token + 5. Identifier token rate 6. Multiplication token * 7. Number token 60 8. Semicolon token ; 9 Compiler Construction Introduction to Compiling

  10. Syntax Analysis • Also called parsing • Constructs a parse tree position = initial + rate * 60; assignment statement identifier = expression ; position expression + expression identifier expression expression * identifier number initial 60 rate 10 Compiler Construction Introduction to Compiling

  11. Rules 1. identifier = expression ; assignment statement − → 2. expression + expression expression − → 3. expression * expression expression − → 4. expression identifier − → 5. expression number − → 6. letter letter * identifier − → 7. letter a | b | c | d | e | f | g | h | i | − → j | k | l | m | n | o | p | q | r | s | t | u | v | w | x | y |z 8. digit digit * number − → 9. digit 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 − → position = initial + rate * 60; assignment statement identifier = expression ; position expression + expression identifier expression expression * identifier number initial 60 rate 11 Compiler Construction Introduction to Compiling

  12. Rules • Observe that the rules are recursive • Lexical constructs do not require recursion – Regular expressions – Identifying identifiers • Syntactic constructs often do require recursion – Context-free grammars – Expressions 12 Compiler Construction Introduction to Compiling

  13. Semantic Analysis • Type checking • Elements of the parse tree are “decorated” with additional information • A syntactically legal statement may be semantically illegal • What is the meaning of position ? position = initial + rate * 60; 13 Compiler Construction Introduction to Compiling

  14. Phases of Compiling Source program Lexical analyzer Tokens Syntax analyzer Abstract syntax tree Semantic analyzer Symbol table manager Error handler Abstract syntax tree Intermediate code generator Three−address code Code optimizer Three−address code Code generator Target program 14 Compiler Construction Introduction to Compiling

  15. Front End vs. Back End Source program Front end Lexical analyzer Tokens Syntax analyzer Abstract syntax tree Semantic analyzer Symbol table manager Error handler Abstract syntax tree Intermediate code generator Three−address code Code optimizer Three−address code Back end Code generator Target program 15 Compiler Construction Introduction to Compiling

  16. Front End vs. Back End Source program • Front end Front end Lexical analyzer Tokens Syntax analyzer – Target language independent Abstract syntax tree Semantic analyzer – Machine-independent Symbol table manager Error handler Abstract syntax tree Intermediate code generator – Can be used for multiple Three−address code Code optimizer platforms Three−address code Back end Code generator Target program • Back end – Source language independent – Machine dependent – Can be used for multiple languages 16 Compiler Construction Introduction to Compiling

Recommend


More recommend