Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Minimal Java Compiler

End-to-end compiler for a subset of the Java language (MiniJava), translating source code into LLVM IR. Beginning with JavaCC/JTB for the front-end, to semantic analysis and LLVM code generation.

Usage

For compilation, on the src/ directory, execute the following command:

make compile

To test the compiler, run the following command which will compile and execute the program:

make exec file=/path/to/file_to_compile_and_exec.java

Example:

make exec file=../minijava-examples-new/Factorial.java

Documentation

syntaxtree/

Auto-generated by JTB (Java Tree Builder) from the grammar, one class per grammar production (ClassDeclaration, MethodDeclaration, IfStatement, Expression, etc.). Together these classes form the AST node types.

src/visitor/

Also JTB-generated — the visitor interfaces/base classes (GJDepthFirst, DepthFirstVisitor, GJVoidVisitor, etc.) implementing the Visitor pattern, which every custom analysis pass extends to walk the AST.

src/symbol_table/

Hand-written data structures for the symbol table: class_t, method_t, var_t, name_t, vtable_t, and the top-level symbol_table_t that stores classes, methods, fields, and inheritance relationships (including virtual tables for polymorphism).

src/semantic_analysis/

Custom visitors implementing semantic analysis, run in sequence from Main.java:

  • declarations_visitor — first pass; walks the AST and populates the symbol table with class/method/variable declarations.
  • declarations_visitor_two — second pass; validates that declared types actually exist (checking against the populated symbol table).
  • type_check_visitor — the type-checker; verifies expressions, assignments, method calls, and return types are consistent with MiniJava's type rules.

src/offsets_table/

offsets_table_t computes and stores memory offsets for fields and methods (including inherited/overridden members) — needed for generating correct LLVM IR field access and virtual dispatch.

src/llvm_structs/

The code generation back-end:

  • llvm_visitor — the main visitor that walks the (type-checked) AST and emits LLVM IR.
  • llvm_vtable_visitor — specifically emits virtual table structures for classes with inheritance/polymorphism.
  • tuple_t / collocations_t — helper structures tracking IR value types and generated temporaries/labels during codegen.

src/error_print/

Small utility (error_printing) for consistent, color-coded error message output during parsing/semantic analysis.

minijava-examples-new/

Extensive tests for the compiler implementation

References

[1] Project Specification https://cgi.di.uoa.gr/~compilers/19_20/project.html

About

A full-blown compiler implementation for a subset of the Java language.

Topics

Resources

Stars

3 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages