This project implements a compiler for MiniJava, including a MiniC subset, using JavaCC for parsing and the Visitor pattern for compiler phases. The compiler transforms MiniJava source code through multiple stages such as syntax tree construction, pretty printing, type checking, and x86-64 assembly code generation.
- Parses MiniJava and MiniC programs using JavaCC
- Builds Abstract Syntax Trees (AST)
- Pretty prints MiniJava source code
- Converts C code to equivalent Python code
- Generates x86-64 assembly code
- Constructs symbol tables for scope and type tracking
- Performs type checking for type safety
- Tokenizes MiniJava/MiniC syntax
- Defines grammar rules for expressions, statements, and method declarations
- Builds the AST representing the program structure
- Generate the following files that compile and parse the program
Compiler.javaCompilerConstants.javaCompilerTokenManager.javaParseException.javaSimpleCharStream.javaToken.javaTokenMgrError.java
- Traverses the AST and prints its structure with indentation
- Displays node class names, hierarchy, and literal values
public Object visit(Program node, Object data){
System.out.println(indentString() + getClassName(node));
++indent;
node.m.accept(this,data);
if (node.c!=null){
node.c.accept(this,data);
}
--indent;
return data;
}
PP_Visitor (Demo)
- Pretty prints MiniJava programs with proper formatting
C2Python_Visitor (Demo)
- Converts C code to Python, handling:
- Function declarations
- Control structures (if, while)
- Expressions and type conversions
public Object visit(MethodDecl node, Object data){
int indent = (int) data;
String id = (String) node.i.accept(this,indent);
String formalList = "";
if (node.f!=null){
formalList = (String) node.f.accept(this,indent);
}
// ...
return indentString(indent) + "def " + id + "(" + formalList + "):\n" +
statementList + indentString(indent+1) + "return " + expr;
}
CodeGen_Visitor_Tiara (Demo)
- Generates x86-64 assembly code
- Manages:
- Register allocation
- Stack frame setup
- Procedure calls
- Array access
- Control flow
public Object visit(Print node, Object data){
Exp e=node.e;
String expCode = (String) node.e.accept(this, data);
String result =
expCode
+ "# "+node.accept(ppVisitor, 0)
+ "popq %rdi\n"
+ "movb $0, %al\n"
+ "callq print\n";
return result;
}
SymbolTableVisitor (Demo)
- Builds symbol tables for:
- Variable declarations
- Method signatures
- Class scopes and types
TypeCheckingVisitor (Demo)
- Validates type correctness throughout the AST
- Classes with methods and instance variables
- Integer, boolean, and array types
- If/else and while control structures
- Binary operations:
+,-,*,<,&& - Method calls
- No classes or instance variables
- Only method declarations
- Integer and boolean types
- Limited expressions:
<,+,-,*, literals, and identifiers - Statements: assignment,
if, andprint
The project includes several build scripts:
The tests directory contains various test files including:
hello.c: Simple C program that calculates and prints a squareprint.c: Support code for print functionality- Other test cases for different language features
src: Java source files for visitors and compiler logicbin: Compiled classes and JavaCC grammar filetests: Test input files and generated outputssyntaxtree: AST node classes
The AST represents programs as a hierarchy of node types:
- Program
Top-level node containingMainClassand class declarations - MainClass
Entry point withmainmethod - Statement nodes
BlockIfWhilePrintAssign
- Expression nodes
PlusMinusTimesLessThan- …etc.
- Type nodes
IntegerTypeBooleanTypeIntArrayType
- Lists
StatementListExpListVarDeclList- …etc.