Top Banner
Compiler Construction Lecture 1: Introduction Thomas Noll Lehrstuhl f¨ ur Informatik 2 (Software Modeling and Verification) RWTH Aachen University [email protected] http://www-i2.informatik.rwth-aachen.de/i2/cc09/ Winter semester 2009/10
43

Compiler Construction - Lecture :

Jan 02, 2017

Download

Documents

phamkhuong
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Compiler Construction - Lecture :

Compiler Construction

Lecture 1: Introduction

Thomas Noll

Lehrstuhl fur Informatik 2(Software Modeling and Verification)

RWTH Aachen University

[email protected]

http://www-i2.informatik.rwth-aachen.de/i2/cc09/

Winter semester 2009/10

Page 2: Compiler Construction - Lecture :

Outline

1 Preliminaries

2 Introduction

Compiler Construction Winter semester 2009/10 2

Page 3: Compiler Construction - Lecture :

People

Lectures: Thomas Noll

Lehrstuhl fur Informatik 2, Room 4211E-mail [email protected] (0241)80-21213

Exercise classes:

Martin Neuhaußer ([email protected])Haidi Yue ([email protected])

Student assistant:

Vira Falak Sher ([email protected])

Compiler Construction Winter semester 2009/10 3

Page 4: Compiler Construction - Lecture :

Target Audience

Bachelor program (Informatik): V3 U2Wahlpflichtfach Theorie

Master programs (Informatik, Software Systems Engineering):

V4 U2Theoretical (+ Practical) CSSpecialization Formal Methods, Programming Languages and

Software Validation

Diplomstudiengang (Informatik): V4 U2Theoretische (+ Praktische) InformatikVertiefungsfach Formale Methoden, Programmiersprachen und

Softwarevalidierung

Compiler Construction Winter semester 2009/10 4

Page 5: Compiler Construction - Lecture :

Target Audience

Bachelor program (Informatik): V3 U2Wahlpflichtfach Theorie

Master programs (Informatik, Software Systems Engineering):

V4 U2Theoretical (+ Practical) CSSpecialization Formal Methods, Programming Languages and

Software Validation

Diplomstudiengang (Informatik): V4 U2Theoretische (+ Praktische) InformatikVertiefungsfach Formale Methoden, Programmiersprachen und

Softwarevalidierung

In general:interest in implementation of (imperative) programming languagesapplication of theoretical conceptscompiler = example of a complex software architecturegaining experience with tool support

Expected: basic knowledge inimperative programming languagesformal languages and automata theory

Compiler Construction Winter semester 2009/10 4

Page 6: Compiler Construction - Lecture :

Organization

Schedule:

Lecture Tue 14:00–15:30 AH2Lecture Thu 13:30–15:00 AH1 (starting October 15)Exercise class Wed 10:00–11:30 AH2 (starting October 28)see overview at http://www-i2.informatik.rwth-aachen.de/i2/cc09/

Compiler Construction Winter semester 2009/10 5

Page 7: Compiler Construction - Lecture :

Organization

Schedule:

Lecture Tue 14:00–15:30 AH2Lecture Thu 13:30–15:00 AH1 (starting October 15)Exercise class Wed 10:00–11:30 AH2 (starting October 28)see overview at http://www-i2.informatik.rwth-aachen.de/i2/cc09/

1st assignment sheet next week, presented October 28

Work on assignments in groups of three

Compiler Construction Winter semester 2009/10 5

Page 8: Compiler Construction - Lecture :

Organization

Schedule:

Lecture Tue 14:00–15:30 AH2Lecture Thu 13:30–15:00 AH1 (starting October 15)Exercise class Wed 10:00–11:30 AH2 (starting October 28)see overview at http://www-i2.informatik.rwth-aachen.de/i2/cc09/

1st assignment sheet next week, presented October 28

Work on assignments in groups of three

Written exam on Wed February 3

for BSc candidates (6 ECTS credit points)for BSc candidates (8 ECTS credit points)for Diplom candidates (Ubungsschein)

Admission requires at least 50% of the points in the exercises

Compiler Construction Winter semester 2009/10 5

