GithubHelp home page GithubHelp logo

compilers-project's Introduction

Compilers Project

C(-ish) compiler made for a Compilers course by Jonathan Meyer & Sergio Fenoll.

This document contains an overview of the project structure, as well as an overview of the implemented features and instruction on how to build the project itself.

Project structure

parser/:

This folder contains the files generated by the ANTLR. These files are provided in case one cannot/does not wish to generate the ANTLR grammar themselves.

AST/AST.py:

This file contains all the different nodes our AST can contain. Instead of treating each AST node purely as a container that links to its parent and children and with basic information, each different kind of node contains specific information. This is especially helpful when doing operations on the nodes themselves (i.e. optimisations or code generation) since a node with more topical information can do more without requiring information from adjacent nodes.

AST/STT.py:

In this file, the structure of Symbol Tables is defined. We decided to store our Symbol Tables in a tree structure that mimics the different scopes. Anytime a new scope is opened, a new symbol table is generated and linked as necessary. STTNodes consist of some information required for e.g. dotfile generation as well as a python dict that maps identifiers to STTEntries. These entries contain information about the symbols.

AST/ASTBuilder.py:

This is our implementation of the ANTLR Listener that we utilise to create the AST.

Implemented features

LLVM:

  • Types: char, float, int
  • Reserved words: if else, return, while, for, break, continue
  • Functions:
    • Function definitions
  • Local and global variables
  • Operations:
    • Arithmetic operators: +, -, *, /, %
    • Comparison operators: >, <, !=, ==, >=, <=
    • Logical operators: &&, ||
    • Assignment operators: =, +=, -=, *=, /=
  • Arrays:
    • 1-dimensional arrays
    • Array initialization (using initializer lists {})
    • Array access
  • Comments
    • Line comments //
    • Block comments /* */

MIPS:

  • Types: char, float, int
  • Reserved words: if else, return, while, for, continue, break
  • Functions:
    • Function definitions
    • Function calls
  • Local and global variables
  • Operations:
    • Arithmetic operators: +, -, *, /, %
    • Comparison operators: >, <, !=, ==, >=, <=
    • Logical operators: &&, ||
    • Assignment operators: =, +=, -=, *=, /=
  • Arrays:
    • 1-dimensional arrays
    • Array initialization
    • Array access
  • Comments
    • Line comments //
    • Block comments /* */

Error analysis:

  • Typechecking (warning/errors for unsupported operand types)
  • Syntax errors
  • No main
  • Invalid declarations/assignments

Optimisations:

  • Unreachable/dead code:
    • No code after return
    • No code after break/continue
  • Constant folding and propagation
  • No declarations/assignments for unused variables

Testfiles:

  • additiveOperationsTest, multiplicativeOperationsTest --> operations
  • declarationsTest, variableScope --> variables, scopes
  • ifElseTest --> if-else
  • full --> all of the implemented features

Requirements & Build process

Requirements:

  • antlr4
  • antlr4-python3-runtime
  • graphviz
  • python3 (v3.6 or higher)

The shell script build will generate the ANTLR grammar using the C.g4 file, afterwards it will parse a default C file and output a dotfile visualization of the generated AST and STT as well as the LLVM code using c2llvm.py. Finally, the dotfiles are generated using graphviz and the images will be found in the root directory as ast.png and stt.png respectively.

You can execute this shell script file by typing ./build in the root directory of the project.

The shell script test will parse all the C files in the directory testfiles/, output dotfiles and code for each file and generate the images of each tree.

You can execute this shell script by typing ./test [mips/llvm] in the root directory of the project.

compilers-project's People

Contributors

meyerjon avatar sergiofenoll avatar

Watchers

 avatar

Forkers

meyerjon

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.