25

Simple Tiny Compiler in C

 4 years ago
source link: https://github.com/zakirullin/tiny-compiler
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

A tiny compiler for a simple synthetic language featuring LL(2) grammar , written in pure C

The compiler consist of typical parts, known as:

The compiler is implemented for educational purposes. Some parts are simplified for the sake of better understanding

Build

$ gcc main.c -o compiler

Usage

$ ./compiler source

An example program for Pythagorean theorem:

cath1 = 3;
cath2 = 4;
hypsquare = cath1 * cath1 + cath2 * cath2;

Execution result:

hypsquare = 25

Generated ASM:

PUSH 3
WRITE cath1
PUSH 4
WRITE cath2
READ cath1
READ cath1
MUL POP, POP
READ cath2
READ cath2
MUL POP, POP
ADD POP, POP
WRITE hypsquare

The language description in EBNF :

program = expr, ";", { program } ;
expr = id, "=", expr | ("+"|"-"), term, { ("+"|"-"), term } ;
term = factor, { ("*"|"/"), factor } ;
factor = "id" | "num" | (expr) ;

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK