This script serves as a compiler for MiniJava programs. It performs lexical, syntactical, and semantic analysis before generating Three-Address Code (TAC) for the input Java program. Additionally, it visualizes the parse tree.
- Lexical Analysis: Tokenizes the input Java file using ANTLR.
- Syntax Parsing: Builds a parse tree from the tokenized input.
- Semantic Analysis: Constructs a symbol table and performs type checking.
- Code Generation: Produces Three-Address Code (TAC) for valid MiniJava programs.
- Visualization: Generates a graphical representation of the parse tree.
This image is a sample parse tree for the file Factorial.java:

+-------------------------------+---------------------+----------------------------+
| ID | TYPE | SCOPE/RECORD |
+-------------------------------+---------------------+----------------------------+
| Factorial | Factorial | ClassRecord |
+-------------------------------+---------------------+----------------------------+
| Fac | Fac | ClassRecord |
+-------------------------------+---------------------+----------------------------+
| Factorial.main | None | MethodRecord |
+-------------------------------+---------------------+----------------------------+
| Fac.ComputeFac | int | MethodRecord |
+-------------------------------+---------------------+----------------------------+
| num | int | Record |
+-------------------------------+---------------------+----------------------------+
| num_aux | int | Record |
+-------------------------------+---------------------+----------------------------+
METHOD Factorial.main
0 push param 10
1 t1 = lcall Fac.ComputeFac, 1
2 pop param 8
3 push param t1
4 lcall print, 1
5 return None
METHOD Fac.ComputeFac
0 t0 = (num < 1)
1 if not t0 goto L4
2 num_aux = 1
3 goto L11
4 label L4:
5 t5 = num - 1
6 push param t5
7 t7 = lcall Fac.ComputeFac, 1
8 pop param 8
9 t9 = num * t7
10 num_aux = t9
11 label L11:
12 return num_aux
Ensure you have Python (>=3.6) installed along with the required dependencies:
pip install -r requirements.txt- Windows: Download and install from Graphviz Official Site. Add the
bindirectory to system PATH. - Linux (Debian/Ubuntu):
sudo apt install graphviz
- Mac (Homebrew):
brew install graphviz
To run the script, use the following command:
python main.py <path_to_java_file>Example:
python main.py examples/BinarySearch.javaIf no file is provided, the default example (examples/BinarySearch.java) is used.
MiniJavaCompiler/
│── .gitignore # Ignore virtual environments, compiled files, etc.
│── README.md # Project documentation
│── requirements.txt # List of dependencies
│── main.py # Entry point of the compiler
│
├───code_generator/ # Code generation module
│ ├── __init__.py
│ ├── controller.py # Handles code generation (CodeGenVisitor)
│ ├── models.py # Data models for code generation
│
├───examples/ # Sample MiniJava programs & visual outputs
│ ├── BinarySearch.java
│ ├── BinarySearch.png
│ ├── BinaryTree.java
│ ├── BubbleSort.java
│ ├── Factorial.java
│ ├── LinearSearch.java
│ ├── LinkedList.java
│ ├── QuickSort.java
│ ├── Test.java
│
├───grammar/ # ANTLR grammar files
│ ├── MiniJavaGrammar.g4
│
├───parser/ # Generated ANTLR parser and lexer files
│ ├── MiniJavaGrammar.interp
│ ├── MiniJavaGrammar.tokens
│ ├── MiniJavaGrammarLexer.interp
│ ├── MiniJavaGrammarLexer.py
│ ├── MiniJavaGrammarLexer.tokens
│ ├── MiniJavaGrammarParser.py
│ ├── MiniJavaGrammarVisitor.py
│
├───semantic_analysis/ # Semantic analysis module
│ ├── __init__.py
│ ├── controller.py # Handles semantic checks
│ ├── models.py # Symbol table and type definitions
│
└───visual/ # Visualization tools for parse trees
├── __init__.py
├── visualizer.py # Parse tree visualizer
- Lexical Analysis: Uses
MiniJavaGrammarLexerto tokenize the input file. - Parsing:
MiniJavaGrammarParsergenerates a parse tree. - Symbol Table Construction:
SymbolTableVisitorbuilds a symbol table. - Type Checking:
TypeCheckVisitorensures type correctness. - Code Generation:
CodeGenVisitorproduces TAC. - Visualization:
Visualizerdraws the parse tree using Graphviz.
Mahdi Shahmoradi