Posted by: Rosario Barbagallo | February 5, 2013

Compiler C

I have developed a compiler&interpreter using the tools: FLEX and BISON (C Language) from MIL to MIN.


The lexical analyzer should take as input a MINI-L program, parse it, and output the sequence of lexical tokens associated with the program.
The software takes a syntactically-correct MINI-L program (a program that can be parsed without any syntax errors), verify that it has no semantic errors, and then generate its corresponding intermediate code.
A source program in MINI-L will be translated by the compiler into an intermediate code program, and that intermediate code program will be the input to the interpreter which will take the intermediate code, execute it and give the final results.


The MINI-L Language has the following features:

  1. Integer scalar variables.
  2. Unidimensional arrays of integers.
  3. Assignment statements.
  4. While loops.
  5. If-then-else statements.
  6. Read/Write statements.

In MINI-L a comment is introduced by “- -” and goes to the end of the line. All tokens are separated by blanks, tabs, newlines, delimiters, operators or comments. A valid identifier begins with a letter and may be followed by additional letters, digits or underscores. The length of an identifier does not exceed 8 characters. Finally, MINI-L is case sensitive with all reserved words written in lower case.

Errors handled:

  1. Error Handling for the use of variables that have not been declared at the beginning of the program
  2. Error Handling for a declaration of the same variable multiple times
  3. Error Handling in the use of an array variable as a normal integer variable
  4. Error Handling negative array size
  5. Error management use of an array index greater than the maximum size declared (in the case where the index is a number)
  6. Management error identifier with less than 8 characters long and ends with ‘_’
  7. Management error identifier with more than 8 characters

Syntax diagrams describing the syntax of MINI-L are given to you. Your first task in this assignment
is to develop a context-free grammar from the syntax diagrams. The parser will get the necessary information about the tokens from the lexical analyzer.

syntax1 syntax2 syntax3 syntax4 syntax5


Each line in the MIN file

1. contains at most one MIL instruction

2. Each line is at most 254 characters long

3. All variables are defined before they are used



Code Source Example:

program test_er;

n,x: integer;

d: integer;

a,b : array(1000) of integer;

c : array(10) of integer;

beginprogram — main program

— test op

n:= x+10*20/n-d%40+(2+20)*a(2);

— test if

if x>0 and n==d or a(10)>=c(n)

then d:=d*d*d*d; endif;

–test =

d:=11; d:=x; a(10):=2; a(10):=c(2);

— test if-else

if x+20/n>x-1*n

then n:=10+d;

if a(10)>=c(n)

then n:=d*x;

else n:=20*n;



— test while

while x<-10 or not c(2)!=0 loop



— test read

read n;

read n,x,a(10);


Output Code:

.[] a, 1000
.[] b, 1000
.[] c, 10
/ t0, 20,n
* t1,10,t0
% t2,d,40
– t3,t1,t2
+ t4, x,t3
+ t5,2,20
=[] t6,a,2
* t7,t5,t6
+ t8,t4,t7
= n,t8
> p0,x,0
== p1, n,d
&& p2,p0,p1
=[] t9,a,10
=[] t10,c,n
>= p3,t9,t10
|| p4,p2,p3
== p5,p4,0
?:= L000 ,p5
* t11, d,d
* t12, t11,d
* t13, t12,d
= d,t13
: L000
= d,11
= d,x
[]= a,10,2
=[] t14,c,2
[]= a,10,t14
/ t15, 20,n
+ t16, x,t15
* t17,1,n
– t18, x,t17
> p6,t16,t18
== p7,p6,0
?:= L001 ,p7
+ t19,10,d
= n,t19
=[] t20,a,10
=[] t21,c,n
>= p8,t20,t21
== p9,p8,0
?:= L002 ,p9
* t22, d,x
= n,t22
:= L003
: L002
* t23, 20,n
= n,t23
: L003
: L001
: L004
– t24,10
< p10, x,t24
=[] t25,c,2
!= p11, t25,0
! p12, p11
|| p14,p10,p12
== p15,p14,0
?:= L005 ,p15
+ t26,x,1
= x,t26
:= L004
: L005
.< n
.< n
.< x
.[]< a,10
.> n
.> x
.[]> a,10


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s


%d bloggers like this: