This project is an implementation of a compiler for the MicroJava programming language, a simple programming language based on Java, developed as part of the Programming Translators 1 course at the School of Electrical Engineering, University of Belgrade. The compiler translates syntactically and semantically correct MicroJava programs into MicroJava bytecode, which can be executed on the MicroJava Virtual Machine (MJVM). Detailed project text can be found in Serbian in pp1_2023_2024_jan.pdf, and specification of MicroJava language in mikrojava_2023_2024_jan.pdf.
The compiler consists of four main phases:
- Lexical Analysis – Tokenizing the source code.
- Syntax Analysis – Checking if the code conforms to MicroJava grammar.
- Semantic Analysis – Ensuring correct usage of variables, types, and function calls.
- Code Generation – Translating validated code into MJVM bytecode.
-
Lexical Analysis
- Uses JFlex to tokenize MicroJava source code.
- Detects identifiers, constants, keywords, operators, and comments.
- Reports lexical errors with precise location information.
-
Syntax Analysis
- Implements a LALR(1) parser using AST-CUP.
- Constructs an Abstract Syntax Tree (AST) from tokenized input.
- Recovers from syntax errors and reports them clearly.
-
Semantic Analysis
- Uses a symbol table to verify variable and function declarations.
- Detects type mismatches, undefined symbols, and invalid expressions.
- Performs contextual checks to ensure program correctness.
-
Code Generation
- Converts the AST into MicroJava bytecode executable on MJVM.
- Supports function calls, loops, and conditional statements.
- Generates
.objfiles for execution on the MicroJava Virtual Machine.
- Java (JDK 1.8) – Main programming language.
- JFlex – For lexical analysis.
- AST-CUP – For syntax analysis and AST generation.
- MJVM Runtime – For executing generated bytecode.
.
├── /bin # Generated files
├── /config # Log4j config file
├── /lib # JAR files for used libraries
├── /logs # Log files
├── /spec # Specifications for lexer and cup tools
├── /src/rs/ac/bg/etf/pp1 # Source code files
| ├── /ast # Source files generated by the CUP tool
| └── /util # Files for Log4j
├── /test # MicroJava programs used for testing
├── build # Build script
└── README.md # Project documentation