SlideShare a Scribd company logo
1 of 18
1
Compiler Construction
Vana Doufexi
2
Administrative info
 Instructor
 Name: Vana Doufexi
 E-mail: vdoufexi@cs.northwestern.edu
 Office: Ford Building, #2-229
 Hours: E-mail to set up appointment
 Teaching Assistant
 TBA
3
Administrative info
 Course webpage
 http://www.cs.northwestern.edu/academics/courses/322
 contains:
 news
 staff information
 lecture notes & other handouts
 homeworks & manuals
 policies, grades
 newsgroup info
 useful links
 Newsgroup
 Name: cs.322
 nntp: news.cs.northwestern.edu
4
What is a compiler
 A program that reads a program written in some
language and translates it into a program written in
some other language
 Modula-2 to C
 Java to bytecodes
 COOL to MIPS code
 How was the first compiler created?
5
Why study compilers?
 Application of a wide range of theoretical techniques
 Data Structures
 Theory of Computation
 Algorithms
 Computer Architecture
 Good SW engineering experience
 Better understanding of programming languages
6
Features of compilers
 Correctness
 preserve the meaning of the code
 Speed of target code
 Speed of compilation
 Good error reporting/handling
 Cooperation with the debugger
 Support for separate compilation
7
Compiler structure
 Use intermediate representation
 Why?
source
code
target
code
Front End Back End
IR
8
Compiler Structure
 Front end
 Recognize legal/illegal programs
 report/handle errors
 Generate IR
 The process can be automated
 Back end
 Translate IR into target code
 instruction selection
 register allocation
 instruction scheduling
 lots of NPC problems -- use approximations
9
Compiler Structure
 Optimization
 goals
 improve running time of generated code
 improve space, power consumption, etc.
 how?
 perform a number of transformations on the IR
 multiple passes
 important: preserve meaning of code
10
The Front End
 Scanning (a.k.a. lexical analysis)
 recognize "words" (tokens)
 Parsing (a.k.a. syntax analysis)
 check syntax
 Semantic analysis
 examine meaning (e.g. type checking)
 Other issues:
 symbol table (to keep track of identifiers)
 error detection/reporting/recovery
11
The Scanner
 Its job:
 given a character stream, recognize words (tokens)
 e.g. x = 1 becomes IDENTIFIER EQUAL INTEGER
 collect identifier information
 e.g. IDENTIFIER corresponds to a lexeme (the actual
word x) and its type (acquired from the declaration of x).
 ignore white space and comments
 report errors
 Good news
 the process can be automated
12
The Parser
 Its job:
 Check and verify syntax based on specified syntax rules
 e.g. IDENTIFIER LPAREN RPAREN make up an
EXPRESSION.
 Coming soon: how context-free grammars specify syntax
 Report errors
 Build IR
 often a syntax tree
 Good news
 the process can be automated
13
Semantic analysis
 Its job:
 Check the meaning of the program
 e.g. In x=y, is y defined before being used? Are x and y
declared?
 e.g. In x=y, are the types of x and y such that you can
assign one to the other?
 Meaning may depend on context
 Report errors
14
IRs
 Graphical
 e.g. parse tree, DAG
 Linear
 e.g. three-address code
 Hybrid
 e.g. linear for blocks of straight-line code, a graph to
connect blocks
 Low-level or high-level
15
The scanning process
 Main goal: recognize words
 How? by recognizing patterns
 e.g. an identifier is a sequence of letters or digits that starts
with a letter.
 Lexical patterns form a regular language
 Regular languages are described using regular
expressions (REs)
 Can we create an automatic RE recognizer?
 Yes! (Hold that thought)
16
The scanning process
 Definition: Regular expressions (over alphabet )
  is an RE denoting {}
 If , then  is an RE denoting {}
 If r and s are REs, then
 (r) is an RE denoting L(r)
 r|s is an RE denoting L(r)L(s)
 rs is an RE denoting L(r)L(s)
 r* is an RE denoting the Kleene closure of L(r)
 Property: REs are closed under many operations
 This allows us to build complex REs.
