Invitation to Computer Science 5 th Edition Chapter 11 Compilers and Language Translation.

  • Published on
    26-Dec-2015

  • View
    216

  • Download
    4

Transcript

Slide 1 Invitation to Computer Science 5 th Edition Chapter 11 Compilers and Language Translation Slide 2 Objectives In 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 Invitation to Computer Science, 5th Edition2 Slide 3 Introduction Compiler Translates high-level language into machine language prior to execution Assembly language and machine language are related one to one One to many Relationship between a high-level language and machine language Compiler goals Correctness; efficient and concise Invitation to Computer Science, 5th Edition33 Slide 4 The Compilation Process Four phases of compilation Phase I: Lexical analysis Phase II: Parsing Phase III: Semantic analysis and code generation Phase IV: Code optimization Invitation to Computer Science, 5th Edition44 Slide 5 5 Figure 11.1 General Structure of a Compiler Slide 6 Invitation to Computer Science, 5th Edition6 Figure 11.2 Overall Execution Sequence of a High-level Language Program Slide 7 Invitation to Computer Science, 5th Edition7 Phase I: Lexical Analysis Lexical analyzer Groups input characters into units called tokens Scanner Discards nonessential characters, such as blanks and tabs Groups remaining characters into high-level syntactical units such as symbols, numbers, and operators Slide 8 Invitation to Computer Science, 5th Edition8 Figure 11.3 Typical Token Classifications Slide 9 Phase II: Parsing During the parsing phase: Compiler determines whether the tokens recognized by the scanner during phase I fit together in a grammatically meaningful way Parsing Process of diagramming a high-level language statement Done by a program called a parser Invitation to Computer Science, 5th Edition9 Slide 10 10 Grammars, Languages, and BNF Backus-Naur Form Named after its designers John Backus and Peter Naur Syntax of a language is specified as a set of rules, also called productions Entire collection of rules is called a grammar Lefthand side of a BNF rule is the name of a single grammatical category Operator ::= means is defined as, and definition, also called righthand side Slide 11 Invitation to Computer Science, 5th Edition11 Grammars, Languages, and BNF (continued) Backus-Naur Form Nonterminal: intermediate grammatical category used to help explain and organize the language Goal symbol: final nonterminal Language: collection of all statements that can be successfully parsed Metasymbols:, and ::= Slide 12 Invitation to Computer Science, 5th Edition12 Parsing Concepts and Techniques Parser Receives as input the BNF description of a high- level language and a sequence of tokens recognized by the scanner Look-ahead parsing algorithms Look down the road a few tokens to see what would happen if a certain choice is made Ambiguous Grammar that allows the construction of two or more distinct parse trees for the same statement Slide 13 Invitation to Computer Science, 5th Edition13 Figure 11.4 First Attempt at a Grammar for a Simplified Assignment Statement Slide 14 Invitation to Computer Science, 5th Edition14 Figure 11.5 Parse Tree Produced by the Parser Slide 15 Invitation to Computer Science, 5th Edition15 Figure 11.6 Second Attempt at a Grammar for Assignment Statements Slide 16 Invitation to Computer Science, 5th Edition16 Figure 11.7 Two Parse Trees for the Statement x = x + y + z Slide 17 Invitation to Computer Science, 5th Edition17 Figure 11.8 Third Attempt at a Grammar for Assignment Statements Slide 18 Invitation to Computer Science, 5th Edition18 Figure 11.9 Grammar for a Simplified Version of an if-else Statement Slide 19 Invitation to Computer Science, 5th Edition19 Figure 11.10 Parse Tree for the Statement if (x55y)x5z; else x5y; Slide 20 Phase III: Semantics and Code Generation Semantic record Data structure that stores information about a nonterminal First part of code generation Involves a pass over the parse tree to determine whether all branches of the tree are semantically valid Code generation Compiler must determine how transformation of grammatical objects can be accomplished in machine language Invitation to Computer Science, 5th Edition20 Slide 21 Phase III: Semantics and Code Generation (continued) Optimization Compiler polishes and fine-tunes the translation so that it runs a little faster or occupies a little less memory Invitation to Computer Science, 5th Edition21 Slide 22 Invitation to Computer Science, 5th Edition22 Figure 11.11 Code Generation for the Assignment Statement x = x + y + z Slide 23 Phase IV: Code Optimization Efficiency Ability to write highly optimized programs that contained no wasted microseconds or unnecessary memory cells Goal in compiler design today Provide a wide array of compiler tools to simplify the programmers task and increase productivity Integrated development environment Compiler is embedded within a collection of supporting software development routines Invitation to Computer Science, 5th Edition23 Slide 24 Phase IV: Code Optimization (continued) Two types of optimization Local optimization and global optimization Possible local optimizations Constant evaluation Strength reduction Eliminating unnecessary operations Invitation to Computer Science, 5th Edition24 Slide 25 Invitation to Computer Science, 5th Edition25 Figure 11.12 Optimized Code for the Assignment Statement x = x + y + z Slide 26 Invitation to Computer Science, 5th Edition26 Summary Compiler Piece of system software that translates high-level languages into machine language Goals of a compiler Correctness and the production of efficient and concise code Source program High-level language program Slide 27 Invitation to Computer Science, 5th Edition27 Summary (continued) Object program The machine language translation 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

Recommended

View more >