A full compiler for the Dana programming language, implemented in C++17, Flex, Bison, and LLVM 14.
It translates Dana programs into LLVM IR, optimizes them, and produces native executables linked with the runtime library libdanart.a.
- Contributors
- Features
- Installation & Usage
- Dana Runtime Library
- Components
- Testing
- Continuous Integration
- Requirements
- Nikolas Spyropoulos (AM: 03121202)
- Giannis Polychronopoulos (AM: 03121089)
-
Lexical Analysis – implemented with Flex, precise error reporting (line/column + caret).
-
Parsing – LALR(1) grammar in Bison, constructs an AST.
-
Semantic Analysis – scope resolution, type checking, control-flow validation.
-
LLVM IR Code Generation – LLVM backend with support for:
- Functions, recursion, closures (nested functions with captures)
- Variables, arrays (1D and multi-dimensional)
- Control flow:
if,loop,break,continue,return - Procedure calls & function returns
- String operations & runtime calls
-
LLVM IR Optimizations – configurable optimization levels (
-O0to-O3). -
Outputs:
- Intermediate machine code (
.imm) - Assembly (
.asm) - Native executables (linked with
libdanart.a)
- Intermediate machine code (
To install and build the compiler, follow these steps:
- Clone the repository
git clone https://github.com/nspyrop03/DanaCompiler.git- Navigate to
compilerdirectory and build:
cd compiler
make- Make the wrapper script executable
chmod +x ./danac.shThe main frontend is danac.
For convenience, we provide danac.sh, which automates IR generation, optimization, and linking.
cd compiler
./danac.sh ../testing/programs/hello.dana
./a.outThis compiles hello.dana into a.out and runs it.
Intermediate files hello.imm and hello.asm are also produced in ../testing/programs/.
Usage:
./danac.sh <input-file.dana> [-o output-file] [-O0|-O1|-O2|-O3]
./danac.sh -i [-O0|-O1|-O2|-O3] # interactive stdin, print IR to stdout
./danac.sh -f [-O0|-O1|-O2|-O3] # interactive stdin, print ASM to stdout
./danac.sh -h | --help # show this help message
| Option | Description |
|---|---|
-o <file> |
Set output executable name (default: a.out). |
-O0..-O3 |
Select optimization level (default: -O0). |
-i |
Interactive mode, output LLVM IR. |
-f |
Interactive mode, output assembly. |
-h, --help |
Show help message. |
Dana built-ins (writeString, readInteger, etc.) are implemented in C (/compiler/dana_runtime.c).
We built the runtime with:
gcc -c dana_runtime.c -o dana_runtime.o
ar rcs libdanart.a dana_runtime.ogcc -ccompiles to object file.o.ar rcsarchives into static library.a.libdanart.ais linked into all Dana executables byclang.
Each compiler stage lives in its own subdirectory with a dedicated README:
Additional documentation:
- LLVM IR Optimization – explains our optimization pipeline, levels, and LLVM passes used in the final compiler.
- Projects – analysis of large/complex Dana programs in
testing/extra-hard/.
The repository includes a full Python test framework in testing/.
- Lexer tests →
python3 testing/test_lexer.py - Parser tests →
python3 testing/test_parser.py - Semantic tests →
python3 testing/test_semantic.py - LLVM tests →
python3 testing/test_llvm.py
The end-to-end compiler test (python3 testing/test_compiler.py) does the following:
- Builds the compiler.
- Compiles all
.danaprograms intesting/programs/with all optimization flags. - Executes each program 3 times and averages runtime.
- Runs programs with matching
.inputfiles. - Compares outputs against
.resultfiles. - Produces a benchmark table for all optimization levels.
Complex stress-test programs live in testing/extra-hard/.
These are analyzed in detail in PROJECTS.md.
GitHub Actions run tests on every push/PR:
lexer.yml→ runs lexer testsparser.yml→ runs parser testssemantic.yml→ runs semantic testsllvm.yml→ builds LLVM backend and runs tests (LLVM 14)compiler.yml→ builds full compiler and runs all tests
- LLVM 14 (headers +
llvm-config) - Clang 14
- Flex & Bison
- g++ (C++17 or newer)
- Python 3.10+ (for testing)
sudo apt update
sudo apt install llvm-14 llvm-14-dev flex bison g++ python3This downloads and configures LLVM 14.