Coder Social home page Coder Social logo

hyper5phere / simple-c-compiler Goto Github PK

View Code? Open in Web Editor NEW
50.0 3.0 15.0 653 KB

Simple C Compiler written in Python

License: MIT License

Python 97.14% C 2.86%
python lexer parser code-generator semantic-analyzer compiler from-scratch interpreter blog

simple-c-compiler's Introduction

Simple C Compiler written in Python

Simple C Compiler supports a subset of C programming language. Most notably it has only integer typed variables. Nevertheless, it is a fully functional C compiler front-end for generating intermediate representation (IR) three address codes from a C source file. Furthermore, an interpreter program [1] for the three address "assembly" code is provided for all major platforms (Windows, Linux and Mac) to execute the programs with ease.

As the name hints, it is also simple to use! Only one python module needed to start using it!

This compiler implementation is also part of a blog post about designing and implementing compilers, you might wanna check it out!

Requirements

All major operating systems (Windows, Linux and Mac) should be supported.

Make sure Python 3.6 or newer interpreter is installed on your system. Then install the anytree package with pip

pip install anytree

Installation and Testing

For testing the compiler here is a simple test program that prints the 15 first odd numbers

/* 
 * Simple example source code
 * 
 * prints N odd numbers
 */

void main(void) {
    /* all variables need to be declared first */
    int i;
    int j;
    int m;
    int N;
    
    /* ...and then assigned to */
    i = 1;
    j = 1;
    m = 0-1; // syntax only supports binary operations, this is how we get -1

    N = 15; // change me to increase number of odd numbers

    while (i < N * 2 + 1) {
        j = m * j;
        if (j < 0) {
            output(i);
        } else {
            // do nothing, the syntax does not support if without else :^)
        }
        i = i + 1;
    }
}

To compile and run it, type

git clone https://github.com/Hyper5phere/simple-c-compiler.git
cd simple-c-compiler
python compiler.py input/input_simple.c --run

To see all input arguments type

python compiler.py --help

All output of the compiler is stored in the ./output folder and all the input examples can be found from ./input folder. Additionally errors are logged in the ./errors folder if --error-files input flag is used.

Possible Future Improvements

Language support for

  • arrays
  • recursion
  • string types
  • nested functions (not really C though)

References

[1] The interpreter program has been implemented by Romina Jafarian, at Computer Engineering Department of Sharif University of Technology (Tehran) in Fall 2017.

simple-c-compiler's People

Contributors

hyper5phere avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

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