compiler design nai-wei lin department of computer science national chung cheng university
TRANSCRIPT
![Page 1: Compiler Design Nai-Wei Lin Department of Computer Science National Chung Cheng University](https://reader036.vdocuments.mx/reader036/viewer/2022082610/56649dc65503460f94ab9ad4/html5/thumbnails/1.jpg)
Compiler Design
Nai-Wei Lin
Department of Computer Science
National Chung Cheng University
![Page 2: Compiler Design Nai-Wei Lin Department of Computer Science National Chung Cheng University](https://reader036.vdocuments.mx/reader036/viewer/2022082610/56649dc65503460f94ab9ad4/html5/thumbnails/2.jpg)
Chapter 1
Introduction to Compilers
![Page 3: Compiler Design Nai-Wei Lin Department of Computer Science National Chung Cheng University](https://reader036.vdocuments.mx/reader036/viewer/2022082610/56649dc65503460f94ab9ad4/html5/thumbnails/3.jpg)
3
Outline of This Chapter
♥ Introduction to compilers
♥ Introduction to compiler generators
♥ Introduction to automatic tool generators
![Page 4: Compiler Design Nai-Wei Lin Department of Computer Science National Chung Cheng University](https://reader036.vdocuments.mx/reader036/viewer/2022082610/56649dc65503460f94ab9ad4/html5/thumbnails/4.jpg)
4
Programming Languages
♥ Human uses human languages to communicate with each other– Chinese, English, French
♥ Human uses programming languages to communicate with computers– Fortran, C, Java
![Page 5: Compiler Design Nai-Wei Lin Department of Computer Science National Chung Cheng University](https://reader036.vdocuments.mx/reader036/viewer/2022082610/56649dc65503460f94ab9ad4/html5/thumbnails/5.jpg)
5
Computer Organization
Operating System (Low Level Language)
Hardware Machine
Compiler
Applications (High Level Language)
![Page 6: Compiler Design Nai-Wei Lin Department of Computer Science National Chung Cheng University](https://reader036.vdocuments.mx/reader036/viewer/2022082610/56649dc65503460f94ab9ad4/html5/thumbnails/6.jpg)
6
Compilers
Compiler
Programwritten in
High-LevelLanguage
Programwritten in
Low-LevelLanguage
Error Message
![Page 7: Compiler Design Nai-Wei Lin Department of Computer Science National Chung Cheng University](https://reader036.vdocuments.mx/reader036/viewer/2022082610/56649dc65503460f94ab9ad4/html5/thumbnails/7.jpg)
7
Components of a Compiler
♥ Analysis– Lexical Analysis– Syntax Analysis– Semantic Analysis
♥ Synthesis– Intermediate Code Generation– Code Optimization– Code Generation
![Page 8: Compiler Design Nai-Wei Lin Department of Computer Science National Chung Cheng University](https://reader036.vdocuments.mx/reader036/viewer/2022082610/56649dc65503460f94ab9ad4/html5/thumbnails/8.jpg)
8
Lexical Analysis
Lexical Analysis
final := initial + rate * 60
id1 := id2 + id3 * 60Someone breaks the ice
S o m e o n e b r e a k st h e i c e
Lexical Analysis
![Page 9: Compiler Design Nai-Wei Lin Department of Computer Science National Chung Cheng University](https://reader036.vdocuments.mx/reader036/viewer/2022082610/56649dc65503460f94ab9ad4/html5/thumbnails/9.jpg)
9
Syntax Analysis
Syntax Analysis
id1 := id2 + id3 * 60
:=id1 +
id2 *id3 60
Someone breaks the ice
Someone breaks the ice
subject verb object
sentence
Syntax Analysis
![Page 10: Compiler Design Nai-Wei Lin Department of Computer Science National Chung Cheng University](https://reader036.vdocuments.mx/reader036/viewer/2022082610/56649dc65503460f94ab9ad4/html5/thumbnails/10.jpg)
10
Semantic Analysis:=
id1 +id2 *
id3 60
:=id1
+
id2*
id3
60
i2r
Someone plays the piano
The piano plays someone
(meaningful)
(meaningless)
Semantic Analysis
![Page 11: Compiler Design Nai-Wei Lin Department of Computer Science National Chung Cheng University](https://reader036.vdocuments.mx/reader036/viewer/2022082610/56649dc65503460f94ab9ad4/html5/thumbnails/11.jpg)
11
Intermediate Code Generation
:=id1
+id2
*id3
60i2r
temp1 := i2r ( 60 )temp2 := id3 * temp1temp3 := id2 + temp2id1 := temp3
Intermediate Code Generation
Someone breaks the ice
有人打破冰
![Page 12: Compiler Design Nai-Wei Lin Department of Computer Science National Chung Cheng University](https://reader036.vdocuments.mx/reader036/viewer/2022082610/56649dc65503460f94ab9ad4/html5/thumbnails/12.jpg)
12
Code Optimization
temp1 := i2r ( 60 )temp2 := id3 * temp1temp3 := id2 + temp2id1 := temp3
Code Optimization
temp1 := id3 * 60.0id1 := id2 + temp1
有人打破冰
有人打破沉默
Code Optimization
![Page 13: Compiler Design Nai-Wei Lin Department of Computer Science National Chung Cheng University](https://reader036.vdocuments.mx/reader036/viewer/2022082610/56649dc65503460f94ab9ad4/html5/thumbnails/13.jpg)
13
Code Generation
Code Generation
temp1 := id3 * 60.0id1 := id2 + temp1
movf id3, r2mulf #60.0, r2movf id2, r1addf r2, r1movf r1, id1
有人打破沉默
有人打破沉默
![Page 14: Compiler Design Nai-Wei Lin Department of Computer Science National Chung Cheng University](https://reader036.vdocuments.mx/reader036/viewer/2022082610/56649dc65503460f94ab9ad4/html5/thumbnails/14.jpg)
14
Metalanguages
Metalanguage: a language used to define another language
We will use different metalangauges to define the various components of a programming language so that these components can be generated automatically
![Page 15: Compiler Design Nai-Wei Lin Department of Computer Science National Chung Cheng University](https://reader036.vdocuments.mx/reader036/viewer/2022082610/56649dc65503460f94ab9ad4/html5/thumbnails/15.jpg)
15
Compiler Generators
Compiler GeneratorCompiler
Definition inMetalanguage
Compiler
Error Message
![Page 16: Compiler Design Nai-Wei Lin Department of Computer Science National Chung Cheng University](https://reader036.vdocuments.mx/reader036/viewer/2022082610/56649dc65503460f94ab9ad4/html5/thumbnails/16.jpg)
16
Definition of Programming Definition of Programming LanguagesLanguages
♥ Lexical analysis: regular expressions♥ Syntax analysis: context free grammars♥ Semantics analysis: attribute grammars♥ Intermediate code generation:
attribute grammars♥ Code generation: tree grammars
![Page 17: Compiler Design Nai-Wei Lin Department of Computer Science National Chung Cheng University](https://reader036.vdocuments.mx/reader036/viewer/2022082610/56649dc65503460f94ab9ad4/html5/thumbnails/17.jpg)
17
Automatic Tool Generators
Tool Generator Tool
Error Message
ToolDefinition in
Metalanguage
![Page 18: Compiler Design Nai-Wei Lin Department of Computer Science National Chung Cheng University](https://reader036.vdocuments.mx/reader036/viewer/2022082610/56649dc65503460f94ab9ad4/html5/thumbnails/18.jpg)
18
Applications of Compilation Techniques
♥ Web Browsers (HTML, XML, …)♥ Word Processors (postscript, pdf, …)♥ Computer-Aided Software Engineering (UM
L)♥ Computer-Aided Design (VHDL, Verilog, …)♥ Computer-Aided Manufacturing (Pattern Reco
gnition)
![Page 19: Compiler Design Nai-Wei Lin Department of Computer Science National Chung Cheng University](https://reader036.vdocuments.mx/reader036/viewer/2022082610/56649dc65503460f94ab9ad4/html5/thumbnails/19.jpg)
19
Outline of This Course
♥ Lexical analysis
♥ Syntax analysis
♥ Semantic analysis
♥ Code generation