Page 9: Compiler Construction - Lecture :

Organization

Schedule:

Lecture Tue 14:00–15:30 AH2Lecture Thu 13:30–15:00 AH1 (starting October 15)Exercise class Wed 10:00–11:30 AH2 (starting October 28)see overview at http://www-i2.informatik.rwth-aachen.de/i2/cc09/

1st assignment sheet next week, presented October 28

Work on assignments in groups of three

Written exam on Wed February 3

for BSc candidates (6 ECTS credit points)for BSc candidates (8 ECTS credit points)for Diplom candidates (Ubungsschein)

Admission requires at least 50% of the points in the exercises

Written material in English, lecture and exercise classes inGerman, rest up to you

Compiler Construction Winter semester 2009/10 5

Page 10: Compiler Construction - Lecture :

Outline

1 Preliminaries

2 Introduction

Compiler Construction Winter semester 2009/10 6

Page 11: Compiler Construction - Lecture :

What Is It All About?

Compiler = Program: Source code → Machine code

Source code: in high-level programming language, tailored to problem(imperative/declarative [functional, logic]/object-oriented, sequential/concurrent)

Machine code: architecture dependent(von Neumann; RISC/CISC/parallel)

Compiler Construction Winter semester 2009/10 7

Page 12: Compiler Construction - Lecture :

What Is It All About?

Compiler = Program: Source code → Machine code

Source code: in high-level programming language, tailored to problem(imperative/declarative [functional, logic]/object-oriented, sequential/concurrent)

Machine code: architecture dependent(von Neumann; RISC/CISC/parallel)

Important issues

Correctness: “equivalence” of source and machine code( =⇒ compiler verification, proof-carrying code, ...)

Efficiency of generated code: machine code as fast and/or memoryefficient as possible( =⇒ program analysis and optimization)

Efficiency of compiler: translation process as fast and/or memoryefficient as possible( =⇒ sophisticated algorithms and data structures; bootstrapping)

Efficiency depends on system environment; mutual efficiency tradeoffsCompiler Construction Winter semester 2009/10 7

Page 13: Compiler Construction - Lecture :

Aspects of a Programming Language

Syntax: “How does a program look like?”

hierarchical composition of programs from structural components

Compiler Construction Winter semester 2009/10 8

Page 14: Compiler Construction - Lecture :

Aspects of a Programming Language

Syntax: “How does a program look like?”

hierarchical composition of programs from structural components

Semantics: “What does this program mean?”

“Static semantics”: properties which are not (easily) definable insyntax(declaredness of identifiers, type correctness, ...)

“Dynamic semantics”: execution evokes state transformations of an(abstract) machine

Compiler Construction Winter semester 2009/10 8

Page 15: Compiler Construction - Lecture :

Aspects of a Programming Language

Syntax: “How does a program look like?”

hierarchical composition of programs from structural components

Semantics: “What does this program mean?”

“Static semantics”: properties which are not (easily) definable insyntax(declaredness of identifiers, type correctness, ...)

“Dynamic semantics”: execution evokes state transformations of an(abstract) machine

Pragmatics

length and understandability of programs

learnability of programming language

appropriateness for specific applications

...

Compiler Construction Winter semester 2009/10 8

Page 16: Compiler Construction - Lecture :

Motivation for Rigorous Formal Treatment

Example

1 From NASA’s Mercury Project: FORTRAN DO loop

DO 5 K = 1,3: DO loop with index variable K

DO 5 K = 1.3: assignment to (real) variable DO5K

Compiler Construction Winter semester 2009/10 9

Page 17: Compiler Construction - Lecture :

Motivation for Rigorous Formal Treatment

Example

1 From NASA’s Mercury Project: FORTRAN DO loop

DO 5 K = 1,3: DO loop with index variable K

DO 5 K = 1.3: assignment to (real) variable DO5K

2 How often is the following loop traversed?

for i := 2 to 1 do ...

FORTRAN IV: oncePASCAL: never

Compiler Construction Winter semester 2009/10 9

Page 18: Compiler Construction - Lecture :

