CIT 594 Bugs Grammar
Spring 2008, David Matuszek

This page describes the grammar used by our Recognizer for the "Bugs" language.

Extended BNF Key
::= "is defined by"
| "or"
<...> nonterminal
"..." terminal
[...] optional
{...} zero or more

The following productions are handled by the Tokenizer, and are the raw input to your Recognizer:
<NAME> ::= <letter> { <letter> | <digit> }

<KEYWORD> ::= Same as a NAME, but one that occurs in the list of recognized keywords
 
<NUMBER> ::= <digit> { <digit> } [ "." { <digit> } ]

<SYMBOL> ::= "+" | "-" | "*" | "/" | "=" | "!"
           | "<" | ">" | "(" | ")"| "{" | "}" | ","  

<EOL> ::=  "\n"

<EOF> ::= end of string

The following productions are not handled by your Tokenizer, but must instead be handled by your Recognizer. Color has been used to help distinguish between terminals and nonterminals, and indentation has been used to suggest how the statements should be laid out, but neither is significant.

<program> ::= [ <allbugs code> ]
                <bug definition>
              { <bug definition> } 

<allbugs code> ::= "Allbugs"  "{" <eol>
                       { <var declaration> }
                       { <function definition> }
                   "}" <eol>

<bug definition> ::= "Bug" <name> "{" <eol>
                         { <var declaration> }
                         [ <initialization block> ]
                         <command>
                         { <command> }
                         { <function definition> }
                    "}" <eol>
  
<var declaration> ::= "var" <NAME> { "," <NAME> } <eol>                                      

<initialization block> ::= "initially" <block>

<command> ::= <action>
            | <statement> 

<statement> ::= <assignment statement>
              | <loop statement>
              | <exit if statement>
              | <switch statement>
              | <return statement>
              | <do statement>
              | <color statement>

<action> ::= <move action>
           | <moveto action>
           | <turn action>
           | <turnto action>
           | <line action>

<move action> ::= "move" <expression> <eol>

<moveto action> ::= "moveto" <expression> "," <expression> <eol>

<turn action> ::= "turn" <expression> <eol>

<turnto action> ::= "turnto" <expression> <eol>

<line action> ::= "line" <expression> ","<expression> ","<expression> "," <expression> <eol>

<assignment statement> ::= <variable> "=" <expression> <eol>

<loop statement> ::= "loop" <block>

<exit if statement> ::= "exit" "if" <expression> <eol>

<switch statement> ::= "switch" "{" <eol>
                         { "case" <expression> <eol>
                              { <command> } }
                       "}" <eol>

<return statement> ::= "return" <expression> <eol>

<do statement> ::= "do" <variable> [ <parameter list> ] <eol>

<color statement> ::= "color" <KEYWORD> <eol>

<variable> ::= <NAME>

<block> ::= "{" <eol> { <command> }  "}" <eol>

<expression> ::= <arithmetic expression> {  <comparator> <arithmetic expression> }

<comparator> ::= "<" | "<=" | "=" | "!=" ">=" | ">"

<function definition> ::= "define" <NAME> [ "using" <variable> { "," <variable> }  ] <block>

<function call> ::= <NAME> <parameter list>

<parameter list> ::= "(" [ <expression> { "," <expression> } ] ")" 

<eol> ::= <EOL> { <EOL> } 
I have given you starter code for your Recognizer, with methods to handle the following productions:
<arithmetic expression> ::= <term> { <add operator> <term> } 


<term> ::= <factor> { <multiply operator> <factor> }


<factor> ::= [ "+" | "-" ] <variable> "." <variable>
           | [ "+" | "-" ] <function call>
           | [ "+" | "-" ] <variable>
           | [ "+" | "-" ] <NUMBER>
           | [ "+" | "-" ] "(" <expression> ")"

<addOperator> ::= "+" | "-"

<multiplyOperator> ::= "*"| "/"