Coder Social home page Coder Social logo

cs335_project_c_compiler's Introduction

CS335 Compiler Design Project, IIT Kanpur

This repository contains the code for our course project in Compiler Design (CS335) at IIT Kanpur.

The compiler is being developed in stages. Currently, Stage - 1 of the project has been completed.

Basic Details

Source Language : C
Implementation Language : C
Target Language : MIPS Assembly

Building the Project

To build the project :

make

To clear the binaries and compiled source :

make clean

Usage Instructions

Milestone - 2 (Lexer)

In this stage, a standalone scanner is generated in the bin directory on building. The scanner, when fed an input program in our source program (C), outputs a table describing the stream of tokens. It lists the line number and the column number where the token starts, and the lexeme associated with the token. To test the scanner,

./bin/scanner <path_to_C_file>

5 sample test cases have been provided in the tests directory.

NOTE - Preprocessing directives will be ignored by the scanner at this stage of the project.

Milestone - 3 (Parser)

At this stage, a parser is generated in the bin directory on building the project using make command.

Execute the parser using the following command

$./bin/parser ./tests/<test-name>.c

The parser as of now takes a C file as input and parses it. If the parse is unsuccessful then it throws error, otherwise it doesn't give any output.

graph.svg file

This file contains the states and the edges in form of a graph. Currently edge labels are not visible. But after zooming in and hovering the mouse over the edge, the edge label and the label type along with the start and end state of the edge will be visible.

You can also generate graph with edge labels visible, folow the instructions below.

For generating the LR Parser Automata:

1) Execute the following command
$make plot
2) Prompt will appear like below. If you want curved lines then enter y else enter n.
Do you want splines in your graph? (y/n)

If you enter y, then the graph generation will take around 2-3 minutes, so wait till the graph is generated.
Otherwise it will be generated instantly.

3) Another prompt will appear. If you want the edge labels to be visible on the graph then enter y else enter n.
Do you want edge labels to be visible? (y/n)

If you enter n, then a graph with no visible edge labels will be generated in root folder.

On hovering over the edge, you will see the edge label.

A graph.svg file will be generated in the root folder.

Test Files:

The grammar has been designed such that the C program must contain all the declarations first and then the normal statements.
If you does not do so, then the parser will throw error.

Errors in the grammar

There is a shift-reduce conflict in the grammar, which is due to the dangling else condition. We were not able to come-up with a fix for this conflict as of now.
This error emerges when there is an else condition which can be linked to multiple if conditions, and the compiler is not able to figure that out.

In our compiler, this can be avoided if we use '{' '}'. We need to write the body of the if and else statement inside blocks while writing C program.

Milestone - 4 (Semantics)

In this stage, we updated to add symbol table generation code and type checking. We also generated AST for good programs.

Build the program using:

make
./bin/symtab <path_to_C_file> -o <filename_you_want.dot>

Generate the AST using:

dot -Tpng <filename>.dot -o <filename>.png

5 sample test cases have been provided in the tests directory.

Milestone - 6 (Final)

Finally, we created the c compiler for MIPS. For building the compiler follow below procedure:

  1. Download the project on your ubuntu system.
  2. bison (GNU Bison) 3.5.1 and flex 2.6.4 must be on your system.
  3. In the main folder, use the following command.
make

This will generate codegen named file in bin directory.

Now just use the following command and all the files will be generated in the main folder.

./bin/codegen ./tests/<filename>.c

References

For Grammar: https://www.lysator.liu.se/c/ANSI-C-grammar-y.html
For Lexer: https://www.lysator.liu.se/c/ANSI-C-grammar-l.html

cs335_project_c_compiler's People

Contributors

divigarg avatar atreya221 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.