MASSACHUSETTS INSTITUTE OF TECHNOLOGY Fall Test I

Similar documents
1 Lexical Considerations

Lexical Considerations

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

UNIVERSITY OF CALIFORNIA

Lexical Considerations

Decaf Language Reference

CSE P 501 Exam 8/5/04

CS164: Midterm I. Fall 2003

CSE 582 Autumn 2002 Exam 11/26/02

Test I Solutions MASSACHUSETTS INSTITUTE OF TECHNOLOGY Spring Department of Electrical Engineering and Computer Science

Department of Electrical Engineering and Computer Science MASSACHUSETTS INSTITUTE OF TECHNOLOGY

Compilers. Compiler Construction Tutorial The Front-end

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

CSCE 531 Spring 2009 Final Exam

WARNING for Autumn 2004:

CSE 401 Midterm Exam 11/5/10

ECE251 Midterm practice questions, Fall 2010

Syntax and Grammars 1 / 21

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

Midterm 1. CMSC 430 Introduction to Compilers Spring Instructions Total 100. Name: March 14, 2012

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

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

The Decaf language 1

The Decaf Language. 1 Lexical considerations

COP4020 Programming Assignment 2 - Fall 2016

First Midterm Exam CS164, Fall 2007 Oct 2, 2007

Principles of Programming Languages 2017W, Functional Programming

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

UMBC CMSC 331 Final Exam

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

CSCI 3155: Principles of Programming Languages Exam preparation #1 2007

CPS 506 Comparative Programming Languages. Syntax Specification

Question Marks 1 /11 2 /4 3 /13 4 /5 5 /11 6 /14 7 /32 8 /6 9 /11 10 /10 Total /117

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

MIT Specifying Languages with Regular Expressions and Context-Free Grammars. Martin Rinard Massachusetts Institute of Technology

MIT Specifying Languages with Regular Expressions and Context-Free Grammars

2.2 Syntax Definition

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

A simple syntax-directed

Syntax-Directed Translation. Lecture 14

Specifying Syntax. An English Grammar. Components of a Grammar. Language Specification. Types of Grammars. 1. Terminal symbols or terminals, Σ

MIDTERM EXAMINATION - CS130 - Spring 2003

Department of Electrical Engineering and Computer Science MASSACHUSETTS INSTITUTE OF TECHNOLOGY Fall Quiz I

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.

CSE 401 Midterm Exam Sample Solution 11/4/11

Midterm I - Solution CS164, Spring 2014

CSE 401 Midterm Exam Sample Solution 2/11/15

Visitors. Move functionality

Homework #3: CMPT-379

CSCI Compiler Design

CS143 Handout 03 Summer 2012 June 27, 2012 Decaf Specification

Decaf Language Reference Manual

CSE P 501 Exam 11/17/05 Sample Solution

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

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

Department of Electrical Engineering and Computer Science MASSACHUSETTS INSTITUTE OF TECHNOLOGY Fall Quiz I Solutions

Project Compiler. CS031 TA Help Session November 28, 2011

More Assigned Reading and Exercises on Syntax (for Exam 2)

Department of Electrical Engineering and Computer Science MASSACHUSETTS INSTITUTE OF TECHNOLOGY Fall Quiz I

Semantic Analysis. Lecture 9. February 7, 2018

Today. Assignments. Lecture Notes CPSC 326 (Spring 2019) Quiz 5. Exam 1 overview. Type checking basics. HW4 due. HW5 out, due in 2 Tuesdays

Decaf Language Reference

CSE 401 Final Exam. December 16, 2010

CSE 582 Autumn 2002 Exam Sample Solution

Introduction to Programming Using Java (98-388)

Question Bank. 10CS63:Compiler Design

Department of Electrical Engineering and Computer Science MASSACHUSETTS INSTITUTE OF TECHNOLOGY Fall Quiz I

Lexical Analysis. Introduction

