Documente Academic
Documente Profesional
Documente Cultură
Compiler
Design
Robb T.
Koether
The Stages of
Introduction to Compiler Design
Compilation
Lexical Analysis
Lecture 1
Syntactic Analysis
Semantic Analysis
Chapter 1
Intermediate Code
Generation
Optimization
Machine Code
Generation
Robb T. Koether
Assignment
Hampden-Sydney College
Introduction to
Compiler
Design
Robb T.
Koether
1 The Stages of Compilation
The Stages of Lexical Analysis
Compilation
Lexical Analysis Syntactic Analysis
Syntactic Analysis
Semantic Analysis Semantic Analysis
Intermediate Code
Generation
Optimization
Intermediate Code Generation
Machine Code
Generation
Optimization
Assignment Machine Code Generation
2 Assignment
The Stages of Compilation
Introduction to
Compiler
Design
Robb T.
Koether
The Stages of
Compilation The stages of compilation
Lexical Analysis
Syntactic Analysis
Lexical analysis
Semantic Analysis
Intermediate Code
Syntactic analysis.
Generation
Optimization
Semantic analysis.
Machine Code
Generation
Intermediate code generation.
Assignment
Optimization.
Machine code generation.
Lexical Analysis
Introduction to
Compiler
Design
Definition (Token)
Robb T. A token is a smallest meaningful group symbols.
Koether
The Stages of
Compilation Definition (Lexical analyzer)
Lexical Analysis
Syntactic Analysis A lexical analyzer, also called a lexer or a scanner, receives
Semantic Analysis
Intermediate Code
Generation
a stream of characters from the source program and groups
Optimization
Machine Code
them into tokens.
Generation
Assignment
Introduction to
Compiler
Design
Robb T.
Koether
The Stages of
Compilation
Lexical Analysis Each token has a type and a value.
Syntactic Analysis
Semantic Analysis For example,
Intermediate Code
Generation
Optimization
The variable count has type id and value “count”.
Machine Code
Generation
The number 123 has type num and value “123”.
Assignment
The keyword int has type int and value “int”.
Example
Introduction to
Compiler
Design
Robb T.
Koether
The Stages of
Example (Lexical Analysis)
Compilation
Lexical Analysis
What are the tokens in the following program?
Syntactic Analysis
Semantic Analysis
Intermediate Code
Generation
int main()
Optimization
Machine Code
{
Generation
int a = 123;
Assignment
return 0;
}
Example
Introduction to
Compiler
Design
Robb T.
Koether
Example (Lexical Analysis)
The Stages of
The statement
Compilation
Lexical Analysis position = initial + rate * 60;
Syntactic Analysis
Semantic Analysis
Intermediate Code
Generation
would be viewed as
Optimization
Machine Code
Generation
id1 = id2 + id3 ∗ num ;
Assignment
or
id1 assign id2 plus id3 times num semi
by the lexer.
Lexical Analysis Tools
Introduction to
Compiler
Design
Robb T.
Koether
The Stages of
Compilation There are tools available to assist in the writing of
Lexical Analysis
Syntactic Analysis lexical analyzers.
Semantic Analysis
Intermediate Code lex - produces C source code (UNIX).
Generation
Optimization flex - produces C source code (gnu).
Machine Code
Generation JLex - produces Java source code.
Assignment
We will use JLex.
Syntactic Analysis
Introduction to
Compiler
Design
Robb T.
Koether
The Stages of
Compilation
Lexical Analysis
Syntactic Analysis
Definition (Syntax analyzer)
Semantic Analysis
Intermediate Code A syntax analyzer, also called a parser, receives a stream of
Generation
Optimization tokens from the lexer and groups them into phrases that
Machine Code
Generation match specified grammatical patterns.
Assignment
Syntactic Analysis
Introduction to
Compiler
Design
Robb T.
Koether Definition (Abstract syntax tree)
The Stages of The output of the parser is an abstract syntax tree
Compilation
Lexical Analysis
representing the syntactical structure of the tokens.
Syntactic Analysis
Semantic Analysis
Intermediate Code
Generation
Optimization
Machine Code
Generation
Stream Syntax Abstract
Assignment
of Analyzer Syntax
Tokens Tree
Grammatical Patterns
Introduction to
Compiler
Design
Robb T.
Koether
Grammatical patterns are described by a context-free
The Stages of
Compilation grammar.
Lexical Analysis
Syntactic Analysis
Semantic Analysis
For example, an assignment statement may be defined
Intermediate Code
Generation
as
Optimization
Machine Code
Generation
Assignment
stmt → id = expr ;
expr → expr + expr | expr ∗ expr | id | num
Example
Introduction to
Compiler Example (Syntactic Analysis)
Design
Robb T.
The form
Koether
The Stages of
id1 = id2 + id3 ∗ num ;
Compilation
Lexical Analysis
Syntactic Analysis
Semantic Analysis
Intermediate Code
Generation
may be represented by the following tree.
Optimization
Machine Code
Generation
Assignment
=
id1 +
id2 *
id3 num
Syntax Analysis Tools
Introduction to
Compiler
Design
Robb T.
Koether
The Stages of
Compilation There are tools available to assist in the writing of
Lexical Analysis
Syntactic Analysis parsers.
Semantic Analysis
Intermediate Code yacc - produces C source code (UNIX).
Generation
Optimization bison - produces C source code (gnu).
Machine Code
Generation CUP - produces Java source code.
Assignment
We will use CUP.
Semantic Analysis
Introduction to
Compiler
Design
Robb T.
Koether
The Stages of
Compilation
Lexical Analysis
Definition (Semantic analyzer)
Syntactic Analysis
Semantic Analysis A semantic analyzer traverses the abstract syntax tree,
Intermediate Code
Generation checking that each node is appropriate for its context, i.e., it
Optimization
Machine Code
Generation
checks for semantic errors. It outputs a refined abstract
Assignment syntax tree.
Example: Semantic Analysis
Introduction to
Compiler
Design
Example (Semantic Analysis)
Robb T.
Koether
The previous tree may be refined to
The Stages of
Compilation
Lexical Analysis
Syntactic Analysis
Semantic Analysis =
Intermediate Code
Generation
Optimization id1 +
Machine Code
Generation
Assignment id2 *
id3 inttoreal
num
Intermediate Code Generation
Introduction to
Compiler Definition (Intermediate code)
Design
Introduction to
Compiler
Design
Robb T.
Koether
The Stages of
Compilation This stage marks the boundary between the front end
Lexical Analysis
Syntactic Analysis
and the back end.
Semantic Analysis
Intermediate Code
Generation
The front end is language-specific and
Optimization
Machine Code
machine-independent.
Generation
Assignment
The back end is machine-specific and
language-independent.
Intermediate Code
Introduction to
Compiler
Design
Robb T.
Koether
C
The Stages of
Compilation
Program x86
Lexical Analysis Code
Syntactic Analysis
Semantic Analysis
Intermediate Code
Generation
Java Intermediate
Optimization Program Code
Machine Code
Generation
Assignment MIPS32
Pascal Code
Program
Introduction to
Compiler
Design
Robb T.
Koether
Introduction to
Compiler
Design
Definition (Optimizer)
Robb T.
Koether An optimizer reviews the code, looking for ways to reduce
The Stages of
the number of operations and the memory requirements.
Compilation
Lexical Analysis
Syntactic Analysis
Semantic Analysis
A program may be optimized for speed or for size.
Intermediate Code
Generation
Optimization
Typically there is a trade-off between speed and size.
Machine Code
Generation
Assignment
Intermediate Intermediate
Optimizer
Code Code
Example
Introduction to
Compiler
Design
Robb T.
Koether
The Stages of
Compilation
Lexical Analysis
Example (Optimization)
Syntactic Analysis
Semantic Analysis The intermediate code in this example may be
Intermediate Code
Generation optimized as
Optimization
Machine Code
Generation temp1 = id3 * 60.0
Assignment id1 = id2 + temp1
Machine Code Generation
Introduction to
Compiler
Design
Robb T.
Koether
Introduction to
Compiler
Design
Robb T.
Koether
The Stages of
Compilation
Lexical Analysis
Intermediate Code Assembly
Syntactic Analysis
Semantic Analysis Code Generator Code
Intermediate Code
Generation
Optimization
Machine Code
Generation
Assignment
Machine
Assembler Code
Example: Machine Code Generation
Introduction to
Compiler
Design
Robb T.
Koether
The Stages of
The intermediate code may be translated into the
Compilation
Lexical Analysis
assembly code
Syntactic Analysis
Semantic Analysis
Intermediate Code
Generation
movf id3,R2
Optimization mulf #60.0,R2
Machine Code
Generation
movf id2,R1
Assignment
addf R2,R1
movf R1,id1
Assignment
Introduction to
Compiler
Design
Robb T.
Koether
The Stages of
Compilation
Lexical Analysis Homework
Syntactic Analysis
Semantic Analysis
Intermediate Code
Read Chapter 1.
Generation
Optimization Install Cygwin on the lab machine of your choice.
Machine Code
Generation
Arrange with me to turn off Deep Freeze.
Assignment