CSE 5317 Midterm Examination 4 March Solutions

Similar documents
Question Points Score

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

CS606- compiler instruction Solved MCQS From Midterm Papers

MIDTERM EXAM (Solutions)

Midterm I (Solutions) CS164, Spring 2002

CSE 401 Midterm Exam Sample Solution 2/11/15

CSE P 501 Exam 8/5/04

ECE 468/573 Midterm 1 September 30, 2015

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

CS 406/534 Compiler Construction Putting It All Together

CS 536 Midterm Exam Spring 2013

List of Figures. About the Authors. Acknowledgments

CS 2210 Sample Midterm. 1. Determine if each of the following claims is true (T) or false (F).

CS5363 Final Review. cs5363 1

Lexical Analysis. Lexical analysis is the first phase of compilation: The file is converted from ASCII to tokens. It must be fast!

EDAN65: Compilers, Lecture 06 A LR parsing. Görel Hedin Revised:

Lexical Analysis. Chapter 2

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

About the Authors... iii Introduction... xvii. Chapter 1: System Software... 1

CSE 401/M501 18au Midterm Exam 11/2/18. Name ID #

CS143 Handout 20 Summer 2011 July 15 th, 2011 CS143 Practice Midterm and Solution

CSE 401 Compilers. LR Parsing Hal Perkins Autumn /10/ Hal Perkins & UW CSE D-1

LR Parsing E T + E T 1 T

CS 4120 Introduction to Compilers

CJT^jL rafting Cm ompiler

A simple syntax-directed

PSD3A Principles of Compiler Design Unit : I-V. PSD3A- Principles of Compiler Design

Implementation of Lexical Analysis

CS 4240: Compilers and Interpreters Project Phase 1: Scanner and Parser Due Date: October 4 th 2015 (11:59 pm) (via T-square)

Introduction to Lexical Analysis

1. Consider the following program in a PCAT-like language.

CSE P 501 Exam 8/5/04 Sample Solution. 1. (10 points) Write a regular expression or regular expressions that generate the following sets of strings.

Compiler Theory. (Semantic Analysis and Run-Time Environments)

CS 164 Handout 11. Midterm Examination. There are seven questions on the exam, each worth between 10 and 20 points.

CS 44 Exam #2 February 14, 2001

CSE P 501 Compilers. LR Parsing Hal Perkins Spring UW CSE P 501 Spring 2018 D-1

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

Lexical Analysis. Lecture 2-4

CSE 401 Midterm Exam Sample Solution 11/4/11

Static Semantics. Winter /3/ Hal Perkins & UW CSE I-1

Context-free grammars

BSCS Fall Mid Term Examination December 2012

1. Explain the input buffer scheme for scanning the source program. How the use of sentinels can improve its performance? Describe in detail.

CSE 582 Autumn 2002 Exam 11/26/02

