0% found this document useful (0 votes)
20 views

Three Address Code For Boolean Expression Coding

The document describes a program that takes a Boolean expression as input and generates three-address code as the output. It reads the expression from a file, prints the original expression, and then analyzes the syntax to generate six lines of three-address code corresponding to the if-else blocks and variable assignments in the expression.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Three Address Code For Boolean Expression Coding

The document describes a program that takes a Boolean expression as input and generates three-address code as the output. It reads the expression from a file, prints the original expression, and then analyzes the syntax to generate six lines of three-address code corresponding to the if-else blocks and variable assignments in the expression.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

THREE ADDRESS CODE FOR BOOLEAN EXPRESSION

CODING:

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
FILE*in;
char temp[20],a[20],inp[20];
int i,j,k,f,f1;
clrscr();
i=0;
in=fopen("inp.txt","r");
while((fscanf(in,"%s\n",inp))!=EOF)
{
printf("\n%s",inp);
}rewind(in);
printf("\n\nThree address code\n");
while((fscanf(in,"%s\n",inp))!=EOF)
{
if((inp[i]=='i')&&(inp[++i]=='f'))
{
printf("1 if");
i=i+2;
do
{
printf("%c",inp[i]);
i++;
}
while(inp[i]!=')');
printf("goto 5\n");
fscanf(in,"%s",a);
f=1;
}
else if(strcmp(inp,"else")==0)
{
i=0;
fscanf(in,"%s",temp);
while(temp[i]!='=')
{
i++;
}
k=i;
printf("2 t=");
for(j=k+1;j<strlen(temp)-1;j++)
{
printf("%c",temp[j]);
}
printf("\n3%c=t",temp[0]);

printf("\n 4 goto 7");


f1=1;
}
if(f==1&&f1==1)
{
i=0,k=0;
while(a[i]!='=')
{
i++;
}
k=i;
printf("\n 5 t1=");
for(j=k+1;j<strlen(a)-1;j++)
{
printf("%c",a[j]);
}
printf("\n 6%c=t1\n",a[0]);
}
}
getch();
}
INP.TXT:

If(1<10)
a=c+d;
else
a=c-d;

OUTPUT:

if(1<10)
a=c+d;
else
a=c-d;

Three address code

1 if 1<10 goto 5
2 t=c-d
3 a=t
4 goto 7
5 t1=c+d
6 a=t1

You might also like