GithubHelp home page GithubHelp logo

frinipanteliadi / mini-java-compiler Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 4.97 MB

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

Java 99.58% Makefile 0.11% Lex 0.31%
java visitor-pattern minijava minijava-compiler javacup jtb

mini-java-compiler's Introduction

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.