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
- Implemented using JFlex
- Recognition of keywords, identifiers, literals and operators
- Comment handling
- Lexical error reporting
- Specification file mjlexer.lex
- 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
- 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.
- 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.
- int
- char
- bool
- enum
- One dimensional arrays
- Global variables
- Local variables
- Constants
- Enums
- Methods
- Assignment
- Method calls
- if else
- break
- continue
- read
- return
- ord – converts char to int
- chr – converts int to char
- len – returns array length
- eol – end of line constant
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
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
- 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.
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