BNF for MeggyJava

Based on a MiniJava grammar. MeggyJava is a variant of MiniJava developed for the compiler course at Colorado State University. 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.

First, we list the full grammar for MeggyJava, which will be completed in PA7. Below that grammar are versions of the grammar for PA2 through PA6 in CS 453.

NON-TERMINALS

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 "else" Statement
| "while" "(" Expression ")" Statement
| Identifier "=" Expression ";"
| Identifier "[" 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>

PA3 MeggyJava Grammar

Only includes the Meggy.setPixel() statement, the block statement, Meggy.Color literals, byte and int literals, byte casts, and arithmetic expressions.

NON-TERMINALS

Goal ::= "import" "meggy.Meggy" ";" MainClass <EOF>
MainClass ::= "class" Identifier "{" "public" "static" "void" "main" "(" "String" "[" "]" Identifier ")" "{" Statement "}" "}"
Statement ::= "{" ( Statement )* "}"
| "Meggy.setPixel" "(" Expression "," Expression "," Expression ")" ";"
Expression ::= Expression ( "+" | "-" | "*" ) Expression
| "(" "byte" ")" Expression
| <INT_LITERAL>
| <COLOR_LITERAL>
| "(" Expression ")"

PA4 MeggyJava Grammar

Adds some boolean expressions and control flow as well as more built-in Meggy functionality to the PA3 MeggyJava Grammar.

NON-TERMINALS

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 "else" Statement
| "while" "(" Expression ")" Statement
Expression ::= Expression ( "&&" | "==" | "+" | "-" | "*" ) Expression
| "Meggy.getPixel" "(" Expression "," Expression ")"
| "Meggy.checkButton" "(" Expression ")"
| "(" "byte" ")" Expression
| <INT_LITERAL>
| <COLOR_LITERAL>
| <BUTTON_LITERAL>
| "true"
| "false"
| "!" Expression
| "(" Expression ")"

PA5 MeggyJava Grammar

Adds function definitions, function calls, parameter variables, and the less than expression to the PA4 MeggyJava Grammar. Need object creation syntax and this expression syntax to enable method calls that also compile with Java.

NON-TERMINALS

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 "else" Statement
| "while" "(" Expression ")" Statement
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>

PA6 MeggyJava Grammar

Adds variable declarations, assignment statements, and object creation to the PA5 MeggyJava Grammar.

NON-TERMINALS

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 ::= "boolean"
| "byte"
| "int"
| "void"
| "Meggy.Color"
| "Meggy.Button"
| "Meggy.Tone"
| Identifier
Statement ::= "{" ( Statement )* "}"
| "Meggy.setPixel" "(" Expression "," Expression "," Expression ")" ";"
| "Meggy.toneStart" "(" Expression "," Expression ")" ";"
| "Meggy.delay" "(" Expression ")" ";"
| Expression "." Identifier "(" ( Expression ( "," Expression )* )? ")" ";"
| "if" "(" Expression ")" Statement "else" Statement
| "while" "(" Expression ")" Statement
| Identifier "=" 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>