Motivation for Rigorous Formal Treatment

Example

1 From NASA’s Mercury Project: FORTRAN DO loop

DO 5 K = 1,3: DO loop with index variable K

DO 5 K = 1.3: assignment to (real) variable DO5K

2 How often is the following loop traversed?

for i := 2 to 1 do ...

FORTRAN IV: oncePASCAL: never

3 What if p = nil in the following program?

while p <> nil and p^.key < val do ...

Pascal: strict Boolean operations

Modula: non-strict Boolean operationsCompiler Construction Winter semester 2009/10 9

Page 19: Compiler Construction - Lecture :
Page 20: Compiler Construction - Lecture :

Historical Development

Formal syntax: since 1960s

LL/LR parsingsemantics defined by compiler/interpreter

Compiler Construction Winter semester 2009/10 11

Page 21: Compiler Construction - Lecture :

Historical Development

Formal syntax: since 1960s

LL/LR parsingsemantics defined by compiler/interpreter

Formal semantics: since 1970s

operationaldenotationalaxiomaticsee course Semantics and Verification of Software

Compiler Construction Winter semester 2009/10 11

Page 22: Compiler Construction - Lecture :

Historical Development

Formal syntax: since 1960s

LL/LR parsingsemantics defined by compiler/interpreter

Formal semantics: since 1970s

operationaldenotationalaxiomaticsee course Semantics and Verification of Software

Automatic compiler generation: since 1980s

[f]lex, yacc, ANTLR, action semantics, ...see http://catalog.compilertools.net/

Compiler Construction Winter semester 2009/10 11

Page 23: Compiler Construction - Lecture :

Compiler Phases

Lexical analysis (Scanner):

recognition of symbols, delimiters, and commentsby regular expressions and finite automata

Compiler Construction Winter semester 2009/10 12

Page 24: Compiler Construction - Lecture :

Compiler Phases

Lexical analysis (Scanner):

recognition of symbols, delimiters, and commentsby regular expressions and finite automata

Syntactic analysis (Parser):

determination of hierarchical program structureby context-free grammars and pushdown automata

Compiler Construction Winter semester 2009/10 12

Page 25: Compiler Construction - Lecture :

Compiler Phases

Lexical analysis (Scanner):

recognition of symbols, delimiters, and commentsby regular expressions and finite automata

Syntactic analysis (Parser):

determination of hierarchical program structureby context-free grammars and pushdown automata

Semantic analysis:

checking context dependencies, data types, ...by attribute grammars

Compiler Construction Winter semester 2009/10 12

Page 26: Compiler Construction - Lecture :

Compiler Phases

Lexical analysis (Scanner):

recognition of symbols, delimiters, and commentsby regular expressions and finite automata

Syntactic analysis (Parser):

determination of hierarchical program structureby context-free grammars and pushdown automata

Semantic analysis:

checking context dependencies, data types, ...by attribute grammars

Generation of intermediate code:

translation into (target-independent) intermediatecodeby tree translations

Compiler Construction Winter semester 2009/10 12

Page 27: Compiler Construction - Lecture :

Compiler Phases

Lexical analysis (Scanner):

recognition of symbols, delimiters, and commentsby regular expressions and finite automata

Syntactic analysis (Parser):

determination of hierarchical program structureby context-free grammars and pushdown automata

Semantic analysis:

checking context dependencies, data types, ...by attribute grammars

Generation of intermediate code:

translation into (target-independent) intermediatecodeby tree translations

Code optimization: to improve runtime and/or memory behavior

Compiler Construction Winter semester 2009/10 12

Page 28: Compiler Construction - Lecture :

Compiler Phases

Lexical analysis (Scanner):

recognition of symbols, delimiters, and commentsby regular expressions and finite automata

Syntactic analysis (Parser):

determination of hierarchical program structureby context-free grammars and pushdown automata

Semantic analysis:

checking context dependencies, data types, ...by attribute grammars

Generation of intermediate code:

translation into (target-independent) intermediatecodeby tree translations

Code optimization: to improve runtime and/or memory behaviorGeneration of target code: tailored to target system

