introduction to compiler design - hampden-sydney...
TRANSCRIPT
![Page 1: Introduction to Compiler Design - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 2015-01-14 · Introduction to Compiler Design Lecture 1 Chapters](https://reader033.vdocuments.us/reader033/viewer/2022052408/5f09b7d37e708231d4282ef4/html5/thumbnails/1.jpg)
Introduction to Compiler DesignLecture 1
Chapters 1 and 2
Robb T. Koether
Hampden-Sydney College
Wed, Jan 14, 2015
Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 1 / 33
![Page 2: Introduction to Compiler Design - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 2015-01-14 · Introduction to Compiler Design Lecture 1 Chapters](https://reader033.vdocuments.us/reader033/viewer/2022052408/5f09b7d37e708231d4282ef4/html5/thumbnails/2.jpg)
Outline
1 The Stages of CompilationLexical AnalysisSyntactic AnalysisSemantic AnalysisIntermediate Code GenerationOptimizationMachine Code Generation
2 Assignment
Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 2 / 33
![Page 3: Introduction to Compiler Design - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 2015-01-14 · Introduction to Compiler Design Lecture 1 Chapters](https://reader033.vdocuments.us/reader033/viewer/2022052408/5f09b7d37e708231d4282ef4/html5/thumbnails/3.jpg)
Outline
1 The Stages of CompilationLexical AnalysisSyntactic AnalysisSemantic AnalysisIntermediate Code GenerationOptimizationMachine Code Generation
2 Assignment
Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 3 / 33
![Page 4: Introduction to Compiler Design - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 2015-01-14 · Introduction to Compiler Design Lecture 1 Chapters](https://reader033.vdocuments.us/reader033/viewer/2022052408/5f09b7d37e708231d4282ef4/html5/thumbnails/4.jpg)
The Stages of Compilation
The stages of compilationLexical analysisSyntactic analysis.Semantic analysis.Intermediate code generation.Optimization.Machine code generation.
Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 4 / 33
![Page 5: Introduction to Compiler Design - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 2015-01-14 · Introduction to Compiler Design Lecture 1 Chapters](https://reader033.vdocuments.us/reader033/viewer/2022052408/5f09b7d37e708231d4282ef4/html5/thumbnails/5.jpg)
Outline
1 The Stages of CompilationLexical AnalysisSyntactic AnalysisSemantic AnalysisIntermediate Code GenerationOptimizationMachine Code Generation
2 Assignment
Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 5 / 33
![Page 6: Introduction to Compiler Design - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 2015-01-14 · Introduction to Compiler Design Lecture 1 Chapters](https://reader033.vdocuments.us/reader033/viewer/2022052408/5f09b7d37e708231d4282ef4/html5/thumbnails/6.jpg)
Lexical Analysis
Definition (Token)A token is a smallest meaningful group symbols.
Definition (Lexical analyzer)A lexical analyzer, also called a lexer or a scanner, receives a streamof characters from the source program and groups them into tokens.
SourceProgram
LexicalAnalyzer
Streamof
Tokens
Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 6 / 33
![Page 7: Introduction to Compiler Design - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 2015-01-14 · Introduction to Compiler Design Lecture 1 Chapters](https://reader033.vdocuments.us/reader033/viewer/2022052408/5f09b7d37e708231d4282ef4/html5/thumbnails/7.jpg)
Example
Example (Lexical Analysis)What are the tokens in the following program?
int main(){
float a = 123.4;return 0;
}
Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 7 / 33
![Page 8: Introduction to Compiler Design - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 2015-01-14 · Introduction to Compiler Design Lecture 1 Chapters](https://reader033.vdocuments.us/reader033/viewer/2022052408/5f09b7d37e708231d4282ef4/html5/thumbnails/8.jpg)
Tokens
Each token has a type and a value.For example,
The variable count has type id and value “count”.The number 123 has type num and value “123”.The keyword int has type int and value “int”.The symbol { has the type lbrace and value “lbrace”.
Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 8 / 33
![Page 9: Introduction to Compiler Design - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 2015-01-14 · Introduction to Compiler Design Lecture 1 Chapters](https://reader033.vdocuments.us/reader033/viewer/2022052408/5f09b7d37e708231d4282ef4/html5/thumbnails/9.jpg)
Example
Example (Lexical Analysis)The statement
position = initial + rate * 60;
would be viewed as
id1 = id2 + id3 ∗ num ;
orid1 assign id2 plus id3 times num semi
by the lexer.
Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 9 / 33
![Page 10: Introduction to Compiler Design - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 2015-01-14 · Introduction to Compiler Design Lecture 1 Chapters](https://reader033.vdocuments.us/reader033/viewer/2022052408/5f09b7d37e708231d4282ef4/html5/thumbnails/10.jpg)
Lexical Analysis Tools
There are tools available to assist in the writing of lexicalanalyzers.
lex - produces C source code (UNIX).flex - produces C source code (gnu).JLex - produces Java source code.JFlex - produces Java source code.
We will use JFlex.
Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 10 / 33
![Page 11: Introduction to Compiler Design - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 2015-01-14 · Introduction to Compiler Design Lecture 1 Chapters](https://reader033.vdocuments.us/reader033/viewer/2022052408/5f09b7d37e708231d4282ef4/html5/thumbnails/11.jpg)
Outline
1 The Stages of CompilationLexical AnalysisSyntactic AnalysisSemantic AnalysisIntermediate Code GenerationOptimizationMachine Code Generation
2 Assignment
Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 11 / 33
![Page 12: Introduction to Compiler Design - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 2015-01-14 · Introduction to Compiler Design Lecture 1 Chapters](https://reader033.vdocuments.us/reader033/viewer/2022052408/5f09b7d37e708231d4282ef4/html5/thumbnails/12.jpg)
Syntactic Analysis
Definition (Syntax analyzer)A syntax analyzer, also called a parser, receives a stream of tokensfrom the lexer and groups them into phrases that match specifiedgrammatical patterns.
Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 12 / 33
![Page 13: Introduction to Compiler Design - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 2015-01-14 · Introduction to Compiler Design Lecture 1 Chapters](https://reader033.vdocuments.us/reader033/viewer/2022052408/5f09b7d37e708231d4282ef4/html5/thumbnails/13.jpg)
Syntactic Analysis
Definition (Abstract syntax tree)The output of the parser is an abstract syntax tree representing thesyntactical structure of the tokens.
Streamof
Tokens
SyntaxAnalyzer
AbstractSyntaxTree
Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 13 / 33
![Page 14: Introduction to Compiler Design - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 2015-01-14 · Introduction to Compiler Design Lecture 1 Chapters](https://reader033.vdocuments.us/reader033/viewer/2022052408/5f09b7d37e708231d4282ef4/html5/thumbnails/14.jpg)
Grammatical Patterns
Grammatical patterns are described by a context-free grammar.For example, an assignment statement may be defined as
stmt → id = expr ;
expr → expr + expr | expr ∗ expr | id | num
Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 14 / 33
![Page 15: Introduction to Compiler Design - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 2015-01-14 · Introduction to Compiler Design Lecture 1 Chapters](https://reader033.vdocuments.us/reader033/viewer/2022052408/5f09b7d37e708231d4282ef4/html5/thumbnails/15.jpg)
Example
Example (Syntactic Analysis)The form
id1 = id2 + id3 ∗ num ;
may be represented by the following syntax tree.
=
*
+
id3
id2
num
id1
Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 15 / 33
![Page 16: Introduction to Compiler Design - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 2015-01-14 · Introduction to Compiler Design Lecture 1 Chapters](https://reader033.vdocuments.us/reader033/viewer/2022052408/5f09b7d37e708231d4282ef4/html5/thumbnails/16.jpg)
Syntax Analysis Tools
There are tools available to assist in the writing of parsers.yacc - produces C source code (UNIX).bison - produces C source code (gnu).CUP - produces Java source code.
We will use CUP.
Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 16 / 33
![Page 17: Introduction to Compiler Design - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 2015-01-14 · Introduction to Compiler Design Lecture 1 Chapters](https://reader033.vdocuments.us/reader033/viewer/2022052408/5f09b7d37e708231d4282ef4/html5/thumbnails/17.jpg)
Outline
1 The Stages of CompilationLexical AnalysisSyntactic AnalysisSemantic AnalysisIntermediate Code GenerationOptimizationMachine Code Generation
2 Assignment
Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 17 / 33
![Page 18: Introduction to Compiler Design - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 2015-01-14 · Introduction to Compiler Design Lecture 1 Chapters](https://reader033.vdocuments.us/reader033/viewer/2022052408/5f09b7d37e708231d4282ef4/html5/thumbnails/18.jpg)
Semantic Analysis
Definition (Semantic analyzer)A semantic analyzer traverses the abstract syntax tree, checking thateach node is appropriate for its context, i.e., it checks for semanticerrors. It outputs a refined abstract syntax tree.
Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 18 / 33
![Page 19: Introduction to Compiler Design - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 2015-01-14 · Introduction to Compiler Design Lecture 1 Chapters](https://reader033.vdocuments.us/reader033/viewer/2022052408/5f09b7d37e708231d4282ef4/html5/thumbnails/19.jpg)
Example: Semantic Analysis
Example (Semantic Analysis)The previous tree may be refined to
=
*
+
id3
id2
inttoreal
id1
num
Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 19 / 33
![Page 20: Introduction to Compiler Design - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 2015-01-14 · Introduction to Compiler Design Lecture 1 Chapters](https://reader033.vdocuments.us/reader033/viewer/2022052408/5f09b7d37e708231d4282ef4/html5/thumbnails/20.jpg)
Outline
1 The Stages of CompilationLexical AnalysisSyntactic AnalysisSemantic AnalysisIntermediate Code GenerationOptimizationMachine Code Generation
2 Assignment
Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 20 / 33
![Page 21: Introduction to Compiler Design - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 2015-01-14 · Introduction to Compiler Design Lecture 1 Chapters](https://reader033.vdocuments.us/reader033/viewer/2022052408/5f09b7d37e708231d4282ef4/html5/thumbnails/21.jpg)
Intermediate Code Generation
Definition (Intermediate code)Intermediate code is code that represents the semantics of a program,but is machine-independent.
Definition (Intermediate code generator)An intermediate code generator receives the abstract syntax tree andoutputs intermediate code that semantically corresponds to theabstract syntax tree.
IntermediateCode
Generator
IntermediateCode
AbstractSyntaxTree
Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 21 / 33
![Page 22: Introduction to Compiler Design - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 2015-01-14 · Introduction to Compiler Design Lecture 1 Chapters](https://reader033.vdocuments.us/reader033/viewer/2022052408/5f09b7d37e708231d4282ef4/html5/thumbnails/22.jpg)
Intermediate Code
This stage marks the boundary between the front end and theback end.The front end is language-specific and machine-independent.The back end is machine-specific and language-independent.
Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 22 / 33
![Page 23: Introduction to Compiler Design - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 2015-01-14 · Introduction to Compiler Design Lecture 1 Chapters](https://reader033.vdocuments.us/reader033/viewer/2022052408/5f09b7d37e708231d4282ef4/html5/thumbnails/23.jpg)
Intermediate Code
IntermediateCode
CProgram
JavaProgram
PythonProgram
x86Code
MIPS32Code
Front End Back End
Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 23 / 33
![Page 24: Introduction to Compiler Design - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 2015-01-14 · Introduction to Compiler Design Lecture 1 Chapters](https://reader033.vdocuments.us/reader033/viewer/2022052408/5f09b7d37e708231d4282ef4/html5/thumbnails/24.jpg)
Example
Example (Intermediate Code Generation)The tree in our example may be expressed in intermediate codeas
temp1 = inttoreal(60)temp2 = id3 * temp1temp3 = id2 + temp2id1 = temp3
Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 24 / 33
![Page 25: Introduction to Compiler Design - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 2015-01-14 · Introduction to Compiler Design Lecture 1 Chapters](https://reader033.vdocuments.us/reader033/viewer/2022052408/5f09b7d37e708231d4282ef4/html5/thumbnails/25.jpg)
Outline
1 The Stages of CompilationLexical AnalysisSyntactic AnalysisSemantic AnalysisIntermediate Code GenerationOptimizationMachine Code Generation
2 Assignment
Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 25 / 33
![Page 26: Introduction to Compiler Design - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 2015-01-14 · Introduction to Compiler Design Lecture 1 Chapters](https://reader033.vdocuments.us/reader033/viewer/2022052408/5f09b7d37e708231d4282ef4/html5/thumbnails/26.jpg)
Code Optimizer
Definition (Optimizer)An optimizer reviews the code, looking for ways to reduce the numberof operations and the memory requirements.
A program may be optimized for speed or for size.Typically there is a trade-off between speed and size.
Optimizer IntermediateCode
IntermediateCode
Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 26 / 33
![Page 27: Introduction to Compiler Design - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 2015-01-14 · Introduction to Compiler Design Lecture 1 Chapters](https://reader033.vdocuments.us/reader033/viewer/2022052408/5f09b7d37e708231d4282ef4/html5/thumbnails/27.jpg)
Example
Example (Optimization)The intermediate code in this example may be optimized as
temp1 = id3 * 60.0id1 = id2 + temp1
Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 27 / 33
![Page 28: Introduction to Compiler Design - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 2015-01-14 · Introduction to Compiler Design Lecture 1 Chapters](https://reader033.vdocuments.us/reader033/viewer/2022052408/5f09b7d37e708231d4282ef4/html5/thumbnails/28.jpg)
Outline
1 The Stages of CompilationLexical AnalysisSyntactic AnalysisSemantic AnalysisIntermediate Code GenerationOptimizationMachine Code Generation
2 Assignment
Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 28 / 33
![Page 29: Introduction to Compiler Design - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 2015-01-14 · Introduction to Compiler Design Lecture 1 Chapters](https://reader033.vdocuments.us/reader033/viewer/2022052408/5f09b7d37e708231d4282ef4/html5/thumbnails/29.jpg)
Machine Code Generation
The code generator receives the (optimized) intermediate code.It produces either
Machine code for a specific machine, orAssembly code for a specific machine and assembler.
If it produces assembly code, then an assembler is used toproduce the machine code.
Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 29 / 33
![Page 30: Introduction to Compiler Design - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 2015-01-14 · Introduction to Compiler Design Lecture 1 Chapters](https://reader033.vdocuments.us/reader033/viewer/2022052408/5f09b7d37e708231d4282ef4/html5/thumbnails/30.jpg)
Machine Code Generation
CodeGenerator
AssemblyCode
IntermediateCode
AssemblerMachine
Code
Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 30 / 33
![Page 31: Introduction to Compiler Design - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 2015-01-14 · Introduction to Compiler Design Lecture 1 Chapters](https://reader033.vdocuments.us/reader033/viewer/2022052408/5f09b7d37e708231d4282ef4/html5/thumbnails/31.jpg)
Example: Machine Code Generation
The intermediate code may be translated into the assembly code
movf id3,R2mulf #60.0,R2movf id2,R1addf R2,R1movf R1,id1
Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 31 / 33
![Page 32: Introduction to Compiler Design - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 2015-01-14 · Introduction to Compiler Design Lecture 1 Chapters](https://reader033.vdocuments.us/reader033/viewer/2022052408/5f09b7d37e708231d4282ef4/html5/thumbnails/32.jpg)
Outline
1 The Stages of CompilationLexical AnalysisSyntactic AnalysisSemantic AnalysisIntermediate Code GenerationOptimizationMachine Code Generation
2 Assignment
Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 32 / 33
![Page 33: Introduction to Compiler Design - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 2015-01-14 · Introduction to Compiler Design Lecture 1 Chapters](https://reader033.vdocuments.us/reader033/viewer/2022052408/5f09b7d37e708231d4282ef4/html5/thumbnails/33.jpg)
Assignment
AssignmentRead Chapters 1 and 2.
Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 33 / 33