CLiteForge is a Python-based compiler project that parses a simplified C-like language and generates LLVM Intermediate Representation (LLVM IR).
The project includes a full compiler pipeline using PLY for lexical and syntax analysis, an Abstract Syntax Tree (AST) representation, the Visitor design pattern, and llvmlite for LLVM IR generation. It also includes a smaller calculator module that demonstrates expression parsing and LLVM IR generation with arithmetic operations.
Note
This project was originally structured as Mini-Compiler, but CLiteForge is a more professional and descriptive name for the repository.
CLiteForge is an academic compiler construction project focused on translating a simplified C-like language into LLVM IR.
The compiler supports:
- Lexical analysis.
- Syntax analysis.
- AST construction.
- Visitor-based AST traversal.
- LLVM IR generation.
- Global declarations.
- Function definitions.
- Primitive data types.
- Expressions.
- Assignments.
- Function calls.
- Control flow structures.
- Arrays.
- Example C-like programs.
The repository also contains a calculator prototype used to demonstrate how arithmetic expressions can be parsed, represented as an AST, evaluated, and translated into LLVM IR.
Important
This project generates LLVM IR. It does not currently include a full command-line interface for compiling arbitrary files directly from terminal arguments.
CLiteForge/
│
├── Calculator/
│ ├── analisisCalc.py
│ ├── arbolCalc.py
│ ├── parser.out
│ ├── parsetab.py
│ └── README.md
│
├── Compiler/
│ ├── Examples/
│ │ ├── countdown.c
│ │ ├── factorial.c
│ │ ├── fibonacci.c
│ │ ├── hanoiTower.c
│ │ ├── juegoDePalabras.c
│ │ └── taylorSerie.c
│ │
│ ├── analysis.py
│ ├── arbol.py
│ ├── parser.out
│ ├── parsetab.py
│ └── README.md
│
├── Documentation/
│ ├── Actividad 3.2-1.pdf
│ ├── CLite-Syntax.pdf
│ ├── LLVM IR_ Estructuras de control_ Desarrollo de aplicaciones avanzadas de ciencias computacionales (Gpo 501)-1.pdf
│ └── Reporte_Compilador_CLite_LLVM_IR_RodrigoLópezGuerra.pdf
│
├── .gitignore
└── README.md
| Folder | Description |
|---|---|
Compiler/ |
Main CLite compiler implementation. |
Compiler/Examples/ |
Example C-like programs used to test parsing and LLVM IR generation. |
Calculator/ |
Smaller arithmetic expression parser, evaluator, and LLVM IR generator. |
Documentation/ |
Academic documents and reference material related to CLite and LLVM IR. |
| File | Description |
|---|---|
Compiler/analysis.py |
Main compiler file. Defines lexer, parser, grammar rules, test program, file analyzer, and LLVM IR generation flow. |
Compiler/arbol.py |
AST node definitions and visitor implementations, including the LLVM IR generator. |
Calculator/analisisCalc.py |
Arithmetic expression lexer, parser, calculator evaluator, and LLVM IR demo. |
Calculator/arbolCalc.py |
AST and visitor implementation for the calculator module. |
Compiler/Examples/*.c |
Sample CLite-style programs used for compiler testing. |
.gitignore |
Ignores Python cache files and generated PLY parser files. |
| Technology | Purpose |
|---|---|
Python |
Main implementation language. |
PLY |
Lexer and parser generation. |
llvmlite |
LLVM IR generation. |
LLVM IR |
Target intermediate representation. |
AST |
Internal representation of parsed source code. |
Visitor Pattern |
Traverses AST nodes for interpretation or IR generation. |
To run this project, you need:
- Python 3.10 or newer
plyllvmlite
Recommended:
Python 3.11+
Tip
Use a virtual environment so the compiler dependencies stay isolated from your system Python installation.
Clone the repository:
git clone https://github.com/YOUR-USERNAME/CLiteForge.git
cd CLiteForgeCreate a virtual environment:
python -m venv .venvActivate it.
On macOS/Linux:
source .venv/bin/activateOn Windows:
.venv\Scripts\activateIf you use fish shell:
source .venv/bin/activate.fishInstall dependencies:
pip install ply llvmliteGo to the compiler folder:
cd CompilerRun the main compiler script:
python analysis.pyThe script will:
- Build the lexer and parser.
- Parse the built-in CLite sample program inside
analysis.py. - Generate LLVM IR.
- Print the LLVM IR output in the terminal.
- Parse and compile the files inside
Compiler/Examples/.
Note
The current script uses an internal data string and then loops through files in the Examples/ folder. It is not yet structured as a CLI tool that receives a file path as an argument.
Go to the calculator folder:
cd CalculatorRun:
python analisisCalc.pyThe calculator module parses the expression:
10 + 5 * 3
It then:
- Builds an AST.
- Generates LLVM IR.
- Evaluates the expression using a visitor-based calculator.
- Prints the LLVM module and result stack.
Expected arithmetic result:
25
CLiteForge follows a classic compiler structure:
Source Code
↓
Lexer
↓
Parser
↓
Abstract Syntax Tree
↓
Visitor Traversal
↓
LLVM IR Generation
The lexer is implemented with PLY Lex.
It recognizes:
- Identifiers.
- Integer literals.
- Float literals.
- String literals.
- Character literals.
- Boolean literals.
- Reserved keywords.
- Operators.
- Delimiters.
- Single-line comments.
- Multi-line comments.
- Preprocessor-style lines.
Supported reserved words include:
int
float
string
bool
char
void
true
false
return
printf
if
else
switch
case
default
break
while
do
for
The parser is implemented with PLY Yacc.
It defines grammar rules for a simplified C-like language.
The parser builds an AST instead of immediately executing the program.
Supported structures include:
- Program items.
- Global declarations.
- Function definitions.
- Function parameters.
- Local declarations.
- Statements.
- Assignments.
- Function calls.
- Return statements.
- Print statements.
- Conditional statements.
- Switch statements.
- While loops.
- Do-while loops.
- For loops.
- Blocks.
- Empty statements.
- Array access.
- Expressions.
| Type | Description |
|---|---|
int |
32-bit integer type in the compiler module. |
float |
Floating-point value. |
string |
Pointer to 8-bit characters in LLVM IR. |
bool |
Boolean value. |
char |
8-bit character value. |
void |
Used for functions that do not return a value. |
+
-
*
/
%
==
!=
<
>
<=
>=
&&
||
!
=
CLiteForge supports several C-like control flow structures:
if (condition)
statement;
else
statement;while (condition)
statement;do {
statement;
} while (condition);for (i = 0; i < 10; i = i + 1) {
statement;
}switch (value) {
case 1:
statement;
break;
default:
statement;
}Important
This compiler is educational and does not aim to implement the complete C language.
int inc(int x)
{
return x + 1;
}
int main()
{
int i, total, arr[5];
total = 0;
i = 0;
for (i = 0; i < 5; i = i + 1) {
arr[i] = inc(i);
total = total + arr[i];
}
if (total > 10)
total = total + 1;
else
total = total - 1;
return total;
}The compiler parses this type of input and generates LLVM IR.
Running:
python analysis.pyprints LLVM IR similar in structure to:
; ModuleID = "prog"
target triple = "unknown-unknown-unknown"
target datalayout = ""
define i32 @"main"()
{
entry:
...
}The exact output depends on the source program being compiled.
The Calculator/ folder contains a smaller version of the compiler pipeline.
It supports integer arithmetic expressions such as:
10 + 5 * 3
The calculator module demonstrates:
- Tokenizing arithmetic expressions.
- Parsing precedence.
- Building an AST.
- Evaluating the AST.
- Generating LLVM IR for arithmetic operations.
Supported calculator operators:
+
-
*
/
%
Note
The calculator module is useful for understanding the compiler architecture before studying the full CLite compiler.
The Documentation/ folder includes academic and reference documents related to:
- CLite syntax.
- Compiler implementation.
- LLVM IR.
- Control flow structures.
- Project report material.
These documents are useful for understanding the theoretical background of the compiler.
PLY generates files such as:
parser.out
parsetab.py
These files are used to speed up parser construction and debug grammar behavior.
The .gitignore file excludes:
__pycache__/
*.pyc
parser.out
parsetab.py
Tip
If the parser behaves strangely after grammar changes, delete parser.out, parsetab.py, and __pycache__/, then run the script again.
Install PLY:
pip install plyInstall llvmlite:
pip install llvmliteDelete generated parser files:
rm -f parser.out parsetab.py
rm -rf __pycache__Then run the compiler again:
python analysis.pyMake sure you run the compiler from inside the Compiler/ folder:
cd Compiler
python analysis.pyThe script expects:
Compiler/Examples/
relative to the compiler file location.
The compiler supports a simplified C-like grammar, not full C.
Check that the source file only uses supported features such as:
- CLite-compatible types.
- Basic declarations.
- Supported loops.
- Supported expressions.
- Supported function definitions.
- Supported array access.
The compiler requires a main function.
Make sure the source program includes:
int main()
{
return 0;
}Future versions could include:
- Command-line interface for compiling custom files.
- Output
.llfiles instead of only printing LLVM IR. - Better semantic analysis.
- Type checking improvements.
- Scope validation improvements.
- More detailed error messages.
- Line and column information in syntax errors.
- Function declaration prototypes.
- String handling improvements.
- Array bounds checking.
- Support for more C-like syntax.
- Unit tests for grammar rules.
- Automated test suite for example programs.
- Cleaner separation between lexer, parser, AST, and IR generation.
- Better documentation for supported CLite grammar.
- Add
requirements.txt. - Add generated LLVM IR examples.
Tip
A strong next step would be adding a CLI such as:
python analysis.py Examples/factorial.c -o factorial.llThis project is useful for learning:
- Compiler construction.
- Lexical analysis.
- Syntax analysis.
- Grammar design.
- AST construction.
- Visitor pattern.
- LLVM IR generation.
- Function compilation.
- Expression handling.
- Control flow generation.
- Array access.
- Type representation.
- Python compiler tooling.
- PLY.
- llvmlite.
This project is publicly available for educational and portfolio review purposes only.
The source code, visual assets, audio, videos, logos, screenshots, documentation, and other project materials may not be used, copied, modified, redistributed, sublicensed, or used commercially without explicit permission from the project authors.
All rights reserved unless otherwise stated.
Important
Some third-party assets, music, libraries, or references may be subject to their own licenses. Those materials remain owned by their original creators and are not covered by this project license.
CLiteForge is an academic compiler project.
It is intended for learning and experimentation with compiler design, parsing, ASTs, and LLVM IR generation.
Caution
This project does not implement the complete C language and should not be used as a production compiler.
