COMPUTING SCIENCE 3Z: PROGRAMMING LANGUAGES 3

Similar documents
REAL TIME AND EMBEDDED SYSTEMS (M)

CSE 12 Abstract Syntax Trees

CS 320: Concepts of Programming Languages

INSTITUTE OF AERONAUTICAL ENGINEERING

CSE 143 Final Exam Part 1 - August 18, 2011, 9:40 am

CSE 401 Midterm Exam Sample Solution 11/4/11

CSE 143 Final Part 1, August 18, 2011 Sample Solution

G Programming Languages - Fall 2012

A programming language requires two major definitions A simple one pass compiler

About this exam review

COMP171. Hashing.

Chapter 3: Describing Syntax and Semantics. Introduction Formal methods of describing syntax (BNF)

St. MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad

Hash Table and Hashing

ECE251 Midterm practice questions, Fall 2010

Largest Online Community of VU Students

Getting Started. Office Hours. CSE 231, Rich Enbody. After class By appointment send an . Michigan State University CSE 231, Fall 2013

1. Stack overflow & underflow 2. Implementation: partially filled array & linked list 3. Applications: reverse string, backtracking

SOLUTIONS. COMP103 Introduction to Data Structures and Algorithms

ADTS, GRAMMARS, PARSING, TREE TRAVERSALS

Types and Static Type Checking (Introducing Micro-Haskell)

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

Types and Static Type Checking (Introducing Micro-Haskell)

Intro to semantics; Small-step semantics Lecture 1 Tuesday, January 29, 2013

R13 SET Discuss how producer-consumer problem and Dining philosopher s problem are solved using concurrency in ADA.

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

ADTS, GRAMMARS, PARSING, TREE TRAVERSALS

CMSC 330: Organization of Programming Languages

Programming II (CS300)

Chapter 20: Binary Trees

Computer Science 1 Bh

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

The role of semantic analysis in a compiler

Compiler construction

Student Number: Lab day:

Comp 311: Sample Midterm Examination

CIS 194: Homework 3. Due Wednesday, February 11, Interpreters. Meet SImPL

Simple Lexical Analyzer

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

CS134 Spring 2005 Final Exam Mon. June. 20, 2005 Signature: Question # Out Of Marks Marker Total

A Simple Syntax-Directed Translator

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

syntax tree - * * * - * * * * * 2 1 * * 2 * (2 * 1) - (1 + 0)

Programming Languages Third Edition

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

CS 411 Midterm Feb 2008

CSE P 501 Exam 8/5/04

A simple syntax-directed

This book is licensed under a Creative Commons Attribution 3.0 License

CSE 582 Autumn 2002 Exam 11/26/02

CPSC 411, Fall 2010 Midterm Examination

Functional Programming. Pure Functional Programming

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

Dynamic Data Types - Recursive Records

CSE 331 Midterm Exam 2/13/12

CS61B Lecture #7. Announcements:

Outline. Definition. 2 Height-Balance. 3 Searches. 4 Rotations. 5 Insertion. 6 Deletions. 7 Reference. 1 Every node is either red or black.

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

CS 3360 Design and Implementation of Programming Languages. Exam 1

ADTS, GRAMMARS, PARSING, TREE TRAVERSALS. Regrades 10/6/15. Prelim 1. Prelim 1. Expression trees. Pointers to material

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

Related Course Objec6ves

for (i=1; i<=100000; i++) { x = sqrt (y); // square root function cout << x+i << endl; }

CS 415 Midterm Exam Spring 2002

Compilers - Chapter 2: An introduction to syntax analysis (and a complete toy compiler)

Trees. (Trees) Data Structures and Programming Spring / 28

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

Faculty of Science FINAL EXAMINATION

Hierarchical data structures. Announcements. Motivation for trees. Tree overview

Contents. Chapter 1 SPECIFYING SYNTAX 1

a graph is a data structure made up of nodes in graph theory the links are normally called edges

CPSC 411, 2015W Term 2 Midterm Exam Date: February 25, 2016; Instructor: Ron Garcia

1) What is the primary purpose of template functions? 2) Suppose bag is a template class, what is the syntax for declaring a bag b of integers?

Tree Structures. A hierarchical data structure whose point of entry is the root node

Final thoughts on functions F E B 2 5 T H

CPT1. Unit 1 Computer Systems, Programming and Networking Concepts. General Certificate of Education January 2004 Advanced Subsidiary Examination

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


syntax tree - * * * * * *

CE 221 Data Structures and Algorithms

Context Free Languages

CE151-4-AU UNIVERSITY OF ESSEX. Sample Examination Questions 2011 INTRODUCTION TO PROGRAMMING

ADTS, GRAMMARS, PARSING, TREE TRAVERSALS

CSI33 Data Structures

Announcements. Midterm exam 2, Thursday, May 18. Today s topic: Binary trees (Ch. 8) Next topic: Priority queues and heaps. Break around 11:45am

This examination has 11 pages. Check that you have a complete paper.

Chapter 4. Lexical and Syntax Analysis. Topics. Compilation. Language Implementation. Issues in Lexical and Syntax Analysis.

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

