CSE-304 Compiler Design

Similar documents
CSE 582 Autumn 2002 Exam 11/26/02

CSE3322 Programming Languages and Implementation

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

CS143 Final Fall 2009

CSE 431S Final Review. Washington University Spring 2013

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

CSCI Compiler Design

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

MIDTERM EXAM (Solutions)

Question Marks 1 /20 2 /16 3 /7 4 /10 5 /16 6 /7 7 /24 Total /100

Notes on the Exam. Question 1. Today. Comp 104:Operating Systems Concepts 11/05/2015. Revision Lectures (separate questions and answers)

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

2. Reachability in garbage collection is just an approximation of garbage.

COS 226 Midterm Exam, Spring 2009

CSE 5317 Midterm Examination 4 March Solutions

CSCI Compiler Design

Comp 204: Computer Systems and Their Implementation. Lecture 25a: Revision Lectures (separate questions and answers)

Semantic actions for declarations and expressions

CSE P 501 Exam 12/1/11

CSE 401 Final Exam. March 14, 2017 Happy π Day! (3/14) This exam is closed book, closed notes, closed electronics, closed neighbors, open mind,...

COMP 181 Compilers. Administrative. Last time. Prelude. Compilation strategy. Translation strategy. Lecture 2 Overview

Project 2 Interpreter for Snail. 2 The Snail Programming Language

CSE P 501 Exam 8/5/04

1 Lexical Considerations

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part 1

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

First Midterm Exam CS164, Fall 2007 Oct 2, 2007

CS 536 Midterm Exam Spring 2013

Semantic actions for declarations and expressions

JAM 16: The Instruction Set & Sample Programs

APT Session 6: Compilers

NOTE: Answer ANY FOUR of the following 6 sections:

Question 1. Notes on the Exam. Today. Comp 104: Operating Systems Concepts 11/05/2015. Revision Lectures

CPSC 211, Sections : Data Structures and Implementations, Honors Final Exam May 4, 2001

Semantic actions for declarations and expressions. Monday, September 28, 15

CSE P 501 Exam Sample Solution 12/1/11

Action Table for CSX-Lite. LALR Parser Driver. Example of LALR(1) Parsing. GoTo Table for CSX-Lite

CS 432 Fall Mike Lam, Professor. Code Generation

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

CSE 401 Final Exam. December 16, 2010

Code Generation. Frédéric Haziza Spring Department of Computer Systems Uppsala University

Time : 1 Hour Max Marks : 30

Homework #3: CMPT-379 Distributed on Oct 23; due on Nov 6 Anoop Sarkar

APT Session 7: Compilers

A Simple Syntax-Directed Translator

Defining syntax using CFGs

The SPL Programming Language Reference Manual

UNIVERSITY OF CALIFORNIA

Compilers. Compiler Construction Tutorial The Front-end

Parsing a primer. Ralf Lämmel Software Languages Team University of Koblenz-Landau

Problem Score Max Score 1 Syntax directed translation & type

Syntax Analysis. Amitabha Sanyal. ( as) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay

CS 164 Handout 16. Final Examination. There are nine questions on the exam, some in multiple parts. You have 3 hours to work on the

CS241 Final Exam Review

Agenda. CSE P 501 Compilers. Java Implementation Overview. JVM Architecture. JVM Runtime Data Areas (1) JVM Data Types. CSE P 501 Su04 T-1

LANGUAGE RUNTIME NON-VOLATILE RAM AWARE SWAPPING

CS164: Programming Assignment 5 Decaf Semantic Analysis and Code Generation

CSE 12 Week Eight, Lecture One

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

Run-time Environments

Run-time Environments

EXAM. CS331 Compiler Design Spring Please read all instructions, including these, carefully

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

OF VICTORIA EXAMINATIONS- DECEMBER 2010 CSC

Summary: Direct Code Generation

Final Exam. 12 December 2018, 120 minutes, 26 questions, 100 points

Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004

Class Information ANNOUCEMENTS

Midterm 2. CMSC 430 Introduction to Compilers Fall 2018

Configuration Sets for CSX- Lite. Parser Action Table

Using an LALR(1) Parser Generator

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

HW 3: Bottom-Up Parsing Techniques

A Tour of Language Implementation

JVM. What This Topic is About. Course Overview. Recap: Interpretive Compilers. Abstract Machines. Abstract Machines. Class Files and Class File Format

1. (a) What are the closure properties of Regular sets? Explain. (b) Briefly explain the logical phases of a compiler model. [8+8]

CSCE 531, Spring 2015 Final Exam Answer Key

Final CSE 131 Fall 2014

Question Points Score

WARNING for Autumn 2004:

CS2102, D15 Exam 1. Name:

CSE-304 Programming Assignment #4

MODEL ANSWERS COMP36512, May 2016

EXAMINATIONS 2014 TRIMESTER 1 SWEN 430. Compiler Engineering. This examination will be marked out of 180 marks.

CS164: Midterm I. Fall 2003

Programming Language Concepts Scoping. Janyl Jumadinova January 31, 2017

