Giter VIP home page Giter VIP logo

mini-java-compiler's Introduction

README

This aim of this project is to construct a compiler for Extended MiniJava language. The compiler is being constructed in various logical phases. The phases are defined as follows:

  1. Lexical Analysis: Separate a stream of characters into different tokens of the language which are described by using regular expressions. I constructed regular expressions to describe token types of eMiniJava and used it as input for JFlex to generate a Lexer.

  2. Syntax Analysis: We define a class hierarchy to represent nodes in the Abstract Syntax Tree (AST) for eMiniJava programs. This is used to create LL(1) version of context free grammar for eMiniJava. A recursive-descent Parser can parse LL(1) gramar in linear time to create an Abstract Syntax Tree.

  3. Name Analysis: In this phase, we map (multiple) occurrences of class, method and variable names in a program, to their (unique) definition.

  4. Type checking: In this phase, we enforce all the constraints not enforced already by the previous phases.

  5. Byte Code Generation: In this phase, the AST is convertde to intermediate code, which is utiliszed by Jasmin assembler to generate class files. The generated class files should be executable on the Java Virtual Machine.

  6. Optimization: We have implemented the following optimisations in our compiler:

a. Constant folding : Every expression which is made up of only constants can be evaluated at compile time. Further if a condition of an if or while statement can be folded to a bool literal, we can directly skip the if or else clause in an if statement and the whole body in a while statement. It can be done for any expression which has only constants as operands and for if and while loops. The correctness of the program is not affected as the value of the expression remains the same.

b. Using a branch construct: By using a branch construct for conditional expressions of control structures we are saving few byte code instructions. It can be used for all control structures which have a conditional expression. The

Interface

Command-line is the primary interface for users to interact with this compiler. The format of command-line interface is as follows:

emjc [options]

As of now, there are six possible options.

  • ––help: Prints a synopsis of options
  • ––pp: Pretty-prints the input file to the standard output
  • ––lex: Generates output from lexical analysis as described in Assignment 2.
  • ––ast: Generates output from syntactic analysis as described in Assignment 3.
  • ––name: Generates output from name analysis as described in Assignment 4
  • ––type: Generates output from type analysis

After executing the compiler with the option ––type for the source file filename.emj the compiler either accepts the program as a good eMiniJava program with printing out the following line:

Valid eMiniJava Program

Or, it prints out a set of errors (such as unknown identifier). Like the previous phases the format of an error message is the following:

: error: where and indicate the beginning position of the error, and details the error.

mini-java-compiler's People

Contributors

bhavinshah7 avatar

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.