Tiny-Compiler

Compiler for the TINY Programming Language described in the language description section.

View on GitHub

Tiny Programming Language Compiler

Compiler for the TINY Programming Language described in Language Description.

TINY Programming Language

A program in TINY consists of a set of functions (any number of functions and ends with a main function), each function is a sequence of statements including (declaration, assignment, write, read, if, repeat, function, comment, …) each statement consists of (number, string, identifier, expression, condition, …).

Language Description


TINY Language Regular Expressions

digit ::= 0|1|2|3|4……|9

letter ::= [a-z][A-Z]

Number ::= digit.?digit

String ::= “(letter|digit)*”

Reserved_Keywords ::= int | float | string | read | write | repeat | until | if | elseif | else | then | return | endl

Comment ::= /*String*\/

Identifier ::= letter (letter | digit)*

Term ::= Number | Identifier | Function_Call

Arithmatic_Operator ::= + | - | * | /

Equation ::= (Term (Arithmatic_Operator (Equation | Term))+) | (( Term Arithmatic_Operator ( Equation | Term) )) (Arithmatic_Operator (Term | Equation))*

Expression ::= String | Term | Equation

Datatype ::= int | float | string

Condition_Operator ::= < | > | = | <>

Boolean_Operator ::= && | \|\|


TINY Language Deterministic Finite Automaton DFA


TINY Language Context Free Grammar CFG


TINY Code Samples

Sample program includes all 30 rules

int sum(int a, int b)
{
	return a + b;
}
int main()
{
	int val, counter;
	read val;

	counter := 0;

	repeat                                                                                
		val := val - 1;
		write "Iteration number [";
		write counter;
		write "] the value of x = ";
		write val;
		write endl;                          
		counter := counter+1;                                                      
	until val = 1

	write endl;

	string s := "number of Iterations = ";
	write s; 

	counter := counter-1;

	write counter;

	/* complicated equation */    
	float z1 := 3*2*(2+1)/2-5.3;
	z1 := z1 + sum(a,y);

	if  z1 > 5 || z1 < counter && z1 = 1 
	then 
		write z1;
	elseif z1 < 5 
	then
		z1 := 5;
	else
	    z1 := counter;
	end

	return 0;
}

Sample program in Tiny language – computes factorial

/* Sample program in Tiny language – computes factorial*/
int main()
{
	int x;
	read x; /*input an integer*/
	if x > 0 /*don’t compute if x <= 0 */
	then 
		int fact := 1;

		repeat
			fact := fact * x;
			x := x  1;
		until x = 0

		write fact; /*output factorial of x*/
	end
	return 0;
}