Skip to content

A small compiler written in C using Flex and Bison that supports the following features - While loops, for loops, simple arithmetic expressions, array declaration and access. The compiler takes as input a program written in a custom language (like C) and generates assembly code that can run on MARS simulator.

Notifications You must be signed in to change notification settings

nishikantparmariam/Simple-Compiler-in-C

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Compiler-in-C

A small compiler written in C using Flex and Bison.

Features

  • While Loops
  • If Else
  • For Loops
  • Simple Arithmetic Expressions
  • Array declaration and access

Instructions to run

  make

Instructions to run sample program

make test1
make test2

Grammar

PROGRAM: STATEMENTS 
STATEMENTS: %empty
| STATEMENT STATEMENTS 
STATEMENT: WHILE OPENROUNDBRACE VARIABLE LESSTHAN VARIABLE CLOSEROUNDBRACE OPENCURLYBRACE STATEMENTS CLOSECURLYBRACE
| IF OPENROUNDBRACE VARIABLE LESSTHAN VARIABLE CLOSEROUNDBRACE OPENCURLYBRACE STATEMENTS CLOSECURLYBRACE ELSE OPENCURLYBRACE STATEMENTS CLOSECURLYBRACE
| FOR OPENROUNDBRACE VARIABLE EQUAL EXPRESSION PIPE VARIABLE LESSTHAN VARIABLE PIPE VARIABLE EQUAL EXPRESSION CLOSEROUNDBRACE OPENCURLYBRACE STATEMENTS CLOSECURLYBRACE
| VARIABLE EQUAL EXPRESSION 
| VARIABLE OPENSQUAREBRACE VARIABLE CLOSESQUAREBRACE EQUAL EXPRESSION 
| STARTDECL DECLARELIST ENDDECL 
DECLARELIST: DECL VARIABLE OPENSQUAREBRACE NUMBER CLOSESQUAREBRACE SEMICOLON DECLARELIST 
| %empty 
EXPRESSION: X  
| X PLUS X 
| X MINUS X 
| X MULTIPLY X 
| X DIVIDE X
| VARIABLE OPENSQUAREBRACE VARIABLE CLOSESQUAREBRACE
X: VARIABLE 
| NUMBER

This program is an extension of the program discussed in Lab of the course CS327 Compilers offered at IIT Gandhinagar in AY 2020-21 under the guidance of Prof. Bireshwar Das.

About

A small compiler written in C using Flex and Bison that supports the following features - While loops, for loops, simple arithmetic expressions, array declaration and access. The compiler takes as input a program written in a custom language (like C) and generates assembly code that can run on MARS simulator.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published