CIT 594 Grammar for the Bugs Language, version 2
Spring 2015, David Matuszek

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

To correct an error in the handling of unary operators, the rules for <arithmetic expression> and <factor> have been modified, and <unsigned factor> has been added. Please copy these changes to your Recognizer before making a copy of it to begin working on the Parser.

In addition, the code for <function call> has been incorporated into <factor>, in order to avoid a parsing problem.


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> ::= [ <add operator> ] <unsigned factor>

<unsigned factor> ::= <variable> "." <variable>
                    | <function call>
                    | <variable>
                    | <NUMBER>
                    | "(" <expression> ")"

<add operator> ::= "+" | "-"

<multiply operator> ::= "*"| "/"