A complete compiler implementation for the COOL (Classroom Object-Oriented Language) programming language that generates functional MIPS assembly code. This project represents the final version (Tema 3) of a three-stage compiler development process.
© 2024 Adina-Maria Amzarescu.
This compiler was developed as part of a progressive assignment series implementing a complete compiler toolchain:
- Lexical and Syntactic Analysis (ANTLR4 + Java)
- Semantic Analysis (Java)
- Code Generation (StringTemplate + Java) - This Implementation
This project implements a complete compiler pipeline for COOL, including:
- Lexical Analysis: Tokenizes COOL source code using ANTLR4
- Syntactic Analysis: Parses tokens into an Abstract Syntax Tree (AST)
- Semantic Analysis: Type checking, scope resolution, and inheritance validation
- Code Generation: Generates fully functional MIPS assembly code
- CoolLexer.g4: ANTLR4 grammar defining COOL tokens
- Handles keywords, identifiers, literals, operators, and comments
- Includes comprehensive error handling for malformed strings and comments
- CoolParser.g4: ANTLR4 grammar defining COOL syntax
- Generates parse trees for COOL programs
- ASTConstructionVisitor: Converts parse trees to custom AST nodes
Complete set of AST node classes representing COOL language constructs:
- Expressions:
Id,Int,Stringg,Bool,New,ClassMethodCall, etc. - Statements:
Assign,If,While,Case,Let,Block - Definitions:
ClassDef,ClassMethodDef,ClassMemberDef,Formal
- SymbolTable: Global symbol management
- Scope hierarchy:
DefaultScope,ClassSymbol,MethodSymbol - Type system:
TypeSymbol,IdSymbolwith built-in types (Int, String, Bool, Object, IO)
- DefinitionPassVisitor: First pass - builds symbol tables and scopes
- ResolutionPassVisitor: Second pass - type checking and inheritance validation
- Comprehensive error reporting with file names and line numbers
- CodeGenVisitor: Generates MIPS assembly code
- Templates: StringTemplate-based code generation (
cgen.stg) - Supports object-oriented features including inheritance and dynamic dispatch
- Classes: Single inheritance with method overriding
- Types: Built-in types (Int, String, Bool, Object, IO) and user-defined classes
- Expressions: Arithmetic, relational, and logical operations
- Control Flow: if-then-else, while loops, case statements
- Object-Oriented: Method calls (static and dynamic dispatch), attribute access
- Memory Management: Object allocation with
new - Let Expressions: Local variable bindings with scope management
- Object: Base class with
abort(),type_name(),copy()methods - IO: Input/output operations (
out_string(),out_int(),in_string(),in_int()) - String: String manipulation (
length(),concat(),substr()) - Int, Bool: Primitive types
- Lexical errors: Invalid characters, unterminated strings/comments
- Syntax errors: Malformed expressions, missing tokens
- Semantic errors: Type mismatches, undefined identifiers, inheritance cycles
- Static typing with inheritance-based subtyping
- SELF_TYPE support for polymorphic methods
- Type checking for expressions, method calls, and assignments
- Object layout with dispatch tables
- Method dispatch (both static and dynamic)
- String and integer constant pools
- Memory management for object allocation
- Java 8 or higher
- ANTLR4 runtime library
- StringTemplate library for code generation
- QtSpim: MIPS simulator for testing generated code
- COOL Runtime Library: Required for executing compiled programs (provides implementations for built-in classes like Object, IO, String)
This compiler was developed through three progressive stages:
- Foundation with lexical analysis using ANTLR4 grammar and syntactic parsing
- Symbol table construction, scope management, and complete semantic analysis
- Template-based code generation producing executable MIPS assembly
cool/
├── compiler/ # Main compiler driver and semantic analysis
├── lexer/ # Lexical analysis (ANTLR4 generated)
├── parser/ # Syntactic analysis and AST nodes
├── structures/ # Symbol table and type system
└── generator/ # Code generation to MIPS assembly
This compiler serves as a complete reference implementation for educational purposes, demonstrating modern compiler construction techniques including visitor pattern usage, comprehensive error handling, and template-based code generation.