CSE3322 Programming Languages and Implementation

Similar documents
CSE3322 Programming Languages and Implementation

CSE3322 Programming Languages and Implementation

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

CSE341 Spring 2016, Midterm Examination April 29, 2016

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

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

CSE 341 Section 5. Winter 2018

CSE341 Autumn 2017, Midterm Examination October 30, 2017

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

CSE341 Spring 2016, Midterm Examination April 29, 2016

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

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Fall 2011

CSC324- TUTORIAL 5. Shems Saleh* *Some slides inspired by/based on Afsaneh Fazly s slides

CS143 Midterm Sample Solution Fall 2010

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

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Winter 2013

Consider the following EBNF definition of a small language:

CSE341 Spring 2017, Midterm Examination April 28, 2017

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

Parsing. Handle, viable prefix, items, closures, goto s LR(k): SLR(1), LR(1), LALR(1)

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

CSE341, Fall 2011, Midterm Examination October 31, 2011

Standard ML. Data types. ML Datatypes.1

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Subject Name: CS2352 Principles of Compiler Design Year/Sem : III/VI

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

Question Bank. 10CS63:Compiler Design

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

Formal Languages and Compilers Lecture VII Part 3: Syntactic A

PART 3 - SYNTAX ANALYSIS. F. Wotawa TU Graz) Compiler Construction Summer term / 309

UNIVERSITETET I OSLO

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

2068 (I) Attempt all questions.

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

UNIVERSITETET I OSLO

Let us construct the LR(1) items for the grammar given below to construct the LALR parsing table.

Handout 2 August 25, 2008

LR Parsers. Aditi Raste, CCOEW

Compiler Code Generation COMP360

Semantic Analysis. Lecture 9. February 7, 2018

CS153: Compilers Lecture 15: Local Optimization

CSE341, Fall 2011, Midterm Examination October 31, 2011

CSCI Compiler Design

Abstract Syntax Trees & Top-Down Parsing

Talen en Compilers. Jurriaan Hage , period 2. November 13, Department of Information and Computing Sciences Utrecht University

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

CSE 341 : Programming Languages

CSE 341 : Programming Languages Midterm, Spring 2015

COS 320. Compiling Techniques

MIDTERM EXAM (Solutions)

Introduction to SML Getting Started

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

COP5621 Exam 4 - Spring 2005

CSCI-GA Final Exam

Question Marks 1 /16 2 /13 3 /12 4 /15 5 /8 6 /15 7 /8 8 /5 9 /8 Total /100

Section A. A grammar that produces more than one parse tree for some sentences is said to be ambiguous.

Context-free grammars

SRM UNIVERSITY FACULTY OF ENGINEERING AND TECHNOLOGY SCHOOL OF COMPUTING DEPARTMENT OF CSE COURSE PLAN

How does ML deal with +?

COMPUTER SCIENCE TRIPOS

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

Advanced features of Functional Programming (Haskell)

CST-402(T): Language Processors

Lists. Michael P. Fourman. February 2, 2010

LECTURE NOTES ON COMPILER DESIGN P a g e 2

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

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

Functional programming Primer I

Appendix Set Notation and Concepts

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

[Syntax Directed Translation] Bikash Balami

CSE341 Section 3. Standard-Library Docs, First-Class Functions, & More

SRM UNIVERSITY FACULTY OF ENGINEERING AND TECHNOLOGY

COMP 181. Agenda. Midterm topics. Today: type checking. Purpose of types. Type errors. Type checking

Announcements. CSCI 334: Principles of Programming Languages. Lecture 5: Fundamentals III & ML

CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures. Dan Grossman Autumn 2018

Type Checking and Type Inference

Time : 1 Hour Max Marks : 30

SML A F unctional Functional Language Language Lecture 19

Question Points Score

Types and Static Type Checking (Introducing Micro-Haskell)

Recap: Functions as first-class values

Static semantics. Lecture 3-6: Semantics. Attribute grammars (2) Attribute grammars. Attribute grammars example. Dynamic semantics

Inductive Data Types

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

Downloaded from Page 1. LR Parsing

CSci 4223 Lecture 12 March 4, 2013 Topics: Lexical scope, dynamic scope, closures

8 Parsing. Parsing. Top Down Parsing Methods. Parsing complexity. Top down vs. bottom up parsing. Top down vs. bottom up parsing

Syntax-Directed Translation. Lecture 14

Semantic Analysis and Type Checking

CS 360: Programming Languages Lecture 12: More Haskell

Principles of Programming Languages [PLP-2015] Detailed Syllabus

