Skip to content

akhoz/Christmas-Compiler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

102 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐ŸŽ„ Christmas Compiler

An educational compiler implementation featuring a festive, Christmas-themed programming language with complete front-end analysis and partial MIPS assembly code generation.

Java Gradle MIPS

๐Ÿ“‹ Table of Contents

๐ŸŽฏ Overview

This project implements a complete compiler pipeline from source code to MIPS assembly language. Developed as part of a Compilers course (Winter 2024/2025), it showcases the fundamental phases of compilation with a unique twist: all language keywords are Christmas-themed in Spanish!

Key Accomplishments:

  • โœ… Full lexical analysis with custom tokenization
  • โœ… Complete syntactic analysis with error recovery
  • โœ… Semantic analysis with symbol tables and type checking
  • โœ… Partial MIPS assembly code generation
  • โœ… Tested on MARS/SPIM simulators

โœจ Features

Front-End (Complete Implementation)

๐Ÿ”ค Lexical Analysis

  • Tool: JFlex lexer generator
  • Features:
    • Tokenization of Christmas-themed keywords
    • Support for integers, floats, booleans, characters, and strings
    • Single-line and multi-line comment recognition
    • Comprehensive error reporting with line and column numbers

๐Ÿ“ Syntax Analysis

  • Tool: CUP (Constructor of Useful Parsers)
  • Features:
    • Context-free grammar for the Christmas language
    • Robust error recovery mechanisms
    • Support for nested structures
    • Detailed syntax error reporting

๐Ÿ” Semantic Analysis

  • Features:
    • Symbol Table Management: Multi-scope symbol tables with function and global scopes
    • Type Checking: Validation of type compatibility in expressions and assignments
    • Scope Validation: Proper handling of variable declarations and shadowing
    • Type Inference: Basic type resolution for expressions
    • Function Validation: Parameter count and type verification for function calls

Back-End (Partial Implementation)

โš™๏ธ MIPS Code Generation

  • Implemented:

    • Arithmetic operations (+, -, *, /, %, ^)
    • Control flow structures (if-else, while loops, for loops)
    • Function declarations with prologue/epilogue
    • Stack frame management
    • Register allocation for temporary values
    • Basic I/O operations (print statements)
  • Tested On: MARS (MIPS Assembler and Runtime Simulator) and SPIM

๐ŸŽ… Language Syntax

The Christmas language uses festive Spanish keywords. Here's a quick reference:

Data Types

Keyword Standard Equivalent Type
rodolfo int Integer
bromista float Float
trueno boolean Boolean
cupido char Character
cometa string String

Control Structures

Keyword Standard Equivalent
elfo if
hada else
envuelve while
duende for
varios switch
historia case
ultimo default

Operators

Keyword Operation
navidad Addition (+)
intercambio Subtraction (-)
nochebuena Multiplication (*)
reyes Division (/)
magos Modulus (%)
adviento Power (^)
quien Increment (++)
grinch Decrement (--)

Logical Operators

Keyword Operation
melchor AND (&&)
gaspar OR (||)
baltazar NOT (!)
mary Equals (==)
openslae Not equals (!=)
snowball Less than (<)
evergreen Less than or equal (<=)
minstix Greater than (>)
upatree Greater than or equal (>=)

Delimiters

Keyword Meaning
abrecuento Open block ({ )
cierracuento Close block (})
abreregalo Open parenthesis (()
cierraregalo Close parenthesis ())
abreempaque Open bracket ([)
cierraempaque Close bracket (])
finregalo Semicolon (;)
entrega Assignment (=)

I/O Operations

Keyword Operation
narra Print
escucha Read

Other Keywords

Keyword Meaning
envia Return
corta Break
sigue Colon (:)

๐Ÿ—๏ธ Architecture

The compiler follows a traditional multi-phase architecture:

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  Source Code    โ”‚
โ”‚  (.txt file)    โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ”‚
         โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ Lexical Analysisโ”‚ โ—„โ”€โ”€ JFlex (minijava.jflex)
โ”‚    (Lexer)      โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ”‚ Token Stream
         โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚Syntactic Analysisโ”‚ โ—„โ”€โ”€ CUP (parser.cup)
โ”‚    (Parser)     โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ”‚ Abstract Syntax Tree
         โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ Semantic Analysisโ”‚
โ”‚  - Symbol Table โ”‚
โ”‚  - Type Checkingโ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ”‚ Annotated AST
         โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  Code Generator โ”‚
โ”‚  (MIPS ASM)     โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ”‚
         โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  Output (.asm)  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Key Components

1. Lexer (minijava.jflex)

  • Pattern matching for tokens
  • Whitespace and comment handling
  • Token creation with position tracking

