Chapter 1
Introduction
Course “Compiler Construction” Martin Steffen Spring 2020
Chapter 1 Introduction Course Compiler Construction Martin Steffen - - PowerPoint PPT Presentation
Chapter 1 Introduction Course Compiler Construction Martin Steffen Spring 2020 Section Targets Chapter 1 Introduction Course Compiler Construction Martin Steffen Spring 2020 Chapter 1 Learning Targets of Chapter
Course “Compiler Construction” Martin Steffen Spring 2020
Chapter 1 “Introduction” Course “Compiler Construction” Martin Steffen Spring 2020
The chapter gives an overview over different phases of a compiler and their tasks. It also mentions /organizational/ things related to the course.
Targets Introduction Course info Course material and plan Motivation: What is CC good for? Compiler architecture & phases Architecture of a typical compiler Anatomy of a compiler Pre-processor C-style preprocessor examples C-style preprocessor: macros Scanner (lexer . . . ) Scanner: illustration Parser
Chapter 1 “Introduction” Course “Compiler Construction” Martin Steffen Spring 2020
INF5110 – Compiler Construction Targets Targets & Outline Introduction
Course info Course material and plan Motivation: What is CC good for?
Compiler architecture & phases
Architecture of a typical compiler Anatomy of a compiler Pre-processor C-style preprocessor examples C-style preprocessor: macros Scanner (lexer . . . ) Scanner: illustration Parser a[index] = 4 + 2: parse tree/syntax tree a[index] = 4 + 2: abstract syntax tree
1.1 List of authors
http://www.uio.no/studier/emner/matnat/ ifi/INF5110
play a role. A classic is “the dragon book” [1], we might use part of code generation from there
http://www.cs.sjsu.edu/~louden/cmptext/
May
us about such planned group collaboration
12th June, 09:00, 4 hours, written, open-book
but
processes/transforms and outputs “data” ⇒ often involves techniques central to CC
programming language(s)
language paradigms and constructs. . . ) ⇒ CC & their principles will never be “out-of-fashion”.
INF5110 – Compiler Construction Targets Targets & Outline Introduction
Course info Course material and plan Motivation: What is CC good for?
Compiler architecture & phases
Architecture of a typical compiler Anatomy of a compiler Pre-processor C-style preprocessor examples C-style preprocessor: macros Scanner (lexer . . . ) Scanner: illustration Parser a[index] = 4 + 2: parse tree/syntax tree a[index] = 4 + 2: abstract syntax tree
Chapter 1 “Introduction” Course “Compiler Construction” Martin Steffen Spring 2020
INF5110 – Compiler Construction Targets Targets & Outline Introduction
Course info Course material and plan Motivation: What is CC good for?
Compiler architecture & phases
Architecture of a typical compiler Anatomy of a compiler Pre-processor C-style preprocessor examples C-style preprocessor: macros Scanner (lexer . . . ) Scanner: illustration Parser a[index] = 4 + 2: parse tree/syntax tree a[index] = 4 + 2: abstract syntax tree
Figure: Structure of a typical compiler
INF5110 – Compiler Construction Targets Targets & Outline Introduction
Course info Course material and plan Motivation: What is CC good for?
Compiler architecture & phases
Architecture of a typical compiler Anatomy of a compiler Pre-processor C-style preprocessor examples C-style preprocessor: macros Scanner (lexer . . . ) Scanner: illustration Parser a[index] = 4 + 2: parse tree/syntax tree a[index] = 4 + 2: abstract syntax tree
“hack” grafted on top of a compiler.
same as the if-programming-language construct.
INF5110 – Compiler Construction Targets Targets & Outline Introduction
Course info Course material and plan Motivation: What is CC good for?
Compiler architecture & phases
Architecture of a typical compiler Anatomy of a compiler Pre-processor C-style preprocessor examples C-style preprocessor: macros Scanner (lexer . . . ) Scanner: illustration Parser a[index] = 4 + 2: parse tree/syntax tree a[index] = 4 + 2: abstract syntax tree
#i n c l u d e <filename >
Listing 1: file inclusion
#v a r d e f #a = 5 ; #c = #a+1 . . . #i f (#a < #b ) . . #e l s e . . . #e n d i f
Listing 2: Conditional compilation
#macrodef hentdata (#1,#2) − − − #1 − − − − #2−−−(#1)−−− #enddef . . .
INF5110 – Compiler Construction Targets Targets & Outline Introduction
Course info Course material and plan Motivation: What is CC good for?
Compiler architecture & phases
Architecture of a typical compiler Anatomy of a compiler Pre-processor C-style preprocessor examples C-style preprocessor: macros Scanner (lexer . . . ) Scanner: illustration Parser a[index] = 4 + 2: parse tree/syntax tree a[index] = 4 + 2: abstract syntax tree
#hentdata ( k a r i , per )
Listing 3: Macros
− − − k ar i − − − − per −−−(k a r i)−−−
similar)
a [ index ] ␣=␣4␣+␣2
INF5110 – Compiler Construction Targets Targets & Outline Introduction
Course info Course material and plan Motivation: What is CC good for?
Compiler architecture & phases
Architecture of a typical compiler Anatomy of a compiler Pre-processor C-style preprocessor examples C-style preprocessor: macros Scanner (lexer . . . ) Scanner: illustration Parser a[index] = 4 + 2: parse tree/syntax tree a[index] = 4 + 2: abstract syntax tree
lexeme token class value a identifier "a" [ left bracket index identifier "index" ] right bracket = assignment 4 number "4" + plus sign 2 number "2"
INF5110 – Compiler Construction Targets Targets & Outline Introduction
Course info Course material and plan Motivation: What is CC good for?
Compiler architecture & phases
Architecture of a typical compiler Anatomy of a compiler Pre-processor C-style preprocessor examples C-style preprocessor: macros Scanner (lexer . . . ) Scanner: illustration Parser a[index] = 4 + 2: parse tree/syntax tree a[index] = 4 + 2: abstract syntax tree
INF5110 – Compiler Construction Targets Targets & Outline Introduction
Course info Course material and plan Motivation: What is CC good for?
Compiler architecture & phases
Architecture of a typical compiler Anatomy of a compiler Pre-processor C-style preprocessor examples C-style preprocessor: macros Scanner (lexer . . . ) Scanner: illustration Parser a[index] = 4 + 2: parse tree/syntax tree a[index] = 4 + 2: abstract syntax tree
expr assign-expr expr subscript expr expr identifier a [ expr identifier index ] = expr additive expr expr number 4 + expr number 2 assign-expr subscript expr identifier a identifier index additive expr number 2 number 4
INF5110 – Compiler Construction Targets Targets & Outline Introduction
Course info Course material and plan Motivation: What is CC good for?
Compiler architecture & phases
Architecture of a typical compiler Anatomy of a compiler Pre-processor C-style preprocessor examples C-style preprocessor: macros Scanner (lexer . . . ) Scanner: illustration Parser a[index] = 4 + 2: parse tree/syntax tree a[index] = 4 + 2: abstract syntax tree
“annotated” or “decorated” AST
assign-expr additive-expr number 2 number 4 subscript-expr identifier index identifier a :array of int :int :array of int :int :int :int :int :int :int :int : ?
INF5110 – Compiler Construction Targets Targets & Outline Introduction
Course info Course material and plan Motivation: What is CC good for?
Compiler architecture & phases
Architecture of a typical compiler Anatomy of a compiler Pre-processor C-style preprocessor examples C-style preprocessor: macros Scanner (lexer . . . ) Scanner: illustration Parser a[index] = 4 + 2: parse tree/syntax tree a[index] = 4 + 2: abstract syntax tree
assign-expr subscript expr identifier a identifier index number 6
t = 4+2; a[index] = t;
t = 6; a[index] = t;
a[index] = 6;
MOV␣␣R0 , ␣ index ␣ ; ; ␣␣ v a l u e ␣ of ␣ index ␣− >␣R0 MUL␣␣R0 , ␣2␣␣␣␣␣ ; ; ␣␣ double ␣ v a l u e ␣ of ␣R0
INF5110 – Compiler Construction Targets Targets & Outline Introduction
Course info Course material and plan Motivation: What is CC good for?
Compiler architecture & phases
Architecture of a typical compiler Anatomy of a compiler Pre-processor C-style preprocessor examples C-style preprocessor: macros Scanner (lexer . . . ) Scanner: illustration Parser a[index] = 4 + 2: parse tree/syntax tree a[index] = 4 + 2: abstract syntax tree
MOV␣␣R1 , ␣&a␣␣␣␣ ; ; ␣␣ ad d r e s s ␣ of ␣a␣− >␣R1 ADD␣␣R1 , ␣R0␣␣␣␣ ; ; ␣␣add␣R0␣ to ␣R1 MOV␣∗R1 , ␣6␣␣␣␣␣ ; ; ␣␣ const ␣6␣− >␣ a d d r e s s ␣ i n ␣R1 MOV␣R0 , ␣ index ␣␣␣␣␣␣ ; ; ␣ v a l u e ␣ of ␣ index ␣− >␣R0 SHL␣R0␣␣␣␣␣␣␣␣␣␣␣␣␣ ; ; ␣ double ␣ v a l u e ␣ i n ␣R0 MOV␣&a [ R0 ] , ␣6␣␣␣␣␣␣ ; ; ␣ const ␣6␣− >␣ a d d r e s s ␣a+R0
description of language and machine
INF5110 – Compiler Construction Targets Targets & Outline Introduction
Course info Course material and plan Motivation: What is CC good for?
Compiler architecture & phases
Architecture of a typical compiler Anatomy of a compiler Pre-processor C-style preprocessor examples C-style preprocessor: macros Scanner (lexer . . . ) Scanner: illustration Parser a[index] = 4 + 2: parse tree/syntax tree a[index] = 4 + 2: abstract syntax tree
stack, heap), garbage collection?
INF5110 – Compiler Construction Targets Targets & Outline Introduction
Course info Course material and plan Motivation: What is CC good for?
Compiler architecture & phases
Architecture of a typical compiler Anatomy of a compiler Pre-processor C-style preprocessor examples C-style preprocessor: macros Scanner (lexer . . . ) Scanner: illustration Parser a[index] = 4 + 2: parse tree/syntax tree a[index] = 4 + 2: abstract syntax tree
etc.
code in Java)
manner (JIT)
run-time
grammars
INF5110 – Compiler Construction Targets Targets & Outline Introduction
Course info Course material and plan Motivation: What is CC good for?
Compiler architecture & phases
Architecture of a typical compiler Anatomy of a compiler Pre-processor C-style preprocessor examples C-style preprocessor: macros Scanner (lexer . . . ) Scanner: illustration Parser a[index] = 4 + 2: parse tree/syntax tree a[index] = 4 + 2: abstract syntax tree
Chapter 1 “Introduction” Course “Compiler Construction” Martin Steffen Spring 2020
INF5110 – Compiler Construction Targets Targets & Outline Introduction
Course info Course material and plan Motivation: What is CC good for?
Compiler architecture & phases
Architecture of a typical compiler Anatomy of a compiler Pre-processor C-style preprocessor examples C-style preprocessor: macros Scanner (lexer . . . ) Scanner: illustration Parser a[index] = 4 + 2: parse tree/syntax tree a[index] = 4 + 2: abstract syntax tree
“tombstone diagrams” (or T-diagrams). . . .
INF5110 – Compiler Construction Targets Targets & Outline Introduction
Course info Course material and plan Motivation: What is CC good for?
Compiler architecture & phases
Architecture of a typical compiler Anatomy of a compiler Pre-processor C-style preprocessor examples C-style preprocessor: macros Scanner (lexer . . . ) Scanner: illustration Parser a[index] = 4 + 2: parse tree/syntax tree a[index] = 4 + 2: abstract syntax tree
INF5110 – Compiler Construction Targets Targets & Outline Introduction
Course info Course material and plan Motivation: What is CC good for?
Compiler architecture & phases
Architecture of a typical compiler Anatomy of a compiler Pre-processor C-style preprocessor examples C-style preprocessor: macros Scanner (lexer . . . ) Scanner: illustration Parser a[index] = 4 + 2: parse tree/syntax tree a[index] = 4 + 2: abstract syntax tree
bootstrap (verb, trans.): to promote or develop . . . with little or no assistance — Merriam-Webster
INF5110 – Compiler Construction Targets Targets & Outline Introduction
Course info Course material and plan Motivation: What is CC good for?
Compiler architecture & phases
Architecture of a typical compiler Anatomy of a compiler Pre-processor C-style preprocessor examples C-style preprocessor: macros Scanner (lexer . . . ) Scanner: illustration Parser a[index] = 4 + 2: parse tree/syntax tree a[index] = 4 + 2: abstract syntax tree
INF5110 – Compiler Construction Targets Targets & Outline Introduction
Course info Course material and plan Motivation: What is CC good for?
Compiler architecture & phases
Architecture of a typical compiler Anatomy of a compiler Pre-processor C-style preprocessor examples C-style preprocessor: macros Scanner (lexer . . . ) Scanner: illustration Parser a[index] = 4 + 2: parse tree/syntax tree a[index] = 4 + 2: abstract syntax tree
INF5110 – Compiler Construction Targets Targets & Outline Introduction
Course info Course material and plan Motivation: What is CC good for?
Compiler architecture & phases
Architecture of a typical compiler Anatomy of a compiler Pre-processor C-style preprocessor examples C-style preprocessor: macros Scanner (lexer . . . ) Scanner: illustration Parser a[index] = 4 + 2: parse tree/syntax tree a[index] = 4 + 2: abstract syntax tree
INF5110 – Compiler Construction Targets Targets & Outline Introduction
Course info Course material and plan Motivation: What is CC good for?
Compiler architecture & phases
Architecture of a typical compiler Anatomy of a compiler Pre-processor C-style preprocessor examples C-style preprocessor: macros Scanner (lexer . . . ) Scanner: illustration Parser a[index] = 4 + 2: parse tree/syntax tree a[index] = 4 + 2: abstract syntax tree
Bibliography [1] Aho, A. V., Sethi, R., and Ullman, J. D. (1986). Compilers: Principles, Techniques, and Tools. Addison-Wesley. [2] Cooper, K. D. and Torczon, L. (2004). Engineering a Compiler. Elsevier. [3] Louden, K. (1997). Compiler Construction, Principles and Practice. PWS Publishing.