SYLLABUS UNIT - I UNIT - II UNIT - III UNIT - IV CHAPTER - 1 : INTRODUCTION CHAPTER - 4 : SYNTAX AX-DIRECTED TRANSLATION TION CHAPTER - 7 : STORA

Introduction to OCaml

Compiler Construction: Parsing

CMSC 330: Organization of Programming Languages

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

Compiler construction lecture 3

CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion. Zach Tatlock Winter 2018

OCaml Data CMSC 330: Organization of Programming Languages. User Defined Types. Variation: Shapes in Java

Transcription:

Monash University School of Computer Science & Software Engineering Sample Exam 2004 CSE3322 Programming Languages and Implementation Total Time Allowed: 3 Hours 1. Reading time is of 10 minutes duration. 2. Examination time is of 3 hours duration. 3. The total marks are 100. 4. All questions should be attempted. 5. Question 1 should be answered in the exam paper itself, the remaining questions in a script book. Fill in your name and Monash Student ID. Name: Student ID:

Question 1 [30 marks] Answer the following multiple choice questions by ticking the box corresponding to the statement which best answers the question. If you wish to change your answer then cross out the wrong box and tick the new box. You receive 2 points for each correct answer. (a) Simula 67 is famous because it was the first database programming language the precursor to Prolog the precursor to Basic it was designed for graphical user interfaces the first object-oriented programming language (b) A signature in SML is used to initialise global variables in a structure a higher-order function a higher-order function with multiple arguments a function used to handle exceptions none of the above (c) What will the SML function mystery defined as follows do? fun mystery c = map real c; convert the elements of a list of integers to a list of reals covert an integer to a real number convert a character to a real number give a syntax error none of the above (d) Consider the SML program fun dummy [] = 0 dummy [x] = 0 dummy (x::y::xs) = dummy(xs) + x; What does the expression dummy [4,5,6] evaluate to? val it = 15 : val it = 9 : val it = 15 : val it = 9 : int int None of the above. real real

(e) Consider the SML program fun silly f g x = f (g x) What does the expression silly ~ (fn x => 2*x) evaluate to? val it = ~2 : int val it = 2 : int A function which takes an integer and negates it A function which takes an integer and multiplies it by 2 Gives a type error because an argument to silly is missing. (f) Which of the following statements about DNA programming is not true one of the first problems DNA programming was used to solve was the Hamiltonian path problem DNA programs can be used to solve the halting problem for Turing machines in a DNA program it is more expensive to read DNA strands than to write them in theory you can replicate a DNA strand 2 n times in time proportional to n a problem with DNA computing is that long DNA strands are fragile and may break. (g) Consider the query state(city,vic) run with the Prolog program: state(adelaide,sa). state(sydney,nsw). state(newcastle,nsw). state(geelong,vic). state(melbourne,vic). What is the second answer found? City = sa City = nsw City = newcastle City = melbourne none of the above

(h) Why does SML use static binding rather than dynamic binding? Because: dynamic binding doesn t work well with compile-time type checking and inference it s an implementation decision which does not change the meaning of a program dynamic binding only works in imperative programming languages there is no need to since abstract datatypes provide this functionality context dependent overloading only works with static binding (i) Which of the following is not true about the language SML? it provides pattern matching it provides type classes it has polymorphic types it has data constructors it has functors (j) Recall that Cascal is the hypothetical programming language introduced in the lectures. Consider the Cascal program: int s = 2; int d = 4; int function temp(void) { return d+s; } void main(void) { int s := 1; int d := 2; s := temp(); writeln( s ); } What will be written by the above program if Cascal uses static binding? 6 3 2 1 none of the above

(k) Consider the Cascal program: int function temp(int x, int y) { x := y + 1; } void main(void) { int s := 3; int t := 4; temp(s,t); writeln(s); } What will be written by the above program if Cascal uses value-result parameter passing? 3 4 5 6 nothing, but it will generate a run-time error (l) Which of the following items is not usually stored in an activation record? procedure parameters return address global variables local variables temporary variables (m) In which phase of a compiler is the type of a variable typically determined? lexical analysis screening syntactic analysis semantic analysis target code generation

(n) Which of the following parsing methods can process the largest class of grammars? LL(1) parsers canonical LR parsers recursive descent parsers without backtracking SLR parsers CYK parsers (o) Which of the following is not a machine independent code optimisation? Tail-recursion optimisation Procedure call inlining Peephole optimisation Elimination of dead code Moving loop-invariant computation from inside loops

