DO NOT OPEN UNTIL INSTRUCTED

Similar documents
CS 415 Midterm Exam Spring SOLUTION

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

Scope, Functions, and Storage Management

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

MIDTERM EXAMINATION - CS130 - Spring 2005

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

A Second Look At ML. Chapter Seven Modern Programming Languages, 2nd ed. 1

Announcements. Scope, Function Calls and Storage Management. Block Structured Languages. Topics. Examples. Simplified Machine Model.

CS 415 Midterm Exam Spring 2002

ECE251 Midterm practice questions, Fall 2010

CS 415 Midterm Exam Fall 2003

Theoretical Part. Chapter one:- - What are the Phases of compiler? Answer:

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

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

S Y N T A X A N A L Y S I S LR

CS /534 Compiler Construction University of Massachusetts Lowell

Concepts Introduced in Chapter 7

Project 2 Interpreter for Snail. 2 The Snail Programming Language

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

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

Informal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility

Syntax. A. Bellaachia Page: 1

The Language for Specifying Lexical Analyzer

Example. program sort; var a : array[0..10] of integer; procedure readarray; : function partition (y, z :integer) :integer; var i, j,x, v :integer; :

CSE 582 Autumn 2002 Exam 11/26/02

Mini-ML. CS 502 Lecture 2 8/28/08

Midterm II CS164, Spring 2006

Control in Sequential Languages

CS558 Programming Languages

Using an LALR(1) Parser Generator

MIDTERM EXAM (Solutions)

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

Kurt Schmidt. October 30, 2018

LR Parsing LALR Parser Generators

Class Information ANNOUCEMENTS

Begin at the beginning

Introduction to Parsing Ambiguity and Syntax Errors

CSE 401 Midterm Exam Sample Solution 2/11/15

CMSC 330, Fall 2013, Practice Problems 3

Programming Languages Third Edition. Chapter 10 Control II Procedures and Environments

Attributes of Variable. Lecture 13: Run-Time Storage Management. Lifetime. Value & Location

CSCI312 Principles of Programming Languages!

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Midterm I (Solutions) CS164, Spring 2002

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

Introduction to Parsing Ambiguity and Syntax Errors

G Programming Languages - Fall 2012

CSE3322 Programming Languages and Implementation

CSc 520 final exam Wednesday 13 December 2000 TIME = 2 hours

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

ECE468 Fall 2003, Final Exam

Midterm I - Solution CS164, Spring 2014

Scope and Introduction to Functional Languages. Review and Finish Scoping. Announcements. Assignment 3 due Thu at 11:55pm. Website has SML resources

BSCS Fall Mid Term Examination December 2012

Syntax-Directed Translation

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

CS 345. Exceptions. Vitaly Shmatikov. slide 1

Context Free Languages

Compiler Design: Lab 3 Fall 2009

University of Kelaniya Sri Lanka

Programming Language Concepts, cs2104 Lecture 04 ( )

1 Lexical Considerations

News. Programming Languages. Recap. Recap: Environments. Functions. of functions: Closures. CSE 130 : Fall Lecture 5: Functions and Datatypes

CS415 Compilers. Procedure Abstractions. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

UNIT-4 (COMPILER DESIGN)

Bottom-Up Parsing. Lecture 11-12

Midterm Review. CS 211 Fall 2018

CSCE 531, Spring 2015 Final Exam Answer Key

CS558 Programming Languages

CS558 Programming Languages

OCaml. ML Flow. Complex types: Lists. Complex types: Lists. The PL for the discerning hacker. All elements must have same type.

CSE 582 Autumn 2002 Exam Sample Solution

Unit 7. Functions. Need of User Defined Functions

CS 3360 Design and Implementation of Programming Languages. Exam 1

Bottom-Up Parsing. Lecture 11-12

The PCAT Programming Language Reference Manual

COMP 105 Homework: Type Systems

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

CS3360 Design and Implementation of Programming Languages Final Exam May 16, pm to 12:45pm (2 hour and 40 minutes) NAME:

Chapter 7 Control I Expressions and Statements

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

Appendix A The DL Language

Programming Languages

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

I have read and understand all of the instructions below, and I will obey the Academic Honor Code.

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

Standard ML. Data types. ML Datatypes.1

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

Second release of the COMPASS Tool Tool Grammar Reference

Dynamic Types, Concurrency, Type and effect system Section and Practice Problems Apr 24 27, 2018

CS558 Programming Languages

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking

Every language has its own scoping rules. For example, what is the scope of variable j in this Java program?

CS 314 Principles of Programming Languages. Lecture 11

Homework 3 COSE212, Fall 2018

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

SYED AMMAL ENGINEERING COLLEGE (An ISO 9001:2008 Certified Institution) Dr. E.M. Abdullah Campus, Ramanathapuram

SMURF Language Reference Manual Serial MUsic Represented as Functions

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