2. Parser (parser.cup)

  • Grammar rules definition
  • Error recovery strategies
  • AST construction
  • Integration with semantic analyzer

3. Symbol Table (SymbolTable.java)

  • Scope management (global and function-level)
  • Variable and function information storage
  • Type information tracking

4. Semantic Analyzer

  • Type checking for expressions and assignments
  • Function call validation
  • Variable declaration and usage verification
  • Scope resolution

5. Code Generator (CodeGenerator.java)

  • MIPS instruction emission
  • Register allocation
  • Stack frame management
  • Label generation for control flow

๐Ÿ› ๏ธ Technologies

  • Language: Java
  • Build Tool: Gradle
  • Lexer Generator: JFlex 1.8.2
  • Parser Generator: CUP (Java Cup)
  • Target Architecture: MIPS32
  • Testing: MARS, SPIM simulators

๐Ÿ“ฆ Installation

Prerequisites

  • Java Development Kit (JDK) 11 or higher
  • Gradle (included via wrapper)
  • MARS or SPIM simulator (for running generated assembly)

Build Instructions

  1. Clone the repository:
git clone <repository-url>
cd Christmas-Compiler/Programa
  1. Build the project:
# On Windows
gradlew.bat build

# On Linux/Mac
./gradlew build
  1. Generate Lexer and Parser (if needed):
# Run the generator
./gradlew run

๐Ÿš€ Usage

Compiling a Program

  1. Create a source file with Christmas language syntax (e.g., program.txt)

  2. Run the compiler:

// In your Java code
Main compiler = new Main();
compiler.test("path/to/program.txt");
  1. Output: The compiler generates an .asm file with the same base name as the input

Example Workflow

# Input: test01.txt
# Output: test01.asm

# Run the generated assembly in MARS:
# 1. Open MARS simulator
# 2. Load test01.asm
# 3. Assemble the program
# 4. Run it

๐Ÿ“ Project Structure

Christmas-Compiler/
โ”œโ”€โ”€ README.md
โ”œโ”€โ”€ info.txt
โ”œโ”€โ”€ Documentacion/
โ””โ”€โ”€ Programa/
    โ”œโ”€โ”€ build.gradle
    โ”œโ”€โ”€ gradlew
    โ”œโ”€โ”€ gradlew.bat
    โ”œโ”€โ”€ settings.gradle
    โ”œโ”€โ”€ gradle/
    โ””โ”€โ”€ src/
        โ”œโ”€โ”€ lex/
        โ”‚   โ”œโ”€โ”€ minijava.jflex       # Lexer specification
        โ”‚   โ”œโ”€โ”€ parser.cup           # Parser grammar
        โ”‚   โ””โ”€โ”€ salida.txt
        โ”œโ”€โ”€ main/java/
        โ”‚   โ”œโ”€โ”€ compiler/
        โ”‚   โ”‚   โ”œโ”€โ”€ Generator.java   # Lexer/Parser generator
        โ”‚   โ”‚   โ”œโ”€โ”€ Main.java        # Entry point
        โ”‚   โ”‚   โ””โ”€โ”€ Tester.java      # Testing utilities
        โ”‚   โ”œโ”€โ”€ destCodeGenerator/
        โ”‚   โ”‚   โ”œโ”€โ”€ CodeGenerator.java  # MIPS code generation
        โ”‚   โ”‚   โ””โ”€โ”€ Operations.java
        โ”‚   โ”œโ”€โ”€ organizer/
        โ”‚   โ”‚   โ””โ”€โ”€ Organize.java
        โ”‚   โ”œโ”€โ”€ parser/
        โ”‚   โ”‚   โ”œโ”€โ”€ Lexer.java       # Generated lexer
        โ”‚   โ”‚   โ”œโ”€โ”€ parser.java      # Generated parser
        โ”‚   โ”‚   โ””โ”€โ”€ sym.java         # Token symbols
        โ”‚   โ”œโ”€โ”€ semanticalAnalysis/
        โ”‚   โ”‚   โ”œโ”€โ”€ ControlStructureOperations.java
        โ”‚   โ”‚   โ”œโ”€โ”€ Function.java
        โ”‚   โ”‚   โ””โ”€โ”€ Variable.java
        โ”‚   โ””โ”€โ”€ tables/
        โ”‚       โ”œโ”€โ”€ FunctionInfo.java
        โ”‚       โ”œโ”€โ”€ SymbolInfo.java
        โ”‚       โ”œโ”€โ”€ SymbolTable.java
        โ”‚       โ””โ”€โ”€ TokenInfo.java
        โ””โ”€โ”€ tests/
            โ”œโ”€โ”€ test01.txt           # Test programs
            โ”œโ”€โ”€ test01.asm           # Generated assembly
            โ”œโ”€โ”€ testIF.txt
            โ””โ”€โ”€ ...

