You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A Java to x86-64 compiler built from scratch using Flex, Bison, and C++. Takes a subset of Java 17 as input and produces native x86-64 assembly that can be assembled and executed on Linux.
Overview
compilHER implements a complete compilation pipeline — from lexical analysis all the way to executable x86-64 assembly. The compiler was built in four milestones, each adding a new stage to the pipeline.
Java Source (.java)
|
v
Lexer (Flex)
|
v
Parser (Bison)
|
v
AST Construction
|
v
Semantic Analysis + Symbol Table
|
v
Three-Address Code (3AC IR)
|
v
x86-64 Assembly Generation
|
v
Executable Binary
Project Structure
compilHER/
├── include/
│ ├── global_vars.hpp # Global variables and extern declarations
│ ├── node.hpp # AST node structure and methods
│ ├── symbol_table.hpp # Symbol table structures
│ ├── tac.hpp # Three-address code (quad) structure
│ └── x86.hpp # x86-64 code generation structures
│
├── src/
│ ├── lexer.l # Flex lexer for Java tokens
│ ├── semaction.l # Flex file that generates parser actions
│ ├── parser_empty.y # Bison grammar (without semantic actions)
│ ├── main.cpp # Entry point and CLI argument handling
│ ├── node.cpp # AST node methods implementation
│ ├── symbol_table.cpp # Symbol table implementation
│ ├── tac.cpp # 3AC generation and optimization
│ ├── x86.cpp # x86-64 assembly generation
│ ├── allocmem.s # Assembly routine for heap allocation (malloc wrapper)
│ ├── print_func.s # Assembly routine for println
│ └── Makefile
│
├── tests/
│ ├── test_1.java # Knapsack (recursion, arrays)
│ ├── test_2.java # Fibonacci (tail recursion)
│ ├── test_3.java # 100th prime number
│ ├── test_4.java # Memoized Fibonacci (classes, objects)
│ ├── test_5.java # Factorial (basic recursion)
│ ├── test_6.java # Array sum (loops, arrays)
│ ├── test_7.java # Binary search
│ ├── test_8.java # Method overloading
│ ├── test_9.java # Bubble sort
│ ├── test_10.java # Quick sort
│ └── test_11.java # Classes and objects
│
├── scripts/
│ ├── compile.sh # Builds the compiler from source
│ ├── run.sh # Compiles and runs a single test
│ └── script.sh # Batch runs all tests
│
└── out/
├── tac_*.txt # Generated 3AC output per test
├── asm_*.s # Generated assembly per test
├── ast_*.png # Generated AST visualization per test
└── exec_* # Compiled executables per test
Building the Compiler
Prerequisites
Tool
Version
Purpose
flex
>= 2.6
Lexical analysis
bison
>= 3.0
Parsing
g++
>= 9.0
Compiling the compiler
gcc
>= 9.0
Assembling generated code
graphviz
any
AST visualization
Install on Ubuntu/Debian:
sudo apt install flex bison gcc g++ graphviz
Build
cd src/
make
This runs the following steps internally:
Runs semaction.l through Flex to generate parser.y with semantic actions
Runs parser.y through Bison to generate parser.tab.c and parser.tab.h
Runs lexer.l through Flex to generate lex.yy.c
Compiles everything into the WCTC.o binary
Usage
./WCTC.o [options]
Options:
-i, --input <file> Input Java source file
-o, --output <file> Output DOT file for AST (default: tree.gv)
-t, --taco <file> Output file for 3AC (default: tac.txt)
-s, --asm <file> Output file for x86-64 assembly (default: asm.s)
-v, --verbose Print the full parse tree to stdout
-h, --help Show this help message
Example
# Compile a Java file
./WCTC.o -i ../tests/test_5.java -t ../out/tac_5.txt -s ../out/asm_5.s
# Generate AST image
dot -Tpng tree.gv -o ../out/ast_5.png
# Assemble and link
gcc -c ../out/asm_5.s -o ../out/obj_5.o
gcc ../out/obj_5.o -o ../out/exec_5
# Run
../out/exec_5
Running All Tests
cd scripts/
bash script.sh
# Enter number of tests when prompted (1-9)