CMinus Compiler

About

CMinus, a subset of C language, is described in the book Compiler Construction Principles And Practice by Kenneth Louden.

The CMinus compiler is just a simple compiler for a simple language that helps me walk through the development of a compiler front-end, so it's for educational use not practical use. But as you can see in the example below, you can still write some programs in this language. The lex and yacc definition files are given below. You may need to have a look at the syntax of CMinus language defined in the yacc file before you can write program in CMinus.

lex file,yacc file(with sematic rules)

The CMinus compiler compiles source code into assembly code, which is compatible with MASM. The target program runs on Windows in console mode. So to compile and execute your program, you will also need the masm compiler to compile the asm code into machine code.

How to Use

Currently CMinus compiler can only run on Windows platform. The command format is

cminus srcfilename

To complete the compilation, you would also need to:

ml /c /coff /Cp yourprogramname.asm

link /subsystem:console /entry:start yourprogramname.obj

I've written a bat file to do all the works:

cminus.bat

Say if you want to compile the file test.c, type the command:

minus.bat test

The generated files are an asm code file, an intermediate code file, and an executable file.

Get CMinus Compiler

As stated above, CMinus compiler generates asm code. You still need masm compiler and linker to do the rest work.

cminus executable

masm

source code on github

Example

Here a simple program that reads 10 integers from console, uses quick sort to sort them and print the result.

qsort.c

To compile the file and run it, follow the steps:

The image below shows the execution result of the program.

Note

In the qsort.c source file, you will find two strange functions _WINAPI_readconsole and _WINAPI_writeconsole. These two functions are used to interact with the console, their bodies are asm codes, which are embraced between '%{' and '%}'. You don't need to care about how the two functions work, but if you want to read console or write console in you program, these two functions are needed. And you have to handle all the reading and writing formats by yourself. Please take the function readInt and writeInt as examples. Maybe some day I will implement some library functions for the language.




Last updated 8/17/2014