Chapter 10: Compilers andLanguage TranslationInvitation to Computer Science,Java Version, Third Edition
Invitation to Computer Science, Java Version, Third Edition 2ObjectivesIn this chapter, you will learn about The compilat...
Invitation to Computer Science, Java Version, Third Edition 3Introduction High-level language instructions must betransla...
Invitation to Computer Science, Java Version, Third Edition 4Introduction (continued) Goals of a compiler when performing...
Invitation to Computer Science, Java Version, Third Edition 5Figure 10.1General Structure of a Compiler
Invitation to Computer Science, Java Version, Third Edition 6The Compilation Process Phase I: Lexical analysis Compiler ...
Invitation to Computer Science, Java Version, Third Edition 7The Compilation Process (continued) Phase III: Semantic anal...
Invitation to Computer Science, Java Version, Third Edition 8Figure 10.2Overall Execution Sequence on a High-Level Languag...
Invitation to Computer Science, Java Version, Third Edition 9The Compilation Process (continued) Final step Object progr...
Invitation to Computer Science, Java Version, Third Edition 10Phase I: Lexical Analysis Lexical analyzer The program tha...
Invitation to Computer Science, Java Version, Third Edition 11Figure 10.3Typical Token Classifications
Invitation to Computer Science, Java Version, Third Edition 12Phase I: Lexical Analysis (continued) Input to a scanner A...
Invitation to Computer Science, Java Version, Third Edition 13Phase II: ParsingIntroduction Parsing phase A compiler det...
Invitation to Computer Science, Java Version, Third Edition 14Phase II: ParsingIntroduction (continued) Output of a parse...
Invitation to Computer Science, Java Version, Third Edition 15 Example High-level language statement: a = b + c
Invitation to Computer Science, Java Version, Third Edition 16Grammars, Languages, and BNF Syntax The grammatical struct...
Invitation to Computer Science, Java Version, Third Edition 17Grammars, Languages, and BNF(continued) In BNF The syntax ...
Invitation to Computer Science, Java Version, Third Edition 18Grammars, Languages, and BNF(continued) BNF rules use two t...
Invitation to Computer Science, Java Version, Third Edition 19Grammars, Languages, and BNF(continued) Goal symbol The hi...
Invitation to Computer Science, Java Version, Third Edition 20Parsing Concepts and Techniques Fundamental rule of parsing...
Invitation to Computer Science, Java Version, Third Edition 21Parsing Concepts and Techniques(continued) One of the bigge...
Invitation to Computer Science, Java Version, Third Edition 22Parsing Concepts and Techniques(continued) Another problem ...
Invitation to Computer Science, Java Version, Third Edition 23Phase III: Semantics and CodeGeneration Semantic analysis ...
Invitation to Computer Science, Java Version, Third Edition 24Phase III: Semantics and CodeGeneration (continued) Code ge...
Invitation to Computer Science, Java Version, Third Edition 25Phase IV: Code Optimization Two types of optimization Loca...
Invitation to Computer Science, Java Version, Third Edition 26Phase IV: Code Optimization(continued) Examples of possible...
Invitation to Computer Science, Java Version, Third Edition 27Phase IV: Code Optimization(continued) Global optimization...
Invitation to Computer Science, Java Version, Third Edition 28Summary A compiler is a piece of system software thattransl...
Invitation to Computer Science, Java Version, Third Edition 29Summary (continued) Object program: The machine languagetra...
Upcoming SlideShare
Loading in …5
×

Machine language

692 views

