COMP 4426421 Compiler Design 1 Click to edit

  • Slides: 10
Download presentation
COMP 442/6421 – Compiler Design 1 Click to edit Master title style COMPILER DESIGN

COMP 442/6421 – Compiler Design 1 Click to edit Master title style COMPILER DESIGN Review Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2000 -2020

COMP 442/6421 – Compiler Design 2 Examination • Objective: to verify that the students

COMP 442/6421 – Compiler Design 2 Examination • Objective: to verify that the students grasp theoretical aspects of compiler design, as taught in class. • Duration: 24 hours home examination. • The examination paper will be posted on April 29 th at 13 h 00. • You will have until April 30 th at 13 h 00 to submit your answer on the EAS. • You are allowed to use any material or tool that you may find online to answer the questions. If you do, you must cite your sources. • You are not allowed to share answers with fellow students. Plagiarism will be checked and reported. Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2000 -2020

COMP 442/6421 – Compiler Design 3 Course review • Compiler architecture • Phases: •

COMP 442/6421 – Compiler Design 3 Course review • Compiler architecture • Phases: • lexical analysis • syntactic analysis • semantic analysis • code optimization • code generation • Front-end, back-end • Intermediate representations • Mechanisms: • parsing tables • symbol table • semantic actions/semantic records • attribute migration • tree traversal • visitor pattern • Functioning/role of each phase/component/mechanisms • Optionality of some phases Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2000 -2020

COMP 442/6421 – Compiler Design 4 Course review source code token stream syntax tree

COMP 442/6421 – Compiler Design 4 Course review source code token stream syntax tree annotated tree front-end back-end intermediate code target code Concordia University Department of Computer Science and Software Engineering optimized target code Joey Paquet, 2000 -2020

COMP 442/6421 – Compiler Design 5 Course review • Lexical analysis • Roles •

COMP 442/6421 – Compiler Design 5 Course review • Lexical analysis • Roles • • White space removal Processing comments Check and recover from lexical errors Creation of a stream of tokens • Design • Translation of regular expression into a DFA • Thompson construction • Rabin–Scott power set construction • Implementation • Case statement or state transition table/algorithm • Notable examination questions • Generate a NDFA from regular expressions (Thompson’s construction) • Generate a DFA from NDFA (Rabin-Scott power set construction) Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2000 -2020

COMP 442/6421 – Compiler Design 6 Course review • Syntactic analysis • Roles •

COMP 442/6421 – Compiler Design 6 Course review • Syntactic analysis • Roles • Analyze the program’s structure • Check, report and recover from syntax errors leading to useful and comprehensive compiler output • Design • Generative context-free grammars, generating a derivation proving the validity of the input program according to the grammar • First and follow sets • Grammar transformation (removal of left recursions, ambiguities) • All designs are based on a stack mechanism • Top-down: predictive parsing, recursive descent, table-driven (requires removal of left recursions, ambiguities) • Bottom-up: SLR, CLR, LALR (item generation) • Error recovery using “synchronizing tokens” • AST generation as intermediate representation • Attribute migration, semantic stack Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2000 -2020

COMP 442/6421 – Compiler Design 7 Course review • Syntactic analysis (cont. ) •

COMP 442/6421 – Compiler Design 7 Course review • Syntactic analysis (cont. ) • Implementation • Recursive descent top-down: each production is implemented as a function matching terminals and calling other such functions to parse non-terminals according to other rules • Table-driven top-down: table is constructed using the first and follow sets, based on the notion of “generative grammar” • Bottom-up: SLR, CLR, LALR: creation of a DFA using items and first and follow sets, creation of the state transition table with “action” and “goto” parts, called a “shift/reduce” parser • Notable examination questions • Given a grammar, generate a table for a table-driven top-down predictive parser • Given a grammar, eliminate left recursions and ambiguities • Given a grammar and a valid sentence, provide a derivation proving that this sentence is derivable from the grammar • Given a grammar, generate the item sets (CLR, SLR, LALR), then generate the corresponding state transition table and/or state transition diagram • Given a state transition table and a token stream, execute a parse trace for any of the above bottom-up parsing techniques Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2000 -2020

COMP 442/6421 – Compiler Design 8 Course review • Semantic analysis/translation • Roles •

COMP 442/6421 – Compiler Design 8 Course review • Semantic analysis/translation • Roles • Verify the semantic validity of the program • Translate the program into executable code • Design • Symbol table • Intermediate representations, intermediate code (optional) • Optimization, high level and/or low level (optional) • Semantic actions and semantic records • AST traversal and the Visitor design pattern Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2000 -2020

COMP 442/6421 – Compiler Design 9 Course review • Semantic analysis/translation (cont. ) •

COMP 442/6421 – Compiler Design 9 Course review • Semantic analysis/translation (cont. ) • Implementation • Symbol table: nested tables to manage scoping • Intermediate representation: trees, directed acyclic graphs, tree traversal/analysis algorithms for further processing • Intermediate code: postfix notation, three-address code, quadruples, pcode, byte code • Notable examination questions • Given a program, sketch its corresponding symbol table structure. • Given a simple statement/expression write the corresponding Moon code translation. Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2000 -2020

COMP 442/6421 – Compiler Design 10 References • Joey Paquet. COMP 442/6421 course web

COMP 442/6421 – Compiler Design 10 References • Joey Paquet. COMP 442/6421 course web site. Lecture slides. • Joey Paquet. COMP 442/6421 course web site. Exercise slides. • C. N. Fischer, R. K. Cytron, R. J. Le. Blanc Jr. , Crafting a Compiler, Adison-Wesley, 2009. • T. W. Parsons. Introduction to Compiler Construction, W. H. Freeman and Company, 1992. • A. V. Aho, R. Sethi and J. D. Ullman. Compilers, Principles, Techniques, and Tools, Addison-Wesley, 1986. K. C. Louden. • Compiler Construction: Principles and Practice, International Thomson Publishing Inc. , 1997. Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2000 -2020