COP5621 Exam 3 - Spring 2005

Semantic Processing. Semantic Errors. Semantics - Part 1. Semantics - Part 1

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

University of Toronto Department of Electrical and Computer Engineering. Midterm Examination. ECE 345 Algorithms and Data Structures Fall 2012

Decaf Language Reference Manual

ECSE 321 Assignment 2

ECE251 Midterm practice questions, Fall 2010

How do LL(1) Parsers Build Syntax Trees?

Computer Science Foundation Exam. May 3, Section I A. No Calculators! Name: SSN: In this section of the exam, there are three (3) problems.

CS 3360 Design and Implementation of Programming Languages. Exam 1

ESC101 : Fundamental of Computing

Largest Online Community of VU Students

ASTS, GRAMMARS, PARSING, TREE TRAVERSALS. Lecture 14 CS2110 Fall 2018

Finish Lec12 TREES, PART 2. Announcements. JavaHyperText topics. Trees, re-implemented. Iterate through data structure 3/7/19

Transcription:

CSE-304 Compiler Design Fall 09 Final Exam December 14, 2009 Duration: 2 hours, 30 minutes. Maximum Score: 30 Name: USB ID Number: INSTRUCTIONS Read the following carefully before answering any question. Make sure you have filled in your name and USB ID number in the space above. Write your answers in the space provided; Keep your answers brief and precise. The exam consists of 8 questions, in 19 pages (including this page) for a total of 30 points. Pages 17 19 of the exam are intentionally blank. Question Max. Score 1. 3 2. 4 3. 4 4. 3 5. 2 6. 5 7. 4 8. 5 Total: 30 1