Compiler Construction Winter semester 2009/10 12

Page 29: Compiler Construction - Lecture :

Compiler Phases

Lexical analysis (Scanner):

recognition of symbols, delimiters, and commentsby regular expressions and finite automata

Syntactic analysis (Parser):

determination of hierarchical program structureby context-free grammars and pushdown automata

Semantic analysis:

checking context dependencies, data types, ...by attribute grammars

Generation of intermediate code:

translation into (target-independent) intermediatecodeby tree translations

Code optimization: to improve runtime and/or memory behaviorGeneration of target code: tailored to target systemAdditionally: optimization of target code, symbol table, error handling

Compiler Construction Winter semester 2009/10 12

Page 30: Compiler Construction - Lecture :

Conceptual Structure of a Compiler

Source code

Lexical analysis (Scanner)

Syntactic analysis (Parser)

Semantic analysis

Generation of intermediate code

Code optimization

Generation of machine code

Target codeCompiler Construction Winter semester 2009/10 13

Page 31: Compiler Construction - Lecture :

Conceptual Structure of a Compiler

Source code

Lexical analysis (Scanner)

Syntactic analysis (Parser)

Semantic analysis

Generation of intermediate code

Code optimization

Generation of machine code

Target code

x1 := y2 + 1

Compiler Construction Winter semester 2009/10 13

Page 32: Compiler Construction - Lecture :

Conceptual Structure of a Compiler

Source code

Lexical analysis (Scanner)

Syntactic analysis (Parser)

Semantic analysis

Generation of intermediate code

Code optimization

Generation of machine code

Target code

regular expressions/finite automata

x1 := y2 + 1

(id, x1)(gets, )(id, y2)(plus, )(int, 1)

Compiler Construction Winter semester 2009/10 13

Page 33: Compiler Construction - Lecture :

Conceptual Structure of a Compiler

Source code

Lexical analysis (Scanner)

Syntactic analysis (Parser)

Semantic analysis

Generation of intermediate code

Code optimization

Generation of machine code

Target code

context-free grammars/pushdown automata

(id, x1)(gets, )(id, y2)(plus, )(int, 1)

Assgn

Var Exp

Sum

Var Const

Compiler Construction Winter semester 2009/10 13

Page 34: Compiler Construction - Lecture :

Conceptual Structure of a Compiler

Source code

Lexical analysis (Scanner)

Syntactic analysis (Parser)

Semantic analysis

Generation of intermediate code

Code optimization

Generation of machine code

Target code

attribute grammars

Assgn

Var Exp

Sum

Var Const

Assgn

Var Exp

Sum

Var Const

ok

int int

int

int int

Compiler Construction Winter semester 2009/10 13

Page 35: Compiler Construction - Lecture :

Conceptual Structure of a Compiler

Source code

Lexical analysis (Scanner)

Syntactic analysis (Parser)

Semantic analysis

Generation of intermediate code

Code optimization

Generation of machine code

Target code

tree translations

Assgn

Var Exp

Sum

Var Const

ok

int int

int

int int

LOAD y2; LIT 1; ADD; STO x1

Compiler Construction Winter semester 2009/10 13

Page 36: Compiler Construction - Lecture :

Conceptual Structure of a Compiler

Source code

Lexical analysis (Scanner)

Syntactic analysis (Parser)

Semantic analysis

Generation of intermediate code

Code optimization

Generation of machine code

Target code

LOAD y2; LIT 1; ADD; STO x1

...

...

Compiler Construction Winter semester 2009/10 13

Page 37: Compiler Construction - Lecture :

Conceptual Structure of a Compiler

Source code

Lexical analysis (Scanner)

Syntactic analysis (Parser)

Semantic analysis

Generation of intermediate code

Code optimization

Generation of machine code

Target code

[omitted: symbol table, error handling]

Compiler Construction Winter semester 2009/10 13

Page 38: Compiler Construction - Lecture :

Classification of Compiler Phases

Analysis vs. synthesis

Analysis: lexical/syntactic/semantic analysis(determination of syntactic structure, error handling)

