Skip to main content

C program to implement intermediate code generation for simple expression.

 

EXPERIMENT NO : 14

AIM : C program to implement intermediate code generation for simple expression.

 

Program

#include<stdio.h>

#include<conio.h>

#include<string.h>

int i=1,j=0,no=0,tmpch=90;

char str[100],left[15],right[15];

void findopr();

void explore();

void fleft(int);

void fright(int);

struct exp

{

int pos;

char op;

}k[15];

void main()

{

printf("\t\tINTERMEDIATE CODE GENERATION\n\n");

printf("Enter the Expression :");

scanf("%s",str);

printf("The intermediate code:\n");

findopr();

explore();

}

void findopr()

{

for(i=0;str[i]!='\0';i++)

if(str[i]==':')

{

k[j].pos=i;

k[j++].op=':';

}

for(i=0;str[i]!='\0';i++)

if(str[i]=='/')

{

k[j].pos=i;

k[j++].op='/';

}

for(i=0;str[i]!='\0';i++)

if(str[i]=='*')

{

k[j].pos=i;

k[j++].op='*';

}

for(i=0;str[i]!='\0';i++)

if(str[i]=='+')

{

k[j].pos=i;

k[j++].op='+';

}

for(i=0;str[i]!='\0';i++)

if(str[i]=='-')

{

k[j].pos=i;

k[j++].op='-';

}

}

void explore()

{

i=1;

while(k[i].op!='\0')

{

fleft(k[i].pos);

fright(k[i].pos);

str[k[i].pos]=tmpch--;

printf("\t%c := %s%c%s\t\t",str[k[i].pos],left,k[i].op,right);

printf("\n");

i++;

}

fright(-1);

if(no==0)

{

fleft(strlen(str));

printf("\t%s := %s",right,left);

getch();

exit(0);

}

printf("\t%s := %c",right,str[k[--i].pos]);

getch();

}

void fleft(int x)

{

int w=0,flag=0;

x--;

while(x!= -1 &&str[x]!= '+' &&str[x]!='*'&&str[x]!='='&&str[x]!='\0'&&str[x]!='-'&&str[x]!='/'&&str[x]!=':')

{

if(str[x]!='$'&& flag==0)

{

left[w++]=str[x];

left[w]='\0';

str[x]='$';

flag=1;

}

x--;

}

}

void fright(int x)

{

int w=0,flag=0;

x++;

while(x!= -1 && str[x]!= '+'&&str[x]!='*'&&str[x]!='\0'&&str[x]!='='&&str[x]!=':'&&str[x]!='-'&&str[x]!='/')

{

if(str[x]!='$'&& flag==0)

{

right[w++]=str[x];

right[w]='\0';

str[x]='$';

flag=1;

}

x++;

}

}

 



 

Comments

Popular posts from this blog

Implement the back end of the compiler which takes the three address code and produces the 8086 assembly language instructions that can be assembled and run using an 8086 assembler. The target assembly instructions can be simple move, add, sub, jump etc.

 ALGORITHM: 1. Start the program 2. Get the three variables from statements and stored in the text file k.txt.  3. Compile the program and give the path of the source file. 4. Execute the program. 5. Target code for the given statement was produced 6. Stop the program PROGRAM #include   <stdio.h> #include   <stdio.h> #include   <conio.h> #include   <string.h> void   main () {      char   code [ 10 ][ 30 ],  str [ 20 ],  opr [ 10 ];      int   i   =   0 ;      clrscr() ;      printf( " \n  Enter the set of intermediate code (terminated by exit): \n " ) ;      do     {          scanf( " %s " ,  icode [ i ]) ;     }  while  ( strcmp( icode [ i ++ ],  "exit" )   !=   0 );      printf ( " \n  target code generation" ) ;      i   =   0 ;      do     {          strcpy( str ,  icode [ i ]) ;          switch  ( str [ 3 ])         case  '+' :              strcpy ( opr ,  "ADD " );          break ;     case  '-&