Skip to content

Latest commit

 

History

History
114 lines (98 loc) · 6.24 KB

File metadata and controls

114 lines (98 loc) · 6.24 KB

MiniJava Compiler

This is an ARM Assembly Compiler interpretation for the MiniJava Language, a subset of Java by Oracle. The meaning of a MiniJava program is given by its meaning as a Java program. Overloading is not allowed in MiniJava.

Usage

To Clone the Repository

$ git clone https://github.com/addisonpolcyn/MiniJava-Compiler.git

Setup

$ make

Cleaning

$ make clean

Compiling Minijava to ARM Assembly

This will type check, analyze, compile, and produce an ARM Assembly Equivalent file as output.

$ mjavac factorial.java

Running an ARM Assembly file

Not every machine supports ARM Assembly by default. Raspberry Pi does support it. The ARM Assembly output file must be assembled and then executed to see proper output.

Sample MiniJava Program

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 ;
    }

}

Technology Used

One or more of the technologies below may need to be installed during setup.

  • C
  • C++
  • Flex
  • Bison
  • ARM Assembly

MiniJava Grammar

Program ::= MainClass ( ClassDeclaration )*

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

Note: These MiniJava grammar rules are from http://www.cs.tufts.edu/~sguyer/classes/comp181-2006/minijava.html and may share or not share similarities with this MiniJava Compiler.