Question 2 [6 marks] Write an SML function last : a list -> a such that last xs returns the last element in the list xs. For example, evaluating last [4.0,2.0,3.0] should return the value 3.0. If the list is empty the function should raise an appropriate exception. This should be declared. Question 3 [8 marks] Write an SML function find : ( a -> bool) -> a list -> a such that find p xs returns the first element in the list xs for which the function p returns true. For example, evaluating find (fn x => (x < 4.0)) [5.0,2.0,3.0] should return the value 2.0. If the function is not true for any element in the list find should raise an appropriate exception. This should be declared. Question 4 [16 marks] You are required to write functions to manipulate simple geometric shapes. Each shape is either a circle, with a center and a radius, or a box, with a center and height and width. All attributes are reals. (a) Define an SML datatype Shape for representing a circle or box. [4 marks] (b) Write two SML functions circle : (real*real) -> real -> Shape box : (real*real) -> real -> real -> Shape which respectively create a new circle or box given their center and size. For instance circle (1.0,2.0) 3.0 should return a new circle shape with center (1.0, 2.0) and radius 3.0 while box (1.0,2.0) 3.0 4.0 should return a new box shape with center (1.0, 2.0), width 3.0 and height 4.0. [6 marks] (c) Write an SML function contains : Shape -> (real*real) -> bool

which takes a shape and a point and returns true if the point is strictly inside the shape and false otherwise. For instance, both contains (circle (1.0,2.0) 3.0) (1.0,1.0) contains (box (1.0,2.0) 3.0 4.0) (1.0,1.0) should return true while both contains (circle (1.0,2.0) 3.0) (10.0,10.0) contains (box (1.0,2.0) 3.0 4.0) (10.0,10.0) should return false. [6 marks] fun between x y z = x y andalso y z:real; fun contains (Circle(x,y,r)) (x1,y1) = ((x1-x)*(x1-x) + (y1-y)*(y1-y) ) r*r contains (Box(x,y,w,h)) (x1,y1) = (between x-0.5*w x1 x+0.5*w) andalso (between y-0.5*h y1 y+0.5*h); Question 5 [8 marks] Consider the following grammar with terminal symbols a b and non-terminal symbols S and X where S is the start symbol and productions S X X a X X a X b X X b (a) The above grammar recognizes a regular language. language. Give a regular expression for this [2 marks] (b) Add attributes and attribute computation rules and conditions to the above grammar so that it recognises non-empty strings with an equal number of a s and b s. I.e., aabbab should be in the language of this new grammar while aaabb should not. You should indicate whether the attributes are inherited or synthesized. [6 marks] Question 6 [12 marks] Consider the following grammar with terminal symbols a b + non-terminal symbols S, A, B where S is the start symbol and productions

(P 1) S A + B (P 2) S B (P 3) A a A (P 4) A ɛ (P 5) B b B (P 6) B ɛ (a) Compute F IRST (A), F IRST (B), F IRST (S). (b) Compute F OLLOW (A), F OLLOW (B), F OLLOW (S). [2 marks] [2 marks] (c) Consider the following LL(1) parsing table for a predictive table parser: a b + $ S P 1 P 2 P 1 A P 3 P 4 B P 5 P 6 where P i refers to the i th production in the above grammar. Detail how the sentence a + b would be parsed with a predictive table parser using this table. For each step of the process give the parser action, input and stack state. [4 marks] (d) Is the parsing table given in (c) the correct LL(1) predictive parsing table for this grammar? If not identify and correct the errors in the table. [4 marks] Question 7 [10 marks] Consider the augmented grammar which consists of the symbols and productions in the grammar given in Question 6 and the new start symbol S and the additional production (P 0) S S (a) Compute I 0 = closure({s S}) goto(i 0, a) goto(i 0, b) goto(i 0, +) goto(i 0, A) goto(i 0, B) goto(i 0, S) [5 marks]

(b) Consider the following LR parsing table for this grammar ACT ION GOT O ST AT E a b + $ S A B 0 s1 s2 r4 r6 5 3 4 1 s1 r4 6 2 s2 r6 7 3 s8 r6 4 r2 5 acc 6 r3 7 r5 8 s2 9 9 r1 where si is shift and stack state i rj is reduce using production P j acc is accept. Detail how the sentence a + b would be parsed with an LR parser using this table. For each step of the process give the parser action (shift/reduce), input and stack state. [5 marks] Question 8 [10 marks] Consider the core ML program val mystery = fn f => (fn x => f(f(x))) (a) Give its syntax tree and assign a type variable to each subexpression. [3 marks] (b) Generate a set of type equations (or constraints) on the type variables based on the annotated syntax tree from (a) [4 marks] (c) Solve the type equations from (b) and give the type for mystery. [3 marks]