17
The scanning process
 Definition: Deterministic Finite Automaton
 a five-tuple (, S, , s0, F) where
  is the alphabet
 S is the set of states
  is the transition function (SS)
 s0 is the starting state
 F is the set of final states (F  S)
 Notation:
 Use a transition diagram to describe a DFA
 DFAs are equivalent to REs
 Hey! We just came up with a recognizer!
18
The scanning process
 Goal: automate the process
 Idea:
 Start with an RE
 Build a DFA
 How?
 We can build a non-deterministic finite automaton
(Thompson's construction)
 Convert that to a deterministic one
(Subset construction)
 Minimize the DFA
(Hopcroft's algorithm)
 Implement it
 Existing scanner generator: flex

More Related Content

Similar to 01.ppt

cs241-f06-final-overview
cs241-f06-final-overviewcs241-f06-final-overview
cs241-f06-final-overview
Colin Bell
 

Similar to 01.ppt (20)

lec00-Introduction.pdf
lec00-Introduction.pdflec00-Introduction.pdf
lec00-Introduction.pdf
 
Compiler design Introduction
Compiler design IntroductionCompiler design Introduction
Compiler design Introduction
 
Lexical analyzer
Lexical analyzerLexical analyzer
Lexical analyzer
 
Cs6660 compiler design may june 2016 Answer Key
Cs6660 compiler design may june 2016 Answer KeyCs6660 compiler design may june 2016 Answer Key
Cs6660 compiler design may june 2016 Answer Key
 
Ch 2.pptx
Ch 2.pptxCh 2.pptx
Ch 2.pptx
 
Compiler_Lecture1.pdf
Compiler_Lecture1.pdfCompiler_Lecture1.pdf
Compiler_Lecture1.pdf
 
11700220036.pdf
11700220036.pdf11700220036.pdf
11700220036.pdf
 
Compiler design important questions
Compiler design   important questionsCompiler design   important questions
Compiler design important questions
 
cs241-f06-final-overview
cs241-f06-final-overviewcs241-f06-final-overview
cs241-f06-final-overview
 
LANGUAGE TRANSLATOR
LANGUAGE TRANSLATORLANGUAGE TRANSLATOR
LANGUAGE TRANSLATOR
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
Computational model language and grammar bnf
Computational model language and grammar bnfComputational model language and grammar bnf
Computational model language and grammar bnf
 
Software_engineering.pptx
Software_engineering.pptxSoftware_engineering.pptx
Software_engineering.pptx
 
F# for Scala developers
F# for Scala developersF# for Scala developers
F# for Scala developers
 
3a. Context Free Grammar.pdf
3a. Context Free Grammar.pdf3a. Context Free Grammar.pdf
3a. Context Free Grammar.pdf
 
Prof. Chethan Raj C, BE, M.Tech (Ph.D) Dept. of CSE. System Software & Operat...
Prof. Chethan Raj C, BE, M.Tech (Ph.D) Dept. of CSE. System Software & Operat...Prof. Chethan Raj C, BE, M.Tech (Ph.D) Dept. of CSE. System Software & Operat...
Prof. Chethan Raj C, BE, M.Tech (Ph.D) Dept. of CSE. System Software & Operat...
 
Programming for Problem Solving
Programming for Problem SolvingProgramming for Problem Solving
Programming for Problem Solving
 
Compiler gate question key
Compiler gate question keyCompiler gate question key
Compiler gate question key
 
Compiler Design.pptx
Compiler Design.pptxCompiler Design.pptx
Compiler Design.pptx
 
Chapter-3 compiler.pptx course materials
Chapter-3 compiler.pptx course materialsChapter-3 compiler.pptx course materials
Chapter-3 compiler.pptx course materials
 

Recently uploaded

AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
ankushspencer015
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Dr.Costas Sachpazis
 

Recently uploaded (20)

Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGMANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 

01.ppt

  • 2. 2 Administrative info  Instructor  Name: Vana Doufexi  E-mail: vdoufexi@cs.northwestern.edu  Office: Ford Building, #2-229  Hours: E-mail to set up appointment  Teaching Assistant  TBA
  • 3. 3 Administrative info  Course webpage  http://www.cs.northwestern.edu/academics/courses/322  contains:  news  staff information  lecture notes & other handouts  homeworks & manuals  policies, grades  newsgroup info  useful links  Newsgroup  Name: cs.322  nntp: news.cs.northwestern.edu
  • 4. 4 What is a compiler  A program that reads a program written in some language and translates it into a program written in some other language  Modula-2 to C  Java to bytecodes  COOL to MIPS code  How was the first compiler created?
  • 5. 5 Why study compilers?  Application of a wide range of theoretical techniques  Data Structures  Theory of Computation  Algorithms  Computer Architecture  Good SW engineering experience  Better understanding of programming languages
  • 6. 6 Features of compilers  Correctness  preserve the meaning of the code  Speed of target code  Speed of compilation  Good error reporting/handling  Cooperation with the debugger  Support for separate compilation
  • 7. 7 Compiler structure  Use intermediate representation  Why? source code target code Front End Back End IR
  • 8. 8 Compiler Structure  Front end  Recognize legal/illegal programs  report/handle errors  Generate IR  The process can be automated  Back end  Translate IR into target code  instruction selection  register allocation  instruction scheduling  lots of NPC problems -- use approximations
  • 9. 9 Compiler Structure  Optimization  goals  improve running time of generated code  improve space, power consumption, etc.  how?  perform a number of transformations on the IR  multiple passes  important: preserve meaning of code
  • 10. 10 The Front End  Scanning (a.k.a. lexical analysis)  recognize "words" (tokens)  Parsing (a.k.a. syntax analysis)  check syntax  Semantic analysis  examine meaning (e.g. type checking)  Other issues:  symbol table (to keep track of identifiers)  error detection/reporting/recovery
  • 11. 11 The Scanner  Its job:  given a character stream, recognize words (tokens)  e.g. x = 1 becomes IDENTIFIER EQUAL INTEGER  collect identifier information  e.g. IDENTIFIER corresponds to a lexeme (the actual word x) and its type (acquired from the declaration of x).  ignore white space and comments  report errors  Good news  the process can be automated
  • 12. 12 The Parser  Its job:  Check and verify syntax based on specified syntax rules  e.g. IDENTIFIER LPAREN RPAREN make up an EXPRESSION.  Coming soon: how context-free grammars specify syntax  Report errors  Build IR  often a syntax tree  Good news  the process can be automated
  • 13. 13 Semantic analysis  Its job:  Check the meaning of the program  e.g. In x=y, is y defined before being used? Are x and y declared?  e.g. In x=y, are the types of x and y such that you can assign one to the other?  Meaning may depend on context  Report errors
  • 14. 14 IRs  Graphical  e.g. parse tree, DAG  Linear  e.g. three-address code  Hybrid  e.g. linear for blocks of straight-line code, a graph to connect blocks  Low-level or high-level
  • 15. 15 The scanning process  Main goal: recognize words  How? by recognizing patterns  e.g. an identifier is a sequence of letters or digits that starts with a letter.  Lexical patterns form a regular language  Regular languages are described using regular expressions (REs)  Can we create an automatic RE recognizer?  Yes! (Hold that thought)
  • 16. 16 The scanning process  Definition: Regular expressions (over alphabet )   is an RE denoting {}  If , then  is an RE denoting {}  If r and s are REs, then  (r) is an RE denoting L(r)  r|s is an RE denoting L(r)L(s)  rs is an RE denoting L(r)L(s)  r* is an RE denoting the Kleene closure of L(r)  Property: REs are closed under many operations  This allows us to build complex REs.
  • 17. 17 The scanning process  Definition: Deterministic Finite Automaton  a five-tuple (, S, , s0, F) where   is the alphabet  S is the set of states   is the transition function (SS)  s0 is the starting state  F is the set of final states (F  S)  Notation:  Use a transition diagram to describe a DFA  DFAs are equivalent to REs  Hey! We just came up with a recognizer!
  • 18. 18 The scanning process  Goal: automate the process  Idea:  Start with an RE  Build a DFA  How?  We can build a non-deterministic finite automaton (Thompson's construction)  Convert that to a deterministic one (Subset construction)  Minimize the DFA (Hopcroft's algorithm)  Implement it  Existing scanner generator: flex