S ---> A | '(' T
T ---> ')' | S T
A ---> 'a' | 'b' | 'c'
Give the parse tree for the sentence:
<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();
}
}
|
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.
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.
/Times-Roman findfont 80 scalefont setfont
0 0 moveto
(A) show
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).What new facts does this rule provide?
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?
Generic constructs in C, C++, and Java: