S ---> A | '(' T
T ---> ')' | S T
A ---> 'a' | 'b' | 'c'
Give the parse tree for the sentence: ( ( a ) ( b c ) a )
S (Nodes "A" are not shown)
|
+----------+--------------+
| |
"(" T
|
+---------------+------+
| |
S T
| |
+------+----+ +-----+----------+
| | | |
"(" T S T
| | |
+----+----+ +--+---+ +---+----+
| | | | | |
S T "(" T S T
| | | | |
"a" ")" +---+----+ "a" ")"
| |
S T
| |
"b" +--+---+
| |
S T
| |
"c" ")"
<stmt> ---> if ( <log-expr> ) <stmt>
<stmt> ---> if ( <log-expr> ) <stmt> else <stmt>
S ---> b M b ("S" is the start symbol)
M ---> ( L
M ---> a
L ---> M a )
Here is a recursive descent parser for this grammar, with two sections
of code missing. It also has an initial "P" symbol, and a
terminal "$". Fill in the two code sections, including the
calls to error in case of an error.
(You don't need to know anything about Java to do this problem.
If you are not a Java person, just write code as if this were
C. The code should be the same in this case.)
/* GrammarTest.java: simple parser
* grammar:
* P ---> S "$"
* S ---> "b" M "b"
* M ---> "(" L
* M ---> "a"
* L ---> M "a" ")"
*/
import java.io.*;
class GrammarTest {
private int level = 0;
private char next;
private void P() {
scan();
S();
if (next != '$') error(2);
else
System.out.println("Success");
}
private void S() {
if (next == 'b') scan();
else error(0);
M();
if (next == 'b') scan();
else error(1);
}
private void M() {
// supply code here
}
private void L() {
// supply code here
}
|
private void scan() {
do {
try {
next = (char)System.in.read();
} catch (IOException e) {
System.out.println("Excp");
}
} while (Character.isWhitespace(next));
}
private void error(int n) {
System.out.println("*** ERROR: " + n);
System.exit(1);
}
public static void main(String[] args) {
GrammarTest grammarTest =
new GrammarTest();
grammarTest.P();
}
}
|
private void M() {
if (next == '(') {
scan();
L();
}
else if (next == 'a') scan();
else error(3);
}
private void L() {
M();
if (next == 'a') scan();
else error(4);
if (next == ')') scan();
else error(5);
}
> 47
47
> (+ (* 2 3) (/ 8 4))
8
> (car '((a b) c (d)))
(A B)
> (cdr '((a b) c (d)))
(C (D))
> (cons '(a) '(b c))
((A) B C)
> (append '(a) '(b c))
(A B C)
> (list '(a) '(b c))
((A) (B C))
> (cond ((= 3 4) 47)
(t 54))
54
> ()
NIL
>
> (append (a b c) (d e f))
>>Error: The function A is undefined
(Lisp tries to evaluate the arguments first, starting
with the list (a b c). This is evaluated by regarding a
as a function, which is usually would not be.)
> (defun power (b e)
(cond ((= e 0) 1)
(t (* b (power b (1- e)))) ))
POWER
> (power 2 5)
32
> (power 2 20)
1048576
> (power 2 100)
1267650600228229401496703205376
> (power 3 200)
2656139888758747693387813220357796268292334526533
94495974574961739092490901302182994384699044001
In a similar way, give the variable r3 the value of r3 (not just 4096, but based on whatever r is). Then give the variable pi the value 3.14159265. Finally, let vol take on the value (4/3)*pi*r3.
/r 8 def /r3 r r mul r mul def /pi 3.14158265 def /vol 4 3 div pi mul r3 mul def %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Now output the value of vol % Tricky stuff -- see Bluebook, pages 71-73 /volstr 10 string def % string to hold value of vol /Times-Roman findfont 40 scalefont setfont 100 100 moveto (vol = ) show vol volstr cvs show % convert vol to string and print showpage
Here is the result of interpreting this Postscript: vol.
newpath
100 200 moveto
0 150 rlineto
200 0 rlineto
closepath
/figure {
% five lines from the previous part
} def
Now give code to fill in the figure with gray shading
(your choice of how gray) and then code to draw a line
along the path.
/figure {
newpath
100 200 moveto
0 150 rlineto
200 0 rlineto
closepath
} def
3 setlinewidth
figure 0.5 setgray fill
0 setgray
figure stroke
showpage
Here is the result of interpreting this Postscript:
figure.
/Times-Roman findfont 80 scalefont setfont
0 0 moveto
(A) show
/printit {
/Times-Roman findfont 80 scalefont setfont
0 0 moveto
(A) show
} def
printit
612 2 div 792 2 div translate
180 rotate
printit
showpage
Here is the result of interpreting this Postscript:
Rotated A.
/iter 20 def
/xiter 580 iter div def
/yiter 760 iter div def
/printit {
/Times-Roman findfont 80 scalefont setfont
0 0 moveto
(A) show
} def
0 1 iter 1 sub {
pop
printit
xiter yiter translate
} for
showpage
Here is the result of interpreting this Postscript:
Diagonal A's.
male(neal). male(wayne). male(nate). male(ian). female(bethany). female(debbie). parent(neal, nate). parent(neal, ian). parent(neal, bethany). parent(debbie, nate). parent(debbie, ian). parent(debbie, bethany). married(debbie,neal).
father(X, Y) :- male(X), parent(X, Y).It says that X is the father of Y in case X is male and X is the parent of Y.
What new facts does this rule provide?
medusa% pl ?- consult(exam1). exam1 compiled, 0.00 sec, 2,816 bytes. Yes ?- father(X,Y). X = neal Y = nate ; X = neal Y = ian ; X = neal Y = bethany ; X = albertus Y = ralph ; X = ralph Y = neal ; No ?- halt.
male(albertus). male(ralph). parent(albertus, ralph). parent(ralph, neal). ancestor(X,Y) :- parent(X,Y). ancestor(X, Y) :- parent(X, Z), ancestor(Z, Y).Give a new fact about albertus and ian that prolog can deduce. Where does it comes from?
?- ancestor(albertus, ian). Yes
child(Y, X) :- parent(X, Y). daughter(Y, X) :- female(Y), child(Y, X).but one could get by with daughter(Y, X) :- female(Y), parent(X, Y).
Generic constructs in C, C++, and Java: