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.
The grammar supports:
- Global and local variables of basic types:
int,bool,char - Global constants
- One-dimensional arrays
- Sets
- Global methods
- Assignment
if-elsedo-whilebreakandcontinuereadandprint
ord,chr– number/character conversionlen– array lengthadd– add an element to a setaddAll– add all array elements to a seteol– newline constant
- 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
- 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
Compiler– entry point; runs all compiler phases and initializes the symbol tableSemanticPass– performs semantic analysis by traversing the ASTCodeGenerator– generates bytecode during AST traversalActParsCounter– helper visitor for counting actual method parametersMjTest– lexical analyzer testingMjParserTest– lexical and syntax analyzer testing
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