P ---> E '#'
E ---> E '+' T | T
T ---> T '*' F | F
F ---> '(' E ')' | digit
Give the parse tree for the sentence:
P ---> E '#'
E ---> T { '+' T }
T ---> F { '*' F }
F ---> '(' E ')' | digit
Below is a parser for this grammar, written in C. The main function plays the role of P. (The parser in Java could look almost the same.)
#include |
void F(void)
{
if (isdigit(next)) {
scan();
}
else if (next == '(') {
scan();
E();
if (next == ')') scan();
else error(2);
}
else {
error(3);
}
}
void scan(void)
{
while (isspace(next = getchar()))
;
}
void error(int n)
{
printf("\n*** ERROR: %i\n", n);
exit(1);
}
|
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: 666
(car (car '(a b c)))
(car (cdr (a b c)))
> (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))))
[Hint: Use the predicate atom to check if an S-expression is an atom. Think recursively -- handle the car and then recursively handle the cdr.]
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 |
Generic constructs in C, C++, and Java:
?- spring_2003_courses(X, Y, wagner).
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).What new facts does the additional rule provide? What is the term from relational database theory for this type of operation?
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').