Consider the following Java parser, with runs to the right.
Parser | Runs |
---|---|
/* Gramm.java: simple parser * grammar: P ---> S '$' * S ---> A | '(' Op S S ')' * Op ---> '+' | '-' | '*' | '/' * A ---> digit */ import java.io.*; class Lisp0 { private char next; private void P() { S(); scan(); if (next != '$') error(1); else System.out.println("Success"); } private void S() { scan(); if (Character.isDigit(next)) { // nothing! } else if (next == '(') { scan(); // next == Op S(); S(); scan(); /* next == ')' */ if (next != ')') error(2); } else error(3); } 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); } public static void main(String[] args) { Lisp0 lisp = new Lisp0(); lisp.P(); } } | % javac Lisp0.java % java Lisp0 6 $ Success % java Lisp0 (+ 6 7) $ Success % java Lisp0 (* (+ 2 3) (- 5 2)) $ Success % java Lisp0 (* (+ 2 (/ 6 2))(- (* 3 2) 4))$ Success % java Lisp0 (+ 6 7 $ *** ERROR: 3 ------------------------------- % javac Lisp.java % java Lisp 6 $ 6 % java Lisp (+ 6 7) $ 13 % java Lisp (* (+ 2 3) (- 5 2)) $ 15 % java Lisp0 (* (+ 2 (/ 6 2))(- (* 3 2) 4))$ 10 |
The program Lisp0.java is a parser for the grammar in comments at the top of the program code. Sample runs are given at the upper right.
Answer the following questions:
What is the advantange of this kind of scan (which is actually not being utilized here)?
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 W if (Character.isLowerCase(next)) { // POSITION X scan(); } if (next == '=') scan(); // POSITION Y E(); if (next == ';') scan(); // POSITION Z }
Suppose your program is translating the following statement:
a = b + 3*c;
The following Postscript code draws an elipse.
%!PS-Adobe-2.0 /inch {72 mul} def /elipse { gsave 1 2 scale newpath 0 0 1.5 inch 0 360 arc stroke grestore } def elipse showpage
Here x y r ang1 ang2 arc creates an arc of a circle, centered at x and y, with radius r, counterclockwise from angle ang1 to ang2. Remember that gsave and grestore save and restore the graphics state.
17 Cerica S. Johnson @00022222 18 Alejandro Juarez @00111111 19 Erhan J. Kartaltepe @00777777We want to rewrite these in a different way. This assumes that the last name is always followed by one or more blanks and then an '@' character. There may or may not be a middle initial. You should drop the initial number, write the student number without leading 0's, and write the last name first, followed by a comma, followed by a blank and the rest of the name, as shown below:
22222 Johnson, Cerica S. 111111 Juarez, Alejandro 777777 Kartaltepe, Erhan J.
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.
courses(cs1713, tr-1400, wagner, sb3-02-16, fulltime). courses(cs1723, tr-1230, womack, sb3-01-03c, parttime). courses(cs2213, mw-1600, wagner, sb3-02-16, fulltime).
where this means that the course CS 1713 is taught at 2PM, Tues/Thurs, by Neal Wagner, whose office is SB 3.02.16, and Wagner is a full-time employee.