Skip to content

aannjjiiccaa/mikrojava_compiler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MikroJava Compiler

This project implements a compiler for the MikroJava programming language, translating syntactically and semantically correct programs into bytecode executed on the MikroJava Virtual Machine (MJVM).

The compiler covers all major compilation phases:

  • Lexical analysis (JFlex)
  • Syntax analysis and parsing (CUP)
  • Semantic analysis using an Abstract Syntax Tree (AST)
  • Bytecode generation for MJVM

The compiler includes detailed error reporting for lexical, syntactic, and semantic errors, ensuring correct and reliable program translation.

All required libraries are included in the lib/ directory: JFlex, cup_v10k, symboltable, mj-runtime, log4j.


Language Features

The grammar supports:

  • Global and local variables of basic types: int, bool, char
  • Global constants
  • One-dimensional arrays
  • Sets
  • Global methods

Supported Statements

  • Assignment
  • if-else
  • do-while
  • break and continue
  • read and print

Built-in Methods and Constants

  • ord, chr – number/character conversion
  • len – array length
  • add – add an element to a set
  • addAll – add all array elements to a set
  • eol – newline constant

Compiler Phases

  • Lexical Analysis – tokenization of source code using JFlex
  • Syntax Analysis – LALR parsing and AST construction using CUP
  • Semantic Analysis – symbol table management and type checking using Visitor pattern
  • Code Generation – generation of MJVM-compatible bytecode

Tools and Libraries

  • JFlex – lexical analyzer generator
  • CUP – parser generator and AST construction
  • log4j – logging and error reporting
  • symboltable – symbol table implementation
  • mj-runtime – execution and debugging of generated bytecode

Project Structure

  • Compiler – entry point; runs all compiler phases and initializes the symbol table
  • SemanticPass – performs semantic analysis by traversing the AST
  • CodeGenerator – generates bytecode during AST traversal
  • ActParsCounter – helper visitor for counting actual method parameters
  • MjTest – lexical analyzer testing
  • MjParserTest – lexical and syntax analyzer testing

Tests

The test directory contains various test programs covering:

  • Grammar rules and error recovery
  • Arrays and set operations
  • map, read, print
  • Control flow (if-else, do-while, break, continue)
  • Built-in methods (ord, chr)

Each test includes output files for:

  • disassembly
  • execution
  • debug execution

About

MikroJava Compiler

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors