Giter Club home page Giter Club logo

mini_c's Introduction

Atul's Mini-C Compiler June 2, 2004

This is a compiler for a subset of the C programming language. It was written in Python during the spring of 2004.

The lexer and parser were constructed using Dave Beazley's PLY (Python Lex-Yacc), an open-source Python implementation of GNU lex/yacc. Stages of compilation (symbol tree generation, type checking, flow control checking, etc) are performed using an object-oriented design pattern called a visitor (GoF 1995). The output is annotated Intel 80x86 assembly, suitable for translation to machine language using the GNU Assembler (GAS).


LANGUAGE FEATURES

The subset of the C language implemented here includes:

* Functions, variables (local and global), and character and
  string literals.

* Assignments (=, +=, etc), standard arithmetic binary and unary
  operators (+,-,*, etc), logical binary and unary operators (!,
  ==, <, etc).

* Support for the C datatypes char and int, as well as implicit
  type conversion between the two (warnings are raised in
  situations of potential data loss). int variables are assumed to
  be signed, and char variables are assumed to be unsigned (this
  is not a violation of the ANSI C standard).

* Control flow elements including while and for loops,
  if/then/else conditionals, and recursion.

* Support for the C keywords extern for functions and variables,
  and static for functions.

* Pointers, including pointer dereferencing (the * operator),
  multiple levels of indirection (double pointers, triple
  pointers, etc), array indexing notation, and the address-of (&)
  operator.

FILES AND DIRECTORIES

lex.py       - Python Lex (this is part of PLY).
yacc.py      - Python Yacc (this is part of PLY).
clex.py      - Mini-C lexer.
cparse.py    - Mini-C parser.  Contains yacc rules for Mini-C and
               defines the classes that make up the AST.
cvisitors.py - Mini-C visitors.  Defines the base visitor class,
               and concrete visitor classes for printing the AST,
               doing symbol table generation, type checking, and
               flow control.
cx86.py      - Intel 80x86 assembly code generator.  Defines a
               virtual stack machine class and the code generator
               visitor.
c.py         - Front-end to the compiler.  This takes in command-
               line options and runs the compiler on the filenames
               you give it.
samples/     - This directory contains foo.c and foo_lib.c, two
               C files that can be compiled by the mini-c
               compiler.  foo_lib.c is intended to be used as
               a library that foo.c accesses, to show
               that mini-c generates assembly that can be linked
               with gcc.

USING THE COMPILER

The syntax for using the mini-c compiler is as follows:

c.py <source-file-1> [[source-file-2] ...] [-ast] [-annotate]

Source files are the C files you want to compile into assembly (.s files).

The '-ast' option generates a file with extension .ast that is a printout of the abstract syntax tree for the source file, after all stages of compilation occur.

The '-annotate' option generates annotated assembly. That is, assembly is generated with comments describing what each instruction does, its relevance to the original C source code, and so forth. Additional comments are inserted to delimit functions, control structures, and so forth.


THE MAKEFILE

The makefile just compiles the two files in the samples/ directory and outputs an executable called 'foo' into this directory (all other output files are also placed here).

Note that while compiling this, you may receive a bunch of warnings mentioning something about an "Illegal character: ''". This is just an artifact of newline translation differences between platforms and should be ignored.

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.