CSE3322 Programming Languages and Implementation

Similar documents
CSE3322 Programming Languages and Implementation

CSE3322 Programming Languages and Implementation

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

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

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

LL(k) Parsing. Predictive Parsers. LL(k) Parser Structure. Sample Parse Table. LL(1) Parsing Algorithm. Push RHS in Reverse Order 10/17/2012

CS 415 Midterm Exam Spring 2002

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

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

Syntax Analysis. Chapter 4

Top down vs. bottom up parsing

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

Recursive Descent Parsers

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

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

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

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

2068 (I) Attempt all questions.

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

UNIVERSITY OF CALIFORNIA

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

Lecture 14: Parser Conflicts, Using Ambiguity, Error Recovery. Last modified: Mon Feb 23 10:05: CS164: Lecture #14 1

Lecture 8: Deterministic Bottom-Up Parsing

Consider the following EBNF definition of a small language:

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

CS502: Compilers & Programming Systems

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

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

Lecture 7: Deterministic Bottom-Up Parsing

UNIVERSITETET I OSLO

Parsing Wrapup. Roadmap (Where are we?) Last lecture Shift-reduce parser LR(1) parsing. This lecture LR(1) parsing

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

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

CSE431 Translation of Computer Languages

Derivations vs Parses. Example. Parse Tree. Ambiguity. Different Parse Trees. Context Free Grammars 9/18/2012

Lecture Notes on Shift-Reduce Parsing

Error Recovery. Computer Science 320 Prof. David Walker - 1 -

Question Points Score

ML Built-in Functions

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

Parsing Algorithms. Parsing: continued. Top Down Parsing. Predictive Parser. David Notkin Autumn 2008

Question Marks 1 /12 2 /6 3 /14 4 /8 5 /5 6 /16 7 /34 8 /25 Total /120

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

CMSC330 Spring 2008 Final Exam

CSE-304 Compiler Design

Bottom-up parsing. Bottom-Up Parsing. Recall. Goal: For a grammar G, withstartsymbols, any string α such that S α is called a sentential form

4. An interpreter is a program that

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

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

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

Part III : Parsing. From Regular to Context-Free Grammars. Deriving a Parser from a Context-Free Grammar. Scanners and Parsers.

Conflicts in LR Parsing and More LR Parsing Types

Topic 3: Syntax Analysis I

CS5363 Final Review. cs5363 1

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

PL Revision overview

CS 415 Midterm Exam Spring SOLUTION

CSCI Compiler Design

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

Syntax Analysis, VII One more LR(1) example, plus some more stuff. Comp 412 COMP 412 FALL Chapter 3 in EaC2e. target code.

LL parsing Nullable, FIRST, and FOLLOW

4. Lexical and Syntax Analysis

CSE 130 Programming Language Principles & Paradigms Lecture # 5. Chapter 4 Lexical and Syntax Analysis

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

Semantic Analysis. Lecture 9. February 7, 2018

Compiler Code Generation COMP360

Downloaded from Page 1. LR Parsing

4. Lexical and Syntax Analysis

MIDTERM EXAM (Solutions)

Semester Review CSC 301

Compiler Design Aug 1996

Lexical and Syntax Analysis. Top-Down Parsing

CSE341 Autumn 2017, Midterm Examination October 30, 2017

CSE P 501 Compilers. LR Parsing Hal Perkins Spring UW CSE P 501 Spring 2018 D-1

Parsers. Xiaokang Qiu Purdue University. August 31, 2018 ECE 468

EDAN65: Compilers, Lecture 04 Grammar transformations: Eliminating ambiguities, adapting to LL parsing. Görel Hedin Revised:

Building a Parser III. CS164 3:30-5:00 TT 10 Evans. Prof. Bodik CS 164 Lecture 6 1

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

COP5621 Exam 3 - Spring 2005

Wednesday, September 9, 15. Parsers

