The following notes briefly describe how you can use my interpreter classes to implement your spreadsheet interface. You should copy the /home/cs365/www-home/project5/formula directory to your formula directory and then add your interface classes.


The Parser Class

You can parse a new formula and get it assigned to the appropriate cells using the following call:

parser expParser = new parser(new Yylex(new StringReader(formula)));
expParser.parse();
formula should be a String variable that points to the formula you want parsed.


The ParserError Class

If the formula contains either a syntactic or semantic error, the parser will throw a ParserError exception. ParserError extends the RuntimeException class. It contains an explanation of the error in the public variable errorMsg. You can display this string in an error dialog box so that the user knows that an error has occurred. Your spreadsheet interface will need to catch this exception and display an error dialog box.


The Cell Class

My parser will simply assign the new formula to the appropriate set of cells and it will create a list of cells that need to be re-evaluated but it will not actually bring the cells up-to-date. In order to bring the cells up-to-date you will need to make the following call:

LinkedList<Cell> updatedCells = Cell.updateCells();
updateCells is a static method in my Cell class which re-evaluates all out-of-date cells and returns a linked list of cells that have changed. Your interface code can iterate through this list of cells and update the appropriate cell fields in your interface. Before printing the value of a cell your code should check to see whether the cell's value is undefined. If so you should print "Undef" in the cell's field rather than a floating point number. The following instance variables and methods in a Cell object may prove helpful to you:

These instance variables and methods should allow you to locate the appropriate field in your spreadsheet that should be updated and to display the appropriate value in that field.


Example Code

Here is the example code that I used to test my parser code to make sure it works. Basically I updated my formula interpreter to use the new methods that I defined for the Cell class and the new ParserError class to handle invalid formulas.

while (true) { try { System.out.print(">>> "); // get a formula from the user, parse it, and assign it // to the appropriate cells expParser = new parser(new Yylex(new StringReader(input.nextLine()))); expParser.parse(); // update the cells LinkedList<Cell> updatedCells = Cell.updateCells(); // print out the updated cell values. If a cell is // undefined then print the explanation for why it is // undefined and print "undef" for its value. Otherwise // print the cell's floating point value. for (Cell c : updatedCells) { if (c.isUndef()) { System.out.println(c.getUndefExplanation()); System.out.printf("%s[%d] = undef\n", c.name, c.row); } else { System.out.printf("%s[%d] = %3.2f\n", c.name, c.row, c.value); } } } catch (java.util.NoSuchElementException e) { break; } // if the formula has a syntactic or semantic error then // print the error message catch (ParserError perror) { System.err.print(perror.errorMsg); } catch (Exception e) {e.printStackTrace();} }