1 / 19

October 15, 2014

Computer Science at Azusa Pacific University. CS400 Compiler Construction. Sheldon X. Liang Ph. D. October 15, 2014. Azusa, CA. 1. October 15, 2014. Azusa Pacific University, Azusa, CA 91702, Tel: (800) 8 25-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/.

pelham
Télécharger la présentation

October 15, 2014

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computer Science at Azusa Pacific University CS400 Compiler Construction Sheldon X. Liang Ph. D. October 15, 2014 Azusa, CA 1 October 15, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/

  2. CS@APU: CS400 Compiler Construction A Simple Syntax-Directed Translator-- One-Pass Compiler to Generate Bytecode for the JVM Chapter 2 2 October 15, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/

  3. CS@APU: CS400 Compiler Construction Overview • This chapter contains introductory material to Chapters 3 to 8 of the Dragon book  Text: Compilers -- Principles, Techniques, & Tools (2nd Ed) by Alfred V. Aho, Monica S. Lam, Ravi Sethi, and Jeffrey D. Ullman • Combined with material on the JVM to prepare for the laboratory assignments  FYI: “The JavaTM Virtual Machine Specification”, 2nd edition and class handouts 3 October 15, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/

  4. CS@APU: CS400 Compiler Construction Building a Simple Compiler • Building a compiler involves: • Defining the syntax of a programming language • Develop a source code parser: for our compiler we will use predictive parsing • Implementing syntax directed translation to generate intermediate code: our target is the JVM abstract stack machine • Generating Java bytecode for the JVM • Optimize the Java bytecode (optional) 4 October 15, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/

  5. CS@APU: CS400 Compiler Construction The Structure of the Compiler Lexical analyzer Syntax-directedtranslator Characterstream Tokenstream Javabytecode Developparser and codegenerator for translator Syntax definition(BNF grammar) JVM specification 5 October 15, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/

  6. CS@APU: CS400 Compiler Construction Keep in mind following questions • Syntax definition • What is it? • What is ambiguity? • How to remove ambiguity? • Why we need syntax • How to derive from syntax? • What inspires you? • What is your reflection • Like it, why? • Hate it, why? 6 October 15, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/

  7. CS@APU: CS400 Compiler Construction Syntax Definition • Context-free grammar is a 4-tuple with • A set of tokens (terminal symbols) • A set of nonterminals • A set of productions • A designated start symbol 7 October 15, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/

  8. CS@APU: CS400 Compiler Construction Example Grammars Context-free grammar for simple expressions: G = <{list,digit}, {+,-,0,1,2,3,4,5,6,7,8,9}, P, list> with productions P = list [list+|-]digit list list+digit digit 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 list list-digit list [list+ | -] (0 | 1 | … | 9) list digit digit 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 8 October 15, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/

  9. CS@APU: CS400 Compiler Construction Derivation • Given a CF grammar we can determine the set of all strings (sequences of tokens) generated by the grammar using derivation • We begin with the start symbol • In each step, we replace one nonterminal in the current sentential form with one of the right-hand sides of a production for that nonterminal 9 October 15, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/

  10. CS@APU: CS400 Compiler Construction Derivation for the Example Grammar listlist+digit list-digit+digit digit-digit+digit 9 -digit+digit 9 - 5 +digit 9 - 5 + 2 This is an example leftmost derivation, because we replacedthe leftmost nonterminal (underlined) in each step.Likewise, a rightmost derivation replaces the rightmostnonterminal in each step 10 October 15, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/

  11. CS@APU: CS400 Compiler Construction Parse Trees • The root of the tree is labeled by the start symbol • Each leaf of the tree is labeled by a terminal (=token) or  • Each interior node is labeled by a nonterminal • If AX1 X2 … Xn is a production, then node A has immediate childrenX1, X2, …, Xn where Xi is a (non)terminal or  ( denotes the empty string) 11 October 15, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/

  12. CS@APU: CS400 Compiler Construction Parse Tree for the Example Grammar Parse tree of the string 9-5+2 using grammar G list list digit list digit digit The sequence ofleafs is called theyield of the parse tree 9 - 5 + 2 12 October 15, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/

  13. CS@APU: CS400 Compiler Construction Ambiguity Consider the following context-free grammar: G = <{string}, {+,-,0,1,2,3,4,5,6,7,8,9}, P, string> with production P = string string+string | string-string | 0 | 1 | … | 9 This grammar is ambiguous, because more than one parse treerepresents the string 9-5+2 13 October 15, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/

  14. CS@APU: CS400 Compiler Construction Ambiguity string string string string string string string string string string 9 - 5 + 2 9 - 5 + 2 14 October 15, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/

  15. CS@APU: CS400 Compiler Construction Associativity of Operators Left-associative operators have left-recursive productions left left+term | term String a+b+c has the same meaning as (a+b)+c Right-associative operators have right-recursive productions right term=right | term String a=b=c has the same meaning as a=(b=c) 15 October 15, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/

  16. CS@APU: CS400 Compiler Construction Precedence of Operators Operators with higher precedence “bind more tightly” expr expr+term | termterm  term *factor | factorfactor  number | ( expr ) String 2+3*5 has the same meaning as 2+(3*5) expr expr term term term factor factor factor number number number 2 + 3 * 5 16 October 15, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/

  17. CS@APU: CS400 Compiler Construction Syntax of Statements Assignment stmt id := expr | if expr then stmt | if expr then stmt else stmt | while expr do stmt | beginopt_stmts endopt_stmts  stmt ; opt_stmts|  Selective Iterative Block 17 October 15, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/

  18. CS@APU: CS400 Compiler Construction Got it with following questions • Syntax definition • What is it? • What is ambiguity? • How to remove ambiguity? • Why we need syntax • How to derive from syntax? • What inspires you? • What is your reflection • Like it, why? • Hate it, why? 18 October 15, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/

  19. CS@APU: CS400 Compiler Construction Syntax Definition Thank you very much! Questions? 19 October 15, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/

More Related