1. [3 points] L A TEX and HTML are two document formatting languages. We want to build a translator to convert documents structured using HTML into L A TEX. In HTML, the text of the document to be formatted is specified between <html> and </html>. In L A TEX, the text is specified between \begin{document} and \end{document}. In HTML, a list of unnumbered bullet items is given using <UL> and </UL>; in L A TEX such a list is specified using \begin{itemize} and \end{itemize}. Each item in a list of bullet items begins with <LI> in HTML and \item in L A TEX. Font-changing commands in HTML are specified as follows: the text to be displayed in bold face, italics or teletype are marked with <B>, <I> and <TT>, respectively, in the beginning, and </B>, </I> and </TT>, respectively, at the end. In L A TEX text with changed font begins with \textbf{, \textit{ and \texttt{ respectively, and the change of font ends with }. A sample translation of HTML document to L A TEX is shown below: HTML L A TEX Output (formatted) text <html> This is a sample document. A list of bullet items can be created as follows: <UL> <LI> Start an <b>itemize</b> environment; <LI> Place each item with an <i>item</i> command. </UL> </html> \begin{document} This is a sample document. A list of bullet items can be created as follows: \begin{itemize} \item Start an \textbf{itemize} environment; \item Place each item with an \textit{item} command. \end{itemize} \end{document} This is a sample document. A list of bullet items can be created as follows: Start an itemize environment; Place each item with an item command. List the smallest set of tools you will use for building a translator to convert input HTML documents into L A TEX documents. Explain your choice clearly. In particular, you must justify the inclusion of each tool in the set, and show that your choice of tools is complete: i.e. sufficient to carry out the translation. You may assume that all input documents are well-formed HTML documents, i.e. they are correct with respect to HTML syntax. 2

[THIS PAGE IS INTENTIONALLY BLANK] 3

2. [4 points] A grammar is said to be ɛ-free if either it has no ɛ-productions, or the only ɛ-production is S ɛ where S is the start symbol. A grammar is said to be in Chomsky Normal Form (CNF) it is (i) ɛ-free, (ii) its start symbol S does not appear on the right hand side of any production, and (iii) every production is of the form A B C where B and C are some non-terminal symbols, or of the form A a where a is some terminal symbol. Let G 2 be an LL(1) grammar in CNF. Let G 2 have t terminal symbols, n non-terminal symbols and p productions. (a) [1 point] What is the size of the LL(1) parsing table for G 2? Why? (b) [2 points] Let s be a string of length k in L(G 2 ), i.e. the language of G 2. What is the size of the parse tree for s? Justify. (c) [1 point] Let s be a string of length k in L(G 2 ), i.e. the language of G 2. How long does it take to parse s using the LL(1) parsing algorithm? Justify. 4

3. [4 points] Consider the following grammar G 3 : S A S B A A c A ɛ B c A d (a) [1 point] Compute FIRST and FOLLOW sets for S, A and B. (b) [1 point] Show that G 3 is not SLR(1). 5 [CONTINUED ON NEXT PAGE]

[CONTINUED FROM PREVIOUS PAGE] (c) [2 points] Can the conflicts in the action table be resolved arbitrarily (i.e. always choosing one of the conflicting actions instead of the other) without affecting the language accepted by the parser? Explain. 6

4. [3 points] Consider the following grammar describing floating point numbers written in binary notation (with F as the start symbol): F N. N N N B N B B 0 B 1 Write a Yacc/Bison specification of the above grammar, and use that specification to compute the value of a binary floating point number. For example the value of 101010.101 is 42.625. 7

5. [2 points] Consider a system with 4GB address space, of which 1GB (total) is allocated to stack, static area, and code. (a) What is the maximum allowable heap size if Mark-and-Sweep garbage collection is used? (b) What is the maximum allowable heap size of Copying garbage collection is used? (c) If the total size of all live heap objects is 256MB, and it takes 4 nanoseconds to inspect 4 bytes of heap for marking, sweeping or copying, how much time will be taken by a mark-and-sweep collector? (d) If the total size of all live heap objects is 256MB, and it takes 4 nanoseconds to inspect 4 bytes of heap for marking, sweeping or copying, how much time will be taken by a copying collector? 8

6. [5 points] For this question, consider the following class definitions in a Java-like language: class One { private int i; public int j; static int k; public One a; } class Three extends One { public int j; public int n; public int m; private One b; } class Two extends One { public int n; public int m; } class Four extends Three { public int k; static One c; private int n; } (a) [1 point] What is the minimum amount of memory that should be allocated for an object (instance) of class One? Justify your answer. Assume that int s and object references take up 4 bytes of memory. (b) [1 point] Show the memory layout of an object (instance) of class One. Show each field in the object and write the offset of each field relative the base address of the object, assuming int s and object references take up 4 bytes of memory. 9 [CONTINUED ON NEXT PAGE]

[CONTINUED FROM PREVIOUS PAGE] (c) [1 point] Show the memory layout of an object (instance) of class Two. Show each field in the object and write the offset of each field relative the base address of the object, assuming int s and object references take up 4 bytes of memory. (d) [1 point] Show the memory layout of an object (instance) of class Three. Show each field in the object and write the offset of each field relative the base address of the object, assuming int s and object references take up 4 bytes of memory. 10 [CONTINUED ON NEXT PAGE]

[CONTINUED FROM PREVIOUS PAGE] (e) [1 point] Show the memory layout of an object (instance) of class Four. Show each field in the object and write the offset of each field relative the base address of the object, assuming int s and object references take up 4 bytes of memory. 11

7. [4 points] Consider extending Decaf with a Do-While statement, whose syntax is given by the following production: Stmt do Stmt while Expr where Stmt and Expr are non-terminals representing Decaf statements and expressions, respectively. The meaning of a do-while statement is standard: execute the body statement one or more times, exiting the loop when the guard expression is false. (a) [2 points] Write syntax-directed definition to generate code for Do-While statements. You may generate CREAM code (as in HW4) or generate three-address code. 12 [CONTINUED ON NEXT PAGE]

[CONTINUED FROM PREVIOUS PAGE] (b) [2 points] When generating code for while statements, the text book claims that it is better to consider while Expr Stmt as if Expr do Stmt while Expr The code generated for the second form above (if...) is more efficient than the code generated for the first form above (while...). Give a clear argument to show why the claim in the text book is valid. 13

8. [5 points] For this question, recall the following CREAM instructions (as in the CREAM manual,... represents untouched values on stack): ildc(n):... =..., n Push integer constant n on stack. load:..., n =..., v Push the value v of local variable n on stack. n is the offset of the local variable from the base of the current activation record. store:..., n, v =..., v Place the value v in the local variable n. Remove n from stack but retain v on top of stack. aload:..., a, i =..., v a is a handle to an array in heap; the value v of the ith element of the array is retreived and pushed on stack. astore:..., a, i, v =..., v a is a handle to an array in heap; value v is assigned to the ith element of the array. iadd:..., i, j =..., i + j Add integer values of the top two elements on stack, and replace them with their sum. dup:..., v =..., v, v Duplicate the value on top of stack. pop:..., v =... Pop the value on top of stack. swap:..., v 1, v 2 =..., v 2, v 1 Interchange the top two elements of stack. For each of the following Decaf expressions, give the corresponding CREAM code. Your code should only use the instructions listed above. Note that not all above instructions may be necessary. (a) j = i+1 where i and j are local integer variables at offsets 2 and 3 respectively. 14 [CONTINUED ON NEXT PAGE]

[CONTINUED FROM PREVIOUS PAGE] (b) j = ++i where i and j are local integer variables at offsets 2 and 3 respectively. (c) j = a[++i] where i and j are local integer variables at offsets 2 and 3 respectively, and a is a local variable at offset 1 that contains an array reference. 15 [CONTINUED ON NEXT PAGE]

[CONTINUED FROM PREVIOUS PAGE] (d) j = i++ where i and j are local integer variables at offsets 2 and 3 respectively. (e) j = a[i++] where i and j are local integer variables at offsets 2 and 3 respectively, and a is a local variable at offset 1 that contains an array reference. END OF EXAM 16

[THIS PAGE IS INTENTIONALLY BLANK] 17

[THIS PAGE IS INTENTIONALLY BLANK] 18

[THIS PAGE IS INTENTIONALLY BLANK] 19