This is a compiler for MiniJava, a subset of the Java programming language. MiniJava supports basic object-oriented features like classes, fields, methods, and single inheritance. It also includes arrays, logical operators, control flow statements, and simple expressions using integers.
Here's a brief overview of what MiniJava supports:
-
Class Declaration: Define classes with fields and methods. Fields are protected, and methods are inherently polymorphic (virtual).
-
Inheritance: Classes can extend other classes, inheriting their fields and methods. Method overriding is allowed.
-
Basic Types: MiniJava supports
int,boolean,int[](arrays of integers), andboolean[](arrays of booleans). -
Method Calls: Call methods using the dot notation (
object.method()), passing parameters as arguments. -
Arrays: Perform array operations, including array access (
array[index]) and getting the size of an array using thelengthattribute. -
Control Flow: MiniJava has
if-elsestatements andwhileloops for control flow. -
Logical Operators: Supports
&&(logical AND) and!(logical NOT) operators. -
Constructor and Destructor: No explicit constructors or destructors; objects are created with the
newoperator. -
Scoping and Shadowing: Local variables are defined at the beginning of a method and can shadow class fields.
-
Static Methods and Fields: MiniJava does not support static methods or fields.
-
Special Method
main: Themainmethod serves as the entry point for the MiniJava program.
The MiniJava compiler is implemented using JavaCC and JTB tools. It consists of several visitors that perform different tasks:
-
Parser: The MiniJava source code is parsed using the BNF grammar (
minijava.jj) to construct an Abstract Syntax Tree (AST). -
Semantic Analysis: The AST is traversed by the TypeCheckVisitor to perform static semantic analysis, ensuring that the program adheres to the MiniJava language rules.
-
Intermediate Representation (IR) Generation: The IRGenVisitor generates LLVM intermediate representation (IR) code for further compilation.
To compile the MiniJava compiler and execute the examples, follow these steps:
-
Use the provided Makefile to compile the MiniJava compiler:
make
-
Run the MiniJava compiler on a specific file or a folder containing MiniJava files:
make execute FILE=path/to/your_file.java
or
make execute DIR=path/to/your_folder
The compiled LLVM IR files for each MiniJava source file will be generated in the same folder with
.llextensions. -
You can then use Clang or another LLVM-based compiler to compile the generated IR code into an executable:
clang -o output_file out1.ll
Finally, execute the output file:
./output_file
The minijava-examples/passed folder contains MiniJava example files that should compile successfully. The minijava-examples/errors folder contains files that are expected to throw errors during compilation due to semantic issues.
Please note that this is a simplified version of the MiniJava compiler, and it may not cover all edge cases and features of the full Java language.