๐Ÿ“ Examples

Example 1: Simple Arithmetic

Christmas Language:

rodolfo _suma_ abreregalo rodolfo _a_, rodolfo _b_ cierraregalo abrecuento
    rodolfo _resultado_ entrega _a_ navidad _b_ finregalo
    envia _resultado_ finregalo
cierracuento

rodolfo _verano_ abrecuento
    rodolfo _x_ entrega 3 finregalo
    rodolfo _y_ entrega 5 finregalo
    rodolfo _total_ entrega _suma_ abreregalo _x_, _y_ cierraregalo finregalo
    narra abreregalo "La suma es: ", _total_ cierraregalo finregalo
cierracuento

Equivalent in Standard Syntax:

int sum(int a, int b) {
    int result = a + b;
    return result;
}

int main() {
    int x = 3;
    int y = 5;
    int total = sum(x, y);
    print("La suma es: ", total);
}

Example 2: Control Flow

Christmas Language:

rodolfo _verano_ abrecuento
    rodolfo _x_ entrega 10 finregalo
    
    elfo abreregalo _x_ minstix 5 cierraregalo abrecuento
        narra abreregalo "x es mayor que 5" cierraregalo finregalo
    cierracuento
    hada abrecuento
        narra abreregalo "x es menor o igual a 5" cierraregalo finregalo
    cierracuento
    
    envuelve abreregalo _x_ minstix 0 cierraregalo abrecuento
        narra abreregalo _x_ cierraregalo finregalo
        _x_ grinch finregalo
    cierracuento
cierracuento

Equivalent in Standard Syntax:

int main() {
    int x = 10;
    
    if (x > 5) {
        print("x es mayor que 5");
    } else {
        print("x es menor o igual a 5");
    }
    
    while (x > 0) {
        print(x);
        x--;
    }
}

Example 3: Arrays

Christmas Language:

rodolfo _verano_ abrecuento
    rodolfo _arr_ abreempaque 5 cierraempaque entrega 0 finregalo
    _arr_ abreempaque 0 cierraempaque entrega 10 finregalo
    _arr_ abreempaque 1 cierraempaque entrega 20 finregalo
    
    narra abreregalo "Primer elemento: ", _arr_ abreempaque 0 cierraempaque cierraregalo finregalo
cierracuento

โš ๏ธ Limitations

Due to course time constraints, the MIPS code generator was partially implemented. The following features are NOT fully supported in the back-end:

โŒ Not Implemented or Incomplete:

  • Advanced array operations: Dynamic allocation, multi-dimensional arrays
  • String operations: String concatenation, manipulation
  • Float arithmetic: Limited floating-point support
  • Switch statements: Code generation for switch-case structures
  • Nested function calls: Complex call chains
  • Memory management: No heap allocation
  • Standard library: No built-in functions beyond basic I/O
  • Optimization: No code optimization passes

โœ… What Works:

  • Basic arithmetic operations (int)
  • Simple control flow (if-else, while, for)
  • Function calls with integer parameters
  • Basic print statements
  • Variable assignments
  • Simple expressions

Note: The front-end (lexical, syntactic, and semantic analysis) is fully functional and correctly validates all language constructs. Only the code generation phase has limitations.

๐Ÿ”ฎ Future Work

Potential enhancements for this project:

  1. Complete MIPS Code Generation:

    • Full array support
    • Complete floating-point operations
    • Switch statement implementation
  2. Optimizations:

    • Constant folding
    • Dead code elimination
    • Register allocation optimization
  3. Extended Features:

    • Structs/Records
    • Pointers
    • Dynamic memory allocation
    • Standard library functions
  4. Development Tools:

    • Interactive debugger
    • Visual AST representation
    • IDE plugin with syntax highlighting
  5. Testing:

    • Comprehensive test suite
    • Performance benchmarks
    • Fuzzing testing

๐Ÿ‘ฅ Authors

Adriรกn Josรฉ Villalobos Peraza & Isaac Ramรญrez Rojas

  • Course: Compiladores e Intรฉrpretes
  • Term: Winter 2024/2025
  • Institution: Instituto Tecnolรณgico de Costa Rica

๐Ÿ“„ License

This project was developed for educational purposes as part of a university course.


๐ŸŽ„ Made with festive spirit and lots of coffee โ˜•

About

Christmas Compiler is an educational compiler built in Java featuring a festive Spanish-themed programming language. It includes full lexical, syntactic, and semantic analysis with partial MIPS code generation, developed for a Compilers course at TEC.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors