EBNF Grammar for Mini-Java
| Goal | MainClass ClassDeclaration EOF | |
| MainClass | "class" Identifier "{" "public" "static" "void" "main" "(" "String" "[" "]" Identifier ")" "{" Statement "}" "}" | |
| ClassDeclaration | "class" Identifier "extends" Identifier "{" VarDeclaration MethodDeclaration "}" | |
| VarDeclaration | Type Identifier ";" | |
| MethodDeclaration | "public" Type Identifier "(" Type Identifier "," Type Identifier ")" "{" VarDeclaration Statement "return" Expression ";" "}" | |
| Type | "int" "[" "]" | |
| "boolean" | ||
| "int" | ||
| Identifier | ||
| Statement | "{" Statement "}" | |
| "if" "(" Expression ")" Statement "else" Statement | ||
| "while" "(" Expression ")" Statement | ||
| "System.out.println" "(" Expression ")" ";" | ||
| Identifier "=" Expression ";" | ||
| Identifier "[" Expression "]" "=" Expression ";" | ||
| Expression | Expression "&&" | "<" | "+" | "-" | "*" Expression | |
| Expression "[" Expression "]" | ||
| Expression "." "length" | ||
| Expression "." Identifier "(" Expression "," Expression ")" | ||
| IntegerLiteral | ||
| "true" | ||
| "false" | ||
| Identifier | ||
| "this" | ||
| "new" "int" "[" Expression "]" | ||
| "new" Identifier "(" ")" | ||
| "!" Expression | ||
| "(" Expression ")" | ||
| Identifier | one or more letters, digits, and underscores, starting with a letter | |
| IntegerLiteral | one or more decimal digits | |
| EOF | a distinguished token returned by the scanner at end-of-file |
Comments
Comments are // to end of line and /* ... */, just as in Java. The /* ... */ comments do not nest in Java. For example,/*
One commment
/* Nested comment */
Bad things will happen
*/
The second /* will be ignored (it is in a comment), and the first */ will terminate the comment. Now, "bad things will happen" as the remaining text is not a comment.
Appel, 2nd edition, page 484, describes comments in MiniJava as being nestable. This is an interesting exercise for the scanner, but is not required.
EBNF
ISO/IEC 14977: 1996(E)- Terminal symbols are quoted
- and indicate optional symbols
- and indicate repetition
- and group items together; the other brackets do too
