Skip to content

A compiler which produces intermediate code using flex and bison

Notifications You must be signed in to change notification settings

JACOBIN-SCTCS/SUBSET_COMPILER

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 

Repository files navigation

MINI COMPILER USING FLEX & BISON

This repository has been created as a part of the compiler assignment . The compiler developed is upto the intermediate code generation phase.

Compiling the Code

  flex lexanalyser.l
  bison -d parser.y
  gcc -c lex.yy.c parser.tab.c
  gcc -o com.out lex.yy.o parser.tab.o
  

The 3rd command produces a warning which can be ignored .

Running the Compiler

The above sequence of commands produces an executable com.out which is our compiler . You can feed in the program to be compiled after typing

followed by the input program . Type exit ; to escape from the process The intermediate code will be available in a file named output.txt .

Alternatively you can write a program in a file and put it in the same directory and you canb compile it by using the redirection operator '<' Suppose the filename is program.txt

      ./com.out < program.txt

Dont Forget to put exit; at the end of the program to terminate compilations.

Intermediate Code will be available in output.txt

Sample programs with their outputs

Program Intermediate Code
print 5;
exit;
t1 := 5;
print t1;
print ((5+2)*8);
exit;
t1 := 5 + 2
t2 := t1
t3 := t2 * 8
t4 := t3
R1 := t4;
print R1;
def samplefunction(t,y)
{
a = 5;
}
y=4;
d=3;
samplefunction(y,d);
exit;
PROCEDURE samplefunction t,y
R1 := 5;
a := R1;
ENDP
R1 := 4;
y := R1;
R1 := 3;
d := R1;
samplefunction(y,d)
def samplefunction(t,y)
{
a=1;
if(a<4)
{
a = a + 1 ;
}
}
samplefunction(y,d);
exit;
PROCEDURE samplefunction t,y
R1 := 1;
a := R1;
t1 := a < 4
R1 := t1
IF NZ GO TO 0LABEL:
t2 := a + 1
R1 := t2;
a := R1;
0LABEL:
ENDP
samplefunction(y,d)
a=1;
while(a<5)
{
a=a+1;
}
exit;
R1 := 1;
a := R1;
0_LABEL :
t1 := a < 5
R1 := t1
IF NZ GOTO 1_LABEL
t2 := a + 1
R1 := t2;
a := R1;
JMP 0_LABEL
1_LABEL:
ans=1;
i=5;
while(i > 0)
{
ans = ans * i;
i = i - 1;
}
print ans;
exit;
R1 := 1;
ans := R1;
R1 := 5;
i := R1;
0_LABEL :
t1 := i > 0
R1 := t1
IF NZ GOTO 1_LABEL
t2 := ans * i
R1 := t2;
ans := R1;
t3 := i - 1
R1 := t3;
i := R1;
JMP 0_LABEL
1_LABEL:
R1 := ans;
print R1;
ans=1;
i=5;
do
{
ans = ans * i;
i = i - 1;
}
while(i > 0);
print ans;
R1 := 1;
ans := R1;
R1 := 5;
i := R1;
0_LABEL:
t1 := ans * i
R1 := t1;
ans := R1;
t2 := i - 1
R1 := t2;
i := R1;
t3 := i > 0
R1 := t3
if nz goto 0_LABEL
R1 := ans;
print R1;

Documentation

The explanation for code portions are present in the comments in code . If you have difficulty raise an issue .

Contribute

If you feel any changes to be made feel free to issue a pull request . The code performs poorly in memory optimisation for generating code

If you like this project please do ⭐ the repository

About

A compiler which produces intermediate code using flex and bison

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published