This compiler can translate code written in a subset of C to 8086 assembly. Demo in this video. Made for CSE310 (Compiler Sessional) - BUET.
This compiler supports a subset of the C language with the following features:
- Data Types: The compiler handles
int,voidtypes. - Variables: Supports both global and local variable declarations. You can declare multiple variables in a single statement (e.g.,
int x, y;). - Arrays: One-dimensional arrays of
intwith a constant size are supported (e.g.,int data[100];). Array elements can be accessed using an index (e.g.,data[5]). - Literals: The compiler can parse constant integer (
123) values.
Follows standard C precedence rules:
- Arithmetic: Addition
+, subtraction-, multiplication*, division/. - Relational:
==,!=,<,>,<=,>=. - Logical:
&&,||, and!(NOT). - Assignment:
= - Unary: Unary minus (e.g.,
-x) and unary plus (+x). - Increment/Decrement: Post-increment
++(e.g.,i++) and post-decrement--(e.g.,i--).
- Conditionals:
ifandif-elsestatements. - Loops:
forloops (C-stylefor(;;)), andwhileloops. - Statement Blocks: Nested blocks using curly braces
{ ... }are supported to manage scope.
- Declaration & Definition: Supports both function declarations (prototypes) and definitions.
- Parameters: Functions can be defined with multiple parameters or with no parameters.
- Return Values: Functions can return values using the
returnstatement. - Recursion: Recursive function calls are fully supported.
- Printing: Includes a simple built-in
println(variable);statement to print the value of a variable to the console, followed by a newline.
flex 2.6.4(scanner generator)bison 3.8.2(parser generator)g++ 11.4.0
emu8086 was used to run the generated assembly.
specs: specifications and CFGsrc: symbol table, parser, scanner, intermediate code generatorsymbol_info.cpp,scope_table.cpp,symbol_table.cpp: symbol table implementationscanner.l: scannerparsetree.cpp: parse tree implementationparser.y: parserasm.cpp: intermediate code generator
tests: sample code that can be compiled by this compilerbuild.sh: script to build the compilerdebug.sh: sets-gflag in when building the compiler, for debugging purposestest.sh: runs the compiler on all the test files intestsfolder-kflag can be used to keep the log files generated during the test-cclean up the repository-mmake a output directory and store the generated assembly there
verify_output.sh: Converts the test C files (which don't have thestdio.hheader) to make them runnable byg++. This does the following:- append
#include <stdio.h>at the top - add definition of
printlnfunction (wraps aprintf("%d\n", x)) at the top. - write the modified file in
outputdirectory - compile the modified file using
g++and run the executable - write the output to
expected_output.login theoutputdirectory
- append
program : program unit
program : unit
unit : var_declaration
unit : func_declaration
unit : func_definition
func_declaration : type_specifier ID LPAREN parameter_list RPAREN SEMICOLON
func_declaration : type_specifier ID LPAREN RPAREN SEMICOLON
func_definition : type_specifier ID LPAREN parameter_list RPAREN compound_statement
func_definition : type_specifier ID LPAREN RPAREN compound_statement
parameter_list : parameter_list COMMA type_specifier ID
parameter_list : parameter_list COMMA type_specifier
parameter_list : type_specifier ID
parameter_list : type_specifier
compound_statement : LCURL statements RCURL
compound_statement : LCURL RCURL
var_declaration : type_specifier declaration_list SEMICOLON
type_specifier : INT
type_specifier : FLOAT
type_specifier : VOID
declaration_list : declaration_list COMMA ID
declaration_list : declaration_list COMMA ID LSQUARE CONST_INT RSQUARE
declaration_list : ID
declaration_list : ID LSQUARE CONST_INT RSQUARE
statements : statement
statements : statements statement
statement : var_declaration
statement : expression_statement
statement : compound_statement
statement : FOR LPAREN expression_statement expression_statement expression RPAREN statement
statement : IF LPAREN expression RPAREN statement
statement : IF LPAREN expression RPAREN statement ELSE statement
statement : WHILE LPAREN expression RPAREN statement
statement : PRINTLN LPAREN ID RPAREN SEMICOLON
statement : RETURN expression SEMICOLON
expression_statement : SEMICOLON
expression_statement : expression SEMICOLON
variable : ID
variable : ID LSQUARE expression RSQUARE
expression : logic_expression
expression : variable ASSIGNOP logic_expression
logic_expression : rel_expression
logic_expression : rel_expression LOGICOP rel_expression
rel_expression : simple_expression
rel_expression : simple_expression RELOP simple_expression
simple_expression : term
simple_expression : simple_expression ADDOP term
term : unary_expression
term : term MULOP unary_expression
unary_expression : ADDOP unary_expression
unary_expression : NOT unary_expression
unary_expression : factor
factor : variable
factor : ID LPAREN argument_list RPAREN
factor : LPAREN expression RPAREN
factor : CONST_INT
factor : CONST_FLOAT
factor : variable INCOP
factor : variable DECOP
argument_list : arguments
argument_list :
arguments : arguments COMMA logic_expression
arguments : logic_expression