Skip to content

stakicmarija/mikrojava_compiler

Repository files navigation

MikroJava Compiler – PP1 Project

This project implements a compiler for the MikroJava programming language as part of the Programming Languages and Compilers 1 course.

The compiler translates syntactically and semantically correct MikroJava programs into bytecode executable on the MikroJava Virtual Machine (MJVM).

The implementation includes the following compilation phases

  • Lexical analysis
  • Syntax analysis
  • Semantic analysis
  • Code generation

Compiler Phases

Lexical Analysis

  • Implemented using JFlex
  • Recognition of keywords, identifiers, literals and operators
  • Comment handling
  • Lexical error reporting
  • Specification file mjlexer.lex

Syntax Analysis

  • Implemented using CUP LALR parser generator
  • Grammar definition of the MikroJava language
  • Construction of the Abstract Syntax Tree AST
  • Basic error recovery
  • Specification file mjparser.cup

Semantic Analysis

  • Implemented using the Visitor pattern over the AST
  • Symbol table management
  • Scope handling
  • Type checking
  • Validation of assignments and expressions
  • Method declaration and call validation
  • Validation of return statements
  • Validation of break and continue
  • Array and enum usage validation
  • Built-in function validation

All semantic errors are reported with detailed diagnostic messages.

Code Generation

  • Generation of bytecode for the MikroJava Virtual Machine
  • Expression evaluation
  • Method calls
  • Control flow instructions
  • Array handling
  • Enum constant access
  • Built-in functions
  • Stack based instruction generation

Generated bytecode can be executed using the MJ runtime environment.

Supported Language Features

Types

  • int
  • char
  • bool
  • enum
  • One dimensional arrays

Declarations

  • Global variables
  • Local variables
  • Constants
  • Enums
  • Methods

Supported Statements

  • Assignment
  • Method calls
  • if else
  • break
  • continue
  • read
  • print
  • return

Built in Methods and Constants

  • ord – converts char to int
  • chr – converts int to char
  • len – returns array length
  • eol – end of line constant

Tools and Libraries

All required libraries are located in the lib directory

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

Project Structure

src

  • rs.ac.bg.etf.pp1

    • Compiler.java
    • SemanticPass.java
    • CodeGenerator.java
    • ActParsCounter.java

spec

  • mjlexer.lex
  • mjparser.cup

test

  • test programs
  • disassembly outputs
  • execution outputs

Running the Compiler

  • 1 Generate lexer and parser from specification files
  • 2 Compile the project
  • 3 Run

java rs.ac.bg.etf.pp1.Compiler test/program.mj

If there are no lexical, syntax or semantic errors, bytecode is generated and can be executed using the MJ runtime.

Project Goal

The goal of this project is to demonstrate understanding of

  • Compiler architecture
  • LALR parsing and grammar specification
  • Abstract Syntax Trees
  • Symbol table implementation
  • Type checking
  • Stack based bytecode generation

Releases

No releases published

Packages

 
 
 

Contributors