Coder Social home page Coder Social logo

subset_compiler's Introduction

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

subset_compiler's People

Contributors

jacobin-sctcs avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.