compiler lec

Upload: roger-baltazar

Post on 05-Apr-2018

230 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/31/2019 Compiler Lec

    1/31

    Compiler ConstructionLecture 1: Introduction

    Thomas Noll

    Lehrstuhl fur Informatik 2

    RWTH Aachen University

    [email protected]

    http://www-i2.informatik.rwth-aachen.de/Teaching/Course/CB/2006/

    Winter semester 2006/07

    Compiler Construction Winter semester 2006/07 1

    http://[email protected]/http://www-i2.informatik.rwth-aachen.de/Teaching/Course/CB/2006/http://www-i2.informatik.rwth-aachen.de/Teaching/Course/CB/2006/http://[email protected]/
  • 7/31/2019 Compiler Lec

    2/31

    Outline

    1 Preliminaries

    2 Introduction

    Compiler Construction Winter semester 2006/07 2

  • 7/31/2019 Compiler Lec

    3/31

    People

    Lectures: Thomas NollLehrstuhl fur Informatik 2, Room 4211Email [email protected] (0241)8021213

    Exercise classes: Stefan Rieger

    Lehrstuhl fur Informatik 2, Room 4206Email [email protected] (0241)8021210

    Student assistants:

    Christian Lucking ([email protected])

    Denise Nimmerrichter([email protected])Franziska Roloff ([email protected])

    Compiler Construction Winter semester 2006/07 3

    http://[email protected]/http://[email protected]/http://[email protected]/http://[email protected]/http://[email protected]/http://[email protected]/http://[email protected]/http://[email protected]/http://[email protected]/http://[email protected]/http://[email protected]/
  • 7/31/2019 Compiler Lec

    4/31

    Target Audience

    Diploma programme (Informatik)

    Theoretische (+ Praktische) Informatik

    Vertiefungsfach Formale Methoden, Programmiersprachen undSoftwarevalidierung

    Master programme (Software Systems Engineering)

    Theoretical (+ Practical) CSSpecialization Formal Methods, Programming Languages and

    Software ValidationIn general:

    interest in implementation of (highlevel imperative) programminglanguagesapplication of theoretical concepts

    compiler = example of a complex software architecturegaining experience with tool support

    Expected: basic knowledge in

    imperative programming languagesformal languages and automata theory

    Compiler Construction Winter semester 2006/07 4

  • 7/31/2019 Compiler Lec

    5/31

    Organization

    Schedule:

    Lecture Tue 14:0015:30 AH 2Lecture Fri 8:159:45 AH 2(starting 20.10.2006; not 01.12.2006)Exercise class Mon 13:45-15:15 AH 2 (starting 23.10.2006)

    Today: 1st assignment sheetWork on assignments in groups of three

    Examination (8 ECTS credit points):probably written; beginning of February 2007

    Admission requires at least 50% of the points in the exercisesWritten material in English, lecture and presentation ofassignments in German, rest up to you

    Compiler Construction Winter semester 2006/07 5

  • 7/31/2019 Compiler Lec

    6/31

    Outline

    1 Preliminaries

    2 Introduction

    Compiler Construction Winter semester 2006/07 6

    Wh I I All Ab ?

  • 7/31/2019 Compiler Lec

    7/31

    What Is It All About?

    Compiler = Program: Source code Machine codeSource code: in highlevel programming language, tailored to problem

    (imperative/declarative [functional, logic]/objectoriented, sequential/concurrent)

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

    Compiler Construction Winter semester 2006/07 7

    Wh t I It All Ab t?

  • 7/31/2019 Compiler Lec

    8/31

    What Is It All About?

    Compiler = Program: Source code Machine codeSource code: in highlevel programming language, tailored to problem

    (imperative/declarative [functional, logic]/objectoriented, sequential/concurrent)

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

    Important issues:

    Correctness: equivalence of source and machine code( = compiler verification, proofcarrying 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 tradeoffCompiler Construction Winter semester 2006/07 7

    A t f P i L

  • 7/31/2019 Compiler Lec

    9/31

    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?

    (execution evokes state transformations of an [abstract]machine)

    Pragmatics: length and understandability of programs, learnability ofprogramming language, appropriateness for specificapplications, ...

    Compiler Construction Winter semester 2006/07 8

    A t f P i L

  • 7/31/2019 Compiler Lec

    10/31

    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?

    (execution evokes state transformations of an [abstract]machine)

    Pragmatics: length and understandability of programs, learnability ofprogramming language, appropriateness for specificapplications, ...

    Historic development:

    Formal syntax since 1960s (LL/LR parsing);

    semantics defined by compiler/interpreterFormal semantics since 1970s(operational/denotational/axiomatic)

    Automatic compiler generation since 1980s (lex, yacc, action

    semantics, ...Compiler Construction Winter semester 2006/07 8

    M ti ti f Ri F l T t t

  • 7/31/2019 Compiler Lec

    11/31

    Motivation for Rigorous Formal Treatment

    Examples:

    1 How often is the following loop traversed?

    f o r i : = 2 t o 1 d o . . .

    FORTRAN IV: oncePASCAL: never

    Compiler Construction Winter semester 2006/07 9

    Motivation for Rigorous Formal Treatment

  • 7/31/2019 Compiler Lec

    12/31

    Motivation for Rigorous Formal Treatment

    Examples:

    1 How often is the following loop traversed?

    f o r i : = 2 t o 1 d o . . .

    FORTRAN IV: oncePASCAL: never

    2 What if p = n i l in the following program?

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

    Pascal: strict boolean operationsModula: nonstrict boolean operations

    Compiler Construction Winter semester 2006/07 9

    Compiler Phases

  • 7/31/2019 Compiler Lec

    13/31

    Compiler Phases

    Lexical analysis (Scanner):

    recognition of symbols, delimiters, and comments

    by regular expressions and finite automata

    Compiler Construction Winter semester 2006/07 10

    Compiler Phases

  • 7/31/2019 Compiler Lec

    14/31

    Compiler Phases

    Lexical analysis (Scanner):

    recognition of symbols, delimiters, and comments

    by regular expressions and finite automata

    Syntactic analysis (Parser):

    determination of hierarchical program structureby contextfree grammars and pushdown automata

    Compiler Construction Winter semester 2006/07 10

    Compiler Phases

  • 7/31/2019 Compiler Lec

    15/31

    Compiler Phases

    Lexical analysis (Scanner):

    recognition of symbols, delimiters, and comments

    by regular expressions and finite automataSyntactic analysis (Parser):

    determination of hierarchical program structureby contextfree grammars and pushdown automata

    Semantic analysis:checking context dependencies, data types, ...by attribute grammars

    Compiler Construction Winter semester 2006/07 10

    Compiler Phases

  • 7/31/2019 Compiler Lec

    16/31

    Compiler Phases

    Lexical analysis (Scanner):

    recognition of symbols, delimiters, and comments

    by regular expressions and finite automataSyntactic analysis (Parser):

    determination of hierarchical program structureby contextfree grammars and pushdown automata

    Semantic analysis:checking context dependencies, data types, ...by attribute grammars

    Generation of intermediate code:

    translation into (targetindependent) intermediate code

    by tree translations

    Compiler Construction Winter semester 2006/07 10

    Compiler Phases

  • 7/31/2019 Compiler Lec

    17/31

    Compiler Phases

    Lexical analysis (Scanner):

    recognition of symbols, delimiters, and comments

    by regular expressions and finite automataSyntactic analysis (Parser):

    determination of hierarchical program structureby contextfree grammars and pushdown automata

    Semantic analysis:checking context dependencies, data types, ...by attribute grammars

    Generation of intermediate code:

    translation into (targetindependent) intermediate code

    by tree translations

    Code optimization: to improve runtime and/or memory behavior

    Compiler Construction Winter semester 2006/07 10

    Compiler Phases

  • 7/31/2019 Compiler Lec

    18/31

    Compiler Phases

    Lexical analysis (Scanner):

    recognition of symbols, delimiters, and comments

    by regular expressions and finite automataSyntactic analysis (Parser):

    determination of hierarchical program structureby contextfree grammars and pushdown automata

    Semantic analysis:checking context dependencies, data types, ...by attribute grammars

    Generation of intermediate code:

    translation into (targetindependent) intermediate code

    by tree translations

    Code optimization: to improve runtime and/or memory behavior

    Generation of target code: tailored to target system

    Compiler Construction Winter semester 2006/07 10

    Compiler Phases

  • 7/31/2019 Compiler Lec

    19/31

    Compiler Phases

    Lexical analysis (Scanner):

    recognition of symbols, delimiters, and comments

    by regular expressions and finite automataSyntactic analysis (Parser):

    determination of hierarchical program structureby contextfree grammars and pushdown automata

    Semantic analysis:checking context dependencies, data types, ...by attribute grammars

    Generation of intermediate code:

    translation into (targetindependent) intermediate code

    by tree translations

    Code optimization: to improve runtime and/or memory behavior

    Generation of target code: tailored to target system

    Additionally: optimization of target code, symbol table, error handling

    Compiler Construction Winter semester 2006/07 10

    Conceptual Structure of a Compiler

  • 7/31/2019 Compiler Lec

    20/31

    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 2006/07 11

    Conceptual Structure of a Compiler

  • 7/31/2019 Compiler Lec

    21/31

    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

    x 1 : = y 2 + 1

    Compiler Construction Winter semester 2006/07 11

    Conceptual Structure of a Compiler

  • 7/31/2019 Compiler Lec

    22/31

    p p

    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

    x 1 : = y 2 + 1

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

    Compiler Construction Winter semester 2006/07 11

    Conceptual Structure of a Compiler

  • 7/31/2019 Compiler Lec

    23/31

    p p

    Source code

    Lexical analysis (Scanner)

    Syntactic analysis (Parser)

    Semantic analysis

    Generation of intermediate code

    Code optimization

    Generation of machine code

    Target code

    contextfree grammars/pushdown automata

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

    Assgn

    Var Exp

    SumVar Const

    Compiler Construction Winter semester 2006/07 11

    Conceptual Structure of a Compiler

  • 7/31/2019 Compiler Lec

    24/31

    p p

    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 2006/07 11

    Conceptual Structure of a Compiler

  • 7/31/2019 Compiler Lec

    25/31

    p p

    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 ExpSum

    Var Const

    ok

    int intint

    int int

    LOAD y2; LIT 1; ADD; STO x1

    Compiler Construction Winter semester 2006/07 11

    Conceptual Structure of a Compiler

  • 7/31/2019 Compiler Lec

    26/31

    p p

    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 2006/07 11

    Conceptual Structure of a Compiler

  • 7/31/2019 Compiler Lec

    27/31

    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 2006/07 11

    Classification of Compiler Phases

  • 7/31/2019 Compiler Lec

    28/31

    Analysis: lexical/syntactic/semantic analysis

    (determination of syntactic structure, error handling)Synthesis: generation of (intermediate/machine) code + optimization

    Compiler Construction Winter semester 2006/07 12

    Classification of Compiler Phases

  • 7/31/2019 Compiler Lec

    29/31

    Analysis: lexical/syntactic/semantic analysis

    (determination of syntactic structure, error handling)Synthesis: generation of (intermediate/machine) code + optimization

    Alternatively:

    Frontend: machineindependent parts(analysis + intermediate code + machineindependentoptimizations)

    Backend: machinedependent parts(generation + optimization of machine code)

    Compiler Construction Winter semester 2006/07 12

    Classification of Compiler Phases

  • 7/31/2019 Compiler Lec

    30/31

    Analysis: lexical/syntactic/semantic analysis

    (determination of syntactic structure, error handling)Synthesis: generation of (intermediate/machine) code + optimization

    Alternatively:

    Frontend: machineindependent parts(analysis + intermediate code + machineindependentoptimizations)

    Backend: machinedependent parts(generation + optimization of machine code)

    Another classification: npass compiler(number of runs through source program; nowadays mainly onepass)

    Compiler Construction Winter semester 2006/07 12

    Literature

  • 7/31/2019 Compiler Lec

    31/31

    (also see the collection [Handapparat] at the CS Library)

    A. Aho, R. Sethi, J. Ullman: Compilers Principles, Techniques,

    and Tools, AddisonWesley, 1988

    W. Waite, G. Goos: Compiler Construction, 2nd edition, Springer,1985

    R. Wilhelm, D. Maurer: Ubersetzerbau, 2. Auflage, Springer, 1997

    N. Wirth: Grundlagen und Techniken des Compilerbaus,Addison-Wesley, 1996

    J.R. Levine et al.: lex & yacc, OReilly, 1992

    A.W. Appel, J. Palsberg: Modern Compiler Implementation in

    Java, Cambridge University Press, 2002D. Grune, H.E. Bal, C.J.H. Jacobs, K.G. Langendoen: ModernCompiler Design, Wiley & Sons, 2000

    O. Mayer: Syntaxanalyse, BIVerlag, 1978

    Compiler Construction Winter semester 2006/07 13