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

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

Fall 2012 CS345 Project Requirements and Suggestions (Version 4)

Programming Languages. Dr. Philip Cannata 1

Functions and Recursion. Dr. Philip Cannata 1

Programming Languages!

CS 415 Midterm Exam Spring 2002

Lecture 12: Parser-Generating Tools

fjyswan Dr. Philip Cannata 1

Programming Languages. Dr. Philip Cannata 1

Higher-Order Functions

3. Context-free grammars & parsing

Introduction to Lexing and Parsing

Comp 311: Sample Midterm Examination

CS164: Midterm I. Fall 2003

Question Points Score

High Level Languages. Java (Object Oriented) This Course. Jython in Java. Relation. ASP RDF (Horn Clause Deduction, Semantic Web) Dr.

COP4020 Spring 2011 Midterm Exam

A simple syntax-directed

First Midterm Exam CS164, Fall 2007 Oct 2, 2007

CS 415 Midterm Exam Fall 2003

cs173: Programming Languages Final Exam

Review main idea syntax-directed evaluation and translation. Recall syntax-directed interpretation in recursive descent parsers

Chapter 4. Lexical and Syntax Analysis

CS 275 Name Final Exam Solutions December 16, 2016

MORE SCHEME. 1 What Would Scheme Print? COMPUTER SCIENCE MENTORS 61A. October 30 to November 3, Solution: Solutions begin on the following page.

Syntax and Grammars 1 / 21

Homework. Lecture 7: Parsers & Lambda Calculus. Rewrite Grammar. Problems

cs173: Programming Languages Midterm Exam

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

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

CS 406/534 Compiler Construction Putting It All Together

CS 536 Midterm Exam Spring 2013

CPSC 411, Fall 2010 Midterm Examination

1.3. Conditional expressions To express case distinctions like

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

CMSC330 Spring 2017 Midterm 2

ASTs, Objective CAML, and Ocamlyacc

CS 415 Midterm Exam Spring SOLUTION

Dr. D.M. Akbar Hussain

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

CS 11 Ocaml track: lecture 6

Examination in Compilers, EDAN65

Structure and Interpretation of Computer Programs

JavaCC Parser. The Compilation Task. Automated? JavaCC Parser

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

COMP3131/9102: Programming Languages and Compilers

The Parsing Problem (cont d) Recursive-Descent Parsing. Recursive-Descent Parsing (cont d) ICOM 4036 Programming Languages. The Complexity of Parsing

UMBC CMSC 331 Final Exam

Defining syntax using CFGs

CS 360 Programming Languages Interpreters

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

Functional Programming. Pure Functional Programming

Structure and Interpretation of Computer Programs

Principles of Programming Languages

Lecture #13: Type Inference and Unification. Typing In the Language ML. Type Inference. Doing Type Inference

JavaCC: SimpleExamples

FP Foundations, Scheme

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

Project 1: Scheme Pretty-Printer

MIDTERM EXAMINATION - CS130 - Spring 2005

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

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

CSCE 531 Spring 2009 Final Exam

CSE341 Spring 2017, Final Examination June 8, 2017

Programming Languages. Dr. Philip Cannata 1

Lambda Calculus as a Programming Language

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

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

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

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

Project 2: Scheme Interpreter

CS 230 Programming Languages

MIDTERM EXAM (Solutions)

Final Exam CS164, Fall 2007 Dec 18, 2007

FUNCTIONAL AND LOGIC PROGRAMS

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

CSE341 Spring 2017, Final Examination June 8, 2017

Build your own languages with

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs Summer 2014 Midterm 1

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

UMBC CMSC 331 Final Exam Section 0101 December 17, 2002

Chapter 4. Abstract Syntax

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

CSEP 501 Compilers. Languages, Automata, Regular Expressions & Scanners Hal Perkins Winter /8/ Hal Perkins & UW CSE B-1

Principles of Programming Languages 2017W, Functional Programming

CS 61A, Fall, 2002, Midterm #2, L. Rowe. 1. (10 points, 1 point each part) Consider the following five box-and-arrow diagrams.

COP4020 Programming Assignment 1 - Spring 2011

Parsing a primer. Ralf Lämmel Software Languages Team University of Koblenz-Landau

9/23/2014. Why study? Lambda calculus. Church Rosser theorem Completeness of Lambda Calculus: Turing Complete

