Consider the following grammar:
E ---> dig | '(' P E E ')' P ---> '+' | '-' | '*' | '/' dig ---> '0'|'1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9'
Here is a Java parser for this grammar:
public class Evaluate { private char tok; // eval: evaluate (after making additions below) public void eval() { E(); System.out.println("Successful Parse!"); } private void E() { tok = gettoken(); if (Character.isDigit(tok)) ; // do nothing for now else if (tok == '(') { tok = gettoken(); if (tok == '+' || tok == '-' || tok == '*' || tok == '/' || tok == '^') { E(); E(); tok = gettoken(); if (tok == ')') ; // do nothing for now else { err(0);} } else { err(1);} } else { err(2);} } // err: error message private void err(int i) { System.out.println("Error number: " + i); System.exit(1); } // gettoken: fetch next token (here just a char) private char gettoken() { char next = ' '; do { try { next = (char)System.in.read(); } catch (Exception e){ err(2); } } while ( next == ' ' || next == '\n'); return next; } public static void main(String[] args) { Evaluate e = new Evaluate(); e.eval(); } }You are to add code to the listing above so that the program will return the correct double value as it parses input. Here is an interactive session showing legal input and what should be returned by your program.
% java Evaluate 6 6.0 % java Evaluate (+ (* 3 3) (* 4 4)) 25.0 % java Evaluate (* (+ (* 5 4) (/ 4 2) ) 3) 66.0 % java Evaluate (+ 3 (/ 1 (+ 7 (/ 1 (+ (* 4 4)))))) 3.1415929203539825Answer the following questions:
(* (+ (* 5 4) (/ 4 2) ) 3)
private double eval2(char op, double arg1, double arg2) { switch (op) { case '+': return arg1 + arg2; case '-': return arg1 - arg2; case '*': return arg1 * arg2; case '/': return arg1 / arg2; case '^': return Math.pow(arg1, arg2); default: { err(3); return 0; } } }
This question concerns the series of recitations used to translate programs from the "Tiny" language to MIPS assembler code.
The portion of the grammar for Tiny that handles assignment statements is:
A ---> lower-case '=' E ';'
Here is my version of the portion of the parser needed to handle this assignment statement. I removed three occurrences of else error(9) in order to simplify the code. I also marked 4 "POSITIONS" in the code, locations to refer to below. This is just the bare parser, and you should realize that a number of additional features and statements must be added to this function to get the final compiler.
private void A() { // here have already scanned for the first token of A // POSITION A if (Character.isLowerCase(next)) { // POSITION B scan(); } if (next == '=') scan(); // POSITION C E(); if (next == ';') scan(); // POSITION D }
Suppose your program is translating the following statement:
a = b + 3*c;
((a b) c (d))
/Times-Roman findfont 100 scalefont setfont 0 0 moveto (UTSA) show showpage
Rewrite this code so that it prints the UTSA 8 inches up from the bottom, and exactly right-justified 50 points from the right edge. Thus the right side of the "A" should be 50 points from the right edge of the paper. [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.]
Part a. | Part b. | Part c. | Part d. |
---|---|---|---|
/box { newpath 0 0 moveto 0 72 rlineto 72 0 rlineto 0 -72 rlineto closepath } def 45 rotate 200 200 translate box stroke showpage | /box { newpath 0 0 moveto 0 72 rlineto 72 0 rlineto 0 -72 rlineto closepath } def 200 200 translate 45 rotate box stroke showpage | /box { newpath 0 0 moveto 0 72 rlineto 72 0 rlineto 0 -72 rlineto closepath } def 200 200 translate 45 rotate 0.25 0.25 scale box stroke showpage | /box { newpath 0 0 moveto 0 72 rlineto 72 0 rlineto 0 -72 rlineto closepath } def 0.25 0.25 scale 200 200 translate 45 rotate box stroke showpage |
/line { newpath 0 0 moveto 1000 1000 lineto stroke } def /lines { 1 1 300{ 5 0 translate line } for } def % PART A: 1 setlinewidth gsave -900 0 translate lines grestore % PART B: /Helvetica-Bold findfont 100 scalefont setfont 1 setgray % white 50 500 moveto (SUBVERT) show 0 setgray % black % PART C: newpath 50 500 moveto (SUBVERT) false charpath clip 1.25 setlinewidth -901 0 translate lines showpage |
Emma by Austen, Jane Released: Aug 1994 Mansfield Park by Austen, Jane Released: Jun 1994 Doctor Marigold by Dickens, Charles Released: Aug 1998 George Silverman's Explanation by Dickens, Charles Released: Feb 1997 Sir Nigel by Doyle, Arthur Conan Released: Aug 2005 Tales of Terror and Mystery by Doyle, Arthur Conan Released: Aug 2005
We want to rewrite these in the following form. This assumes that the book title is always ended by "by", that the author's last name is always followed by "," and that the first name(s) is(are) always followed by "Released:".
Jane Austen, "Emma" (Aug 1994) Jane Austen, "Mansfield Park" (Jun 1994) Charles Dickens, "Doctor Marigold" (Aug 1998) Charles Dickens, "George Silverman's Explanation" (Feb 1997) Arthur Conan Doyle, "Sir Nigel" (Aug 2005) Arthur Conan Doyle, "Tales of Terror and Mystery" (Aug 2005)Write a Ruby program using Ruby code and regular expressions to do this translation. If you have forgotten various items, just fake it as best you can, but you are not supposed to be using Perl for this problem. If you didn't do Recitation 13 the way I suggested but used some method of your own, then you should mention this.