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 } "}"
| "record", Identifier, "(" [ Type, Identifier { ",", Type, Identifier } ] ")", "{", "}"
;
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 , "." , Identifier , "(" , [ Expression { "," , Expression } ] , ")"
| Expression, "[", Expression, "]"
| Expression, ".", "length"
| "new", "int", "[", Expression, "]"
| "new" , Identifier , "(" , [ Expression { "," , Expression } ] , ")"
| "!", Expression
| IntegerLiteral
| "true"
| "false"
| Identifier
| "this"
| "(", Expression, ")"
;
Identifier is one or more letters, digits, and underscores, starting with a letter
IntegerLiteral is one or more decimal digits [see Java spec for largest]
EOF is a distinguished token returned by the scanner at end-of-file

There is a problem of consistency with Java concerning array access and array creation. They are both expressions. In Java new int [5][4] is 2D array creation and not array access as in MiniJava (new int [5])[4].

EBNF

ISO/IEC 14977: 1996(E)

MiniJava Character Set

Any sequence of bytes is potential input to the MiniJava compiler. The MiniJava compiler should interpret the input as a text file of Latin-1 characters. This fixes the meaning of every 8-bit byte as a single character (unlike UTF-8)--not necessarly a printable character. The syntax of a MiniJava program actually requires only a subset of US-ASCII characters (which is a subset of Latin-1). This is different than Java which handle multiple character sets as input.

No Java/Python unicode escapes \uXXXX are used in MiniJava, but are easy to include using a JavaCC options: JAVA_UNICODE_ESCAPE=true.

Line terminators are LF, CF, or CF followed by LF. In addition to the line terminators, white space is SP, HT, or the FF characters. (In Java the input can end with the US-ASCII SUB character, also known as "control-Z".)

Java 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 correct.

Any Latin-1 character is legal in a Java comment.

Programming Meaning

Mini Java is supposed to be a subset of Java. If the input is not a legal (lexically, syntactically, and semantically correct) Java 20 program, then it is not a legal Mini Java program.
javac -encoding ISO-8859-1 ClassFile.java
A Mini Java program behaves exactly like the Java program.

The following program is not legal in Java:

class Main {
  void m() {
    int goto = 3; // 'goto' is a reserved word in Java
  }
}
it should not be legal in Mini-Java.

A Mini-Java program with uninitilized variables ought to be illegal just as in Java. A Mini-java program with unreachable statements ought to be illegal just as in Java.

Java Keywords

Many Java keywords are not used in MiniJava. Consult the Java language specification for details. Java now has contextual keywords.

Relaxations from Java

In order to simplify the programming project, we make the following simplifications: (However, you may include these features, if you wish.)

A minimal Mini-Java compiler must, however, include the following features: