(taken from Appel and Palsberg's Modern Compiler Implementation in Java, pages 484-486)
MiniJava is a subset of Java. The meaning of a MiniJava program is given by its meaning as a Java program. Overloading is not allowed in MiniJava. The MiniJava statement System.out.println( ... ); can only print integers. The MiniJava expression e.length only applies to expressions of type int [].
| 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 )* )? ")" | |
| | | <INTEGER_LITERAL> | |
| | | "true" | |
| | | "false" | |
| | | Identifier | |
| | | "this" | |
| | | "new" "int" "[" Expression "]" | |
| | | "new" Identifier "(" ")" | |
| | | "!" Expression | |
| | | "(" Expression ")" | |
| Identifier | ::= | <IDENTIFIER> |
class Factorial{
public static void main(String[] a){
System.out.println(new Fac().ComputeFac(10));
}
}
class Fac {
public int ComputeFac(int num){
int num_aux ;
if (num < 1)
num_aux = 1 ;
else
num_aux = num * (this.ComputeFac(num-1)) ;
return num_aux ;
}
}