CS/ECE 374 Fall Homework 1. Due Tuesday, September 6, 2016 at 8pm

Basic Python 3 Programming (Theory & Practical)

CSci 450, Org. of Programming Languages. Chapter 12 Expression Language Compilation

CSE3322 Programming Languages and Implementation

BRONX COMMUNITY COLLEGE of the City University of New York DEPARTMENT OF MATHEMATICS AND COMPUTER SCIENCE. Sample Final Exam

Lexical and Syntax Analysis

Test 6. moodle.yorku.ca CSE 1020

Topic 7: Algebraic Data Types

Announcements. Last modified: Fri Sep 8 00:59: CS61B: Lecture #7 1

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

Data Types. (with Examples In Haskell) COMP 524: Programming Languages Srinivas Krishnan March 22, 2011

Transcription:

Tuesday, 28 May 2009 2.00 pm 3.30 pm (Duration: 1 hour 30 minutes) DEGREES OF MSci, MEng, BEng, BSc, MA and MA (Social Sciences) COMPUTING SCIENCE 3Z: PROGRAMMING LANGUAGES 3 Answer all 4 questions. This examination paper is worth a total of 80 marks. You must not leave the examination room within the first half-hour or the last fifteen minutes of the examination.

1. (a) Pure functional languages (such as Haskell) are highly expressive, despite lacking the assignments and loops of imperative (and object-oriented) languages. Identify and briefly explain three features of functional languages that account for their expressive power. A dictionary (of the kind used by a spell-checker) is a set of words. (i) Define a Haskell type suitable for implementing a dictionary. (ii) Write a Haskell function, lookup w d, that yields true if and only if dictionary d contains word w. (iii) Write a Haskell function, add w d, that yields the dictionary obtained by adding word w to dictionary d (or yields d if it already contains w). [1] Explicitly declare the type of each function in (ii) and (iii). (Note: A simple implementation using linear search is acceptable.) (c) Consider the following Haskell type definition: data Tree a = NULL NODE a (Tree a) (Tree a) -- A value of type Tree a is a binary tree whose nodes contain elements of type a. (i) Write a Haskell function, depth t, that yields the depth of tree t. (Note: a tree with a single node has depth 0; an empty tree has depth 1.) (ii) Write a Haskell function, postorder t, that yields a list of all elements of tree t, using post-order traversal. (iii) Write a Haskell function, mirror t, that yields the mirror-image of tree t (i.e., the tree obtained from t by swapping every pair of subtrees). Explicitly declare the type of each function in (ii) and (iii). Summer Diet - 1 - Continued Overleaf/

2. The following is part of the BNF grammar of a hypothetical programming language: exp ::= pexp exp + exp exp - exp pexp ::= var ( exp ) var ::= a b c d (Here exp is an expression; pexp is a primary expression; var is a variable.) (a) Show that the expression a-b+c is ambiguous by drawing its two syntax trees. Show how to modify the grammar to eliminate the ambiguity, in such a way that expressions associate to the left. For example, a-b+c should be interpreted like (a-b)+c. Illustrate your answer by drawing the unique syntax tree of ab+c. Summer Diet - 2 - Continued Overleaf/

3. (a) What is an interpretive compiler? Why are interpretive compilers useful? The remainder of this question is about the Tiny interpretive compiler presented and used in the Programming Languages 3 course. (c) Draw tombstone diagrams representing the components of the Tiny interpretive compiler (expressed in Haskell). [2] Draw tombstone diagrams showing how to install the Tiny interpretive compiler on machine M. (Assume that a Haskell M compiler is available.) Also show how the interpretive compiler would be used to compile and run a Tiny program P. (d) Suppose now that you are required to build a compiler that will translate Tiny to M machine code. Using tombstone diagrams, show how you would build your compiler using the Tiny interpretive compiler components, adding just one new component. Also show how your compiler would be used to compile and run a Tiny program P. [10] Summer Diet - 3 - Continued Overleaf/

4. (a) Define Cartesian products ( ), disjoint unions (+), and mappings ( ). Briefly explain how each concept is relevant to the understanding of programming languages. [6] Using the concepts of part (a), write equations defining the set of values of each of the following Haskell types: data Piece = PAWN KING data Colour = WHITE BLACK type ColouredPiece = (Colour, Piece) data Square = EMPTY CONT ColouredPiece type Board = Int -> Square (c) Using the concepts of part (a), write an equation defining the set of objects in a Java program that includes the following classes: class Event { private Date date; private String description; // methods class Appointment extends Event { private Time time; // methods class Meeting extends Appointment { private int location; private String[] participants; // methods (Assume that Date and Time are library classes.) (d) Explain the difference between statically-typed and dynamically-typed programming languages. (e) [continued on next page] Summer Diet - 4 - Continued Overleaf/

(e) What are the advantages and disadvantages of static and dynamic typing? Illustrate your answer using the following Java method definition and method call: static int max (int x, int y) { if (x > y) then return x; else return y; int m = ; int n = ; int p = max(m, n); and the following Python function definition and function call: def max (x, y) : if x > y : return x else : return y m = n = p = max(m, n); (Java is statically-typed, whilst Python is dynamically-typed.) [10] Summer Diet -5- /END