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