Directions: Use your own paper for this exam.
E ---> E '+' T | T
T ---> T '*' F | F
F ---> '(' E ')' | 'a' | 'b' | 'c' |
E ---> E + Tis used in a reduction? What is true about the stack before the reduction? What is true about the stack after the reduction?
W ---> '{' E '?' { S } '}'
What do the curly brackets in quotes mean?
What do the curly brackets surrounding the S mean (those not in quotes)?
There is a crucial memory address that is needed to complete the code. Where does this address come from?
class Parens {
GetChar getChar = new GetChar();
char next;
void P() {
scan();
Q();
if (next == '#') System.out.println("Success!");
else error(1);
}
void Q() {
if (next == '(') {
scan();
}
else error(2);
R();
}
void R() {
if (next == ')') {
scan();
}
else {
Q();
R();
}
}
void scan() {
while (Character.isWhitespace(next = getChar.getNextChar()))
;
}
void error(int n) {
System.out.println("*** ERROR: " + n);
System.exit(1);
}
public static void main(String[] args) {
Parens parens = new Parens();
parens.P();
}
}
Here are three separate runs (boldface = input):
(()())# Success! ( () (( )) (() () ) () ) # Success! ( () (( ))# *** ERROR: 2
(()())# (0 (1 1)(1 1) 0) ( () (( )) (() () ) () ) # (0 (1 1)(1 (2 2) 1)(1 (2 2)(2 2) 1)(1 1) 0) ((((((((((()))))))))))# (0 (1 (2 (3 (4 (5 (6 (7 (8 (9 (10 10) 9) 8) 7) 6) 5) 4) 3) 2) 1) 0) ( () (( )) (()) () (((() (()) ) () ) ) )# (0 (1 1)(1 (2 2) 1)(1 (2 2) 1)(1 1)(1 (2 (3 (4 4)(4 (5 5) 4) 3)(3 3) 2) 1) 0)