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