Skip to content

haile9-hash/Java-Compilar

Repository files navigation

Mini Java Compiler

A complete educational compiler for a subset of Java (Mini-Java) implemented in C++ using Flex (lexical analyzer) and Bison (parser generator). This compiler demonstrates all phases of compilation from source code to execution.

πŸ“‹ Table of Contents

  1. Project Overview
  2. Features
  3. Project Structure
  4. Installation & Prerequisites
  5. Building the Compiler
  6. Usage
  7. Compiler Workflow
  8. Supported Language Features
  9. Example Programs
  10. Troubleshooting
  11. Project Details for Teachers

🎯 Project Overview

This Mini Java Compiler is a complete compiler implementation that processes Mini-Java source code through the following phases:

  1. Lexical Analysis - Tokenizes the source code
  2. Syntax Analysis - Builds an Abstract Syntax Tree (AST)
  3. Semantic Analysis - Performs type checking and scope validation
  4. Code Generation - Produces Three-Address Code (TAC)
  5. Interpretation - Executes the program and produces output

The compiler uses modern C++17 features, follows the Visitor pattern for AST traversal, and implements proper error handling at each phase.

✨ Features

Supported Language Constructs

  • Data Types: int, boolean
  • Variables: Declaration and assignment
  • Arithmetic Operators: +, -, *, /
  • Relational Operators: <, >, ==
  • Control Structures: if, if-else, while
  • Output: System.out.println
  • Program Structure: Single class with main method

Compiler Capabilities

  • βœ… Complete lexical analysis with error reporting
  • βœ… Grammar-based parsing with AST construction
  • βœ… Type checking and semantic validation
  • βœ… Scope management with nested scopes
  • βœ… Three-Address Code (TAC) generation
  • βœ… Runtime interpreter for program execution
  • βœ… Comprehensive error messages with line numbers

πŸ“ Project Structure

Java compiler/
β”œβ”€β”€ scanner.l                  # Flex lexical analyzer specification
β”œβ”€β”€ parser.y                   # Bison parser grammar
β”œβ”€β”€ ast.h                      # Abstract Syntax Tree node definitions
β”œβ”€β”€ symbol_table.h             # Symbol table for scope management
β”œβ”€β”€ semantic_analyzer.h/cpp    # Semantic analysis (type checking)
β”œβ”€β”€ code_generator.h/cpp       # TAC code generation
β”œβ”€β”€ interpreter.h/cpp          # Runtime interpreter
β”œβ”€β”€ main.cpp                   # Main compiler driver
β”œβ”€β”€ Makefile                   # Build configuration
β”œβ”€β”€ build.bat                  # Windows build script
β”œβ”€β”€ test.java                  # Sample test program
β”œβ”€β”€ simple_test.java           # Simple test program
└── README.md                  # This file

Generated Files (created during build)

  • lex.yy.c - Generated lexer from scanner.l
  • parser.tab.c - Generated parser from parser.y
  • parser.tab.h - Parser header file
  • *.o - Object files
  • minijava.exe - Final executable

πŸ› οΈ Installation & Prerequisites

Required Tools

To build and run this compiler, you need:

  1. C++ Compiler - g++ with C++17 support (version 7.0 or later)
  2. Flex - Lexical analyzer generator
  3. Bison - Parser generator
  4. Make - Build automation tool (optional but recommended)

Installation for Windows (VS Code Terminal)

Option 1: MSYS2 (Recommended - Easiest Setup)

  1. Download and Install MSYS2

  2. Open MSYS2 Terminal (not VS Code terminal initially)

  3. Update package database:

    pacman -Syu
  4. Install required tools:

    pacman -S mingw-w64-x86_64-gcc mingw-w64-x86_64-flex mingw-w64-x86_64-bison make
  5. Add to Windows PATH:

    • Open System Properties β†’ Environment Variables
    • Add to Path:
      • C:\msys64\mingw64\bin
      • C:\msys64\usr\bin
    • Restart VS Code terminal
  6. Verify Installation:

    g++ --version
    flex --version
    bison --version
    make --version

Option 2: WinFlexBison + MinGW-w64

  1. Download WinFlexBison:

  2. Download MinGW-w64:

  3. Add to PATH:

    • Add both bin folders to Windows PATH
    • Restart VS Code terminal
  4. Use mingw32-make instead of make

Option 3: WSL (Windows Subsystem for Linux)

  1. Install WSL:

    wsl --install
  2. Inside WSL:

    sudo apt update
    sudo apt install build-essential flex bison
  3. Access project:

    cd /mnt/c/Users/hp/Desktop/Java\ compiler
    make

Installation for Linux/macOS

Ubuntu/Debian:

sudo apt-get update
sudo apt-get install build-essential flex bison

macOS:

brew install flex bison

πŸ”¨ Building the Compiler

Using Make (Recommended)

  1. Navigate to project directory:

    cd "C:\Users\hp\Desktop\Java compiler"
  2. Build the compiler:

    make

    This will:

    • Generate lex.yy.c from scanner.l
    • Generate parser.tab.c and parser.tab.h from parser.y
    • Compile all source files
    • Link into minijava.exe (Windows) or minijava (Linux/macOS)
  3. Clean build files:

    make clean

Using Windows Batch Script

On Windows, you can also use:

build.bat

Manual Build (if Make doesn't work)

# Generate parser
bison -d parser.y

# Generate lexer
flex scanner.l

# Compile all files
g++ -std=c++17 -c parser.tab.c -o parser.tab.o
g++ -std=c++17 -c lex.yy.c -o lex.yy.o
g++ -std=c++17 -c semantic_analyzer.cpp -o semantic_analyzer.o
g++ -std=c++17 -c code_generator.cpp -o code_generator.o
g++ -std=c++17 -c interpreter.cpp -o interpreter.o
g++ -std=c++17 -c main.cpp -o main.o

# Link
g++ -std=c++17 -o minijava.exe *.o -lfl

Note for Windows: If linking fails with -lfl, try removing -lfl or install flex library separately.

πŸš€ Usage

Compile a Mini-Java Program

./minijava.exe test.java

Or on Linux/macOS:

./minijava test.java

Expected Output

The compiler will display:

  1. Parsing status
  2. Semantic analysis results
  3. Generated Three-Address Code
  4. Program execution output

Example:

=== Mini Java Compiler ===
Parsing...

Parse successful!

=== Semantic Analysis ===
Semantic analysis successful!

=== Code Generation (Three-Address Code) ===

Generated Intermediate Code:

t0 = 20 * 2
t1 = 10 + t0
z = t1
if t2 == false goto L0
goto L1
L0:
x = 50
goto L2
L1:
x = 25
L2:
L3:
if t3 == false goto L5
goto L4
L4:
t4 = x + 10
x = t4
goto L3
L5:

=== Program Execution ===
100

=== Compilation Complete ===

πŸ”„ Compiler Workflow

Phase 1: Lexical Analysis (Scanner)

File: scanner.l

  • Converts source code into tokens
  • Recognizes keywords: class, public, static, void, main, int, boolean, if, else, while
  • Identifies identifiers, numbers, operators, and punctuation
  • Removes whitespace and comments
  • Reports lexical errors

Example tokens: IDENTIFIER, NUMBER, PLUS, LT, IF, LPAREN, etc.

Phase 2: Syntax Analysis (Parser)

File: parser.y

  • Uses grammar rules to validate token sequence
  • Builds Abstract Syntax Tree (AST) representing program structure
  • Handles operator precedence and associativity
  • Reports syntax errors with line numbers

AST Nodes:

  • Expression nodes: NumberExpr, BooleanExpr, IdentifierExpr, BinaryExpr
  • Statement nodes: DeclStmt, AssignStmt, IfStmt, WhileStmt, PrintStmt
  • Program structure: Program β†’ Class β†’ Method β†’ statements

Phase 3: Semantic Analysis

Files: semantic_analyzer.h/cpp, symbol_table.h

  • Type checking: ensures type compatibility
  • Scope management: tracks variables in nested scopes
  • Declaration checking: ensures variables are declared before use
  • Duplicate declaration detection
  • Control flow validation: ensures conditions are boolean

Symbol Table:

  • Maintains variable information (name, type, scope level)
  • Supports nested scopes (for if/while blocks)
  • Lookup traverses scopes from inner to outer

Phase 4: Code Generation

Files: code_generator.h/cpp

  • Generates Three-Address Code (TAC) from AST
  • Creates temporary variables for complex expressions
  • Handles control flow with labels and jumps
  • Produces intermediate representation suitable for optimization or target code generation

TAC Instructions:

  • Assignment: var = value
  • Arithmetic: t0 = a + b
  • Conditional jump: if condition == false goto label
  • Unconditional jump: goto label
  • Labels: L0:, L1:, etc.
  • Print: print expr

Phase 5: Interpretation

Files: interpreter.h/cpp

  • Executes the program using the AST
  • Maintains runtime symbol table with variable values
  • Evaluates expressions and executes statements
  • Produces program output

Runtime Features:

  • Integer and boolean value handling
  • Arithmetic and relational operations
  • Control flow execution (if/while)
  • Division by zero detection

πŸ“ Supported Language Features

Data Types

int x;        // Integer variable
boolean flag; // Boolean variable

Variables

int a;
int b;
a = 10;
b = 20;

Expressions

// Arithmetic
x = a + b;
y = a * b - c;
z = a / b;

// Relational (result is boolean)
if (x > 10) { ... }
if (x == y) { ... }

Control Structures

// If statement
if (x > 0) {
    x = 10;
}

// If-else statement
if (x > 0) {
    x = 10;
} else {
    x = 20;
}

// While loop
while (x < 100) {
    x = x + 1;
}

Output

System.out.println(x);
System.out.println(10 + 20);

Program Structure

class Main {
    public static void main(String[] args) {
        // Variable declarations
        int x;
        int y;
        
        // Statements
        x = 10;
        y = x * 2;
        
        // Control flow
        if (y > 15) {
            System.out.println(y);
        }
    }
}

Not Supported (Future Extensions)

  • Arrays
  • User-defined methods (except main)
  • Classes and inheritance
  • Exception handling
  • Full Java standard library
  • String operations

πŸ’‘ Example Programs

Example 1: Simple Arithmetic

File: simple_test.java

class Main {
    public static void main(String[] args) {
        int a;
        int b;
        int sum;
        
        a = 5;
        b = 10;
        sum = a + b;
        
        System.out.println(sum);
    }
}

Output: 15

Example 2: Conditional and Loop

File: test.java

class Main {
    public static void main(String[] args) {
        int x;
        int y;
        int z;
        
        x = 10;
        y = 20;
        z = x + y * 2;
        
        if (z > 30) {
            x = 50;
        } else {
            x = 25;
        }
        
        while (x < 100) {
            x = x + 10;
        }
        
        System.out.println(x);
    }
}

Output: 100

πŸ”§ Troubleshooting

Error: "flex: command not found"

Solution: Install Flex

  • Windows (MSYS2): pacman -S mingw-w64-x86_64-flex
  • Linux: sudo apt-get install flex
  • macOS: brew install flex

Error: "bison: command not found"

Solution: Install Bison

  • Windows (MSYS2): pacman -S mingw-w64-x86_64-bison
  • Linux: sudo apt-get install bison
  • macOS: brew install bison

Error: "undefined reference to yywrap"

Solution: This is handled in scanner.l with %option noyywrap. If error persists, ensure Flex is properly installed.

Error: "make: command not found" (Windows)

Solution: Use MSYS2 (recommended) or mingw32-make instead of make

Error: Compilation fails with C++17 features

Solution: Ensure g++ version 7.0 or later:

g++ --version

Error: "Cannot open file"

Solution:

  • Check file path (use forward slashes or double backslashes on Windows)
  • Ensure file exists and has correct permissions
  • On Windows, try: .\minijava.exe test.java

Parser conflicts

Note: Some shift-reduce conflicts may appear but are resolved by operator precedence rules defined in parser.y.

πŸ‘¨β€πŸ« Project Details for Teachers

Technical Implementation

Language: C++17 Parser Generator: Bison (YACC-compatible) Lexer Generator: Flex (LEX-compatible) Design Patterns:

  • Visitor Pattern for AST traversal
  • Builder Pattern for AST construction

Architecture Highlights

  1. Modular Design: Each compiler phase is separated into distinct modules
  2. AST-Based: Uses Abstract Syntax Tree for intermediate representation
  3. Visitor Pattern: Enables multiple passes (semantic analysis, code generation, interpretation) without modifying AST nodes
  4. Scope Management: Proper handling of nested scopes using stack-based symbol table
  5. Error Reporting: Comprehensive error messages at all phases

Educational Value

This compiler demonstrates:

  • Lexical Analysis: Regular expressions, tokenization
  • Syntax Analysis: Context-free grammars, parsing algorithms
  • Semantic Analysis: Type systems, symbol tables, scope rules
  • Code Generation: Intermediate code generation, three-address code
  • Compiler Design: Multi-pass compilation, visitor pattern

Code Quality

  • Modern C++17 features (smart pointers, variant types)
  • Memory safety (RAII, unique_ptr)
  • Error handling at each phase
  • Well-documented code structure

Testing

The compiler includes test files:

  • test.java - Comprehensive test with all features
  • simple_test.java - Minimal test case

Extensibility

The compiler architecture allows easy extension:

  • Add new AST nodes in ast.h
  • Extend grammar in parser.y
  • Add semantic rules in semantic_analyzer.cpp
  • Extend code generation in code_generator.cpp

Build System

  • Uses Make for automated building
  • Supports Windows (MSYS2/MinGW) and Unix-like systems
  • Handles dependency tracking for generated files

πŸ“š References

πŸ“„ License

This is an educational project for compiler design courses.

πŸ‘€ Author

Student Project - Mini Java Compiler


Note: This compiler implements a subset of Java for educational purposes. It is not intended to be a complete Java compiler but rather demonstrates compiler construction principles and techniques.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published