CE305 - MiniJava Compiler Project: University of Essex, March 2019

Verified

Added on  2023/04/06

|4
|450
|88
Project
AI Summary
This document details the implementation of a MiniJava compiler, developed as a project for CE305 at the University of Essex. The compiler processes MiniJava source code through three main stages: parsing and syntax analysis using ANTLR4, static semantics and symbol table construction, and bytecode generation. It includes features such as error reporting, type checking, and handling of variable initialization. The project also provides instructions on building the compiler from source, requiring Java 1.8, ANTLR4, and the ASM Java library. The compiler generates bytecode compatible with Java 1.1 and includes specifications for basic language notions, control flow structures, and additional features like abstract syntax trees and local variables. The assignment fulfills requirements of using ANTLR with Java, generating Python code, and including modular code with comprehensive documentation. Desklib provides this and other solved assignments for students.
Document Page
MiniJava-Compiler
=================
The compiler was written as the main project in an assignment
Run
---
minijavac requires java 1.8 to run.
Minijava source code can be compiled as follows:
java -jar minijavac.jar [source-file-name] that is you want for compiler purpose
The generated byte code is backwards compatible with java 1.1 and will run on any version of java.
java [main-class-name]
Project Description
______________________
Compilers operate in three main stages:
1. Parse source files to generate a parse tree. Report any syntax errors as another compiler working.
2. Traverse the parse tree, building a symbol table from the source code and reporting all semantic errors
3. Generate code in the target language
1
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
### Parsing and Syntax Analysis ###
To parse minijava source code, a [parser]() was generated with the Antlr4 parser generator tool. Antlr
generates a parser in java for the given minijava grammar.
The parser attempts to generates a parse tree given some minijava source code.
If any syntax errors are encountered during parsing, an error message is generated [underlining]() the
offending tokens.
If there are no syntax errors, the parser builds a parse tree and the compiler proceeds to semantic analysis.
### Static Semantics and the Symbol Table ###
Semantic analysis proceeds in the following fashion.
1. A symbol table is constructed be traversing the parse tree. During this phase, the program is checked for:
1. [Duplicate klasses]()
2. [Cyclic inheritance errors]()
3. [Declaration before use]()
2. The program is [type checked]() to grant as many static typing Type_system#Static_type-checking
guarantees
3. Variables are checked to ensure that they are [initialized before use]().
For a given method fun() taking no arguments and returning an integer:
2
Document Page
```java
int fun(){
int sum; //declaration
sum=0; //initialization
return sum; //use
}
```
should compile, while
```java
int fun(){
int sum; //declaration
return sum; //error, variable x may not have been initialized.
}
```
### Bytecode Generation ###
This is where the [magic](CodeGenerator Documentation) happens. The parse tree is traversed a final time
in order to generate byte code.
[//]: # (The ObjectWeb ASM java libraries is used to generate class files.
[//]: # (Generating bytecode, even with ASM, is akin to [printing assembly]
3
Document Page
Build
-----
Building the tool from source requires the [Antlr4 java library] and the [ASM java library]
0. Copy the project in your folder
1. Download [Antlr4](http://www.antlr.org/download/antlr-4.4-complete.jar) and [ASM] asm-5.0.3-
bin.zip)
2. Unzip asm-5.0.3-bin.zip and add asm-5.0.3/lib/all/asm-all-5.03.jar and the antlr jar to your path.
3. Generate the lexer and parser by feeding Antlr4 the [minijava grammar]( Minijava.g4).
`java -jar ~/path/to/antlr-4.4-complete.jar -visitor Minijava.g4`
4. compile with the java 1.8 compiler
`javac *.java`
And you're done. If you want to package it as a jar file:
jar cf minijavac.jar *.class
4
chevron_up_icon
1 out of 4
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]