Parsers. What is a parser. Languages. Agenda. Terminology. Languages. A parser has two jobs:

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

Context-free grammars


Lecture Notes on Bottom-Up LR Parsing

Recap from last time. Programming Languages. CSE 130 : Fall Lecture 3: Data Types. Put it together: a filter function

Lexical and Syntax Analysis

10/5/17. Lexical and Syntactic Analysis. Lexical and Syntax Analysis. Tokenizing Source. Scanner. Reasons to Separate Lexical and Syntax Analysis

CSE341 Spring 2016, Midterm Examination April 29, 2016

DEPARTMENT OF INFORMATION TECHNOLOGY / COMPUTER SCIENCE AND ENGINEERING UNIT -1-INTRODUCTION TO COMPILERS 2 MARK QUESTIONS

Parsing III. (Top-down parsing: recursive descent & LL(1) )

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

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

VALLIAMMAI ENGNIEERING COLLEGE SRM Nagar, Kattankulathur

Syntax/semantics. Program <> program execution Compiler/interpreter Syntax Grammars Syntax diagrams Automata/State Machines Scanning/Parsing

Syntax-Directed Translation. Lecture 14

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

CSE P 501 Compilers. Parsing & Context-Free Grammars Hal Perkins Spring UW CSE P 501 Spring 2018 C-1

Abstract Syntax Trees & Top-Down Parsing

Transcription:

Monash University School of Computer Science & Software Engineering Exam 2005 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. Questions which require explanations will receive no marks unless an appropriate explanation is given. 5. All questions should be attempted. 6. All questions should be answered in the exam paper itself. Fill in your name and Monash Student ID. Name: Student ID:

(Extra space for working)

Question 1 [6 marks] Each of the following pieces of SML code is inefficient. For each one, write an efficient piece of code that achieves the same result. For each answer state why your solution is more efficient. 1. length s > 0 2. take n (map f s) Note: take and map are standard functions from the list in the appendix. 3. map f (map g s) Question 2 [6 marks] Consider the following ML function fun foldl f z [] = z foldl f z (x::xs) = foldl f (f z x) xs; 1. What does the SML compiler infer as the type of the above function foldl? 2. Briefly describe how the SML compiler infers this type.

3. Write an SML expression that uses foldl to find the largest value in a list of integers, xs. Question 3 [6 marks] The following program does not work in SML. val rec ns = 0 :: map (fn n => n+1) ns; take 5 ns Note: take is a standard function from the list in the appendix. 1. Explain concisely why this program does not work. 2. Name the feature that ML would need to have for this program to work. 3. Give the value that would be computed given the feature under (2)

Question 4 [6 marks] In SML it is not possible to write a function sort which has just one parameter, that parameter being a list, and which can sort a list of integers, or a list of reals, or a list of strings, etc. 1. Describe the reason why this is not possible in SML. 2. Name the language feature that Haskell has which makes the task possible. 3. Describe the standard work around for this general problem in SML? Question 5 [5 marks] There are four functions defined in the following SML program and five code sections. For each code section list the functions that are in scope for that code section. let fun a() = let fun b() =...code1... in...code2... and c() = let fun d() =...code3... in...code4... in...code5...

Question 6 [8 marks] The following program is in a hypothetical programming language which resembles C but which may use a different parameter-passing mechanism. int i; int a[4]; void p( int m, int n ) { i++; m++; n++; } void main() { i = 1; a[0] = 0; a[1] = 1; a[2] = 2; a[3] = 3; } p( i, a[i] ); write( i ); write( a[0], a[1], a[2], a[3] ) For each of the following parameter-passing mechanisms write down the output of the program. Also explain for each answer how this output is computed. 1. By value (by input) 2. By value-result (by input-output) 3. By reference 4. By name