Midterm 2 Solutions Many acceptable answers; one was the following: (defparameter g1

COP4020 Spring 2011 Midterm Exam

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

Compiler Construction: Parsing

Midterm I - Solution CS164, Spring 2014

Syntax Errors; Static Semantics

Implementation of Lexical Analysis

RYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 97 INSTRUCTIONS

CSE 401 Final Exam. December 16, 2010

Heap, Variables, References, and Garbage. CS152. Chris Pollett. Oct. 13, 2008.

Time : 1 Hour Max Marks : 30

LR Parsing. The first L means the input string is processed from left to right.

Test 1 Summer 2014 Multiple Choice. Write your answer to the LEFT of each problem. 5 points each 1. Preprocessor macros are associated with: A. C B.

CSCE 531 Spring 2009 Final Exam

The role of semantic analysis in a compiler

CPSC 411, Fall 2010 Midterm Examination

CS 415 Midterm Exam Spring SOLUTION

Context-Free Grammar. Concepts Introduced in Chapter 2. Parse Trees. Example Grammar and Derivation

MidTerm Papers Solved MCQS with Reference (1 to 22 lectures)

Lexical Analysis. Lecture 3-4

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

Parsing Scheme (+ (* 2 3) 1) * 1

UMBC CMSC 331 Final Exam

Midterm Exam 2 CS 455, Spring 2011

Question Marks 1 /16 2 /13 3 /12 4 /15 5 /8 6 /15 7 /8 8 /5 9 /8 Total /100

CMSC330 Fall 2016 Midterm #2 2:00pm/3:30pm

Bottom-Up Parsing. Lecture 11-12

Lexical and Syntax Analysis

3.5 Practical Issues PRACTICAL ISSUES Error Recovery

Compiler Design 1. Bottom-UP Parsing. Goutam Biswas. Lect 6

CS164: Midterm I. Fall 2003

LECTURE NOTES ON COMPILER DESIGN P a g e 2

10/4/18. Lexical and Syntactic Analysis. Lexical and Syntax Analysis. Tokenizing Source. Scanner. Reasons to Separate Lexical and Syntactic Analysis

Torben./Egidius Mogensen. Introduction. to Compiler Design. ^ Springer

Lecture 7: Deterministic Bottom-Up Parsing

Lexical Analysis. Implementation: Finite Automata

Compiler Construction

Short Notes of CS201

Programming Languages Third Edition. Chapter 7 Basic Semantics

Problem Score Max Score 1 Syntax directed translation & type

Lexical Analysis. COMP 524, Spring 2014 Bryan Ward

Parsing Algorithms. Parsing: continued. Top Down Parsing. Predictive Parser. David Notkin Autumn 2008

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

CS201 - Introduction to Programming Glossary By

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

Gujarat Technological University Sankalchand Patel College of Engineering, Visnagar B.E. Semester VII (CE) July-Nov Compiler Design (170701)

Semantic Analysis. Lecture 9. February 7, 2018

Final Term Papers 2013

Chapter 3: Lexing and Parsing

In One Slide. Outline. LR Parsing. Table Construction

Lecture 8: Deterministic Bottom-Up Parsing

ECE 468/573 Midterm 1 October 1, 2014

CMSC 202 Final May 19, Name: UserID: (Circle your section) Section: 101 Tuesday 11: Thursday 11:30

LR Parsing - The Items

CIS 341 Midterm March 2, 2017 SOLUTIONS

Transcription:

CSE 5317 Midterm Examination 4 March 2010 1. / [20 pts] Solutions (parts a j; -1 point for each wrong answer, 0 points for each blank answer, 2 point for each correct answer. Therefore, the score for this problem is max(0,2 #correct #incorrect). (a) Every LL(1) grammar is also LR(1). (b) Every LR(1) grammar is unambiguous. (c) The following grammar is LL(1): A B C C B B a B b x C a C b y (d) LL parsers are often implemented with a bottom-up recursive-descent parser. (e) Reference counting garbage collection cannot collect cycles of garbage. (f) A mark sweep garbage collector takes time proportional to the number of objects allocated in the heap. (g) Generational garbage collection is based on the assumption that older objects are more likely to be garbage. 1

(h) A call to a method inherited from more than one superclass is always ambiguous. (i) In the worst case, translation of an NFA to a DFA can result in an automaton with twice as many states. (j) Local variables are allocated on the call stack or in registers. 2. Scanning [12 pts] In a simplified XML, there are three kinds of tags: a start tag, consisting of a <, an identifier, and a > (e.g., <p>), an end tag, consisting of a <, /, an identifier, and a > (e.g., </p>), and a start end tag, consisting of a <, an identifier, a /, and a > (e.g., <br/>). For our purposes, an identifier is a non-empty sequence of letters. The regular expression for a letter is. (a) [4 pts] Write a single regular expression that matches all three kinds of tags. Feel free to define abbreviations as long as the language remains regular. < ( + /? / + ) > (b) [6 pts] Draw, as a circle-and-arrows diagram, an NFA for the regular expression you wrote in (a). Be sure to indicate the start state and any final, accepting states. Use a double circle to indicate an accepting state. / > > < > / 2

(c) [2 pts] Is the NFA you drew in (b) deterministic? Explain why or why not. Yes. Every node has a single out-edge for each symbol. There are no ε- transitions. 3. Parsing [20 pts] Consider the following LL(1) grammar for a subset of Lisp: Pgm Exp $ Exp atom Exp ( Exp Exps ) Exps Exp Exps ε $ is used to indicate end-of-file. ε indicates the empty string. The goal symbol is Pgm. (a) [6 pts] Give a parse tree for (cdr (a b c)) $. (Pgm (Exp (Exps (Exp "cdr") (Exps (Exp " " (Exp "(" (Exp "a") (Exps (Exp "b") (Exps (Exp "c") (Exps))) ")") (Exps)))) "$")) (b) [6 pts] Consider a recursive descent parser running on the same input as in (b). When the is matched, what functions of the parser (including the current function) are active on the parser s call stack? Pgm Exp Exps Exp 3

(c) [6 pts] In an OO programming language of your choice, sketch how you would implement AST classes for this subset of Lisp. Include the superclass and fields of each class, if any; do not bother with methods or constructors. class Pgm { Exp e; } class Exp { } class Atom extends Exp { String name; } class Quote extends Exp { Exp e; } class Apply extends Exp { Exp e; List<Exp> es; } 4. Type-checking, symbol tables [30 pts] In this problem, we will explore how to extend a MiniJava type-checker similar to the one use in the project to support abstract classes and methods. An abstract method cannot have a body. A class with an abstract method must also be declared abstract. A class that inherits an abstract method and does not override it must also be abstract. It is illegal to create (with new) an instance of an abstract class. An abstract method or class is declared with the abstract keyword. The class and method declaration AST classes (ClassDeclaration, ClassExtendsDeclaration, and MethodDeclaration) are extended with a field that indicates whether the class or method is abstract. Since an abstract method cannot have a body, this is indicated in the AST by making the appropriate fields null. Given an implementation of a compiler similar to the one used in the project, describe the changes you would make to the compiler to support abstract classes and methods. You need not write code or give specific names for classes, fields, etc., but be specific and be as complete as possible. Do not assume we know the details of the compiler you are extending beyond the basic structure that was outlined in class and in the project description if a detail or an assumption is important, be sure to state it explicitly. In particular: (a) [10 pts] Describe what changes, if any, must be made to symbol table data structures (including the class table, type descriptors, typing environment, etc.). Add a boolean flag isabstract to the class table entry for each class. Add a boolean flag isabstract to the method entry in the class table. (b) [10 pts] Describe what changes, if any, must be made to the class-table-building pass. At each class declaration, set the isabstract flag in the class table entry if the class is declared abstract. At each method declaration, set the isabstract flag in the method entry if the method is declared abstract. 4

(c) [10 pts] Describe what changes, if any, must be made to the type-checking pass. At each class declaration, if the class is not declared abstract, iterate through all the methods of the class. If a method is declared abstract, report an error. We can define the set of methods we need to check recursively. If C extends B, then methods(c) includes methods declared in C plus methods(b) minus methods declared in C. At each method declaration, check isabstract in the class table is set iff the method has a body. This could be done in the previous pass. At each new expression, lookup in the class table to check if the class being instantiated is abstract. 5. IR [20 pts] In this problem, we ll consider a stack-based IR similar to Java bytecode. Each instruction pops zero or values from the operand stack, performs an operation, and then pushes the result (if any) onto the stack. Note that in this IR, the only values are integers. push n push the integer constant n push x push the value stored in local variable x pop pop a value from the stack store x pop the top-of-stack and store the value into local variable x add pop two operands from the stack and push their sum sub pop two operands from the stack and push their difference goto L jump to label L label L place label L in the instruction stream ifeqz L pop one value from the stack and jump to L if = 0 ifltz L pop one value from the stack and jump to L if < 0 ifgtz L pop one value from the stack and jump to L if > 0 (a) [15 pts] Write the translation of the following constructs into the above IR: i. [3 pts] x = y; push y store x ii. [3 pts] a - (b + 1) push a push b push 1 add sub 5

iii. [4 pts] i = 0; while (true) i = i + 1; push 0 label L push 1 add goto L iv. [5 pts] i = 1; while (i < n) i = i + i; push 1 label L add push n sub ifltz L (b) [5 pts] One constraint on this IR is that the operand stack must be exactly the same height for every jump to a given label L. Why is this constraint needed? What could happen without it? The problem occurs if there are multiple jumps to the same label where the heights differ. The code at the label might pop more elements from the stack than are present. Another problem is that if the stack at the end of a loop body is higher than the stack before the loop body is executed, the stack may grow without bound. 6