Consider the following grammar that was part of one of the recitations.
S ---> b M b ("S" is the start symbol) M ---> ( L M ---> a L ---> M a )
The language describes by the grammar below gives fully parenthesized postfix expressions:
P ----> E E ----> dig | '(' E E Op ')' Op ----> '+' | '-' | '*'| '/' | '^' dig ----> '0'|'1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9'Here is a Java parser for the language:
// Parser.java: parser for parenthesized postfix language public class Parser { private char next; // = ' '; // P: recognize a program public void P() { E(); } // E: recognize a statement private void E() { scan(); if (Character.isDigit(next)) { // next is a single digit here } else if (next == '(') { E(); E(); scan(); scan(); // for the ')' if (next != ')') error(1); } else error(3); } // scan: put the next non whitespace char into next private void scan() { do { try { next = (char)System.in.read(); } catch (Exception e){ error(2); } } while ( next == ' ' || next == '\n'); } // error: error encountered during the parse private void error(int n) { System.out.println("ERROR " + n + ", Token: " + next ); System.exit(1); } public static void main(String[] args) { Parser parser = new Parser(); parser.P(); } }You are to add code to the listing above so that the program will return the correct integer values when the operations have been carried out. Here is an interactive session showing how the language works and showing what your code should do. Each input is a single expression, either just a constant or a parenthesized expression starting with two similar expressions and ending with an operator.
% java Parser 6 6 % java Parser ( (2 3 +) (3 4 *) -) -7 % java Parser ((2 4 ^) ( (8 2 /) (5 3 -) *) +) 24
What does postfix mean?
((2 4 ^) ( (8 2 /) (5 3 -) *) +)carefully by hand and show that 24 is the correct answer.
private int eval(int r1, int r2, char op) { switch (op) { case '+': return r1 + r2; case '-': return r1 - r2; case '*': return r1 * r2; case '/': return r1 / r2; case '^': int res = r1; while (r2 != 1) { res = res*r1; r2--; } return res; default: error(4); return 0; } }
> (switch '(a b)) (B A) > (switch '((a) (b c))) ((B C) (A))
> (remove 'a '(a b c a b)) (B C B) > (remove 'a '(a a a)) NIL > (remove 'a '(a)) NIL > (remove 'a '(b c d)) (B C D) > (remove '(a b) '(a (a b) c (a b) d (a b)) (A C D)
Consider the following two Postscript programs:
/Times-Roman findfont 20 scalefont setfont /inch { 72 mul} def /square { newpath 0 0 moveto 1 inch 0 rlineto 0 1 inch rlineto -1 inch 0 rlineto closepath fill 0.1 inch 1.1 inch moveto (A Box) show } def square 4 inch 2 inch translate 60 rotate square 4 inch 2 inch translate 60 rotate square showpage |
/inch { 72 mul} def /square { % stack: side => --- /side exch def newpath 0 0 moveto side 0 rlineto 0 side rlineto side neg 0 rlineto closepath fill } def 1 inch square 4 inch 2 inch translate 60 rotate 1.5 inch square 4 inch 2 inch translate 60 rotate 2 inch square showpage |
0 1 20 { pop square 1.5 inch 0.5 inch translate 45 rotate 0.5 sqrt dup scale % scale factor: 0.707 } for
Say approximately what will be printed with this replacement.
/Times-Roman findfont 100 scalefont setfont 0 0 moveto (UTSA) show showpage
Rewrite this code so that is prints the UTSA 5 inches up from the bottom, and exactly centered horizontally on the page. [Hints: The page is 8.5 inches wide. There are 72 points to an inch. The Postscript code (UTSA) stringwidth pop will give the horizontal extent of the string.]
The use of Interfaces in Java:
Consider the Prolog facts and rules on the separate sheet, an example similar to one from class about CS courses. [Recall that any name starting with a capital letter is a variable.]
?- spr03(X, Y, wagner, Z).
?- faculty_teaches_in_room(_, Y, Z, '3.03.04BB').