Code Monkey home page Code Monkey logo

sdt-for-javac's Introduction

sdt-for-javac [from 2011]

Syntax-Directed Translation for Javac: Modified Javac to compile a new language (C-Minus-Minus).

Main Points

  • In this project I modified Javac (openjdk-6-src-b22-28_feb_2011) for building a compiler-compiler using Syntax-Directed Translation (SDT).
  • I added a new construct to the Javac parser called grammar, representing a full-fledged Syntax-Directed Definition which maps a CFG to its production rules
  • I then used that grammar to build a compiler for a simple language called C-Minus-Minus (short: CMM) [CMM specs pdf] [CMM samples] [my CMM compiler source code] and convert it to a proprietary assembly language called BASS (BASS documentation + examples).
  • I even patched a bug in Javac (enum parsing was broken) that was in that build (openjdk-6-src-b22-28_feb_2011) [official download link]
  • Within my CMM compiler, the language is defined in the CMMGrammar.
    • NOTE: You see this right - This .java file does not contain a class, interface etc... but instead it defines a public grammar CMMGrammar - That is why I had to make modifications to Javac! ๐Ÿ˜Š

Compiler Steps

The CMMCompiler uses the CMMGrammar to compile everything in a few simple steps.

  1. The CMMParser builds the AST, based on the Grammar rules
    • The parser internally calls the Scanner which generates a token stream
  2. When the AST is ready, we start running through it and execute the grammar-defined rules.
    • The grammar can define an arbitrary amount of grammarpasses, and the CMMGrammar defines two, each with their own production rules (code to be executed when matching the lefthand-side patterns in the AST): prep0 to prepare everything and gen to generate code (using the Gen class)
  3. The CMMGrammar is only 724 lines long (including comments!). Our modification to the Javac parser then takes that and converts it into a regular Java class before letting the Javac code generator worry about the rest.
    • NOTE: You find that the equivalent CMMGrammar class emitted by our modified Javac has over 5000 lines, that's almost 7 times as many lines, and does contain any comments!
  4. I then used this compiler to successfully compile these 7 sample CMM programs to BASS assembly code

You can find the initial proposal and 2011 final report in this folder. NOTE: The report was not very well written (I loved coding those thousands of lines, but report writing I did not enjoy back then...)

sdt-for-javac's People

Contributors

domiii avatar

Stargazers

horlar avatar  avatar Mahmoud Rusty Abdelkader avatar

Watchers

 avatar James Cloos 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.