/Mini-Java-Grammar-Parser

Mini Java Grammar Parser implemented in python using ANTLR API

Primary LanguagePython

Mini-Java-Grammar-Parser

Mini Java Grammar Parser and AST Generator implemented in python using ANTLR API

Code Structure

Mini Java Grammar Link which this project is based on:

http://www.cs.tufts.edu/~sguyer/classes/comp181-2006/minijava.html

grammars directory

Includes two grammar files, one leverages attributes which is used in the actual code to help us keep more detailed info about each parsed node.

gen directory

generated files by the ANTLR plugin in pycharm according to grammars written in the grammars directory which were discussed earlier.

ASTGenerator.py

Consists of three main classes:

  • ASTListener which is responsible for parsing the tree of a given MiniJavaGrammar-compliant code and building child and brother relationships needed for an AST.
  • ASTGenerator is responsible to convert the given parse tree with child-brother info to a binary tree which is the essence of an AST.
  • ThreeAddressCode is a class which does somewhat of a mimic of an actual 3-address code generation procedure given the AST.

sample directoty

Consists of four files:

  • Sample.java is a sample Java code which is consistent with the grammar in previously mentioned link.
  • parseTree.png is the parse tree of the sample.java code according to the given grammar in the link. The parse tree was generated using the ANTLR plugin for pycharm.
  • AST.png is the AST generated by the program given the aforementioned code in sample.java based on the given grammar. Note: Blue and green edges delegate brotherhood and parent-child relationships, respectively. Child nodes have (c) mark on them for emphasis.
  • Instructions.txt is the mimical 3-address code generated based on the AST depicted in AST.png

Note: Digraph.gv file was generated during runtime.

How to run the project

This project was implemented using pycharm. You can easily install the ANTLR4 plugin using the IDE.

Other essential dependencies

  • networkx
  • matplotlib