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

Similar documents
MIDTERM EXAMINATION - CS130 - Spring 2005

Qualifying Exam in Programming Languages and Compilers

An Introduction to Subtyping

Functional Programming. Pure Functional Programming

INSTITUTE OF AERONAUTICAL ENGINEERING

The PCAT Programming Language Reference Manual

Lisp. Versions of LISP

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

The Design of Core C++ (Notes)

The SPL Programming Language Reference Manual

Functional Programming. Pure Functional Languages

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

Programming Languages, Summary CSC419; Odelia Schwartz

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

PL Revision overview

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

UMBC CMSC 331 Final Exam

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

CMSC 331 Final Exam Section 0201 December 18, 2000

PAIRS AND LISTS 6. GEORGE WANG Department of Electrical Engineering and Computer Sciences University of California, Berkeley

6.821 Programming Languages Handout Fall MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Compvter Science

NOTE: Answer ANY FOUR of the following 6 sections:

CS 3360 Design and Implementation of Programming Languages. Exam 1

CS 415 Midterm Exam Spring 2002

Announcements. The current topic: Scheme. Review: BST functions. Review: Representing trees in Scheme. Reminder: Lab 2 is due on Monday at 10:30 am.

Programming Languages

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Functional Programming. Pure Functional Languages

MIDTERM EXAMINATION - CS130 - Spring 2003

Functional Programming. Big Picture. Design of Programming Languages

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

Programming Languages Lecture 14: Sum, Product, Recursive Types

St. MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad

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

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

CS 842 Ben Cassell University of Waterloo

Text Input and Conditionals

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

Syntax and Grammars 1 / 21

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

Programming Languages Third Edition

CS558 Programming Languages

2.2 Syntax Definition

CSCC24 Functional Programming Scheme Part 2

Lecture Overview. [Scott, chapter 7] [Sebesta, chapter 6]

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

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

Topic IV. Parameters. Chapter 5 of Programming languages: Concepts & constructs by R. Sethi (2ND EDITION). Addison-Wesley, 1996.

CIS4/681 { Articial Intelligence 2 > (insert-sort '( )) ( ) 2 More Complicated Recursion So far everything we have dened requires

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

Lecture 2: SML Basics

Lisp Basic Example Test Questions

CS 242. Fundamentals. Reading: See last slide

Pierce Ch. 3, 8, 11, 15. Type Systems

Lecture 09: Data Abstraction ++ Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree.

The role of semantic analysis in a compiler

CSCE 531, Spring 2015 Final Exam Answer Key

Topic IV. Block-structured procedural languages Algol and Pascal. References:

CS450: Structure of Higher Level Languages Spring 2018 Assignment 7 Due: Wednesday, April 18, 2018

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

Gene Kim 9/9/2016 CSC 2/444 Lisp Tutorial

CSCI Compiler Design

SE352b: Roadmap. SE352b Software Engineering Design Tools. W3: Programming Paradigms

CSE413 Midterm. Question Max Points Total 100

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

Informatica 3 Syntax and Semantics

CS558 Programming Languages

CSC 533: Organization of Programming Languages. Spring 2005

Intermediate Code Generation

CSCI 1260: Compilers and Program Analysis Steven Reiss Fall Lecture 4: Syntax Analysis I

Types and Static Type Checking (Introducing Micro-Haskell)

11/6/17. Functional programming. FP Foundations, Scheme (2) LISP Data Types. LISP Data Types. LISP Data Types. Scheme. LISP: John McCarthy 1958 MIT

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

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

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.

System Verilog Tagged Unions and Pattern Matching

CSCI-GA Scripting Languages

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

A Simple Syntax-Directed Translator

CSCI Compiler Design

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

CS152 Programming Language Paradigms Prof. Tom Austin, Fall Syntax & Semantics, and Language Design Criteria

Lambda Calculus see notes on Lambda Calculus

CSCI 3155: Lab Assignment 2

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

CS 415 Midterm Exam Spring SOLUTION

MASSACHUSETTS INSTITUTE OF TECHNOLOGY Fall Test I

CS61A Notes 02b Fake Plastic Trees. 2. (cons ((1 a) (2 o)) (3 g)) 3. (list ((1 a) (2 o)) (3 g)) 4. (append ((1 a) (2 o)) (3 g))

Semantic Analysis. How to Ensure Type-Safety. What Are Types? Static vs. Dynamic Typing. Type Checking. Last time: CS412/CS413

CMSC 330: Organization of Programming Languages

Data Types The ML Type System

CS 3360 Design and Implementation of Programming Languages. Exam 1

CMSC 330: Organization of Programming Languages. Architecture of Compilers, Interpreters

CSE 12 Abstract Syntax Trees

Note 3. Types. Yunheung Paek. Associate Professor Software Optimizations and Restructuring Lab. Seoul National University

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

CMSC 331 Final Exam Section 0201 December 18, 2000

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division

Transcription:

CSCI 3155: Principles of Programming Languages Exam preparation #1 2007 Exercise 1. Consider the if-then-else construct of Pascal, as in the following example: IF 1 = 2 THEN PRINT X ELSE PRINT Y (a) Assume that the if-then-else is statement, i.e., one possible production of the nonterminal stmt (you don t have to specify any others), and both branches of the conditional should again allow statements. The condition is an arbitrary expression, expressed as the nonterminal expr. Write a BNF grammar for the above. (b) Add another rule that allows programmers to omit the ELSE branch. Do not use an ε construct. (c) Is your grammar ambiguous? If so, alter it so that it is no longer ambiguous. If not, explain why it is not ambiguous. Exercise 2. Consider the following BNF grammar of an expression syntax, with the start symbol A : A B + C var B D E E C A D B E ( A ) var (a) Is the grammar ambiguous? If so, show that it is. If not, explain. (b) Determine the associativity of + and *, and explain. 1

Exercise 3. In the programming language LISP, all definitions and expressions are constructed from so-called S-Expressions. An S-Expression can be one of three things: A simple name, a pair, or a function call. A pair is a pair of parentheses containing an S-Expression followed by a period (. ) and another S-Expression. A function call is a pair of parentheses containing a sequence of S-Expressions; this sequence must not be empty. Below are examples of LISP expressions: ( defun tuple ( a b) ( a. b )) ( defun mapcar ( f l ) ( i f ( null l ) n i l ( cons ( funcall f ( car l )) (mapcar f ( cdr l ) ) ) ) ) A LISP program is a list of S-Expressions. A LISP program must not be empty. (a) Assume a terminal symbol name that describes the class of LISP names. Describe the syntax of LISP programs. (b) Addition in LISP can take arbitrarily many arguments, as in (+ 1 2 (+ 3 4 5) 6) Give the denotational semantics of LISP addition. (c) All LISP programs are constructed as above. There are no formal control structures, only special names that happen to have the semantics of control structures. Give a critique of this syntax, keeping in mind that LISP is nowadays seen as a general-purpose programming language. 2007 2

Exercise 4. Consider the following program: VAR y : INTEGER; PROCEDURE P ( x : INTEGER) : INTEGER = y := y + x ; IF 15 > y THEN VAR x : [0 TO 15] x := y ; y := P( y + 1); RETURN x + y ; ELSE RETURN 2; Assume static scoping. (a) Determine the scopes of the paramter x and the global variable y. (b) Determine the lifetimes of variables x and y. Exercise 5. For the program from the previous exercise, list four different bindings that affect variable x. 2007 3

Exercise 6. (Tricky!) Assume the following program: VAR x : INTEGER; VAR r : INTEGER; PROCEDURE P( y : INTEGER; z : INTEGER) = VAR v : INTEGER z := r ; v := y + y ; x := x + v + z ; ; PROCEDURE Q( y : INTEGER) : INTEGER = x := x + 1; RETURN y ; ; PROCEDURE F( x : INTEGER; y : INTEGER) : INTEGER = IF y > x THEN RETURN x ELSE x := x + F(x, y + 1); RETURN x ; x := 1; r := F(2, 0); VAR r : INTEGER r := 0; P(Q(0), x ) ; ; PRINT x Assume stack-dynamic storage. Choose a parameter passing mode and scoping to make the program print a certain result, and explain: (a) 2 (b) 4 (c) 8 (d) 16 (e) 32 2007 4

Exercise 7. A language designer proudly shows you the subtyping rules he came up with for his language: word <: short int <: long double <: long int :> short word :> long :> long double Now, some of these may seem a bit debatable, but trust me, I have my reasons to choose these. The language designer then explains that he wants to allow implicit widening conversions throughout the program. (a) What can you tell about the type system, from the above rules? (b) Can you find a simpler description of the type system? If so, give it below. If not, explain why the above cannot be simplified. Exercise 8. Some programming languages, such as C++, allow both pointers and references. In languages that have explicit pointers or references, we can think of both as type constructors; in terms of C++, * is a pointer type constructor, and & is a reference type constructor. Due to orthogonality, we can apply both type constructors to results from each other. Explain the difference between a pointer to a reference and a reference to pointer. Exercise 9. Following up on our discussion of subtyping in class, we can also allow subtyping for arrays. Consider the following Mystery program fragment: TYPE ATYPE = ARRAY AR OF AT; TYPE PTYPE = ARRAY PR OF PT; VAR A : ATYPE;... PROCEDURE F(P : PTYPE) =... (a) Assume Pass-by-value. When should ATYPE and PTYPE be subtypes of each other? Explain why your choice is natural, i.e., at most requires information to be discarded. (b) Assume Pass-by-value-result. When should ATYPE and PTYPE be subtypes of each other? Explain why your choice is natural, i.e., at most requires information to be discarded. 2007 5

Exercise 10. Python is a language with implicit heap-dynamic variables. As we discussed in class, Python uses indentation to indicate block structure. Consider the following Python program fragment: v = read some input () i f v > 0: k = compute something ( v ) i f k < 0: k = Result i s undefined. else : k = Input i s negative. i f i s i n s t a n c e (k, s t r ) : print Error : + k else : print ( Result :, k) Note the use of isinstance(v, str) to determine whether the type of k is a string (indicating an error) or not. (a) Describe, abstractly, how you could re-write this code to use unions. (b) Would tagged or untagged unions be more appropriate? Explain. Exercise 11. A language designer decides to introduce combined subrange types, i.e., types that describe values out of a list of subranges, to the language she is working on. Such combined subrange types allow us to describe types such as ([1 TO 4],[7 TO 8], [100 TO 102]): A value v is of this type iff any one of the following holds: 1 v 4 7 v 8 100 v 102 Consider the following combined subrange types: (a) Which of the following types should naturally be subrange types of each other? Annotate appropriately. ([1 TO 7]) ([2 TO 5]) ([17 TO 22], [39 TO 141]) ([94 TO 97], [99 TO 100], [107 TO 111]) ([1 TO 713], [715 TO 982]) ([512 TO 982]) ([52 TO 54]) ([61 TO 115], [31 TO 55]) (b) All of the above types were valid examples of combined subrange types. Discuss the utility of these combined subrange types, given our criteria. 2007 6

Exercise 12. A friend shows you the following program and mentions that it doesn t typecheck; it died on the line marked(* error *) during dynamic type checking. Your friend knows that the language uses pass-by-reference for arrays, but is otherwise somewhat unfamiliar with its semantics. TYPE T = ARRAY [0 TO 99] OF INTEGER; VAR B : ARRAY [0 TO 99] OF INTEGER; VAR A : T; VAR I : [0 TO 99]; PROCEDURE P(X : ARRAY [0 TO 99] OF INTEGER) = IF 100 > X[ 1 ] AND 100 > X[X[X[ 1 ] + 1 ] ] THEN X[X[ 1 ] ] := X[X[ 1 ] ] + X[X[X[ 1 ] + 1 ] ] ; ELSE RETURN; I := 0; WHILE (100 > I ) DO A[ I ] := I ; I := I + 1; ; B := A; P(A) ; ( e rror ) I := 0; WHILE (100 > I ) DO PRINT A[ I ] + B[ I ] ; Give the most plausible explanation for the type error. Exercise 13. What is the difference between static type checking and static type binding? Explain. Exercise 14. Consider the following BNF grammar with start symbol S : S L! A L! L A A L A A + B B B a! L! b (Skills 2.2, 2.5; tricky!) Is this grammar ambiguous? If so, prove that it is ambiguous. If not, explain why you believe that it is not, by analogy with the examples used in the book for discussing ambiguity. 2007 7