Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

COOL Compiler

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.

Copyright

© 2024 Adina-Maria Amzarescu.

Project Context

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

Overview

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

Architecture

Core Components

1. Lexer (cool.lexer)

  • CoolLexer.g4: ANTLR4 grammar defining COOL tokens
  • Handles keywords, identifiers, literals, operators, and comments
  • Includes comprehensive error handling for malformed strings and comments

2. Parser (cool.parser)

  • CoolParser.g4: ANTLR4 grammar defining COOL syntax
  • Generates parse trees for COOL programs
  • ASTConstructionVisitor: Converts parse trees to custom AST nodes

3. AST Nodes (cool.parser.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

4. Symbol Table (cool.structures)

  • SymbolTable: Global symbol management
  • Scope hierarchy: DefaultScope, ClassSymbol, MethodSymbol
  • Type system: TypeSymbol, IdSymbol with built-in types (Int, String, Bool, Object, IO)

5. Semantic Analysis (cool.compiler)

  • 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

6. Code Generation (cool.generator)

  • CodeGenVisitor: Generates MIPS assembly code
  • Templates: StringTemplate-based code generation (cgen.stg)
  • Supports object-oriented features including inheritance and dynamic dispatch

Language Features

Supported COOL Features

  • 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

Built-in Classes

  • 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

Implementation Details

Error Handling

  • Lexical errors: Invalid characters, unterminated strings/comments
  • Syntax errors: Malformed expressions, missing tokens
  • Semantic errors: Type mismatches, undefined identifiers, inheritance cycles

Type System

  • Static typing with inheritance-based subtyping
  • SELF_TYPE support for polymorphic methods
  • Type checking for expressions, method calls, and assignments

Code Generation Features

  • Object layout with dispatch tables
  • Method dispatch (both static and dynamic)
  • String and integer constant pools
  • Memory management for object allocation

Build Requirements

  • Java 8 or higher
  • ANTLR4 runtime library
  • StringTemplate library for code generation

Runtime Dependencies

  • 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)

Development Timeline

This compiler was developed through three progressive stages:

  1. Foundation with lexical analysis using ANTLR4 grammar and syntactic parsing
  2. Symbol table construction, scope management, and complete semantic analysis
  3. Template-based code generation producing executable MIPS assembly

Project Structure

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.

About

A complete COOL language compiler built in Java using ANTLR4, covering lexing, parsing, semantic analysis, and MIPS assembly code generation via StringTemplate

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages