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

Similar documents
Name SOLUTIONS EID NOTICE: CHEATING ON THE MIDTERM WILL RESULT IN AN F FOR THE COURSE.

CS 415 Midterm Exam Spring 2002

Functions and Recursion. Dr. Philip Cannata 1

CS 536 Midterm Exam Spring 2013

Comp 311: Sample Midterm Examination

CS 415 Midterm Exam Spring SOLUTION

An Introduction to Functions

User-defined Functions. Conditional Expressions in Scheme

MIDTERM EXAMINATION - CS130 - Spring 2005

CS 415 Midterm Exam Fall 2003

Reminder About Functions

Principles of Programming Languages

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

COP4020 Spring 2011 Midterm Exam

Functional Programming. Pure Functional Programming

Functions and Recursion

FP Foundations, Scheme

1 Lexical Considerations

Defining syntax using CFGs

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

LECTURE 16. Functional Programming

CUP. Lecture 18 CUP User s Manual (online) Robb T. Koether. Hampden-Sydney College. Fri, Feb 27, 2015

Higher-Order Functions (Part I)

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

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

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

Syntax-Directed Translation. Introduction

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

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

Higher-Order Functions

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

CS 275 Name Final Exam Solutions December 16, 2016

Principles of Programming Languages 2017W, Functional Programming

Question Points Score

Bindings & Substitution

Lex and Yacc. More Details

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

Lexical Considerations

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

MIDTERM EXAMINATION - CS130 - Spring 2003

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

;;; Determines if e is a primitive by looking it up in the primitive environment. ;;; Define indentation and output routines for the output for

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

Evaluating Scheme Expressions

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

JavaCC: SimpleExamples

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

Project 2: Scheme Interpreter

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

A LISP Interpreter in ML

Parsing III. CS434 Lecture 8 Spring 2005 Department of Computer Science University of Alabama Joel Jones

Structure and Interpretation of Computer Programs

Last Time. What do we want? When do we want it? An AST. Now!

Lecture 12: Parser-Generating Tools

TML Language Reference Manual

CPS 506 Comparative Programming Languages. Programming Language Paradigm

Functional Programming Languages (FPL)

cs173: Programming Languages Final Exam

CPS 506 Comparative Programming Languages. Syntax Specification

CSCE 314 Programming Languages. Type System

Every language has its own scoping rules. For example, what is the scope of variable j in this Java program?

SCHEME The Scheme Interpreter. 2 Primitives COMPUTER SCIENCE 61A. October 29th, 2012

Lexical Considerations

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

Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003

Lecture Code Generation for WLM

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

CS 4240: Compilers and Interpreters Project Phase 1: Scanner and Parser Due Date: October 4 th 2015 (11:59 pm) (via T-square)

PROGRAMMING FUNDAMENTALS

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

Why do we need an interpreter? SICP Interpretation part 1. Role of each part of the interpreter. 1. Arithmetic calculator.

Chapter 15. Functional Programming Languages

YOUR NAME PLEASE: *** SOLUTIONS ***

Syntax and Grammars 1 / 21

UNIVERSITY OF TORONTO Faculty of Arts and Science. Midterm Sample Solutions CSC324H1 Duration: 50 minutes Instructor(s): David Liu.

Racket: Modules, Contracts, Languages

Chapter 5 Control Statements: Part 2 Section 5.2 Essentials of Counter-Controlled Repetition

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

CS 314 Principles of Programming Languages

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

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

Qualifying Exam in Programming Languages and Compilers

Structure and Interpretation of Computer Programs

Functions & First Class Function Values

Answer: Early binding generally leads to greater efficiency (compilation approach) Late binding general leads to greater flexibility

An Introduction to Scheme

SCHEME AND CALCULATOR 5b

Lecture Outline. COOL operational semantics. Operational Semantics of Cool. Motivation. Lecture 13. Notation. The rules. Evaluation Rules So Far

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

CS 11 Ocaml track: lecture 6

Scheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax

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

Introduction to Typed Racket. The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples

CS 314 Principles of Programming Languages

TAIL RECURSION, SCOPE, AND PROJECT 4 11

Procedures. EOPL3: Section 3.3 PROC and App B: SLLGEN

Introduction to Scheme

