Assignment 3 with Solutions | Programming Languages | CS 401, Assignments of Programming Languages

Material Type: Assignment; Class: Programming Languages; Subject: Computer Science; University: University of Alabama - Birmingham; Term: Spring 2008;

Typology: Assignments

Pre 2010

Uploaded on 04/12/2010

koofers-user-sv4
koofers-user-sv4 🇺🇸

9 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
ASSIGNMENT #3
Due Wednesday, March 5, 20081
1. Design and implement a symbol table “class” in Lisp. The symbol table elements will be of
the form (name category type value) and the symbol table itself will be a list of such values.
The following functions are required:
(a) add (symboltable name category type value) - returns a symbol table with the
name, category, type, and value fields added; if name is already in the symbol table,
returns nil
(b) entry (symboltable name) - returns the category, type and value associated with
name; if name is not in the symbol table, returns nil
(c) category (symboltable name) - returns the category associated with name; if name is
not in the symbol table, returns nil
(d) type (symboltable name) - returns the type associated with name; if name is not in
the symbol table, returns nil
(e) value (symboltable name) - returns the value associated with name; if name is not in
the symbol table, returns nil
Test your class using the following sequence of calls.
(setq test2st (add nil ’h ’var ’int 0))
(setq areast (add nil ’x ’var ’int 0))
(setq areast (add areast ’y ’var ’int 1))
(setq areast (add areast ’z ’var ’int 2))
(setq test2st (add test2st ’area ’method ’int (list ’(x y) areast
’(: (= z (* 2 (+ (+ (* x y) (* x h)) (* y h)))) (return z)))))
(setq mainst (add nil ’a ’var ’int 0))
(setq mainst (add mainst ’b ’var ’int 1))
(setq mainst (add mainst ’s ’var ’int 2))
(setq test2st (add test2st ’Main ’method ’void (list ’() mainst
’(: (: (: (: (= a 3) (= b 4)) (= h 5)) (= s (apply area (@ a b)))) (print s)))))
(setq st (add nil ’Test2 ’class ’Test2 test2st))
(entry test2st ’h)
(entry test2st ’area)
(entry mainst ’b)
(entry st ’Main)
(category areast ’y)
(category mainst ’y)
(type st ’Test2)
(type st ’class)
(value mainst ’s)
(value areast ’area)
1This assignment may be turned in by Monday, March 10, 12:00 P.M., without penalty but that is the absolute
deadline for turning it in.
pf2

Partial preview of the text

Download Assignment 3 with Solutions | Programming Languages | CS 401 and more Assignments Programming Languages in PDF only on Docsity!

ASSIGNMENT

Due Wednesday, March 5, 2008^1

  1. Design and implement a symbol table “class” in Lisp. The symbol table elements will be of the form (name category type value) and the symbol table itself will be a list of such values. The following functions are required:

(a) add (symboltable name category type value) - returns a symbol table with the name, category, type, and value fields added; if name is already in the symbol table, returns nil (b) entry (symboltable name) - returns the category, type and value associated with name; if name is not in the symbol table, returns nil (c) category (symboltable name) - returns the category associated with name; if name is not in the symbol table, returns nil (d) type (symboltable name) - returns the type associated with name; if name is not in the symbol table, returns nil (e) value (symboltable name) - returns the value associated with name; if name is not in the symbol table, returns nil

Test your class using the following sequence of calls.

(setq test2st (add nil ’h ’var ’int 0)) (setq areast (add nil ’x ’var ’int 0)) (setq areast (add areast ’y ’var ’int 1)) (setq areast (add areast ’z ’var ’int 2)) (setq test2st (add test2st ’area ’method ’int (list ’(x y) areast ’(: (= z (* 2 (+ (+ (* x y) (* x h)) (* y h)))) (return z))))) (setq mainst (add nil ’a ’var ’int 0)) (setq mainst (add mainst ’b ’var ’int 1)) (setq mainst (add mainst ’s ’var ’int 2)) (setq test2st (add test2st ’Main ’method ’void (list ’() mainst ’(: (: (: (: (= a 3) (= b 4)) (= h 5)) (= s (apply area (@ a b)))) (print s))))) (setq st (add nil ’Test2 ’class ’Test2 test2st)) (entry test2st ’h) (entry test2st ’area) (entry mainst ’b) (entry st ’Main) (category areast ’y) (category mainst ’y) (type st ’Test2) (type st ’class) (value mainst ’s) (value areast ’area)

(^1) This assignment may be turned in by Monday, March 10, 12:00 P.M., without penalty but that is the absolute deadline for turning it in.

  1. Consider the Core program below:

input n; i := 1; while (i <= n) loop i := i + 1; end loop; output i;

Assuming that the input file contains the integers 3 and 4 (i.e., the list <3, 4>), use the denotational semantics of Core to trace the interpretation of the program.