P ---> E '#'
E ---> E '+' T | T
T ---> T '*' F | F
F ---> '(' E ')' | digit
Give the parse tree for the sentence:
P
|
+---------------+-------------------------+
| |
E |
| |
T |
| |
+------+--------------------+ |
| | | |
| | F |
| | | |
| | +-------------+-------------+ |
| | | | | |
| | | E | |
| | | | | |
| | | + -----+------+ | |
| | | | | | | |
| | | E | | | |
| | | | | | | |
T | | T | T | |
| | | | | | | |
F | | F | F | |
| | | | | | | |
7 * ( 3 + 2 ) #
P ---> E '#'
E ---> T { '+' T }
T ---> F { '*' F }
F ---> '(' E ')' | digit
pandora% evala (4 + 5)# Value: 9 pandora% evala 1+3+5+7+9# Value: 25 pandora% evala 2*2 + 3*3 + 5*5 + 7*7 + (2+9)*(3+8) + (6+7)*(5+8) + (5+6+6)*(4+5+8)# Value: 666Here is an answer, with changes and additions in bold:
#include |
int F(void) {
int res;
if (isdigit(next)) {
res = next - '0';
scan();
}
else if (next == '(') {
scan();
res = E();
if (next == ')') scan();
else error(2);
}
else {
error(3);
}
return res;
}
void scan(void) {
while (isspace(next = getchar()))
;
}
void error(int n) {
printf("\n*** ERROR: %i\n", n);
exit(1);
}
|
> 17
17
> (+ (* 2 3) (* 8 4))
38
> (car '((a b c) d (e)))
(A B C)
> (cdr '((a b c) d (e)))
(D (E))
> (car (cdr '(a b c)))
B
> (cons '(a) '(b c))
((A) B C)
> (append '(a) '(b c))
(A B C)
> (list '(a) '(b c))
((A) (B C))
> (cond ((= 4 (* 2 2)) 47)
(t 54))
47
> ()
NIL
> (car (cdr '(a b c)) ;;; missing final )
)
B
> (car (car '(a b c))) ;;; can't car of an atom
>>Error: The value of X, A, should be a LIST
> (car (cdr (a b c))) ;;; Lisp will try to evaluate (a b c)
>>Error: The function A is undefined
> (defun dropatoms (x)
(cond ((null x) nil)
((atom (car x)) (dropatoms (cdr x)))
(t (cons (car x) (dropatoms (cdr x)))) ))
DROPATOMS
> (dropatoms '((a) b c))
((A))
> (dropatoms '(a b c))
NIL
> (dropatoms '(a (b) c))
((B))
> (dropatoms '(a b (c)))
((C))
> (dropatoms '(4 a c (a b) d e (g h)))
((A B) (G H))
> (dropatoms '(6 (a (b) c) (((b))) a b))
((A (B) C) (((B))))
Consider the following Postscript programs:
|
/Times-Roman findfont 144 scalefont setfont 0 0 moveto (UTSA) show showpage |
/Times-Roman findfont 144 scalefont setfont /utsa { 0 0 moveto (UTSA) show } def % answer to part (b) utsa showpage |
5 inch 3 inch translate 90 rotate
Result looks like:
.ps, .pdf,
with possible code as follows:
/Times-Bold findfont 144 scalefont setfont
/utsa { % stack: x y
moveto
(UTSA) show
} def
612 2 div
(UTSA) stringwidth pop 2 div sub % x coordinate
350 % y coordinate
utsa
showpage
/inch { 72 mul } def
/Times-Bold findfont 2 inch scalefont setfont
/Logo { (NEAL) } def
/makegray {
newpath 0 0 moveto
8.5 inch 0 rlineto 0 11 inch rlineto 8.5 inch neg 0 rlineto
closepath fill
} def
/start { % starting coords to center Logo
8.5 inch 2 div
Logo stringwidth pop 2 div sub % x coordinate
11 inch 2 div 50 sub % y coordinate
} def
0.5 setgray
makegray
1 setgray start moveto Logo true charpath fill
0 setgray start moveto Logo true charpath stroke
showpage
Generic constructs in C, C++, and Java:
medusa% pl ?- consult(courses). courses compiled, 0.01 sec, 8,676 bytes. Yes ?- spring_2003_courses(X, Y, wagner). X = cs3723 Y = 1200-mwf ; X = cs4363 Y = 1000-mwf ; No
Suppose the following additional rule is included in the source:
course_and_office(Course, Fac, Office) :- spring_2003_courses(Course, _ , Fac), faculty(Fac, _, Office).
?- course_and_office(X, Y, Z). X = cs3233 Y = tian Z = 'SB 3.02.00 ' ; X = cs3343 Y = kwek Z = 'SB 3.02.01E' ; X = cs3723 Y = wagner Z = 'SB 3.02.16 ' ; X = cs3733 Y = srobbins Z = 'SB 3.02.01A' (and so forth, with many more examples)What new facts does the additional rule provide? Facts about course, faculty member, and office as shown above. What is the term from relational database theory for this type of operation? This is the relational join.
spring_2003_courses(cs3233, 1230-tr, tian).Give at least three reasons why one would not want to include the office number of the faculty member as part of this fact, producing facts that would look like:
spring_2003_courses(cs3233, 1230-tr, tian, 'SB 3.02.00').Here are the problems as described in a database book by J. Ullman:
tr_at_1230(Fac, Office) :- spring_2003_courses(_, 1230-tr, Fac), faculty(Fac, _, Office).Assuming this new rule is incorporated into the set of rules, then the following would result from a query:
?- tr_at_1230(X, Y). X = tian Y = 'SB 3.02.00 ' ; X = srobbins Y = 'SB 3.02.01A' ; No