CIT 594 Assignment 4: Parser Final Corrections
Spring 2004, David Matuszek

In the Parser Addendum I said:

"I haven't provided eol() for you, but it should leave a BinaryTree on the stack containing an EOL Token."

Actually, it's better if the eol() method just returns true if it finds one or more '\n's, and throws them all away (doesn't put them on the stack). Otherwise you just have to pop them off later, because they aren't good for anything when you are building the parse tree.


If you haven't been reading the announcements on the class website, you may have missed the fact that there is a corrected version of DrDavesTests.java. It's going to be difficult for correct code to pass the incorrect tests!


Please double-check that all your class names and constructor and method signatures are exactly as required. If you misspell a name (for example, colorname instead of colorName), that is a serious error.

Before we can test your code, we have to find and correct your misnamings, and recompile. And it's not just that we don't like the extra work (although we don't), it also means that anyone else who depends on your code is going to have the same problems.

Follow the contract.


In class I had talked about changing the order of parameters to makeTree, but I did not post the code for doing so. In addition to changing the comments, there are three changes to make:

Original Revised
private BinaryTree getStackItem(int n) {
    return (BinaryTree) stack.get(stack.size() - 4 + n);
}
private BinaryTree getStackItem(int n) {
    return (BinaryTree) stack.get(stack.size() - n);
}
In expression:
     makeTree(2, 1, 3);
In expression:
     makeTree(2, 3, 1);
In term:
     makeTree(2, 1, 3);
In term:
     makeTree(2, 3, 1);

I also added a two-argument version of makeTree:

    private void makeTree(int rootIndex, int leftIndex) {
               
        BinaryTree root = getStackItem(rootIndex);
        BinaryTree leftChild = getStackItem(leftIndex);
        
        Token rootValue = (Token) (root.value);
        
        stack.pop();
        stack.pop();
        
        stack.push(new BinaryTree(rootValue, leftChild, null));
    }