Compilers project specification: Tiny


Tiny is a small language based on a subset of C.

The aim of the project is to develop a compiler that compiles Tiny source into x86 assembler language that can be processed by the gnu assembler.

The requirements below specify a minimum. You are free to extend the language as you see fit (and this will be appreciated).

It suffices to write a program t2s that



program		: [declaration]+

declaration	: fun_declaration
		| var_declaration

fun_declaration	: type NAME LPAR formal_pars RPAR block

formal_pars	: formal_par [ COMMA formal_par ]*
		| 	// empty

formal_par	: type NAME

block		: LBRACE var_declaration* statements RBRACE

var_declaration	: type NAME SEMICOLON

type		: INT
		| CHAR
		| type LBRACK exp RBRACK // array type

statements	: statement [ SEMICOLON statement]*

statement	: IF LPAR exp RPAR statement
		| IF LPAR exp RPAR statement ELSE statement
		| WHILE LPAR exp RPAR statement
		| lexp ASSIGN exp
		| RETURN exp 
		| NAME LPAR pars RPAR		// function call
		| block
		| WRITE exp
		| READ lexp

lexp		: var
		| lexp LBRACK exp RBRACK	// array access

exp		: lexp
		| exp binop exp		
		| unop exp
		| LPAR exp RPAR
		| NAME LPAR pars RPAR		// function call
		| LENGTH lexp			// size of an array

binop		: MINUS
		| PLUS
		| LESS

unop		: MINUS
		| NOT

pars		: exp [COMMA exp]*	

var		: NAME


Data types
Tiny supports two primitive data types: char, and int. The only type constructor is the array type.
int		a1;
int[32]		a2;	// an array of 32 integers
int[10][2]	a3;	// an array of 2 arrays of 10 integers
Passing paramemters
As in C and java: primitive data types are always passed by value, arrays are passed by address.

There is an automatic conversion between integers and characters.

char	c;
c = 10;	// c is the newline character
There is no support for external functions, file inclusion etc.
Since there is no support for libraries, I/O is built in.
Expressions are standard. As in C, there is no boolean type and an integer value of 0 stands for false, any other integer for true.
Every function must be declared with a return type but this type may be ignored when called outside an expression. Array parameters need not match exactly. The size of an array can be tested using the lenth built-in function.
	int f(int[1] a)

	int[10]	b;
	f(b);	// OK
Main function
When executed, the program starts up by executing the function tiny.
int tiny()


Use make. The default target should construct the compiler. For an example organization, see the example Micro compiler.
Dirk Vermeir ( [Last modified: Wed Feb 4 11:14:37 CET 2009 ]