6.001, Spring Semester, 1998, Final Exam Solutions Your Name: 2 Part c: The procedure eval-until: (define (eval-until exp env) (let ((return (eval-seq

CS 360 Programming Languages Interpreters

Transcription:

CS 345 Spring 2010 Midterm Exam Name EID 1. [4 Points] Circle the binding instances in the following expression: (calc (parse '+ with x + 5 5 with y - x 3 + y y z ) ) 2. [7 Points] Using the following grammar: Exp Fact Exp + Fact Exp * Fact Fact 0... 9 ( Exp ) draw a parse tree for the following expression: 5 * (4 + 3) Exp Exp * Fact Fact ( Exp ) 5 Exp + Fact Fact 3 4 3. [2 Points] Circle the free instances in the following expression: (calc (parse '+ with x + 5 5 with y - x 3 + y y z ) ) 1

4. [3 Points] What is the structure of Production Rules for the following Grammars? [1.5 Points] Right regular grammar α β α Terminal*, β Nonterminal [1.5 Points] Context-Free Grammar or BNF α β α Nonterminal β (Nonterminal Terminal)* 5. [4 Points] Circle the scope of each binding instance in the following expression: (calc (parse '+ with x + 5 5 with y - x 3 + y y z ) ) 6. [7 Points] Using the PLAI DrShceme code on the last page, show the order in which the numbered lines will be executed for the following invocation (calc (parse 'with x 4 + x x)) starting with line 18 and ending with the first time line 37 is executed. These are not part of the solution 18 (with 'x 19 10 (num 4) 20 12 13 14 (add 11 (id 'x) 15 11 (id 'x) 44 47 41 25 (add 37 4 7. [ 3 Points] When subst is called for the first time in the problem above, what will be the value for each of the following variables? (Getting the s correct is not necessary for the grading of this problem) 1. named-expr: (add (id 'x) (id 'x)) 2. sub-id: x 3. val: (num 4) 2

8. [10 Points] Complete the following sim.jj program that will parse the following statements from the test.sim file $ cat test.sim CLASS Person ( person-id : INTEGER, REQUIRED; first-name : STRING, REQUIRED; last-name : STRING, REQUIRED; home_address : STRING; zipcode : INTEGER; home-phone : INTEGER; us-citizen : BOOLEAN, REQUIRED; ); Sim.jj CLASS Department ( dept-no : INTEGER, REQUIRED; dept-name : STRING; ); and produce the following output: $ cat test.sim sim Saw IDENTIFIER person-id Saw IDENTIFIER first-name Saw IDENTIFIER last-name Saw IDENTIFIER home_address Saw IDENTIFIER zipcode Saw IDENTIFIER home-phone Saw IDENTIFIER us-citizen Saw IDENTIFIER Person Saw IDENTIFIER dept-no Saw IDENTIFIER dept-name Saw IDENTIFIER Department PARSER_BEGIN(Sim) import java.io.*; import java.util.*; public class Sim public static void main(string args[]) throws ParseException Sim parser = new Sim(System.in); parser.ddl(); 3

PARSER_END(Sim) SKIP : " " "\t" "\n" "\r" <"//" (~["\n","\r"])* ("\n" "\r")> TOKEN: < CLASS: "CLASS" > < REQUIRED: "REQUIRED" > < BOOLEAN: "BOOLEAN" > < INTEGER: "INTEGER" > < STRING: "STRING" > < LPAREN: "(" > < RPAREN: ")" > < SEMI: ";" > < COMMA: "," > < COLON: ":" > TOKEN: /* Literals */ < IDENTIFIER: [ "A"-"Z", "a"-"z" ] ( [ "A"-"Z", "a"-"z", "0"-"9", "_", "-" ] )* > TOKEN: <ERROR: ~[] > void DDL() : String s; ( <CLASS> s = identifier() <LPAREN> declarations() <RPAREN> <SEMI> System.out.println("Saw IDENTIFIER " + s); )+ void declarations() : ( declaration() )* 4

void declaration() : String s; s = identifier() <COLON> type() <SEMI> System.out.println(" Saw IDENTIFIER " + s); void type() : <INTEGER> [ <COMMA> <REQUIRED> ] <STRING> [ <COMMA> <REQUIRED> ] <BOOLEAN> [ <COMMA> <REQUIRED> ] String identifier() : Token t; t = <IDENTIFIER> return new String(t.image.trim()); 9. [7 Points ] Draw the Symbol Table with Lifetimes for the following program. 1 int answer; 2 int fibonacci(int n) 3 4 int temp1, temp2; 5 if(n==0) return 0; 6 else 7 if(n==1) return 1; 8 else 9 temp1 = fibonacci(n-1); 10 temp2 = fibonacci(n-2); 11 int r; 12 r = temp1 + temp2; 13 return r ; 14 15 16 17 int main () 18 19 int num; 20 num = 9; 22 answer = fibonacci(num); 23 1. Outer scope: <answer, 1> <fibonacci, 2> <main, 17> 1-23 2. Function fibonacci nested scope: <n> 2-16 3. <temp1, 4> <temp2, 4> 4 16 4. <r, 11> 11-14 5. Function main nested scope <num 19> 19-23 5

10. [5 Points] What is the definition of a Relation? A Relation is the subset of the cross-product of a set of domains. 11. [5 Points] What will the following lisp expression evaluate to if Static Scoping is used? (let ((z 20)) (let ((z 3) (a 5) (x (lambda (x y) (- x (+ y z))))) (let ((z 10) (a 5)) (x z a)))) -15 12. [5 Points ] Complete the following definition of the factorial function and its application to 5 using the Primitive Lisp discussed in class. (letrec ((factorial (lambda (N) (if (= N 0) 1 (* N (factorial (- N 1)))) ))) (factorial 5)) 13. [5 Points]What will the following lisp return? (letrec ( (List (list 1 2 3 4 5 6 7 )) (first (lambda (List) (if (null? List) (list) (car List)))) (sum-list (lambda (List) (if (null? List) 0 (+ (car List) (sum-list (cdr List)))))) (nth (lambda (N List) (if (not (= N 0))(nth (- N 1) (cdr List))(car List)))) (head (lambda (N List) (if (= N 0) (list) (cons (car List) (head (- N 1) (cdr List)))))) ) (head (nth 3 List) List) ) (list 1 2 3 4) 14. [5 Points] What will the following lisp expression evaluate to if Dynamic Scoping is used? (let ((z 20)) (let ((z 3) (a 5) (x (lambda (x y) (- x (+ y z))))) (let ((z 10) (a 5)) (x z a)))) -5 6

15. [5 Points] Draw the runtime stack at the point where B has executed its last line in the program below. int h, x; void B(int w) int y, k; x = 3*w; w = w+1; void A(int c, int d) bool e, f; B(h); int main() int a, b; h = 5; a = 7; b = 8; A(a, b); h 5 x 15 a 7 b 8 c 7 d 8 e undefined f undefined w 6 y undefined k undefined 7

16. [8 Points] Give the proof by contradiction steps to show that father is True in the following Prolog database. Justify each step. You may find the patterns on the right useful. 1. father :- parent, male. 2. parent. 3. female. 4. male. 5. -father 6. 5 and 1 and Pattern 1 -> -(parent, male) 7. 6 and 2 and Pattern 2 -> -male 8. 7 and 4 and Pattern 3 -> Contradiction therefore father is True Pattern 1: Q :- (P1, P2). -Q -(P1, P2) Pattern 2: P1. -(P1, P2) -P2 Pattern 3: P2. -P2 Contradiction 8

(define-type WAE [num (n number?)] [add (lhs WAE?) (rhs WAE?)] [sub (lhs WAE?) (rhs WAE?)] [with (name symbol?) (named-expr WAE?) (body WAE?)] [id (name symbol?)]) (define parse (lambda (sexp) (cond 10 [(number? sexp) (num sexp)] 11 [(symbol? sexp) (id sexp)] 12 [(list? sexp) 13 (case (first sexp) 14 [(+)(add (parse (second sexp)) 15 (parse (third sexp)))] 16 [(-) (sub (parse (second sexp)) 17 (parse (third sexp)))] 18 [(with) (with (first (second sexp)) 19 (parse (second (second sexp))) 20 (parse (third sexp)))] )]))) (define (subst expr sub-id val) (type-case WAE expr 24 [num (n) expr] 25 [add (l r) (add (subst l sub-id val) 26 (subst r sub-id val))] 27 [sub (l r) (sub (subst l sub-id val) 28 (subst r sub-id val))] 29 [with (bound-id named-expr bound-body) 30 (if (symbol=? bound-id sub-id) 31 (with bound-id 32 (subst named-expr sub-id val) 33 bound-body) 34 (with bound-id 35 (subst named-expr sub-id val) 36 (subst bound-body sub-id val)))] 37 [id (v) (if (symbol=? v sub-id) val expr)])) ;; calc : WAE!number (define (calc expr) (type-case WAE expr 41 [num (n) n] 42 [add (l r) (+ (calc l) (calc r))] 43 [sub (l r) (- (calc l) (calc r))] 44 [with (bound-id named-expr bound-body) 45 (calc (subst bound-body 46 bound-id 47 (num (calc named-expr))))] 48 [id (v) (error 'calc "free identifier")])) 9