A lightweight compiler in C++ that performs lexical analysis, parsing, semantic analysis, and static security checks (division by zero, uninitialized variables, infinite loops) with an optional Qt GUI.
A lightweight compiler built in C++ that translates a simplified programming language while catching security vulnerabilities at compile time. This project was built as part of a Compiler Construction course and covers all major phases of a compiler β from lexical analysis to static security analysis β with an optional Qt-based GUI.
- What This Project Does
- Project Structure
- Requirements
- Getting Started
- How to Use (Terminal Mode)
- How to Use (GUI Mode)
- Writing Your Own Test File
- What Each Phase Does
- What the Security Analyzer Catches
- File Reference
Most compilers just check if your code is syntactically correct. This one goes further β it also checks if your code is safe. It runs your source file through four phases:
- Lexical Analysis β Breaks your code into tokens
- Parsing β Checks the grammar and builds an AST (Abstract Syntax Tree)
- Semantic Analysis β Makes sure variables are declared, types match, etc.
- Security Analysis β Catches division by zero, uninitialized variables, and infinite loops
You can run it through the terminal or through a graphical interface built with Qt.
SecureMiniCompiler/
β
βββ main.cpp # Terminal entry point
βββ main_gui.cpp # GUI entry point (Qt)
βββ compiler.pro # Qt project file
βββ test.smc # Sample source file to test the compiler
β
βββ lexer/
β βββ lexer.h # Token types and Lexer class declaration
β βββ lexer.cpp # Tokenizer implementation
β
βββ parser/
β βββ parser.h # Parser class declaration
β βββ parser.cpp # Recursive descent parser
β
βββ ast/
β βββ ast.h # All AST node definitions
β
βββ semantic/
β βββ analyzer.h # Semantic analyzer declaration
β βββ analyzer.cpp # Scope checking, type checking
β
βββ security/
β βββ security.h # Security analyzer declaration
β βββ security.cpp # Static analysis checks
β
βββ utils/ # Reserved for future utilities
- A C++ compiler that supports C++17
- Recommended:
g++via MinGW (Windows) or GCC (Linux/Mac)
- Qt 6.x β Download from https://www.qt.io/download-qt-installer
- During Qt installation make sure you select:
- Qt 6.x β MinGW 64-bit
- Qt Creator
git clone https://github.com/your-username/SecureMiniCompiler.git
cd SecureMiniCompilerThere are two ways to run this compiler. Pick whichever works for you:
- Terminal Mode β Simpler, no extra installs needed
- GUI Mode β Requires Qt, but gives a visual interface with phase indicators
g++ -std=c++17 main.cpp lexer/lexer.cpp parser/parser.cpp semantic/analyzer.cpp security/security.cpp -o compiler./compiler test.smcOn Windows:
.\compiler.exe test.smc==========================================
Secure Mini Compiler v1.0
==========================================
Source file loaded: test.smc
[ Phase 1 ] Lexical Analysis...
Tokens generated: 52
[ Phase 2 ] Parsing...
Statements found: 7
[ Phase 3 ] Semantic Analysis...
Semantic analysis passed with no errors.
[ Phase 4 ] Security Analysis...
===== Security Analysis Report =====
[ERROR] variable 'z' is used before being initialized
[ERROR] division by zero detected
[WARNING] infinite loop detected, while condition is always true
====================================
Total issues found: 3
==========================================
Compilation Complete
==========================================
- Open Qt Creator
- Go to
FileβOpen File or Project - Select
compiler.profrom the project folder - When asked to configure, select Desktop Qt 6.x MinGW 64-bit
- Click Configure Project
- Press the green Run button or
Ctrl + R - The GUI window will open
- Click Browse to select your
.smcfile - Click Compile & Analyze
Make sure Qt's MinGW is in your PATH first:
set PATH=C:\Qt\Tools\mingw1310_64\bin;C:\Qt\6.11.0\mingw_64\bin;%PATH%Then:
qmake compiler.pro
mingw32-make
.\release\SecureMiniCompiler.exe- A file picker to load your
.smcsource file - Four phase indicator boxes that turn green (pass), red (error), or yellow (warning)
- A live output console showing all results
- Color coded error and warning messages
Create a file with a .smc extension. The language supports:
| Feature | Example |
|---|---|
| Integer variable | int x = 10; |
| Float variable | float y = 3.14; |
| Assignment | x = x + 1; |
| If statement | if (x > 0) { ... } |
| If-else | if (x > 0) { ... } else { ... } |
| While loop | while (x > 0) { ... } |
| Return | return x; |
| Operators | + - * / == != < > <= >= |
| Comments | // this is a comment |
int x = 10;
float y = 3.14;
int z;
int a = z + 5;
int b = x / 0;
int c = y + 1;
while (1 > 0) {
x = x + 1;
}
return x;
This file will trigger all four types of issues the compiler can catch.
Reads the source file character by character and converts it into a list of tokens. A token is the smallest meaningful unit β a keyword, variable name, number, or operator.
Takes the token list and checks if the structure of the code is grammatically correct. Also builds an Abstract Syntax Tree (AST) which is a tree representation of your entire program.
Walks through the AST and checks:
- Variables are declared before use
- No variable is declared twice in the same scope
- Type compatibility (assigning float to int triggers a warning)
Walks through the AST again looking for dangerous patterns:
- Uninitialized variable usage β variable used before being given a value
- Division by zero β literal zero on the right side of a
/operator - Infinite loops β
whilecondition that is always mathematically true
| Issue | Severity | Example |
|---|---|---|
| Uninitialized variable | ERROR | int x; int y = x + 1; |
| Division by zero | ERROR | int z = a / 0; |
| Infinite loop | WARNING | while (1 > 0) { ... } |
| File | Purpose |
|---|---|
main.cpp |
Terminal mode entry point, chains all four phases |
main_gui.cpp |
Qt GUI entry point, same phases with visual output |
compiler.pro |
Qt build configuration file |
lexer/lexer.h |
Token types enum and Lexer class header |
lexer/lexer.cpp |
Full tokenizer implementation |
parser/parser.h |
Parser class header |
parser/parser.cpp |
Recursive descent parser implementation |
ast/ast.h |
All AST node structs (VarDecl, If, While, BinaryOp, etc.) |
semantic/analyzer.h |
Semantic analyzer header |
semantic/analyzer.cpp |
Scope stack, type checking, variable tracking |
security/security.h |
Security analyzer header with SecurityIssue struct |
security/security.cpp |
Static analysis checks implementation |
test.smc |
Sample source file for testing |
- C++17
- Qt 6.11 (GUI only)
- Qt Creator (IDE)
Rehan Khan Built as a 6th semester Compiler Construction course project.
Β© 2026 Rehan Khan. All rights reserved. Unauthorized copying, distribution, or modification of this project without explicit permission is prohibited.