Skip to content

A compiler implementation for the MiniJava language (a subset of Java)

Notifications You must be signed in to change notification settings

frinipanteliadi/Mini-Java-Compiler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

65 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MiniJava Compiler

My implementation of a compiler for the MiniJava language (a small subset of Java).

Its development process consists of three different projects, where each one served as an assignment for the Department of Informatics and Telecommunications undergraduate Compilers course.

What is it? 🤔

The compiler is a program that converts instructions written in the MiniJava language into the intermediate representation used by the LLVM Compiler Project (LLVM Language Reference Manual).

Its operation includes the following stages:

  1. Parsing the MiniJava source files in order to generate a parse tree, detect and report syntax errors.
  2. Traversing the parse tree using the Visitor pattern, creating a symbol table and reporting semantic errors.
  3. Generating code in the LLVM assembly language using, once again, the Visitor pattern.

How does it work? 🤷‍♀️

Semantic Analysis and Type Checking

The compiler uses a symbol table for the purpose of keeping track of information for the different entities found within the source code. Specifically,

  • regarding the classes, it records their name as well as both the fields and methods they contain,
  • regarding the methods, it records their variables (local and arguments),
  • regarding the variables, it records their type and whether they've been initialized or not.

Afterwards, the source code is type checked.

If at least one of MiniJava's rules is being broken, the compilation process terminates and a message, that describes the error, is displayed.

Intermediate Code Generation

By using once again the Visitor pattern alongside a subset of LLVM's instructions, we produce the intermediate representation used by the LLVM compiler project for the original MiniJava code that was provided.

What technologies were used? 🖥

  • Java
  • JavaCC
  • JTB
  • JFlex
  • Java CUP

How do I run it? 🏃‍♀️

We can run each one of the projects that represent the final compiler separately. Specifically,

  • for Project 2, which covers the Semantic Analysis phase, we simply type the following on the command line prompt:

    make -C Project\ 2
    java Main <source_code>.txt

    <source_code>.txt is a .txt file that holds the MiniJava code we wish to use.

    If no errors have been found, information about the source file's classes will be displayed on the screen. Otherwise, a message explaining what the first error was and where it was found will appear.

  • for Project 3, which covers both the Type Checking and the Intermediate Code Generation phases, we type the following:

    make -C Project\ 3
    java Main <input_file>.java

    <input_file.java>* is a .java file that holds the Miniava code we wish to compile.

    After all of the above have been executed, a .ll file named <input_file>.ll will have been created. To execute it, we run the following commands:

    clang -o <name_of_result_file> <input_file>.ll
    ./<result_file>

    When we're done with running both projects, we remove all of the intermediate files that have been created by running:

    make clean -C Project\ 2
    make clean -C Project\ 3

Can I see it? 📸

Example of a successful Semantic Analysis (Project 2)

Project2-Example

Example of an unsuccessful Semantic Analysis (Project 2)

Project2-ErrorExample

Example of generating and executing code in the LLVM assembly language (Project 3)

Project3-Example(Part 1)

Project 3-Example(Part 2)

About

A compiler implementation for the MiniJava language (a subset of Java)

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages