Skip to content

Repository files navigation

Secure-Mini-Compiler-v1.0

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.

πŸ” Secure Mini Compiler

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.


πŸ“‹ Table of Contents


What This Project Does

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:

  1. Lexical Analysis β€” Breaks your code into tokens
  2. Parsing β€” Checks the grammar and builds an AST (Abstract Syntax Tree)
  3. Semantic Analysis β€” Makes sure variables are declared, types match, etc.
  4. 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.


Project Structure

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

Requirements

For Terminal Mode

  • A C++ compiler that supports C++17
  • Recommended: g++ via MinGW (Windows) or GCC (Linux/Mac)

For GUI Mode


Getting Started

Step 1 β€” Clone the Repository

git clone https://github.com/your-username/SecureMiniCompiler.git
cd SecureMiniCompiler

Step 2 β€” Choose Your Mode

There 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

How to Use (Terminal Mode)

Compile

g++ -std=c++17 main.cpp lexer/lexer.cpp parser/parser.cpp semantic/analyzer.cpp security/security.cpp -o compiler

Run

./compiler test.smc

On Windows:

.\compiler.exe test.smc

Expected Output

==========================================
       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
==========================================

How to Use (GUI Mode)

Option A β€” Using Qt Creator (Recommended)

  1. Open Qt Creator
  2. Go to File β†’ Open File or Project
  3. Select compiler.pro from the project folder
  4. When asked to configure, select Desktop Qt 6.x MinGW 64-bit
  5. Click Configure Project
  6. Press the green Run button or Ctrl + R
  7. The GUI window will open
  8. Click Browse to select your .smc file
  9. Click Compile & Analyze

Option B β€” Using Terminal with Qt

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

What the GUI Shows

  • A file picker to load your .smc source 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

Writing Your Own Test File

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

Sample Test File with Intentional Errors

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.


What Each Phase Does

Phase 1 β€” Lexical Analysis

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.

Phase 2 β€” Parsing

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.

Phase 3 β€” Semantic Analysis

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)

Phase 4 β€” Security Analysis

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 β€” while condition that is always mathematically true

What the Security Analyzer Catches

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 Reference

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

Built With

  • C++17
  • Qt 6.11 (GUI only)
  • Qt Creator (IDE)

Author

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.

About

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.

Resources

Stars

5 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages