Invitation to Computer Science 5th Edition - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

Invitation to Computer Science 5th Edition

Description:

... Code Optimization Efficiency Ability to write highly optimized programs that contained no wasted microseconds or unnecessary memory cells Goal in compiler ... – PowerPoint PPT presentation

Number of Views:294
Avg rating:3.0/5.0
Slides: 28
Provided by: csGsuEdu7
Category:

less

Transcript and Presenter's Notes

Title: Invitation to Computer Science 5th Edition


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

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

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

3
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

4
5
Figure 11.1 General Structure of a Compiler
6
Figure 11.2 Overall Execution Sequence of a
High-level Language Program
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

8
Figure 11.3 Typical Token Classifications
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

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

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 lt, gt, and

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

13
Figure 11.4 First Attempt at a Grammar for a
Simplified Assignment Statement
14
Figure 11.5 Parse Tree Produced by the Parser
15
Figure 11.6 Second Attempt at a Grammar for
Assignment Statements
16
Figure 11.7 Two Parse Trees for the Statement x
x y z
17
Figure 11.8 Third Attempt at a Grammar for
Assignment Statements
18
Figure 11.9 Grammar for a Simplified Version of
an if-else Statement
19
Figure 11.10 Parse Tree for the Statement if
(x55y)x5z else x5y
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

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

22
Figure 11.11 Code Generation for the Assignment
Statement x x y z
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

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

25
Figure 11.12 Optimized Code for the Assignment
Statement x x y z
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

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
Write a Comment
User Comments (0)
About PowerShow.com