Department of Electrical Engineering and Computer Science MASSACHUSETTS INSTITUTE OF TECHNOLOGY Fall Quiz I

Midterm I (Solutions) CS164, Spring 2002

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Compiler Design

Syntax Analysis Check syntax and construct abstract syntax tree

Examination in Compilers, EDAN65

ECE468 Fall 2003, Final Exam

Principles of Programming Languages COMP251: Syntax and Grammars

More on Arrays CS 16: Solving Problems with Computers I Lecture #13

Semantic Analysis. Compiler Architecture

Examples of attributes: values of evaluated subtrees, type information, source file coordinates,

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

Part 5 Program Analysis Principles and Techniques

CS 411 Midterm Feb 2008

CSE3322 Programming Languages and Implementation

Syntax. A. Bellaachia Page: 1

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

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

11. a b c d e. 12. a b c d e. 13. a b c d e. 14. a b c d e. 15. a b c d e

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

CS 44 Exam #2 February 14, 2001

Syntax-Directed Translation Part I

Department of Electrical Engineering and Computer Science MASSACHUSETTS INSTITUTE OF TECHNOLOGY Fall Quiz II

CMSC330 Spring 2008 Final Exam

Midterm I Exam Principles of Imperative Computation Frank Pfenning. February 17, 2011

CSCI Compiler Design

Syntax Directed Translation

COP4020 Programming Assignment 2 Spring 2011

Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres

Compiler Construction Assignment 3 Spring 2018

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

ECE 2400 Computer Systems Programming Fall 2018 Topic 1: Introduction to C

CS5363 Final Review. cs5363 1

Transcription:

Department of Electrical Engineering and Computer Science MASSACHUSETTS INSTITUTE OF TECHNOLOGY 6.035 Fall 2014 Test I You have 50 minutes to finish this quiz. Write your name and athena username on this cover sheet. Some questions may be harder than others. Read them all through first and attack them in the order that allows you to make the most progress. If you find a question ambiguous, be sure to write down any assumptions you make. Be neat. If we can t understand your answer, we can t give you credit! This exam is open book and open laptop. Additionally, you may access the course website, but aside from that you may NOT USE THE NETWORK. Please do not write in the boxes below. I (xx/20) II (xx/30) III (xx/30) IV (xx/20) Total (xx/100) Name: Athena username: 1

I Regular Expressions and Context-Free Grammars The languages in Questions 1, 2, and 3 are over the alphabet {0, 1. 1. [6 points]: Write a regular expression that recognizes the language of sequences of zeros and ones such that the number of ones is a positive even number. 2. [7 points]: Draw a state diagram of a DFA that recognizes the language of non-empty strings that start with 1 and end with 0. 3. [7 points]: Write a context-free grammar that recognizes the language 0 n 1 n (n > 0). 2

II Hacking Context-Free Grammars Consider the following grammar: 1: S S; S 2: S while(v ) S 3: S V = C Here, V denotes string identifiers that represent variables and C denotes integer constants. 4. [10 points]: Show that the grammar is ambiguous by providing: A. A sentence in the language with two parse trees. B. The two parse trees. 3

5. [10 points]: Consider the following sentence ( 1 and 2 denote two locations): y = 6; while (x) 1 x = 5 2 Assume you are working with a shift-reduce parser for this language. A. Draw the parser s stack after shifting the token ) (location 1) and after shifting the token 5 (location 2). B. What will be the next two actions after shifting the token 5? If the action is shift, then specify the token. If the action is reduce, then specify the production rule. 6. [10 points]: For the same language, provide a grammar that is not ambiguous. 4

III Symbol Tables and Semantic Analysis Consider the following Decaf program: callout printf; int A[100]; // Reads len elements from input file to the array A and sorts the array void read_and_sort(int len) { /*... */ // Binary search for the element value int bins(int value, int len) { int left; int right; left = 0; right = len - 1; while (left < right) { int mid; mid = (left + right)/2; if (value == A[mid]) { return mid; else { if (value > A[mid]){ left = mid + 1; else { right = mid - 1; //--- LOC 1 ---// return -1; void main () { int l; int res; l = 100; read_and_sort(l); res = bins(42, l); if (res >= 0) { printf ("Yup!\n"); else { printf ("Nope!\n"); 5

7. [15 points]: Complete the entries of the symbol table for the program at the position LOC 1. Function Name IsCallout? Return Type Parameter Types Figure 1: Function Symbol Table: IsCallout? is yes if the function is a callout, no otherwise. Return type {int, bool, void, parameter type {int, bool. Global Level Variable Name IsParameter? Type Length bins Level 1 Variable Name IsParameter? Type Length bins Level 2 Variable Name IsParameter? Type Length Figure 2: Variable Symbol Table: IsParameter? is yes if the variable is a parameter, no otherwise. Variable s type {int, bool, int[], bool[]. Length is 0 for scalars, or a positive number for arrays. Table has multiple scope levels: for global variables (global level), function parameters and locals declared at the function block level (level 1), and locals declared at the levels of the function s internal blocks (levels greater than 1). 6

8. [5 points]: Assume that in the function bins we renamed the parameter value to bins. Then the new definition of the function bins looks like: int bins(int bins, int len) { //... // if (bins == A[mid]) //... //... // Is this new program still a legal Decaf program? Explain your answer. 9. [5 points]: Assume that we declared a local variable int A; at the beginning of the body of the function bins. Is this new program still a legal Decaf program? Explain your answer. 10. [5 points]: Would the program be legal if instead we declared the local variable int A; at the beginning of the body of the function main? Explain your answer. 7

IV Semantic Analysis 11. [20 points]: In this exercise, you will implement a function that checks whether an expression (e.g., a condition of an if or a while statement) is of type bool. IMPORTANT NOTE: We provided two skeletons for implementation, one using pattern matching, another using visitor pattern. Please choose only one skeleton in which to implement the analysis. Write here which skeleton you chose: Pattern matching version: Assume you are given the function get_type(id: string), which returns the type of the declared identifier id. The type of the identifier and the return value of get_type can be int, bool, or err (when the identifier is not declared). The type Expr is the parent class for all arithmetic, relational, and logical operators. We use several representative subtypes in the matching rule below. We omit their definitions, but they should be clear from the context. Assume you are given the function is_expr_int(expr) that checks if an expression s type is integer. Fill in the lines in the pattern matching statement so that the function is_expr_boolean checks whether an expression has the type bool: def is_expr_boolean (expr : Expr) : Boolean = expr match { case IntConst (intvalue) => case BoolConst (boolvalue) => case Variable (strvarname) => case Add (expr1, expr2) => case LessThan (expr1, expr2) => case Or (expr1, expr2) => //... 8

Visitor pattern version: Assume you are given the function Util.get_type(String id), which returns the type of the declared identifier id. The type of the identifier and the return value of get_type can be int, bool, or err (when the identifier is not declared). The class Expr is the parent class for all arithmetic, relational, and logical operators. It has an overloaded method accept(visitor visitor) that calls the appropriate visitor. Each subclass implements the method accept. We use several representative subclasses of the class Expr below. We omit their full definitions, but the relevant fields are passed as arguments to the visitor functions. Assume you are also given the visitor class IsIntExprVisitor (with a default constructor) that checks if an expression s type is integer. Fill in the lines in the IsBoolExprVisitor so that it checks whether an expression s type is bool: class IsBoolExprVisitor { //... bool visitintconst (int intvalue) { bool visitboolconst (bool boolvalue) { bool visitvariable (String varname) { bool visitadd (Expr expr1, Expr expr2) { bool visitlessthan (Expr expr1, Expr expr2) { bool visitor (Expr expr1, Expr expr2) { //... 9