A feature-rich arithmetic expression compiler with a GTK3 graphical interface, smart error detection, and assembly-like code generation.
- โ Lexical Analysis - Tokenizes arithmetic expressions
- โ Syntax Parsing - Validates grammar using Bison
- โ Error Detection - Smart error messages with suggestions
- โ Code Generation - Generates three-address assembly-like code
- โ GTK3 GUI - User-friendly graphical interface
- โ
File Upload - Load
.txtfiles directly - โ Real-time Compilation - Instant feedback on code validity
/MiniCompilerGUI
โโโ src/
โ โโโ lexer.l # Flex: tokenization
โ โโโ parser.y # Bison: grammar & syntax checking
โ โโโ codegen.c # Assembly code generator
โ โโโ error_handler.c # Error messages & suggestions
โ โโโ ui.c # GTK GUI implementation
โ โโโ main.c # Entry point & integration
โ
โโโ include/
โ โโโ codegen.h
โ โโโ error_handler.h
โ โโโ ui.h
โ
โโโ samples/ # Test input files
โ โโโ test_valid.txt # Valid expressions
โ โโโ test_error.txt # Error examples
โ โโโ test_complex.txt # Complex expressions
โ
โโโ Makefile # Build automation
โโโ README.md # This file
- GCC - C compiler
- Flex - Lexical analyzer generator
- Bison - Parser generator
- GTK3 - GUI library
- Make - Build tool
Arch Linux (RECOMMENDED - Auto Setup):
# One-command setup (installs deps + builds)
./setup-arch.sh
# Or manual steps:
sudo pacman -S --needed gtk3 flex bison base-devel
makeUbuntu/Debian:
sudo apt-get update
sudo apt-get install libgtk-3-dev flex bison build-essential
makeFedora/RHEL/CentOS:
sudo dnf install gtk3-devel flex bison gcc make
makeQuick Makefile Commands:
make install-deps-arch # Arch Linux
make install-deps # Ubuntu/Debian
make install-deps-fedora # Fedora/RHEL# Build the compiler
make
# Build and run
make run
# Clean build artifacts
make clean
# Rebuild from scratch
make rebuild# Generate lexer
flex src/lexer.l
# Generate parser
bison -d src/parser.y
# Compile
gcc -Wall `pkg-config --cflags gtk+-3.0` -I./include \
lex.yy.c parser.tab.c src/*.c \
-o MiniCompilerGUI \
`pkg-config --libs gtk+-3.0` -lfl
# Run
./MiniCompilerGUI-
Launch the application:
./MiniCompilerGUI
-
Enter code or open a file:
- Type expressions directly in the input area
- Click "๐ Open File" to load a
.txtfile
-
Compile:
- Click "โ๏ธ Compile" button
- View results in the output area
-
Other buttons:
- "๐๏ธ Clear" - Clear input and output
- "โ Exit" - Close the application
Valid Expressions:
x = 10 + 5;
y = x * 2;
z = (x + y) / 3;
result = x + y * z - 10;Operators:
+Addition-Subtraction*Multiplication/Division()Parentheses for grouping=Assignment;Statement terminator
Comments:
// This is a comment
x = 5; // Inline commentTest valid expressions:
# In GUI: Open File โ samples/test_valid.txt โ CompileTest error detection:
# In GUI: Open File โ samples/test_error.txt โ CompileTest complex expressions:
# In GUI: Open File โ samples/test_complex.txt โ CompileValid Code:
โ
Compilation Successful!
; Generated Assembly-like Code
; Three-Address Code Format
MOV x, 15
MOV y, 30
ADD t0, x, y
MOV z, t0
Error Detection:
โ Error at line 2: Invalid expression in assignment
๐ก Suggestion: Check for missing operators or unmatched parentheses
โ Error at line 4: Missing closing parenthesis ')'
๐ก Suggestion: Add ')' to close the expression
NUMBER : [0-9]+
ID : [a-zA-Z_][a-zA-Z0-9_]*
ASSIGN : =
SEMICOLON : ;
PLUS : +
MINUS : -
MUL : *
DIV : /
LPAREN : (
RPAREN : )
program โ statement*
statement โ ID = expression ;
expression โ expression + term
| expression - term
| term
term โ term * factor
| term / factor
| factor
factor โ NUMBER
| ID
| ( expression )
| Member | Responsibility |
|---|---|
| Member 1 | Lexer implementation (lexer.l) |
| Member 2 | Parser implementation (parser.y) |
| Member 3 | Code generator (codegen.c/h) |
| Member 4 | Error handler (error_handler.c/h) |
| Member 5 | GUI & Integration (ui.c, main.c) |
The compiler generates three-address code in assembly-like format:
Instructions:
ADD dest, src1, src2- AdditionSUB dest, src1, src2- SubtractionMUL dest, src1, src2- MultiplicationDIV dest, src1, src2- DivisionMOV dest, src- Assignment
Example:
Input: result = (a + b) * c;
Output:
ADD t0, a, b
MUL t1, t0, c
MOV result, t1
The compiler provides intelligent error messages:
-
Missing operators:
x = 10 +; โ Error: Invalid expression in assignment โ Suggestion: Check for missing operators -
Unmatched parentheses:
z = (10 + 5; โ Error: Missing closing parenthesis ')' โ Suggestion: Add ')' to close the expression -
Invalid characters:
result = x @ y; โ Error: Unknown character or token โ Suggestion: Remove invalid characters
Place your GUI screenshots in the screenshots/ directory:
screenshots/gui_main.png- Main interfacescreenshots/valid_compile.png- Successful compilationscreenshots/error_detection.png- Error messages
This project demonstrates:
- Compiler Design - Lexical analysis, parsing, code generation
- Error Recovery - Graceful error handling
- GUI Development - GTK3 programming
- Build Systems - Makefile automation
- Team Collaboration - Modular architecture
make # Build the compiler
make run # Build and run
make clean # Remove build artifacts
make rebuild # Clean and rebuild
make install-deps # Install deps (Ubuntu/Debian)
make install-deps-fedora # Install deps (Fedora)
make install-deps-arch # Install deps (Arch)
make test # Run with sample input
make help # Show help message- Add
ifandwhilestatements - Support for functions
- Floating-point arithmetic
- Variable type checking
- Optimization passes
- Syntax highlighting in GUI
- Save generated code to file
- Debug mode with step-by-step execution
This is an educational project for learning compiler construction and GUI development.
Built by a team of 5 members as part of a compiler design course.
Happy Compiling! ๐