LARA

MiniJava+ Resource Page

MiniJava+ is our extension to MiniJava, and is a strict subset of Java.

BNF

(reproduced and extended from here)

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
String
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 )* )? )
<INTEGER_LITERAL>
" <STRING_LITERAL> "
true
false
Identifier
this
new int [ Expression ]
new Identifier ( )
! Expression
( Expression )
Identifier::=<IDENTIFIER>
  • <IDENTIFIER> represents a sequence of letters, digits and underscores, starting with a letter. Identifiers are case-sensitive.
  • <INTEGER_LITERAL> represents a sequence of digits, with no leading zeros
  • <STRING_LITERAL> represents a sequence of arbitrary characters, except new lines and ". We don't ask you to support escape characters such as \n.
  • <EOF> represents the special end-of-file character

Typing rules

Typing rules are on this separate page.

Miscellaneous

  • Comments in MiniJava+ can be marked using the end-of-line notation (//) or blocks ( /* */ ). Nested blocks are not allowed, contrary to MiniJava.
  • Inheritance works as in Java, except that we don't have the notions of interfaces, abstract members or abstract classes.
  • No form of overloading is permitted.
  • Note that the else branch of the if construct is optional, as opposed to in MiniJava where it always has to be specified.

Examples

The following are examples of program in MiniJava.1) Since MiniJava+ is a superset of MiniJava, they are of course also MiniJava+ programs.

Some clarifications

The answer to (most of) these questions is in the grammar, but to avoid confusion, here are some details that may seem curious:

  • Yes, if the main method has more than one statement, they must be in a block and thus will be surrounded by two pairs of curly braces.
  • No, you cannot define extra variables in the main method.
  • No, you cannot use non-default constructors (constructors that take parameters).
  • Method overloading is not allowed, but method overriding is. If you are unclear about the difference between the two, see this page. The only overloading in MiniJava+ is on the + operator, which can be used with integers, strings, and between the two types.
  • Only constant strings (strings given as literals) are allowed, and although the grammar specifies that expressions such as “my string”.length() are legal, we exclude them. The only operations allowed are: concatenating strings with other strings or with integers, printing strings, passing strings as argument and returning strings.
1)
examples copyright MiniJava project by Joao Cangussu, Jens Palsberg and Vidyut Samanta