Transcription:

CS 345 - Programming Languages Fall 2010 MIDTERM #1 October 5, 2010 DO NOT OPEN UNTIL INSTRUCTED YOUR NAME: Collaboration policy No collaboration is permitted on this midterm. Any cheating (e.g., submitting another person s work as your own, or permitting your work to be copied) will automatically result in a failing grade. The Computer Sciences department code of conduct can be found at http://www.cs.utexas.edu/academics/conduct/. 1

Midterm #1 (85 points) Problem 1 (15 points) Circle only one of the choices (3 points each). 1. TRUE FALSE Algol 60 was the first language to introduce lexical block scoping. 2. TRUE FALSE A shift-reduce conflict can only occur if the grammar is ambiguous. 3. TRUE FALSE The lifetime of a variable is equal to the lifetime of the activation record corresponding to its scope. 4. TRUE FALSE Closures are necessary in dynamically scoped languages to keep track of each function s scope. 5. TRUE FALSE Finding an exception handler in ML simply requires walking up the stack from where the exception occurred. Problem 2 (15 points) Define the following terms: Ambiguous grammar: R-value of a variable: Scope of a variable: Pass-by-reference: Access link: 2

Problem 3 Problem 3a (3 points) What is the purpose of lexical analysis? What type of abstract machine is used to implement it? Problem 3b (3 points) What is the purpose of syntax analysis? What type of abstract machine is used to implement it? Problem 4 Problem 4a (3 points) What is the dangling else problem? Problem 4b (8 points) Write a grammar of if-then-else expressions where every if statement must be terminated by endif. Does this grammar solve the dangling else problem? Explain your answer in terms of shift-reduce conflicts. 3

Problem 4c (3 points) Can your new grammar be recognized by a deterministic finite automaton? Explain. Problem 5 Explain the evaluation of the following statements in terms of l-values and r-values. Problem 5a (3 points) int x = 1; int a[5] = {5,4,3,2,1}; Problem 5b (3 points) int *p = a; Problem 5c (3 points) int **q = &p; Problem 5d (3 points) a[3] = a[x+1] * **q; 4

Problem 6 Consider the following program written in Algol-like pseudocode: begin integer i; procedure foo(integer x, integer y); begin x := x+1; y := x+1; x := y; i := i+1; end i := 1; foo(i,i); print i; end What would this program print under each of the following parameter passing mechanisms: Problem 6a (3 points) Pass-by-value: Problem 6a (3 points) Pass-by-reference: Problem 6a (3 points) Pass-by-value-result: Problem 7 In ML and most other functional languages, it is legal to declare a local function, i.e., a function defined within the scope of another function. For example, in ML you might write: fun f() = let val i = 1 fun g() = i in (print(int.tostring( g() )); g) 5

end; print(int.tostring( f()() )); This program declares a function f which declares a local variable i and a local function g. The function g simply returns the value of i. When you execute this program, it prints 1 twice. Because f returns a function, its scope must remain alive even after f finished its execution. As we discussed in class, ML solves this problem by placing both the activation record for the call to f and the closure for g on the heap. In ANSI C, there are no local functions, so there is no way to write an equivalent program. GNU CC (GCC) compiler, however, does allow local function declarations. Here is how to write an equivalent program in GNU C: #include <stdio.h> typedef int (*fn_t)(); fn_t f() { int i=1; int g() { return i; } printf("%d\n", g()); return &g; } int main() { printf("%d\n", (*f())() ); } GCC compiles local functions in the usual way, except that references to the activation record of an enclosing function are done via a static (access) link, like in ML. A particular instance of a local function is a piece of code (called the trampoline) placed on the stack, that sets the static chain and jumps to the beginning of code for the compiled function. The trampoline serves the same purpose as a closure. Unlike ML, however, GCC places both activation records and trampolines on the stack and makes no specific effort to solve the problem of keeping the scope alive after the function returns. 6

Problem 7a (5 points) The output of the GNU C program above is 1-1073743424 Explain why this program does not print 1 twice, as one might expect. Where does the second number come from? Problem 7b (3 points) Why does ML deviate from stack ( last-in-first-out ) storage management for closures and activation records? Problem 8 ML has functions hd and tl to return, respectively, the head (i.e., the first element) and the tail (i.e., the remaining elements) of a list. Both functions raise an exception Empty if the list is empty. Suppose that we redefine these functions so that they raise, respectively, exceptions Hd and Tl when applied to an empty list. The behavior of non-empty lists remains the same. - hd(nil); uncaught exception Hd - tl(nil); uncaught exception Tl Now consider the following identity function on lists: fun id(l) = hd(l)::tl(l) handle Hd => nil; 7

Problem 8a (3 points) What is the result of evaluating id(nil)? Why? Problem 8b (3 points) Does the function id need to handle exception Tl? Why? 8