My implementation of a compiler for the MiniJava language (a small subset of Java).
Its development process consists of three different projects, where each one served as an assignment for the Department of Informatics and Telecommunications undergraduate Compilers course.
The compiler is a program that converts instructions written in the MiniJava language into the intermediate representation used by the LLVM Compiler Project (LLVM Language Reference Manual).
Its operation includes the following stages:
- Parsing the MiniJava source files in order to generate a parse tree, detect and report syntax errors.
- Traversing the parse tree using the Visitor pattern, creating a symbol table and reporting semantic errors.
- Generating code in the LLVM assembly language using, once again, the Visitor pattern.
The compiler uses a symbol table for the purpose of keeping track of information for the different entities found within the source code. Specifically,
- regarding the classes, it records their name as well as both the fields and methods they contain,
- regarding the methods, it records their variables (local and arguments),
- regarding the variables, it records their type and whether they've been initialized or not.
Afterwards, the source code is type checked.
If at least one of MiniJava's rules is being broken, the compilation process terminates and a message, that describes the error, is displayed.
By using once again the Visitor pattern alongside a subset of LLVM's instructions, we produce the intermediate representation used by the LLVM compiler project for the original MiniJava code that was provided.
- Java
- JavaCC
- JTB
- JFlex
- Java CUP
We can run each one of the projects that represent the final compiler separately. Specifically,
-
for Project 2, which covers the Semantic Analysis phase, we simply type the following on the command line prompt:
make -C Project\ 2
java Main <source_code>.txt
<source_code>.txt is a .txt file that holds the MiniJava code we wish to use.
If no errors have been found, information about the source file's classes will be displayed on the screen. Otherwise, a message explaining what the first error was and where it was found will appear.
-
for Project 3, which covers both the Type Checking and the Intermediate Code Generation phases, we type the following:
make -C Project\ 3
java Main <input_file>.java
<input_file.java>* is a .java file that holds the Miniava code we wish to compile.
After all of the above have been executed, a .ll file named <input_file>.ll will have been created. To execute it, we run the following commands:
clang -o <name_of_result_file> <input_file>.ll
./<result_file>
When we're done with running both projects, we remove all of the intermediate files that have been created by running:
make clean -C Project\ 2 make clean -C Project\ 3