Published on

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
692
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
38
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Machine language

  1. 1. Chapter 10: Compilers andLanguage TranslationInvitation to Computer Science,Java Version, Third Edition
  2. 2. Invitation to Computer Science, Java Version, Third Edition 2ObjectivesIn this chapter, you will learn about The compilation process Phase I: Lexical analysis Phase II: Parsing Phase III: Semantics and code generation Phase IV: Code optimization
  3. 3. Invitation to Computer Science, Java Version, Third Edition 3Introduction High-level language instructions must betranslated into machine language prior toexecution Compiler A piece of system software that translates high-level languages into machine language
  4. 4. Invitation to Computer Science, Java Version, Third Edition 4Introduction (continued) Goals of a compiler when performing atranslation Correctness Producing a reasonably efficient and concisemachine language code
  5. 5. Invitation to Computer Science, Java Version, Third Edition 5Figure 10.1General Structure of a Compiler
  6. 6. Invitation to Computer Science, Java Version, Third Edition 6The Compilation Process Phase I: Lexical analysis Compiler examines the individual characters inthe source program and groups them intosyntactical units called tokens Phase II: Parsing The sequence of tokens formed by the scanner ischecked to see whether it is syntactically correct
  7. 7. Invitation to Computer Science, Java Version, Third Edition 7The Compilation Process (continued) Phase III: Semantic analysis and codegeneration The compiler analyzes the meaning of the high-level language statement and generates themachine language instructions to carry out theseactions Phase IV: Code optimization The compiler takes the generated code and seeswhether it can be made more efficient
  8. 8. Invitation to Computer Science, Java Version, Third Edition 8Figure 10.2Overall Execution Sequence on a High-Level Language Program
  9. 9. Invitation to Computer Science, Java Version, Third Edition 9The Compilation Process (continued) Final step Object program is written to an object file Source program Original high-level language program Object program Machine language translation of the sourceprogram
  10. 10. Invitation to Computer Science, Java Version, Third Edition 10Phase I: Lexical Analysis Lexical analyzer The program that performs lexical analysis More commonly called a scanner Job of lexical analyzer Group input characters into tokens Tokens: Syntactical units that are treated as single,indivisible entities for the purposes of translation Classify tokens according to their type
  11. 11. Invitation to Computer Science, Java Version, Third Edition 11Figure 10.3Typical Token Classifications
  12. 12. Invitation to Computer Science, Java Version, Third Edition 12Phase I: Lexical Analysis (continued) Input to a scanner A high-level language statement from the sourceprogram Scanner’s output A list of all the tokens in that statement The classification number of each token found
  13. 13. Invitation to Computer Science, Java Version, Third Edition 13Phase II: ParsingIntroduction Parsing phase A compiler determines whether the tokensrecognized by the scanner are a syntacticallylegal statement Performed by a parser
  14. 14. Invitation to Computer Science, Java Version, Third Edition 14Phase II: ParsingIntroduction (continued) Output of a parser A parse tree, if such a tree exists An error message, if a parse tree cannot beconstructed Successful construction of a parse tree is proofthat the statement is correctly formed
  15. 15. Invitation to Computer Science, Java Version, Third Edition 15 Example High-level language statement: a = b + c
  16. 16. Invitation to Computer Science, Java Version, Third Edition 16Grammars, Languages, and BNF Syntax The grammatical structure of the language The parser must be given the syntax of thelanguage BNF (Backus-Naur Form) Most widely used notation for representing thesyntax of a programming language
  17. 17. Invitation to Computer Science, Java Version, Third Edition 17Grammars, Languages, and BNF(continued) In BNF The syntax of a language is specified as a set ofrules (also called productions) A grammar The entire collection of rules for a language Structure of an individual BNF ruleleft-hand side ::= “definition”
  18. 18. Invitation to Computer Science, Java Version, Third Edition 18Grammars, Languages, and BNF(continued) BNF rules use two types of objects on the right-hand side of a production Terminals The actual tokens of the language Never appear on the left-hand side of a BNF rule Nonterminals Intermediate grammatical categories used to helpexplain and organize the language Must appear on the left-hand side of one or morerules
  19. 19. Invitation to Computer Science, Java Version, Third Edition 19Grammars, Languages, and BNF(continued) Goal symbol The highest-level nonterminal The nonterminal object that the parser is trying toproduce as it builds the parse tree All nonterminals are written inside anglebrackets
  20. 20. Invitation to Computer Science, Java Version, Third Edition 20Parsing Concepts and Techniques Fundamental rule of parsing By repeated applications of the rules of thegrammar If the parser can convert the sequence of inputtokens into the goal symbol, the sequence of tokensis a syntactically valid statement of the language If the parser cannot convert the input tokens intothe goal symbol, the sequence of tokens is not asyntactically valid statement of the language
  21. 21. Invitation to Computer Science, Java Version, Third Edition 21Parsing Concepts and Techniques(continued) One of the biggest problems in building acompiler is designing a grammar that Includes every valid statement that we want to bein the language Excludes every invalid statement that we do notwant to be in the language
  22. 22. Invitation to Computer Science, Java Version, Third Edition 22Parsing Concepts and Techniques(continued) Another problem in constructing a compiler:Designing a grammar that is not ambiguous An ambiguous grammar allows the construction oftwo or more distinct parse trees for the samestatement
  23. 23. Invitation to Computer Science, Java Version, Third Edition 23Phase III: Semantics and CodeGeneration Semantic analysis The compiler makes a first pass over the parsetree to determine whether all branches of the treeare semantically valid If they are valid, the compiler can generate machinelanguage instructions If not, there is a semantic error; machine languageinstructions are not generated
  24. 24. Invitation to Computer Science, Java Version, Third Edition 24Phase III: Semantics and CodeGeneration (continued) Code generation Compiler makes a second pass over the parsetree to produce the translated code
  25. 25. Invitation to Computer Science, Java Version, Third Edition 25Phase IV: Code Optimization Two types of optimization Local Global Local optimization The compiler looks at a very small block ofinstructions and tries to determine how it canimprove the efficiency of this local code block Relatively easy; included as part of mostcompilers
  26. 26. Invitation to Computer Science, Java Version, Third Edition 26Phase IV: Code Optimization(continued) Examples of possible local optimizations Constant evaluation Strength reduction Eliminating unnecessary operations
  27. 27. Invitation to Computer Science, Java Version, Third Edition 27Phase IV: Code Optimization(continued) Global optimization The compiler looks at large segments of theprogram to decide how to improve performance Much more difficult; usually omitted from all butthe most sophisticated and expensive production-level “optimizing compilers” Optimization cannot make an inefficientalgorithm efficient
  28. 28. Invitation to Computer Science, Java Version, Third Edition 28Summary A compiler is a piece of system software thattranslates high-level languages into machinelanguage Goals of a compiler: Correctness and theproduction of efficient and concise code Source program: High-level language program
  29. 29. Invitation to Computer Science, Java Version, Third Edition 29Summary (continued) Object program: The machine languagetranslation of the source program Phases of the compilation process Phase I: Lexical analysis Phase II: Parsing Phase III: Semantic analysis and code generation Phase IV: Code optimization

×