BNF for MeggyJava

Based on a MiniJava grammar. MeggyJava is a variant of MiniJava. The main differences are the addition of calls to a meggyjava package that is assumed to be built in to the MeggyJava language much like System.out.println() was built in to MiniJava. We also did smaller differences such as allowing multiple statements within main.

First, we list the full grammar for MeggyJava, which will be completed in PA5. Below that grammar are versions of the grammar for PA2 through PA4.

This final grammar (PA5) adds variable declarations, assignment statements, objects, and arrays to the PA4 MeggyJava Grammar. The new MeggyJr feature since PA4 is setAuxLEDs.

Full Meggy Java Grammar (PA5)

Goal ::= "import" "meggy.Meggy" ";" MainClass ( ClassDeclaration )* <EOF>
MainClass ::= "class" Identifier "{" "public" "static" "void" "main" "(" "String" "[" "]" Identifier ")" "{" (Statement)* "}" "}"
ClassDeclaration ::= "class" Identifier "{" ( VarDeclaration )* ( MethodDeclaration )* "}"
VarDeclaration ::= Type Identifier ";"
MethodDeclaration ::= "public" Type Identifier "(" ( Type Identifier ( "," Type Identifier )* )? ")" "{" ( VarDeclaration )* ( Statement )* ("return" Expression ";")? "}"
Type ::= "Meggy.Color" "[" "]"
| "int" "[" "]"
| "boolean"
| "byte"
| "int"
| "void"
| "Meggy.Color"
| "Meggy.Button"
| "Meggy.Tone"
| Identifier
Statement ::= "{" ( Statement )* "}"
| "Meggy.setPixel" "(" Expression "," Expression "," Expression ")" ";"
| "Meggy.setAuxLEDs" "(" Expression ")" ";"
| "Meggy.toneStart" "(" Expression "," Expression ")" ";"
| "Meggy.delay" "(" Expression ")" ";"
| Expression "." Identifier "(" ( Expression ( "," Expression )* )? ")" ";"
| "if" "(" Expression ")" Statement
| "if" "(" Expression ")" Statement "else" Statement
| "while" "(" Expression ")" Statement
| Identifier "=" Expression ";"
| Identifier "[" Expression "]" "=" Expression ";"
Expression ::= Expression ( "&&" | "<" | "==" | "+" | "-" | "*" ) Expression
| "-" Expression
| Expression "[" Expression "]"
| Expression "." "length"
| Expression "." Identifier "(" ( Expression ( "," Expression )* )? ")"
| "Meggy.getPixel" "(" Expression "," Expression ")"
| "Meggy.checkButton" "(" Expression ")"
| "(" "byte" ")" Expression
| <INT_LITERAL>
| <COLOR_LITERAL>
| <BUTTON_LITERAL>
| <TONE_LITERAL>
| "true"
| "false"
| Identifier
| "this"
| "new" "Meggy.Color" "[" Expression "]"
| "new" "int" "[" Expression "]"
| "new" Identifier "(" ")"
| "!" Expression
| "(" Expression ")"
Identifier ::= <IDENTIFIER>

PA2 MeggyJava Grammar

Only includes the Meggy.setPixel() statement, Meggy.Color literals, byte and int literals, and byte casts.
Goal ::= "import" "meggy.Meggy" ";" MainClass <EOF>
MainClass ::= "class" Identifier "{" "public" "static" "void" "main" "(" "String" "[" "]" Identifier ")" "{" (Statement)* "}" "}"
Statement ::= "Meggy.setPixel" "(" Expression "," Expression "," Expression ")" ";"
Expression ::= "(" "byte" ")" Expression
| <INT_LITERAL>
| <COLOR_LITERAL>

PA3 MeggyJava Grammar

Adds arithmetic expressions, some boolean expressions, and control flow as well as more built-in Meggy functionality. Byte addition and subtraction should result in byte types being promoted to integer types.
Goal ::= "import" "meggy.Meggy" ";" MainClass <EOF>
MainClass ::= "class" Identifier "{" "public" "static" "void" "main" "(" "String" "[" "]" Identifier ")" "{" (Statement)* "}" "}"
Statement ::= "{" ( Statement )* "}"
| "Meggy.setPixel" "(" Expression "," Expression "," Expression ")" ";"
| "Meggy.delay" "(" Expression ")" ";"
| "if" "(" Expression ")" Statement
| "if" "(" Expression ")" Statement "else" Statement
| "while" "(" Expression ")" Statement
Expression ::= Expression ( "&&" | "==" | "+" | "-" | "*" ) Expression
| "-" Expression
| "Meggy.getPixel" "(" Expression "," Expression ")"
| "Meggy.checkButton" "(" Expression ")"
| "(" "byte" ")" Expression
| <INT_LITERAL>
| <COLOR_LITERAL>
| <BUTTON_LITERAL>
| "true"
| "false"
| "!" Expression
| "(" Expression ")"

PA4 MeggyJava Grammar

Adds function definitions, function calls, parameter variables, and the less than expression to the PA3 MeggyJava Grammar. Need object creation syntax and this expression syntax to enable method calls that also compile with Java. It should be possible to pass an expression of type byte into a method expecting an integer parameter.
Goal ::= "import" "meggy.Meggy" ";" MainClass ( ClassDeclaration )* <EOF>
MainClass ::= "class" Identifier "{" "public" "static" "void" "main" "(" "String" "[" "]" Identifier ")" "{" (Statement)* "}" "}"
ClassDeclaration ::= "class" Identifier "{" ( MethodDeclaration )* "}"
MethodDeclaration ::= "public" Type Identifier "(" ( Type Identifier ( "," Type Identifier )* )? ")" "{" ( Statement )* ("return" Expression ";")? "}"
Type ::= "boolean"
| "byte"
| "int"
| "void"
| "Meggy.Color"
| "Meggy.Button"
| "Meggy.Tone"
Statement ::= "{" ( Statement )* "}"
| "Meggy.setPixel" "(" Expression "," Expression "," Expression ")" ";"
| "Meggy.toneStart" "(" Expression "," Expression ")" ";"
| "Meggy.delay" "(" Expression ")" ";"
| Expression "." Identifier "(" ( Expression ( "," Expression )* )? ")" ";"
| "if" "(" Expression ")" Statement
| "if" "(" Expression ")" Statement "else" Statement
| "while" "(" Expression ")" Statement
Expression ::= Expression ( "&&" | "<" | "==" | "+" | "-" | "*" ) Expression
| "-" Expression
| Expression "." Identifier "(" ( Expression ( "," Expression )* )? ")"
| "Meggy.getPixel" "(" Expression "," Expression ")"
| "Meggy.checkButton" "(" Expression ")"
| "(" "byte" ")" Expression
| <INT_LITERAL>
| <COLOR_LITERAL>
| <BUTTON_LITERAL>
| <TONE_LITERAL>
| "true"
| "false"
| Identifier
| "this"
| "new" Identifier "(" ")"
| "!" Expression
| "(" Expression ")"
Identifier ::= <IDENTIFIER>