A complete educational compiler for a subset of Java (Mini-Java) implemented in C++ using Flex (lexical analyzer) and Bison (parser generator). This compiler demonstrates all phases of compilation from source code to execution.
- Project Overview
- Features
- Project Structure
- Installation & Prerequisites
- Building the Compiler
- Usage
- Compiler Workflow
- Supported Language Features
- Example Programs
- Troubleshooting
- Project Details for Teachers
This Mini Java Compiler is a complete compiler implementation that processes Mini-Java source code through the following phases:
- Lexical Analysis - Tokenizes the source code
- Syntax Analysis - Builds an Abstract Syntax Tree (AST)
- Semantic Analysis - Performs type checking and scope validation
- Code Generation - Produces Three-Address Code (TAC)
- Interpretation - Executes the program and produces output
The compiler uses modern C++17 features, follows the Visitor pattern for AST traversal, and implements proper error handling at each phase.
- Data Types:
int,boolean - Variables: Declaration and assignment
- Arithmetic Operators:
+,-,*,/ - Relational Operators:
<,>,== - Control Structures:
if,if-else,while - Output:
System.out.println - Program Structure: Single class with
mainmethod
- β Complete lexical analysis with error reporting
- β Grammar-based parsing with AST construction
- β Type checking and semantic validation
- β Scope management with nested scopes
- β Three-Address Code (TAC) generation
- β Runtime interpreter for program execution
- β Comprehensive error messages with line numbers
Java compiler/
βββ scanner.l # Flex lexical analyzer specification
βββ parser.y # Bison parser grammar
βββ ast.h # Abstract Syntax Tree node definitions
βββ symbol_table.h # Symbol table for scope management
βββ semantic_analyzer.h/cpp # Semantic analysis (type checking)
βββ code_generator.h/cpp # TAC code generation
βββ interpreter.h/cpp # Runtime interpreter
βββ main.cpp # Main compiler driver
βββ Makefile # Build configuration
βββ build.bat # Windows build script
βββ test.java # Sample test program
βββ simple_test.java # Simple test program
βββ README.md # This file
lex.yy.c- Generated lexer from scanner.lparser.tab.c- Generated parser from parser.yparser.tab.h- Parser header file*.o- Object filesminijava.exe- Final executable
To build and run this compiler, you need:
- C++ Compiler - g++ with C++17 support (version 7.0 or later)
- Flex - Lexical analyzer generator
- Bison - Parser generator
- Make - Build automation tool (optional but recommended)
-
Download and Install MSYS2
- Visit: https://www.msys2.org/
- Download the installer and run it
- Follow installation instructions
-
Open MSYS2 Terminal (not VS Code terminal initially)
-
Update package database:
pacman -Syu
-
Install required tools:
pacman -S mingw-w64-x86_64-gcc mingw-w64-x86_64-flex mingw-w64-x86_64-bison make
-
Add to Windows PATH:
- Open System Properties β Environment Variables
- Add to Path:
C:\msys64\mingw64\binC:\msys64\usr\bin
- Restart VS Code terminal
-
Verify Installation:
g++ --version flex --version bison --version make --version
-
Download WinFlexBison:
- Visit: https://sourceforge.net/projects/winflexbison/
- Extract to a folder (e.g.,
C:\Tools\WinFlexBison)
-
Download MinGW-w64:
- Visit: https://www.mingw-w64.org/downloads/
- Install or extract to a folder
-
Add to PATH:
- Add both
binfolders to Windows PATH - Restart VS Code terminal
- Add both
-
Use
mingw32-makeinstead ofmake
-
Install WSL:
wsl --install -
Inside WSL:
sudo apt update sudo apt install build-essential flex bison
-
Access project:
cd /mnt/c/Users/hp/Desktop/Java\ compiler make
Ubuntu/Debian:
sudo apt-get update
sudo apt-get install build-essential flex bisonmacOS:
brew install flex bison-
Navigate to project directory:
cd "C:\Users\hp\Desktop\Java compiler"
-
Build the compiler:
make
This will:
- Generate
lex.yy.cfromscanner.l - Generate
parser.tab.candparser.tab.hfromparser.y - Compile all source files
- Link into
minijava.exe(Windows) orminijava(Linux/macOS)
- Generate
-
Clean build files:
make clean
On Windows, you can also use:
build.bat# Generate parser
bison -d parser.y
# Generate lexer
flex scanner.l
# Compile all files
g++ -std=c++17 -c parser.tab.c -o parser.tab.o
g++ -std=c++17 -c lex.yy.c -o lex.yy.o
g++ -std=c++17 -c semantic_analyzer.cpp -o semantic_analyzer.o
g++ -std=c++17 -c code_generator.cpp -o code_generator.o
g++ -std=c++17 -c interpreter.cpp -o interpreter.o
g++ -std=c++17 -c main.cpp -o main.o
# Link
g++ -std=c++17 -o minijava.exe *.o -lflNote for Windows: If linking fails with -lfl, try removing -lfl or install flex library separately.
./minijava.exe test.javaOr on Linux/macOS:
./minijava test.javaThe compiler will display:
- Parsing status
- Semantic analysis results
- Generated Three-Address Code
- Program execution output
Example:
=== Mini Java Compiler ===
Parsing...
Parse successful!
=== Semantic Analysis ===
Semantic analysis successful!
=== Code Generation (Three-Address Code) ===
Generated Intermediate Code:
t0 = 20 * 2
t1 = 10 + t0
z = t1
if t2 == false goto L0
goto L1
L0:
x = 50
goto L2
L1:
x = 25
L2:
L3:
if t3 == false goto L5
goto L4
L4:
t4 = x + 10
x = t4
goto L3
L5:
=== Program Execution ===
100
=== Compilation Complete ===
File: scanner.l
- Converts source code into tokens
- Recognizes keywords:
class,public,static,void,main,int,boolean,if,else,while - Identifies identifiers, numbers, operators, and punctuation
- Removes whitespace and comments
- Reports lexical errors
Example tokens: IDENTIFIER, NUMBER, PLUS, LT, IF, LPAREN, etc.
File: parser.y
- Uses grammar rules to validate token sequence
- Builds Abstract Syntax Tree (AST) representing program structure
- Handles operator precedence and associativity
- Reports syntax errors with line numbers
AST Nodes:
- Expression nodes:
NumberExpr,BooleanExpr,IdentifierExpr,BinaryExpr - Statement nodes:
DeclStmt,AssignStmt,IfStmt,WhileStmt,PrintStmt - Program structure:
ProgramβClassβMethodβ statements
Files: semantic_analyzer.h/cpp, symbol_table.h
- Type checking: ensures type compatibility
- Scope management: tracks variables in nested scopes
- Declaration checking: ensures variables are declared before use
- Duplicate declaration detection
- Control flow validation: ensures conditions are boolean
Symbol Table:
- Maintains variable information (name, type, scope level)
- Supports nested scopes (for if/while blocks)
- Lookup traverses scopes from inner to outer
Files: code_generator.h/cpp
- Generates Three-Address Code (TAC) from AST
- Creates temporary variables for complex expressions
- Handles control flow with labels and jumps
- Produces intermediate representation suitable for optimization or target code generation
TAC Instructions:
- Assignment:
var = value - Arithmetic:
t0 = a + b - Conditional jump:
if condition == false goto label - Unconditional jump:
goto label - Labels:
L0:,L1:, etc. - Print:
print expr
Files: interpreter.h/cpp
- Executes the program using the AST
- Maintains runtime symbol table with variable values
- Evaluates expressions and executes statements
- Produces program output
Runtime Features:
- Integer and boolean value handling
- Arithmetic and relational operations
- Control flow execution (if/while)
- Division by zero detection
int x; // Integer variable
boolean flag; // Boolean variableint a;
int b;
a = 10;
b = 20;// Arithmetic
x = a + b;
y = a * b - c;
z = a / b;
// Relational (result is boolean)
if (x > 10) { ... }
if (x == y) { ... }// If statement
if (x > 0) {
x = 10;
}
// If-else statement
if (x > 0) {
x = 10;
} else {
x = 20;
}
// While loop
while (x < 100) {
x = x + 1;
}System.out.println(x);
System.out.println(10 + 20);class Main {
public static void main(String[] args) {
// Variable declarations
int x;
int y;
// Statements
x = 10;
y = x * 2;
// Control flow
if (y > 15) {
System.out.println(y);
}
}
}- Arrays
- User-defined methods (except main)
- Classes and inheritance
- Exception handling
- Full Java standard library
- String operations
File: simple_test.java
class Main {
public static void main(String[] args) {
int a;
int b;
int sum;
a = 5;
b = 10;
sum = a + b;
System.out.println(sum);
}
}Output: 15
File: test.java
class Main {
public static void main(String[] args) {
int x;
int y;
int z;
x = 10;
y = 20;
z = x + y * 2;
if (z > 30) {
x = 50;
} else {
x = 25;
}
while (x < 100) {
x = x + 10;
}
System.out.println(x);
}
}Output: 100
Solution: Install Flex
- Windows (MSYS2):
pacman -S mingw-w64-x86_64-flex - Linux:
sudo apt-get install flex - macOS:
brew install flex
Solution: Install Bison
- Windows (MSYS2):
pacman -S mingw-w64-x86_64-bison - Linux:
sudo apt-get install bison - macOS:
brew install bison
Solution: This is handled in scanner.l with %option noyywrap. If error persists, ensure Flex is properly installed.
Solution: Use MSYS2 (recommended) or mingw32-make instead of make
Solution: Ensure g++ version 7.0 or later:
g++ --versionSolution:
- Check file path (use forward slashes or double backslashes on Windows)
- Ensure file exists and has correct permissions
- On Windows, try:
.\minijava.exe test.java
Note: Some shift-reduce conflicts may appear but are resolved by operator precedence rules defined in parser.y.
Language: C++17 Parser Generator: Bison (YACC-compatible) Lexer Generator: Flex (LEX-compatible) Design Patterns:
- Visitor Pattern for AST traversal
- Builder Pattern for AST construction
- Modular Design: Each compiler phase is separated into distinct modules
- AST-Based: Uses Abstract Syntax Tree for intermediate representation
- Visitor Pattern: Enables multiple passes (semantic analysis, code generation, interpretation) without modifying AST nodes
- Scope Management: Proper handling of nested scopes using stack-based symbol table
- Error Reporting: Comprehensive error messages at all phases
This compiler demonstrates:
- Lexical Analysis: Regular expressions, tokenization
- Syntax Analysis: Context-free grammars, parsing algorithms
- Semantic Analysis: Type systems, symbol tables, scope rules
- Code Generation: Intermediate code generation, three-address code
- Compiler Design: Multi-pass compilation, visitor pattern
- Modern C++17 features (smart pointers, variant types)
- Memory safety (RAII, unique_ptr)
- Error handling at each phase
- Well-documented code structure
The compiler includes test files:
test.java- Comprehensive test with all featuressimple_test.java- Minimal test case
The compiler architecture allows easy extension:
- Add new AST nodes in
ast.h - Extend grammar in
parser.y - Add semantic rules in
semantic_analyzer.cpp - Extend code generation in
code_generator.cpp
- Uses Make for automated building
- Supports Windows (MSYS2/MinGW) and Unix-like systems
- Handles dependency tracking for generated files
- "Compilers: Principles, Techniques, and Tools" (Aho, Lam, Sethi, Ullman) - The Dragon Book
- Flex Manual: https://github.com/westes/flex
- Bison Manual: https://www.gnu.org/software/bison/
- C++17 Standard Documentation
This is an educational project for compiler design courses.
Student Project - Mini Java Compiler
Note: This compiler implements a subset of Java for educational purposes. It is not intended to be a complete Java compiler but rather demonstrates compiler construction principles and techniques.