Skip to content

Latest commit

ย 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿš€ Mini Expression Compiler with GTK GUI

A feature-rich arithmetic expression compiler with a GTK3 graphical interface, smart error detection, and assembly-like code generation.

๐Ÿ“‹ Features

  • โœ… 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 .txt files directly
  • โœ… Real-time Compilation - Instant feedback on code validity

๐Ÿ—๏ธ Project Structure

/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

๐Ÿ”ง Prerequisites

Required Tools

  • GCC - C compiler
  • Flex - Lexical analyzer generator
  • Bison - Parser generator
  • GTK3 - GUI library
  • Make - Build tool

Installation

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
make

Ubuntu/Debian:

sudo apt-get update
sudo apt-get install libgtk-3-dev flex bison build-essential
make

Fedora/RHEL/CentOS:

sudo dnf install gtk3-devel flex bison gcc make
make

Quick Makefile Commands:

make install-deps-arch    # Arch Linux
make install-deps         # Ubuntu/Debian
make install-deps-fedora  # Fedora/RHEL

๐Ÿš€ Building the Project

Quick Start

# Build the compiler
make

# Build and run
make run

# Clean build artifacts
make clean

# Rebuild from scratch
make rebuild

Manual Build

# 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

๐Ÿ“– Usage

GUI Interface

  1. Launch the application:

    ./MiniCompilerGUI
  2. Enter code or open a file:

    • Type expressions directly in the input area
    • Click "๐Ÿ“ Open File" to load a .txt file
  3. Compile:

    • Click "โš™๏ธ Compile" button
    • View results in the output area
  4. Other buttons:

    • "๐Ÿ—‘๏ธ Clear" - Clear input and output
    • "โŒ Exit" - Close the application

Supported Syntax

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 comment

๐Ÿงช Testing

Run with Sample Files

Test valid expressions:

# In GUI: Open File โ†’ samples/test_valid.txt โ†’ Compile

Test error detection:

# In GUI: Open File โ†’ samples/test_error.txt โ†’ Compile

Test complex expressions:

# In GUI: Open File โ†’ samples/test_complex.txt โ†’ Compile

Sample Output

Valid 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

๐Ÿ“š Grammar Rules

Tokens (Lexer)

NUMBER    : [0-9]+
ID        : [a-zA-Z_][a-zA-Z0-9_]*
ASSIGN    : =
SEMICOLON : ;
PLUS      : +
MINUS     : -
MUL       : *
DIV       : /
LPAREN    : (
RPAREN    : )

Grammar (Parser)

program    โ†’ statement*
statement  โ†’ ID = expression ;
expression โ†’ expression + term
           | expression - term
           | term
term       โ†’ term * factor
           | term / factor
           | factor
factor     โ†’ NUMBER
           | ID
           | ( expression )

๐Ÿ‘ฅ Team Roles

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)

๐Ÿ” Code Generation

The compiler generates three-address code in assembly-like format:

Instructions:

  • ADD dest, src1, src2 - Addition
  • SUB dest, src1, src2 - Subtraction
  • MUL dest, src1, src2 - Multiplication
  • DIV dest, src1, src2 - Division
  • MOV dest, src - Assignment

Example:

Input:  result = (a + b) * c;

Output:
ADD t0, a, b
MUL t1, t0, c
MOV result, t1

๐Ÿ› Error Handling

The compiler provides intelligent error messages:

  1. Missing operators:

    x = 10 +;
    โ†’ Error: Invalid expression in assignment
    โ†’ Suggestion: Check for missing operators
    
  2. Unmatched parentheses:

    z = (10 + 5;
    โ†’ Error: Missing closing parenthesis ')'
    โ†’ Suggestion: Add ')' to close the expression
    
  3. Invalid characters:

    result = x @ y;
    โ†’ Error: Unknown character or token
    โ†’ Suggestion: Remove invalid characters
    

๐Ÿ“ธ Screenshots

Place your GUI screenshots in the screenshots/ directory:

  • screenshots/gui_main.png - Main interface
  • screenshots/valid_compile.png - Successful compilation
  • screenshots/error_detection.png - Error messages

๐ŸŽ“ Educational Value

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

๐Ÿ“ Makefile Commands

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

๐Ÿ”ฎ Future Enhancements

  • Add if and while statements
  • 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

๐Ÿ“„ License

This is an educational project for learning compiler construction and GUI development.

๐Ÿ‘จโ€๐Ÿ’ป Contributors

Built by a team of 5 members as part of a compiler design course.


Happy Compiling! ๐ŸŽ‰

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages