chapter 10: compilers and language translation
DESCRIPTION
Chapter 10: Compilers and Language Translation. Invitation to Computer Science, Java Version, Third Edition. Objectives. In this chapter, you will learn about The compilation process Phase I: Lexical analysis Phase II: Parsing Phase III: Semantics and code generation - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Chapter 10: Compilers and Language Translation](https://reader034.vdocument.in/reader034/viewer/2022042703/56815fdd550346895dcee67f/html5/thumbnails/1.jpg)
Chapter 10: Compilers and Language Translation
Invitation to Computer Science,Java Version, Third Edition
![Page 2: Chapter 10: Compilers and Language Translation](https://reader034.vdocument.in/reader034/viewer/2022042703/56815fdd550346895dcee67f/html5/thumbnails/2.jpg)
Invitation to Computer Science, Java Version, Third Edition 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
![Page 3: Chapter 10: Compilers and Language Translation](https://reader034.vdocument.in/reader034/viewer/2022042703/56815fdd550346895dcee67f/html5/thumbnails/3.jpg)
Invitation to Computer Science, Java Version, Third Edition 3
Introduction
High-level language instructions must be translated into machine language prior to execution
Compiler
A piece of system software that translates high-level languages into machine language
![Page 4: Chapter 10: Compilers and Language Translation](https://reader034.vdocument.in/reader034/viewer/2022042703/56815fdd550346895dcee67f/html5/thumbnails/4.jpg)
Invitation to Computer Science, Java Version, Third Edition 4
Introduction (continued)
Goals of a compiler when performing a translation
Correctness
Producing a reasonably efficient and concise machine language code
![Page 5: Chapter 10: Compilers and Language Translation](https://reader034.vdocument.in/reader034/viewer/2022042703/56815fdd550346895dcee67f/html5/thumbnails/5.jpg)
Invitation to Computer Science, Java Version, Third Edition 5
Figure 10.1General Structure of a Compiler
![Page 6: Chapter 10: Compilers and Language Translation](https://reader034.vdocument.in/reader034/viewer/2022042703/56815fdd550346895dcee67f/html5/thumbnails/6.jpg)
Invitation to Computer Science, Java Version, Third Edition 6
The Compilation Process
Phase I: Lexical analysis
Compiler examines the individual characters in the source program and groups them into syntactical units called tokens
Phase II: Parsing
The sequence of tokens formed by the scanner is checked to see whether it is syntactically correct
![Page 7: Chapter 10: Compilers and Language Translation](https://reader034.vdocument.in/reader034/viewer/2022042703/56815fdd550346895dcee67f/html5/thumbnails/7.jpg)
Invitation to Computer Science, Java Version, Third Edition 7
The Compilation Process (continued) Phase III: Semantic analysis and code
generation The compiler analyzes the meaning of the high-
level language statement and generates the machine language instructions to carry out these actions
Phase IV: Code optimization The compiler takes the generated code and sees
whether it can be made more efficient
![Page 8: Chapter 10: Compilers and Language Translation](https://reader034.vdocument.in/reader034/viewer/2022042703/56815fdd550346895dcee67f/html5/thumbnails/8.jpg)
Invitation to Computer Science, Java Version, Third Edition 8
Figure 10.2Overall Execution Sequence on a High-Level Language Program
![Page 9: Chapter 10: Compilers and Language Translation](https://reader034.vdocument.in/reader034/viewer/2022042703/56815fdd550346895dcee67f/html5/thumbnails/9.jpg)
Invitation to Computer Science, Java Version, Third Edition 9
The 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 source program
![Page 10: Chapter 10: Compilers and Language Translation](https://reader034.vdocument.in/reader034/viewer/2022042703/56815fdd550346895dcee67f/html5/thumbnails/10.jpg)
Invitation to Computer Science, Java Version, Third Edition 10
Phase 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
![Page 11: Chapter 10: Compilers and Language Translation](https://reader034.vdocument.in/reader034/viewer/2022042703/56815fdd550346895dcee67f/html5/thumbnails/11.jpg)
Invitation to Computer Science, Java Version, Third Edition 11
Figure 10.3Typical Token Classifications
![Page 12: Chapter 10: Compilers and Language Translation](https://reader034.vdocument.in/reader034/viewer/2022042703/56815fdd550346895dcee67f/html5/thumbnails/12.jpg)
Invitation to Computer Science, Java Version, Third Edition 12
Phase I: Lexical Analysis (continued) Input to a scanner
A high-level language statement from the source program
Scanner’s output
A list of all the tokens in that statement
The classification number of each token found
![Page 13: Chapter 10: Compilers and Language Translation](https://reader034.vdocument.in/reader034/viewer/2022042703/56815fdd550346895dcee67f/html5/thumbnails/13.jpg)
Invitation to Computer Science, Java Version, Third Edition 13
Phase II: Parsing Introduction Parsing phase
A compiler determines whether the tokens recognized by the scanner are a syntactically legal statement
Performed by a parser
![Page 14: Chapter 10: Compilers and Language Translation](https://reader034.vdocument.in/reader034/viewer/2022042703/56815fdd550346895dcee67f/html5/thumbnails/14.jpg)
Invitation to Computer Science, Java Version, Third Edition 14
Phase II: Parsing Introduction (continued) Output of a parser
A parse tree, if such a tree exists
An error message, if a parse tree cannot be constructed
Successful construction of a parse tree is proof that the statement is correctly formed
![Page 15: Chapter 10: Compilers and Language Translation](https://reader034.vdocument.in/reader034/viewer/2022042703/56815fdd550346895dcee67f/html5/thumbnails/15.jpg)
Invitation to Computer Science, Java Version, Third Edition 15
Example High-level language statement: a = b + c
![Page 16: Chapter 10: Compilers and Language Translation](https://reader034.vdocument.in/reader034/viewer/2022042703/56815fdd550346895dcee67f/html5/thumbnails/16.jpg)
Invitation to Computer Science, Java Version, Third Edition 16
Grammars, Languages, and BNF Syntax
The grammatical structure of the language
The parser must be given the syntax of the language
BNF (Backus-Naur Form)
Most widely used notation for representing the syntax of a programming language
![Page 17: Chapter 10: Compilers and Language Translation](https://reader034.vdocument.in/reader034/viewer/2022042703/56815fdd550346895dcee67f/html5/thumbnails/17.jpg)
Invitation to Computer Science, Java Version, Third Edition 17
Grammars, Languages, and BNF (continued) In BNF
The syntax of a language is specified as a set of rules (also called productions)
A grammar The entire collection of rules for a language
Structure of an individual BNF rule
left-hand side ::= “definition”
![Page 18: Chapter 10: Compilers and Language Translation](https://reader034.vdocument.in/reader034/viewer/2022042703/56815fdd550346895dcee67f/html5/thumbnails/18.jpg)
Invitation to Computer Science, Java Version, Third Edition 18
Grammars, 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 help
explain and organize the language Must appear on the left-hand side of one or more
rules
![Page 19: Chapter 10: Compilers and Language Translation](https://reader034.vdocument.in/reader034/viewer/2022042703/56815fdd550346895dcee67f/html5/thumbnails/19.jpg)
Invitation to Computer Science, Java Version, Third Edition 19
Grammars, Languages, and BNF (continued) Goal symbol
The highest-level nonterminal
The nonterminal object that the parser is trying to produce as it builds the parse tree
All nonterminals are written inside angle brackets
![Page 20: Chapter 10: Compilers and Language Translation](https://reader034.vdocument.in/reader034/viewer/2022042703/56815fdd550346895dcee67f/html5/thumbnails/20.jpg)
Invitation to Computer Science, Java Version, Third Edition 20
Parsing Concepts and Techniques Fundamental rule of parsing
By repeated applications of the rules of the grammar
If the parser can convert the sequence of input tokens into the goal symbol, the sequence of tokens is a syntactically valid statement of the language
If the parser cannot convert the input tokens into the goal symbol, the sequence of tokens is not a syntactically valid statement of the language
![Page 21: Chapter 10: Compilers and Language Translation](https://reader034.vdocument.in/reader034/viewer/2022042703/56815fdd550346895dcee67f/html5/thumbnails/21.jpg)
Invitation to Computer Science, Java Version, Third Edition 21
Parsing Concepts and Techniques (continued) One of the biggest problems in building a
compiler is designing a grammar that
Includes every valid statement that we want to be in the language
Excludes every invalid statement that we do not want to be in the language
![Page 22: Chapter 10: Compilers and Language Translation](https://reader034.vdocument.in/reader034/viewer/2022042703/56815fdd550346895dcee67f/html5/thumbnails/22.jpg)
Invitation to Computer Science, Java Version, Third Edition 22
Parsing Concepts and Techniques (continued) Another problem in constructing a compiler:
Designing a grammar that is not ambiguous
An ambiguous grammar allows the construction of two or more distinct parse trees for the same statement
![Page 23: Chapter 10: Compilers and Language Translation](https://reader034.vdocument.in/reader034/viewer/2022042703/56815fdd550346895dcee67f/html5/thumbnails/23.jpg)
Invitation to Computer Science, Java Version, Third Edition 23
Phase III: Semantics and Code Generation Semantic analysis
The compiler makes a first pass over the parse tree to determine whether all branches of the tree are semantically valid If they are valid, the compiler can generate machine
language instructions
If not, there is a semantic error; machine language instructions are not generated
![Page 24: Chapter 10: Compilers and Language Translation](https://reader034.vdocument.in/reader034/viewer/2022042703/56815fdd550346895dcee67f/html5/thumbnails/24.jpg)
Invitation to Computer Science, Java Version, Third Edition 24
Phase III: Semantics and Code Generation (continued) Code generation
Compiler makes a second pass over the parse tree to produce the translated code
![Page 25: Chapter 10: Compilers and Language Translation](https://reader034.vdocument.in/reader034/viewer/2022042703/56815fdd550346895dcee67f/html5/thumbnails/25.jpg)
Invitation to Computer Science, Java Version, Third Edition 25
Phase IV: Code Optimization
Two types of optimization Local Global
Local optimization The compiler looks at a very small block of
instructions and tries to determine how it can improve the efficiency of this local code block
Relatively easy; included as part of most compilers
![Page 26: Chapter 10: Compilers and Language Translation](https://reader034.vdocument.in/reader034/viewer/2022042703/56815fdd550346895dcee67f/html5/thumbnails/26.jpg)
Invitation to Computer Science, Java Version, Third Edition 26
Phase IV: Code Optimization (continued) Examples of possible local optimizations
Constant evaluation
Strength reduction
Eliminating unnecessary operations
![Page 27: Chapter 10: Compilers and Language Translation](https://reader034.vdocument.in/reader034/viewer/2022042703/56815fdd550346895dcee67f/html5/thumbnails/27.jpg)
Invitation to Computer Science, Java Version, Third Edition 27
Phase IV: Code Optimization (continued) Global optimization
The compiler looks at large segments of the program to decide how to improve performance
Much more difficult; usually omitted from all but the most sophisticated and expensive production-level “optimizing compilers”
Optimization cannot make an inefficient algorithm efficient
![Page 28: Chapter 10: Compilers and Language Translation](https://reader034.vdocument.in/reader034/viewer/2022042703/56815fdd550346895dcee67f/html5/thumbnails/28.jpg)
Invitation to Computer Science, Java Version, Third Edition 28
Summary A compiler is a 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
![Page 29: Chapter 10: Compilers and Language Translation](https://reader034.vdocument.in/reader034/viewer/2022042703/56815fdd550346895dcee67f/html5/thumbnails/29.jpg)
Invitation to Computer Science, Java Version, Third Edition 29
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