Download presentation
Presentation is loading. Please wait.
Published byAnthony Pearson Modified over 8 years ago
1
May 31, 2016 1 May 31, 2016May 31, 2016May 31, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS400 Compiler Construction
2
May 31, 2016 2 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ Administrative info Instructor Name: Sheldon X. Liang E-mail: xliang@apu.edu Office: WC 204 Hours:E-mail to set up appointment CS@APU: CS400 Compiler Construction
3
May 31, 2016 3 CS@APU: CS400 Compiler Construction Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ Administrative info Course webpage http:// cs-linux.cs.apu.edu/~sliang contains: lecture notes & other handouts howeworks & manuals policies, grades useful links
4
May 31, 2016 4 CS@APU: CS400 Compiler Construction Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ 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 Ada to C++ How was the first compiler created?
5
May 31, 2016 5 CS@APU: CS400 Compiler Construction Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ Why study compilers? Typical front-end technology: Grammars e.g. human-computer interaction => HCI Regular expressions e.g. limited rules to represent unlimited expressions Parsers e.g. grasp meanings from information stream Syntax-directed e.g. guide to “speak” in right way
6
May 31, 2016 6 CS@APU: CS400 Compiler Construction Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ Why study compilers? Technology found use outside compilers: Used in tools that find bugs in software e.g. human-computer interaction => HCI Used in tools that find security holes in existing code e.g. limited rules to represent unlimited expressions Applied to a wide range of problems e.g. in software design and software development We learn something not just for something Competence of problem-solving Solve new problems we will encounter in the future
7
May 31, 2016 7 CS@APU: CS400 Compiler Construction Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ 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
8
May 31, 2016 8 CS@APU: CS400 Compiler Construction Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ Features of compilers Correctness preserve the meaning of the code check syntactic mistakes alert potential problems Speed of target code Cooperation with debugger Support for separate compilation Good error reporting / handling
9
May 31, 2016 9 CS@APU: CS400 Compiler Construction Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ Compiler structure Use intermediate representation why? source code target code Front EndBack End IR
10
May 31, 2016 10 CS@APU: CS400 Compiler Construction Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ Compiler structure Front end Recognize legal/ illegal programs Generate IR (for further process) The process can be automated !!! Back end Translate IR into target code instruction selection register allocation instruction scheduling
11
May 31, 2016 11 CS@APU: CS400 Compiler Construction Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ Compiler structure
12
May 31, 2016 12 CS@APU: CS400 Compiler Construction Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ 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 what you think and what you get
13
May 31, 2016 13 CS@APU: CS400 Compiler Construction Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ The Front End Scanning (aka lexical analysis) recognize “words” (tokens) key words, variables etc. Parsing (aka syntax analysis) check syntax if - then not if - else Semantic analysis examine meaning (e.g. type checking) duplicate declaration
14
May 31, 2016 14 CS@APU: CS400 Compiler Construction Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ Compiler: structure and process Scanning… Parsing… Checking… IR
15
May 31, 2016 15 CS@APU: CS400 Compiler Construction Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ Compiler tool: lexical analysis Scanner Its job : recognize words (tokens) e.g. x = 1 becomes ID equal Int collect identifier information e.g. ID corresponds to x and its type Ignore white space and comments report errors Good news :~) the process can be automated
16
May 31, 2016 16 CS@APU: CS400 Compiler Construction Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ Compiler tool: syntax analysis Parser Its job : Check & verify syntax based on rules e.g. “ID equal Int” makes 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
17
May 31, 2016 17 CS@APU: CS400 Compiler Construction Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ Compiler tool: semantic analysis checker Its job : Check the meaning of the program e.g. x = y; are x, y declared properly? e.g. x = y; is y defined before being used? e.g. x = y; are theirs types compatible? Meaning may depend on context Report errors (less effectively) Report warnings (more effectively)
18
May 31, 2016 18 CS@APU: CS400 Compiler Construction Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ Compiler result: intermediate IRs Intermediate Representations : Graphical e.g. parse tree, DAG (directed acyclic graph) Linear e.g. three address code (one operator): x+y*z ==> t 1 = y*z / t 2 = x+t 1 Hybrid e.g. linear for blocks of straight-line code, a graph to connect blocks Low-level or high-level machine-dependent or machine-independent
19
May 31, 2016 19 CS@APU: CS400 Compiler Construction Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ Compiler result: intermediate IRs Intermediate Representations : Graphical e.g. parse tree, DAG (directed acyclic graph) Linear e.g. three address code (one operator): x+y*z ==> t 1 = y*z / t 2 = x+t 1 Hybrid e.g. linear for blocks of straight-line code, a graph to connect blocks Low-level or high-level machine-dependent or machine-independent
20
May 31, 2016 20 CS@APU: CS400 Compiler Construction Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ Compiler process: scanning Scanning Lexical analysis : Main goal: recognize words How? by recognizing patterns e.g. an ID 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)
21
May 31, 2016 21 CS@APU: CS400 Compiler Construction Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ Compiler process: scanning Scanning Definition: regular expression (over alphabet ) is an RE denoting { } If , then is an RE denoting { } If r and s are Res, then (r) => RE denoting L(r) r | s => RE denoting L(r) L(s) rs => RE denoting L(r)L(s) r* => RE denoting the Kleene closure of L(r) Property: REs are closed under many operations This allows us to build complex REs
22
May 31, 2016 22 CS@APU: CS400 Compiler Construction Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ Compiler process: scanning Scanning Definition: Deterministic Finite Automaton(DFA) A 5-tuple ( , S, , s 0, F) where is the alphabet S is he set of states is the transition function (S S ) s 0 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!
23
May 31, 2016 23 CS@APU: CS400 Compiler Construction Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ Compiler process: scanning Scanning 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
24
Thank you very much! Questions? May 31, 2016 24 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction Introduction to Compiler
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.