CS 415 Midterm Exam Fall 2003

Similar documents
CS 415 Midterm Exam Spring SOLUTION

CS 415 Midterm Exam Spring 2002

MIDTERM EXAMINATION - CS130 - Spring 2003

NOTE: Answer ANY FOUR of the following 6 sections:

MIDTERM EXAMINATION - CS130 - Spring 2005

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

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

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

G Programming Languages - Fall 2012

Functional Programming. Pure Functional Programming

CS 360 Programming Languages Interpreters

Briefly describe the purpose of the lexical and syntax analysis phases in a compiler.

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

CMSC 331 Final Exam Section 0201 December 18, 2000

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

6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson

Question No: 1 ( Marks: 1 ) - Please choose one One difference LISP and PROLOG is. AI Puzzle Game All f the given

CS 406/534 Compiler Construction Putting It All Together

CSE 413 Midterm, May 6, 2011 Sample Solution Page 1 of 8

CSE413 Midterm. Question Max Points Total 100

Programming Languages Third Edition. Chapter 7 Basic Semantics

Principles of Programming Languages. Lecture Outline

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

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

COP4020 Spring 2011 Midterm Exam

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing

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.

Name EID. (calc (parse '{+ {with {x {+ 5 5}} {with {y {- x 3}} {+ y y} } } z } ) )

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

CSE 582 Autumn 2002 Exam 11/26/02

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

CS 275 Name Final Exam Solutions December 16, 2016

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

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

CD Assignment I. 1. Explain the various phases of the compiler with a simple example.

CS164: Midterm I. Fall 2003

Programming Languages Third Edition. Chapter 9 Control I Expressions and Statements

CS 314 Principles of Programming Languages

Functional Programming. Pure Functional Languages

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

Functional Programming. Pure Functional Languages

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

Organization of Programming Languages CS3200/5200N. Lecture 11

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

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

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Wellesley College CS251 Programming Languages Spring, 2000 FINAL EXAM REVIEW PROBLEM SOLUTIONS

Comp 311: Sample Midterm Examination

Qualifying Exam in Programming Languages and Compilers

CSE 341, Spring 2011, Final Examination 9 June Please do not turn the page until everyone is ready.

COP4020 Programming Assignment 1 - Spring 2011

MIDTERM EXAMINATION. CSE 130: Principles of Programming Languages. Professor Goguen. February 16, points total

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

CS 6353 Compiler Construction Project Assignments

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

User-defined Functions. Conditional Expressions in Scheme

CSE 401 Final Exam. December 16, 2010

Principles of Compiler Construction ( )

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

The role of semantic analysis in a compiler

CS 164 Handout 16. Final Examination. There are nine questions on the exam, some in multiple parts. You have 3 hours to work on the

SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

Compiler Code Generation COMP360

Short Notes of CS201

6.037 Lecture 4. Interpretation. What is an interpreter? Why do we need an interpreter? Stages of an interpreter. Role of each part of the interpreter

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

Scheme: Expressions & Procedures

Syntax. Syntax. We will study three levels of syntax Lexical Defines the rules for tokens: literals, identifiers, etc.

Project 1: Scheme Pretty-Printer

Modern Programming Languages. Lecture LISP Programming Language An Introduction

G Programming Languages - Fall 2012

CS201 - Introduction to Programming Glossary By

CSE 401 Midterm Exam Sample Solution 2/11/15

Stating the obvious, people and computers do not speak the same language.

MIDTERM EXAM (Solutions)

YOUR NAME PLEASE: *** SOLUTIONS ***

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

CS 6353 Compiler Construction Project Assignments

Principle of Complier Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore

CS 314 Principles of Programming Languages

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

CSCE 531 Spring 2009 Final Exam

Language Translation. Compilation vs. interpretation. Compilation diagram. Step 1: compile. Step 2: run. compiler. Compiled program. program.

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

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

COMPILER DESIGN. For COMPUTER SCIENCE

COP4020 Fall 2006 Final Exam

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

LECTURE 18. Control Flow

Fifth Generation CS 4100 LISP. What do we need? Example LISP Program 11/13/13. Chapter 9: List Processing: LISP. Central Idea: Function Application

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.

1 Lexical Considerations

Functional Programming

9/5/17. The Design and Implementation of Programming Languages. Compilation. Interpretation. Compilation vs. Interpretation. Hybrid Implementation

A simple syntax-directed

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

Problem Score Max Score 1 Syntax directed translation & type

DO NOT OPEN UNTIL INSTRUCTED

Time : 1 Hour Max Marks : 30

CSE 413 Final Exam Spring 2011 Sample Solution. Strings of alternating 0 s and 1 s that begin and end with the same character, either 0 or 1.

Transcription:

CS 415 Midterm Exam Fall 2003 Name KEY Email Address Student ID # Pledge: This exam is closed note, closed book. Questions will be graded on quality of answer. Please supply the best answer you can to each question. Good Luck! Score Fortran & Algol 60 Compilation Names, Bindings, Scope Functional Programming Logic Programming Control Flow Total /72 CS415 Anderson Fall 2003 Page 1 of 11

Fortran and Algol 60 (11 points) 1. [1 point] Fortran uses pass by reference for parameters. 2. [1 point] Algol was designed by _a committee of 8, in Zurich in 1958. 3. [5 points] List 3 significant differences between Fortran and Algol 60. (Many different answers possible here) 4. [4 points] Algol 60 introduced blocks of statements. List three reasons why these were useful in Algol 60. - programs easier to read (compared to gotos in Fortran) - supported structured programming - can use in place of a single statement - allows nested scopes - efficient storage management (allocate on entrance to a block and de-allocate on exit) - allows a fix to the dangling else problem CS415 Anderson Fall 2003 Page 2 of 11

Compilation [15 points] 1. [5 points] List the phases a typical compiler uses in converting a program into assembly language. Indicate what is passed from one phase to the next. A full credit answer would have at least 5 phases. character stream -> Scanner (Lexical Analysis) -> token stream -> Parser (Syntax Analysis) -> parse tree (concrete syntax tree) -> Semantic Analysis & -> abstract syntax tree intermediate code generation or other intermediate form -> Machine-Independent Code -> modified intermediate form Improvement (optional) -> Target Code Generation -> assembly code or other target language -> Machine-Specific Code -> modified target language Improvement (optional) 2. [3 points] Why might a program written in a compiled language execute faster than one written in a language that is interpreted? Interpreted must read and execute each statement as it is running, incurs overhead of analysis at run-time. Compiled compiler does analysis at compile-time, so this overhead is not incurred at runtime. In addition, a compiler can afford to take more time to do optimizations and may have access to more information at compile time than can typically be done in an interpreter. CS415 Anderson Fall 2003 Page 3 of 11

3. [2 points] Write a regular expression for an identifier in the cs415 language. Identifiers can consists of only letters (upper and lowercase) and digits, and must start with a capital letter and end with a digit. [A-Z] [A-Za-z0-9]* [0-9] 4. [2 points] Draw a finite automaton for the regular expression you gave in question 3 above. Be sure to properly denote the accepting states. A-Z 0-9 start accept 5. [3 points] Describe the dangling else problem. In the example below, it can be unclear which if stmt the else clause matches with. This can be solved in a number of ways, either by having an else should match the closest unmatched then, or by requiring explicit end markers to show the end of an IF statement. if cond then if cond then stuff else other stuff A-Za-z0-9 CS415 Anderson Fall 2003 Page 4 of 11

Names, Scopes, and Bindings (10 points) 1. [4 points] What does the function test print if the language uses static scoping? What does it print with dynamic scoping? (otherwise assume C++ syntax and semantics). int n = 100; // global print_plus_n(int x) { cout << x + n; } increment_n() { n = n + 1; } test() { int n; n = 1; print_plus_n(25); n = 33; print_plus_n(n); increment_n(); cout << n; } print_plus_n(n); With Static Scoping: 125 133 33 134 With Dynamic Scoping: 26 66 34 68 CS415 Anderson Fall 2003 Page 5 of 11

2. [4 points] Support the claim: Early binding generally leads to greater efficiency. First, define binding, and then give evidence why this might be true. binding an association between two things, such as a name and the thing it names (examples: a variable bound to a location in memory, a function name bound to a specific piece of code) Reasons why early binding can lead to greater efficiency: - compilation generally binds things earlier than interpretation. This can allow the compiler to do more optimization at compile time, or to avoid some indirection at run-time. - binding variables to memory locations early can avoid indirection at run-time (we can use a particular address rather than an indirect access). - binding variables to types ahead of time can eliminate some run-time type checking or can bind function addresses to call sites (rather than needing to consult a vtable at run-time to determine which function to call as in dynamic dispatch of functions). 3. [2 points] What does reference counting refer to? Give one problem with this approach. As discussed in class, this is an approach to garbage collection that keeps track of the number of references to an object. When the count goes to zero, then the object can be reclaimed. One problem with this approach is that it does not work on circularly linked structures (like a circularly-linked list) as each node will always be pointed to even if the structure is unreachable from the program. CS415 Anderson Fall 2003 Page 6 of 11

Functional programming (14 points) 1. [5 points] At several schools (MIT, Berkley, Rice) Scheme is the programming language used in introductory programming courses. Give at least 3 reasons why Scheme could be argued to be well-suited for this purpose? A few reasons: - garbage collection (don t need to worry about memory management) - simple syntax makes it easy to learn - if used in a purely functional style, no side effects will be used, this tends to avoid bugs related to un-expected side effects and makes it easier to reason about programs - good environments/interpreters are available free of charge (Dr. Scheme) - benefits of using an interpreter fast response (don t have to wait to recompile the whole program each time you change something, makes it easy to experiment), good error messages - closely related to math functions draws on students math reasoning abilities. 2. [2 points] What is the result of the following in Scheme. (Be specific) (define y (lambda (x y) (+ x y))) y is bound to a function that takes two parameters and returns their sum 3. [2 points] Assuming that the following definitions are executed in this order: (define b (3 14 27)) (define c (cons (car (cdr b)) (list a b c)))) What is the result of typing the following into the Scheme interpreter: c => (14 a b c) (car (cdr (cdr c))) => b CS415 Anderson Fall 2003 Page 7 of 11

4. [5 points] Write a recursive Scheme function, double_each that takes a single parameter that is a list and returns a list that contains all of the elements of that list doubled. You can assume that you will always be passed a list, and one that contains only integer values. Example: (double_each (4 5 6)) => (8 10 12) (define (double-each x) (if (null? x) () (cons (* 2 (car x)) (double-each (cdr x))))) CS415 Anderson Fall 2003 Page 8 of 11

Prolog and Logic Programming (12 points) 1. [2 points] What is the scope of a variable in Prolog? The clause in which it appears. 2. [2 points] Define backward chaining. Start with the goal and work backward, attempting to un-resolve it into a set of pre-existing clauses. (In contrast to forward chaining, that would start with the set of existing clauses in the database and work forward from that, attempting to derive the goal.) 3. [5 points] Write a function find_nth that will find the Nth element in a list. For example: find_nth([11, 12, 13, 14, 15], 4, X) returns X=14. find_nth([h _], 1, H). find_nth([_ T], N, H2):- N2 is N - 1, find_nth(t, N2, H2). CS415 Anderson Fall 2003 Page 9 of 11

4. [3 points] More Prolog: likes(mary,marlins). likes(fred,dracula). likes(fred,bats). likes(sue,yankees). likes(sue,bats). likes(fred,baseball). likes(fred,screaming). likes(fred,marlins). likes(mary,ghosts). likes(bob,cubs). likes(bob,pumpkins). likes(bob,dracula). likes(mary,bats). friends(x,y, Something) :- likes(x, Something), likes(y, Something), not(x = Y). What does this query produce? Give the complete set of responses obtained after typing ; until a No response is given.?- friends(fred,x,y). X = bob Y = dracula ; X = sue Y = bats ; X = mary Y = bats ; X = mary Y = marlins ; No CS415 Anderson Fall 2003 Page 10 of 11

Control Flow (10 points) 1. [5 points] Define side effect and short circuiting. Give an example where short circuiting and side effects could lead to unexpected results (if you didn t know that short circuiting was being used, for example). side effect influencing subsequent computation in any way other than returning a value for use in the surrounding context. short circuiting (of Boolean expressions) skips over the second half of an AND if the first part is false, skips over the second part of an OR if the first part if true. (Since in both cases the value of the expression can already be determined based on the value of the first part of the expression.) Generic example: ( false AND side effect-producing code) If short circuiting is used, then the side effect-producing code would never be executed when the first condition is false. ( cond1 AND i++) If i is meant to count the number of times the condition is tested, it will not be incremented whenever the cond1 is false. 2. [4 points] Give an example of a tail recursive function. (This can be in any language, and does not have to be perfect syntax, or to actually do anything useful.) What is an optimization that can be applied to tail recursive functions? int func (int x) { if (x == 0) return 1; else stuff return func (x 1); } An optimization is to convert the recursive call to a loop. In this way we avoid the overhead of function call and return and can re-use the stack frame from the original call (we don t need a new stack frame for the recursive calls anymore). 3. [1 point] Macros in C++ are an example of Normal Order evaluation. CS415 Anderson Fall 2003 Page 11 of 11