Invitation to Computer Science 5 th Edition

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

description

Invitation to Computer Science 5 th Edition. Chapter 11 Compilers and Language Translation. 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. - PowerPoint PPT Presentation

Transcript of Invitation to Computer Science 5 th Edition

Page 1: Invitation to Computer Science   5 th Edition

Invitation to Computer Science 5th Edition

Chapter 11

Compilers and Language Translation

Page 2: Invitation to Computer Science   5 th 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 – Phase IV: Code optimization

Invitation to Computer Science, 5th Edition 2

Page 3: Invitation to Computer Science   5 th Edition

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 Edition 33

Page 4: Invitation to Computer Science   5 th Edition

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 Edition 44

Page 5: Invitation to Computer Science   5 th Edition

Invitation to Computer Science, 5th Edition 5

Figure 11.1 General Structure of a Compiler

Page 6: Invitation to Computer Science   5 th Edition

Invitation to Computer Science, 5th Edition 6

Figure 11.2 Overall Execution Sequence of a High-level Language Program

Page 7: Invitation to Computer Science   5 th Edition

Invitation to Computer Science, 5th Edition 7

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

Page 8: Invitation to Computer Science   5 th Edition

Invitation to Computer Science, 5th Edition 8

Figure 11.3 Typical Token Classifications

Page 9: Invitation to Computer Science   5 th Edition

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 Edition 9

Page 10: Invitation to Computer Science   5 th Edition

Invitation to Computer Science, 5th Edition 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

Page 11: Invitation to Computer Science   5 th Edition

Invitation to Computer Science, 5th Edition 11

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 ::=

Page 12: Invitation to Computer Science   5 th Edition

Invitation to Computer Science, 5th Edition 12

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

Page 13: Invitation to Computer Science   5 th Edition

Invitation to Computer Science, 5th Edition 13

Figure 11.4 First Attempt at a Grammar for a Simplified Assignment Statement

Page 14: Invitation to Computer Science   5 th Edition

Invitation to Computer Science, 5th Edition 14

Figure 11.5 Parse Tree Produced by the Parser

Page 15: Invitation to Computer Science   5 th Edition

Invitation to Computer Science, 5th Edition 15

Figure 11.6 Second Attempt at a Grammar for Assignment Statements

Page 16: Invitation to Computer Science   5 th Edition

Invitation to Computer Science, 5th Edition 16

Figure 11.7 Two Parse Trees for the Statement x = x + y + z

Page 17: Invitation to Computer Science   5 th Edition

Invitation to Computer Science, 5th Edition 17

Figure 11.8 Third Attempt at a Grammar for Assignment Statements

Page 18: Invitation to Computer Science   5 th Edition

Invitation to Computer Science, 5th Edition 18

Figure 11.9 Grammar for a Simplified Version of an if-else Statement

Page 19: Invitation to Computer Science   5 th Edition

Invitation to Computer Science, 5th Edition 19

Figure 11.10 Parse Tree for the Statement if (x55y)x5z; else x5y;

Page 20: Invitation to Computer Science   5 th Edition

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 Edition 20

Page 21: Invitation to Computer Science   5 th Edition

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 Edition 21

Page 22: Invitation to Computer Science   5 th Edition

Invitation to Computer Science, 5th Edition 22

Figure 11.11 Code Generation for the Assignment Statement x = x + y + z

Page 23: Invitation to Computer Science   5 th Edition

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

programmer’s task and increase productivity

• Integrated development environment– Compiler is embedded within a collection of

supporting software development routines

Invitation to Computer Science, 5th Edition 23

Page 24: Invitation to Computer Science   5 th Edition

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 Edition 24

Page 25: Invitation to Computer Science   5 th Edition

Invitation to Computer Science, 5th Edition 25

Figure 11.12 Optimized Code for the Assignment Statement x = x + y + z

Page 26: Invitation to Computer Science   5 th Edition

Invitation to Computer Science, 5th Edition 26

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

Page 27: Invitation to Computer Science   5 th Edition

Invitation to Computer Science, 5th Edition 27

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