Synthesis: generation of (intermediate/machine) code + optimization

Compiler Construction Winter semester 2009/10 14

Page 39: Compiler Construction - Lecture :

Classification of Compiler Phases

Analysis vs. synthesis

Analysis: lexical/syntactic/semantic analysis(determination of syntactic structure, error handling)

Synthesis: generation of (intermediate/machine) code + optimization

Frontend vs. backend

Frontend: machine-independent parts(analysis + intermediate code + machine-independentoptimizations)

Backend: machine-dependent parts(generation + optimization of machine code)

Compiler Construction Winter semester 2009/10 14

Page 40: Compiler Construction - Lecture :

Classification of Compiler Phases

Analysis vs. synthesis

Analysis: lexical/syntactic/semantic analysis(determination of syntactic structure, error handling)

Synthesis: generation of (intermediate/machine) code + optimization

Frontend vs. backend

Frontend: machine-independent parts(analysis + intermediate code + machine-independentoptimizations)

Backend: machine-dependent parts(generation + optimization of machine code)

Historical: n-pass compiler

n = number of runs through source program

nowadays mainly one-pass

Compiler Construction Winter semester 2009/10 14

Page 41: Compiler Construction - Lecture :

Literature

(CS Library: “Handapparat Programmiersprachen und Verifikation”)

General

A.V. Aho, M.S. Lam, R. Sethi, J.D. Ullman: Compilers – Principles,

Techniques, and Tools; 2nd ed., Addison-Wesley, 2007A.W. Appel, J. Palsberg: Modern Compiler Implementation in Java,Cambridge University Press, 2002D. Grune, H.E. Bal, C.J.H. Jacobs, K.G. Langendoen: Modern Compiler

Design, Wiley & Sons, 2000R. Wilhelm, D. Maurer: Ubersetzerbau, 2. Auflage, Springer, 1997

Compiler Construction Winter semester 2009/10 15

Page 42: Compiler Construction - Lecture :

Literature

(CS Library: “Handapparat Programmiersprachen und Verifikation”)

General

A.V. Aho, M.S. Lam, R. Sethi, J.D. Ullman: Compilers – Principles,

Techniques, and Tools; 2nd ed., Addison-Wesley, 2007A.W. Appel, J. Palsberg: Modern Compiler Implementation in Java,Cambridge University Press, 2002D. Grune, H.E. Bal, C.J.H. Jacobs, K.G. Langendoen: Modern Compiler

Design, Wiley & Sons, 2000R. Wilhelm, D. Maurer: Ubersetzerbau, 2. Auflage, Springer, 1997

Special

O. Mayer: Syntaxanalyse, BI-Wissenschafts-Verlag, 1978D. Brown, R. Levine T. Mason: lex & yacc, O’Reilly, 1995T. Parr: The Definite ANTLR Reference, Pragmatic Bookshelf, 2007

Compiler Construction Winter semester 2009/10 15

Page 43: Compiler Construction - Lecture :

Literature

(CS Library: “Handapparat Programmiersprachen und Verifikation”)

General

A.V. Aho, M.S. Lam, R. Sethi, J.D. Ullman: Compilers – Principles,

Techniques, and Tools; 2nd ed., Addison-Wesley, 2007A.W. Appel, J. Palsberg: Modern Compiler Implementation in Java,Cambridge University Press, 2002D. Grune, H.E. Bal, C.J.H. Jacobs, K.G. Langendoen: Modern Compiler

Design, Wiley & Sons, 2000R. Wilhelm, D. Maurer: Ubersetzerbau, 2. Auflage, Springer, 1997

Special

O. Mayer: Syntaxanalyse, BI-Wissenschafts-Verlag, 1978D. Brown, R. Levine T. Mason: lex & yacc, O’Reilly, 1995T. Parr: The Definite ANTLR Reference, Pragmatic Bookshelf, 2007

Historical

W. Waite, G. Goos: Compiler Construction, 2nd edition, Springer, 1985N. Wirth: Grundlagen und Techniken des Compilerbaus, Addison-Wesley, 1996

Compiler Construction Winter semester 2009/10 15