A LISP Interpreter in ML

Functional Programming - 2. Higher Order Functions

Programming Languages

Building lexical and syntactic analyzers. Chapter 3. Syntactic sugar causes cancer of the semicolon. A. Perlis. Chomsky Hierarchy

CIT 3136 Lecture 7. Top-Down Parsing

Typed Recursion {with {mk-rec : (((num -> num) -> (num -> num)) -> (num -> num)) {fun {body : ((num -> num) -> (num -> num))} {{fun {fx :

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson

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

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

CPS 506 Comparative Programming Languages. Syntax Specification

Transcription:

CS 345 Fall TTh 2012 Midterm Exam B Name SOLUTIONS EID NOTICE: CHEATING ON THE MIDTERM WILL RESULT IN AN F FOR THE COURSE. 1. [5 Points] Give two of the following three definitions. [2 points extra credit for all three]. a. What is the definition of a relation? A Relation is a subset of the cross-product of a set of domains. b. What is the relation definition of a Function? An n-ary relation R is a function if the first n-1 elements of R are the function s arguments and the last element is the function s results and whenever R is given the same set of arguments, it always returns the same results. c. What is the definition of a primitive recursive function? A function is called primitive recursive if there is a finite sequence of functions ending with f such that each function is a successor, constant or identity function or is defined from preceding functions in the sequence by substitution or recursion. 2. [5 Points] In the DeLong book, primitive recursive addition is defined as f(x, 0) = x f(x, y ) = (f(x, y)) Write this definition of addition in relation notation and show that 2 + 2 = 4 using this relation notation. (x 0 x) (x y ( x y b) ) (0 0 0 ) 2 + 0 = 2 (0 0 (0 0 b) ) => (0 0 (0 ) ) => (0 0 0 ) 2 + 1 = 3 (0 0 (0 0 b) ) => (0 0 (0 ) ) => (0 0 0 ) 2 + 2 = 4 1

3. [5 Points] In DeLong s book, Howard gives the functions for primitive recursive addition as follows. Implement these functions in lisp using let and lambda functions. In the let statement for the solution show 2 + 3. f 1 (x) = x f 2 (x) = x f 3 (y, z, x) = z f 4 (y,z,x) = f 1 (f 3 (y,z,x)) f 5 (0, x) = f 2 (x) f 5 (y, x) = f 4 (y, f 5 (y,x), x) (let ((f1 (lambda (x) (+ x 1))) (f2 (lambda (x) x)) (f3 (lambda (y z x) z))) (let ((f4 (lambda (y z x) (f1 (f3 y z x))))) (letrec ((f5 (lambda (a b) (if (= a 0) (f2 b) (f4 (- a 1) (f5 (- a 1) b) b))))) (f5 2 3)))) 4. [5 Points] Given the following: def zero = λx.x def succ = λn.λs.((s false) n) def pred = λn.(((iszero n) zero) (n select_second)) def iszero = λn.(n select_first) one = (succ zero) = λs.((s false) zero) Show that (pred one) = zero. Hint: don t substitute λs.((s false) zero) for one until you need to apply one. Also, you don t need to show the details that (iszero one) is false or ((select_second a) b) is b. (λn.(((iszero n) zero) (n select_second)) one) (((iszero one) zero) (one select_second)) ((false zero) (one select_second)) (one select_second) (λs.((s false) zero) select_secone) ((select_second false) zero) zero 2

5. [ 5 Points] Evaluate the following λ expressions: (((λx.λy.λz.((x y) z) λf.λa.(f a)) λk.k) λh.h) ((λy.λz.((λf.λa.(f a) y) z) λk.k) λh.h) (λz.((λf.λa.(f a) λk.k) z) λh.h) ((λf.λa.(f a) λk.k) λh.h) (λa.(λk.k a) λh.h) (λk.k λh.h) λh.h 6. [10 Points] Write the Environment (Repository of Differed Substitutions) for the following expression: (with (H 0.001) (with (f (fun (x) (+ x H))) (with (d/dx (fun (x) (/ (- (f (+ x H))(f x)) H))) (with (f (fun (x) (* x x))) (d/dx 10))))) (asub 'f (closurev 'x (mul (id 'x) (id 'x)) (mtsub)) (asub 'd/dx (closurev 'x (div (sub (app (id 'f) (add (id 'x) (id 'H))) (app (id 'f) (id 'x))) (id 'H)) (asub 'H (numv 0.001) (asub 'f (closurev 'x (add (id 'x) (id 'H)) (asub 'H (numv 0.001) (mtsub))) (mtsub)))) (asub 'f (closurev 'x (add (id 'x) (id 'H)) (asub 'H (numv 0.001) (mtsub))) (asub 'H (numv 0.001) (mtsub)) ) ) ) 3

(with (H 0.001) (with (f (fun (x) (+ x H))) (with (d/dx (fun (x) (/ (- (f (+ x H))(f x)) H))) (with (f (fun (x) (* x x))) (d/dx 10))))) 7. [3 Points] With static scoping, what will the above expression approximately evaluate to: 1 8. [3 Points] With dynamic scoping, what will the above expression approximately evaluate to: 20 9. [3 Points] What will the following expression return? (letrec ((f (lambda (f1 v l) (if (null? l) v (f1 (car l) (f f1 v (cdr l))))))) (f cons '() '(1 2 3 4 5 6))) '(1 2 3 4 5 6) 10. [3 Points] What will the following expression return? (letrec ((f (lambda (f1 v l) (if (null? l) v (f1 (car l) (f f1 v (cdr l))))))) (f + 0 '(1 2 3 4 5 6))) 21 11. [3 Points] What will the following expression return? (letrec ((f (lambda (f1 v l) (if (null? l) v (f f1 (f1 (car l) v) (cdr l)))))) (f cons '() '(1 2 3 4 5 6))) '(6 5 4 3 2 1) 4

12. [10 Points] Fill in the blanks in the following description of a javacc-based processing of a sentence in a programming language. Tokens for a programming language are defined using regular expressions in a Parser.jj file. Javacc processes the TOKEN portion of the Parser.jj file and produces a lexical analyzer or tokenizer as a Finite State Machine. Tokens are passed from this to the Parser defined in a Parser.jj file using a context-free grammar, BNF, or EBNF which is. Tokens from the lexical analyzer or tokenizer are accepted by the Parser and it builds an [internal] parse tree. While this is being built, the javacc programmer could have specified how to also build an abstract syntax tree, which would be used by the interpreter. 13. [5 Points] What is the structure of Production Rules for the following Grammars? [2 Points] Context-Free Grammar α β α Nonterminal β (Nonterminal Terminal)* [2 Points] Right regular grammar α ω α ω β ω Terminal*, α and β Nonterminal [1 Points] Context-Sensitive Grammar α β α and β (Nonterminal Terminal)* α <= β 5

14. [10 Points] Complete the cal.jj file below that implements a calculator with the following grammar specified in EBNF using the following metacharacters which are in bold in the grammar: a series of zero or more ( ) must pick one from a list [ ] pick none or one from a list expression -> term ( + - ) term term -> factor ( * / ) factor factor -> ( expression ) number // these parenthesis are part of the grammar not the EBNF number -> 0 1 2 3 4 5 6 7 8 9 PARSER_BEGIN(cal) import java.io.*; import java.util.*; public class cal public static void main(string[] args throws ParseException, FileNotFoundException if ( args.length < 1 ) System.out.println("Pass a filename as an argument."); System.exit(1); cal parser = new cal( new FileInputStream(args[0]) ); parser.calcit(); System.out.println("Parse completed."); PARSER_END(cal) SKIP: /* Whitespace */ "\t" "\n" "\r" " " TOKEN: <LPAREN: "("> <RPAREN: ")"> <ADD_OP: "+" "-"> <MULOP: "*" "/"> <NUM: (["0"-"9"])+> 6

void calcit() : expression() System.out.println("expressioin");< EOF> void expression() : Token a = null; trm() System.out.println("trm"); (a = <ADD_OP> System.out.println(a); trm() System.out.println("trm"); )* void trm(): Token f = null; fct() System.out.println("fct"); (f = <MULOP> System.out.println(f); fct() System.out.println("fct"); )* void fct(): <LPAREN> expression() <RPAREN> System.out.println("(expression)"); numb() System.out.println("numb"); void numb() : Token n; n = <NUM> System.out.println(n); 7

15. [5 Points] Using the grammar in problem 14 (shown again below), draw a parse tree for the following expression: 5 * ( 11 + 3 ) / 4 + 100 a series of zero or more ( ) must pick one from a list [ ] pick none or one from a list expression -> term ( + - ) term term -> factor ( * / ) factor factor -> ( expression ) number // these parenthesis are part of the grammar not the EBNF number -> 0 1 2 3 4 5 6 7 8 9 8

16. [5 Points of EXTRA CREDIT] What will cal.java produce if given the following expression to parse: 6 * ( 11 7 ) / 3 + 100 6 numb fct * 11 numb fct trm - 7 numb fct trm (expression) fct / 3 numb fct trm + 100 numb fct trm expression Parse completed. 9

'<http://www.example.org/people.owl#person>', 'rdf:type', 'rdfs:class')); '<http://www.example.org/people.owl#employee>', 'rdf:type', 'rdfs:class')); '<http://www.example.org/people.owl#employee>', 'rdfs:subclassof', '<http://www.example.org/people.owl#person>')); '<http://www.example.org/people.owl#gov_emp>', 'rdf:type', 'rdfs:class')); '<http://www.example.org/people.owl#gov_emp>', 'rdfs:subclassof', '<http://www.example.org/people.owl#employee>')); 'http://www.example.org/people.owl#boss', 'owl:inverseof', 'http://www.example.org/people.owl#emp')); '<http://www.example.org/people.owl#i10>', 'rdf:type', '<http://www.example.org/people.owl#gov_emp>')); '<http://www.example.org/people.owl#i10>', '<http://www.example.org/people.owl#id>', '"111222"^^xsd:integer')); '<http://www.example.org/people.owl#i20>', 'rdf:type', '<http://www.example.org/people.owl#gov_emp>')); 'http://www.example.org/people.owl#i10', 'http://www.example.org/people.owl#boss', 'http://www.example.org/people.owl#i20')); Commit; 10

