CIT 594 Parser Keywords, Bugs Example
Spring 2008, David Matuszek

In the Parser assignment, there are nodes in the tree which do not correspond to keywords in the Bugs language. These are: program, list, block, assign, function, and call. When you build the Abstract Syntax Tree, please make these Token.Type.NAME, not Token.Type.KEYWORD.



Here is an extra test you can use for your Recognizer.

Although it's a little late to help you with the Recognizer, you need to have the Recognizer correct when you build your Parser. If this test uncovers any problems with the Recognizer, you should add the corresponding fixes to your Parser.
import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test;

public class RecognizerTest3 {

    @Test
    public final void testLongProgram() {
        Recognizer r = new Recognizer(
                        "// Example Bugs program by David Matuszek\n" + 
        		"/* Nonsense program to test out the recognizer */\n" + 
        		"Allbugs {\n" + 
        		"    var abc\n" + 
        		"   \n" + 
        		"    define forward using n {\n" + 
        		"        move n // random pointless comment \n" + 
        		"        return -n\n" + 
        		"    }\n" + 
        		"    define abc123 {\n" + 
        		"        abc = 123\n" + 
        		"    }\n" + 
        		"}\n" + 
        		"\n" + 
        		"Bug Sally {\n" + 
        		"    var a, b, c\n" + 
        		"    var x, y\n" + 
        		"    \n" + 
        		"    initially {\n" + 
        		"        x = -50\n" + 
        		"        color red\n" + 
        		"        line 0, 0, 25.3, 100/3\n" + 
        		"    }\n" + 
        		"    \n" + 
        		"    y = 2 + 3 * a - b / c\n" + 
        		"    y = ((2+3)*a)-(b/c)\n" + 
        		"    loop{\n" + 
        		"        y = y / 2.0\n" + 
        		"        exit if y<=0.5\n" + 
        		"    }\n" + 
        		"    switch {\n" + 
        		"    }\n" + 
        		"    switch {\n" + 
        		"        case x < y\n" + 
        		"            moveto 3, x+y\n" + 
        		"            turn x-y\n" + 
        		"        case a <= x < y = z !=a >= b > c\n" + 
        		"            turnto -abc123() + forward(x)\n" + 
        		"    }\n" + 
        		"    do forward(a)\n" + 
        		"}\n" + 
        		"Bug henry {\n" + 
        		"    x = Sally.x\n" + 
        		"    y = -Sally.y + 100\n" + 
        		"}\n");
        assertTrue(r.isProgram());
    }
}
Here's one possible problem to watch out for. The StreamTokenizer I gave you discards Java-style comments, so the comments at the beginning of this test should be okay. However, the tokenizer did not discard all the newlines (eols) in the comments, and those did cause problems. Here's how you could correct the grammar.
<program> ::= [ <allbugs code> ]
                <bug definition>>
              { <bug definition> }
     should be     
<program> ::= { <eol> }
              [ <allbugs code> ]
                <bug definition>
              { <bug definition> }
To make this change, all you really need to do is to skip newlines at the beginning of your isProgram() method.