Question 7 [6 marks] The following program is in a hypothetical programming language which somewhat resembles C but which may use a different binding mechanism. procedure a() { print 1 }; procedure b() { procedure a() { print 2 }; c() }; procedure c() { a() }; procedure main() { b(); c() } 1. What would the program print if the language used dynaming binding? Explain your answer. [2 mark] 2. What would the program print if the language used static binding? Explain your answer. [2 mark] 3. State a possible advantage and a possible disadvantage of static binding vs. dynamic binding. [2 mark]

Question 8 [7 marks] 1. What is overloading as a programming language concept? [1 mark] 2. What is context-independent overloading as a programming language concept? [1 marks] 3. What is context-dependent overloading as a programming language concept? [1 marks] 4. Does SML use context-independent overloading or context-dependent overloading? [1 marks] 5. Give a simple expression which is ambiguous under context-dependent overloading. [1 marks] 6. Give two different meanings for the expression in your answer to the previous part.

Question 9 [8 marks] 1. What is the purpose of a garbage-collector in an implementation of a programming language? 2. Describe how a mark-scan garbage-collector works. 3. Give an advantage and a disadvantage of a programming language having garbagecollection. 4. Give two features of a programming language that require, or strongly imply, the use of a garbage-collector in implementations of the language.

(Extra space for working)

Question 10 [9 marks] Consider the following grammar with terminal symbols b c d non-terminal symbols A, B where B is the start symbol and with the productions B c A B B c c c A A b b A A d d (a) Explain concisely why this grammar cannot be used directly (ie. without any modifications) to implement a recursive descent parser. Name the technique that you would use to rectify this problem. (b) Give a modified grammar that recognises the same language but is suitable as the basis of a recursive descent parser. (c) Is your solution to part (b) a LL(k) grammar? If no, explain why not. If yes, show what is the smallest k for which it is LL(k)? [2 mark]

(d) In the space below, write down an ML function that implements the recursive descent function for the non-terminal B. You can assume that a datatype for tokens is given as datatype token = b c d; Note that the function does not need to return a parse tree. [3 marks]

(Extra space for working)

Question 11 [16 marks] Consider the following grammar with terminal symbols a b c d non-terminal symbols S, A, B where S is the start symbol and with the productions N umber P roduction 1 S A 2 S B 3 B c A B 4 B b c 5 B ɛ 6 A a B d 7 A d A d (a) Compute F IRST (S), F IRST (A), F IRST (B). [3 marks] (b) Compute F OLLOW (S), F OLLOW (A), F OLLOW (B). [3 marks] (c) Fill in the production numbers in the LL(1) parsing table below for a predictive table parser for this grammar. [6 marks] S B A a b c d $

(d) Detail using the table below how the sentence d a c a b c d d d would be parsed with a predictive table parser using the LL(1) parsing table you gave in (c). For each step of the process give the parser actions (production number / advance / accept), the stack state and the unprocessed rest of the input. Note that you may not need all lines of the table. [4 marks] ACTION STACK INPUT

Question 12 [17 marks] Consider the following grammar with terminal symbols a b c d non-terminal symbols S, A, B where S is the start symbol and with the productions N umber P roduktion 1 S A B 2 B c A B 3 B b c 4 B ɛ 5 A a B a 6 A d A d (a) Give the canonical collection of LR(0) items for this grammar. When computing the item sets, use the symbols in the order a, b, c, d, S, A, B. [7 marks]

(b) Fill in the SLR parsing table for this grammar in the table below. Note that you may not need all rows, i.e. there may be fewer than 16 canonical items. [5 marks] ACT ION GOT O ST AT E a b c d $ S A B 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 (c) Detail using the table below how the sentence a a c d a a d would be parsed with a LR parser using the table you gave in (b). For each step of the process give the parser actions (shift/reduce and production number) and stack state. Note that you may not need all rows. [4 marks] ACTION STACK INPUT (d) The sentence a a c d b a a d is not in the language defined by this grammar. Give the stack state and the current input symbol at the point where the LR parser discovers the syntax error. Describe an appropriate way to recover parsing at this point. [1 mark]