17. [5 Points] In the Python.g file of ReL, what does the following construct do? -> ^(MAKECONNECT<Connection>[$sql_stmt.start, actions.castexprs($sql_stmt::exprs), $expr::ctype, $connection::url, $connection::uname, $connection::pword, $connection::ctype])) This construct creates a new instance of the Connection class by calling a Connection constructor with the arguments inside the square braces. It then adds this instance of the Connection class to the program s Abstract Syntax Tree. 18. [10 Points] What additional triples(s) will be entailed by the RDF triples on the previous page. (You do not need to reproduce the detailed syntax, just give the entailed triples in a simple form with no URIs.) [3 Points] Gov_Emp subclass Person [1 Point] i10 type Employee [1 Point] i10 type Person [3 Points] i20 emp i10 [1 Point] i20 type Employee [1 Point] i20 type Person 19. [5 Points] Give the proof by contradiction steps to show that v is True in the following Prolog database. Justify each step. You may find the patterns on the right useful. a) p. b) q. c) f. d) z :- p, q. e) v :- z, f. f) v. g) (z, f) ( From 5 and 6 and Pattern 1 ) h) z ( From 3 and 7 and Pattern 2 ) i) (p, q) ( From 4 and 8 and Pattern 1 ) j) q ( From 1 and 9 and Pattern 2 ) k) Contradiction (From 2 and 10 and Pattern 3). Therefore v is true. Pattern 1: (Modus Tollens) Q :- (P1, P2). -Q è -(P1, P2) Pattern 2: (Affirming a Conjunct) P1. -(P1, P2) è -P2 Pattern 3: P2. -P2 è Contradiction 11

20. [10 Points] Give a description of the function performed in ReL for each of the numbered boxes below: 2. The Visitor visits each node on the AST. 1. 2. Python.g AST 1. The Python grammar was changed to include SIM, SQL, etc. statements. Visitor Runtime Stack Interpreter 3. CodeCompiler.java 3. The Tuple entry in CodeCompiler was modified to deal with putting SIM, SQL, etc. information and expressions on the runtime stack and to assure that a new instance of PyTuple is created after the expressions are evaluated. jsimparser PyTuple.java ------------ parsesim() 4. jsqlparser 4. jsqlparser parses SQL statements from PyTuple and produces an AST that can be visited. SIMHelper.java SPARQLDoer.java 5. SQLVisitor.java 5. SQLVisitor visits the jsqlparser AST and produces appropriate SPARQL statements for the SQL statements which are sent to SPARQLDoer. Oracle DBMS 12

Blank 13