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 “Introduction”. The chapter gives an overview over different phases of a compiler and their tasks. It also mentions /organizational/ things related to the course.
Chapter 1 Outline of Chapter “Introduction”. 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
Section Introduction Chapter 1 “Introduction” Course “Compiler Construction” Martin Steffen Spring 2020
1. Course material from: 1.1 List of authors • Martin Steffen ( msteffen@ifi.uio.no ) • Stein Krogdahl ( stein@ifi.uio.no ) • Birger Møller-Pedersen ( birger@ifi.uio.no ) INF5110 – Compiler • Eyvind Wærstad Axelsen ( eyvinda@ifi.uio.no ) Construction 2. Course’s web-page Targets http://www.uio.no/studier/emner/matnat/ Targets & Outline ifi/INF5110 Introduction • overview over the course, pensum (watch for updates) Course info • various announcements, beskjeder, etc. Course material and plan Motivation: What is CC good for? • based roughly on [2] and [3], but also other sources will Compiler architecture & play a role. A classic is “the dragon book” [1], we phases Architecture of a typical might use part of code generation from there compiler Anatomy of a compiler • see also errata list at Pre-processor C-style preprocessor examples http://www.cs.sjsu.edu/~louden/cmptext/ C-style preprocessor: macros • approx. 3 hours teaching per week (+ exercises) Scanner (lexer . . . ) Scanner: illustration • mandatory assignments (= “obligs”) Parser a[index] = 4 + 2 : • O 1 published mid-February, deadline mid-March parse tree/syntax tree a[index] = 4 + 2 : abstract syntax tree
• O 2 published beginning of April, deadline beginning of May • group work up-to 3 people recommended. Please inform us about such planned group collaboration • slides: see updates on the net 1. Exam 12th June, 09:00 , 4 hours, written, open-book • not everyone is actually building a full-blown compiler, but • fundamental concepts and techniques in CC • most, if not basically all, software reads, processes/transforms and outputs “data” ⇒ often involves techniques central to CC • understanding compilers ⇒ deeper understanding of programming language(s) • new languages (domain specific, graphical, new language paradigms and constructs. . . ) ⇒ CC & their principles will never be “out-of-fashion”.
INF5110 – Compiler Construction Section Targets Targets & Outline Introduction Compiler architecture & phases 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 Chapter 1 “Introduction” C-style preprocessor: macros Course “Compiler Construction” Scanner (lexer . . . ) Scanner: illustration Martin Steffen Parser a[index] = 4 + 2 : Spring 2020 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 Figure: Structure of a typical compiler 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 & • either separate program or integrated into compiler phases Architecture of a typical • nowadays: C-style preprocessing sometimes seen as compiler Anatomy of a compiler “hack” grafted on top of a compiler. Pre-processor C-style preprocessor • examples (see next slide): examples C-style preprocessor: • file inclusion macros Scanner (lexer . . . ) • macro definition and expansion Scanner: illustration • conditional code/compilation: Note: #if is not the Parser a[index] = 4 + 2 : parse tree/syntax tree same as the if -programming-language construct. a[index] = 4 + 2 : abstract syntax tree
• problem: often messes up the line numbers (among other things) #i n c l u d e <filename > INF5110 – Compiler Construction Listing 1: file inclusion Targets # v a r d e f #a = 5 ; #c = #a+1 Targets & Outline . . . #i f (#a < #b ) Introduction . . Course info #e l s e Course material and plan Motivation: What is CC . . . good for? #e n d i f Compiler architecture & phases Listing 2: Conditional compilation Architecture of a typical compiler Anatomy of a compiler Pre-processor C-style preprocessor examples # macrodef hentdata (#1,#2) C-style preprocessor: − #1 macros − − − − − − Scanner (lexer . . . ) #2 −−− (#1) −−− Scanner: illustration # enddef 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 INF5110 – Compiler − k ar i − Construction − − − − − per −−− (k a r i) −−− Targets Targets & Outline • input: “the program text” ( = string, char stream, or Introduction similar) Course info Course material and plan • task Motivation: What is CC good for? Compiler • divide and classify into tokens , and architecture & phases • remove blanks, newlines, comments .. Architecture of a typical compiler Anatomy of a compiler • theory: finite state automata, regular languages Pre-processor C-style preprocessor examples C-style preprocessor: macros Scanner (lexer . . . ) Scanner: illustration a [ index ] ␣=␣4␣+␣2 Parser a[index] = 4 + 2 : parse tree/syntax tree a[index] = 4 + 2 : abstract syntax tree
lexeme token class value identifier a "a" left bracket [ identifier index "index" INF5110 – right bracket ] Compiler Construction assignment = number 4 "4" Targets plus sign + Targets & Outline number 2 "2" 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 INF5110 – expr expr = Compiler Construction subscript expr additive expr Targets Targets & Outline expr expr expr expr [ ] + Introduction Course info Course material and plan identifier identifier number number Motivation: What is CC good for? index a 4 2 Compiler architecture & phases Architecture of a typical assign-expr compiler Anatomy of a compiler Pre-processor C-style preprocessor subscript expr additive expr examples C-style preprocessor: macros Scanner (lexer . . . ) identifier identifier number number Scanner: illustration Parser a index 2 4 a[index] = 4 + 2 : parse tree/syntax tree a[index] = 4 + 2 : abstract syntax tree
• one standard, general outcome of semantic analysis: “annotated” or “decorated” AST • additional info (non context-free): INF5110 – • bindings for declarations Compiler Construction • (static) type information Targets Targets & Outline assign-expr : ? Introduction Course info subscript-expr :int additive-expr :int Course material and plan Motivation: What is CC good for? identifier :array of int identifier :int number :int number :int Compiler architecture & phases a :array of int index :int 4 :int 2 :int Architecture of a typical compiler Anatomy of a compiler Pre-processor C-style preprocessor examples C-style preprocessor: • here: identifiers looked up wrt. declaration macros Scanner (lexer . . . ) Scanner: illustration • 4, 2: due to their form, basic types. Parser a[index] = 4 + 2 : parse tree/syntax tree a[index] = 4 + 2 : abstract syntax tree
Recommend
More recommend