Experiment No 3,4
Experiment No 3,4
AIM:
Write a C program to generate three address code.
Three address code is a type of intermediate code which is easy to generate and can be
easilyconverted to machine code.It makes use of at most three addresses and one
operator to represent anexpression and the value computed at each instruction is stored
in temporary variable generated bycompiler. The compiler decides the order of operation
given by three address code.
General representation :
a = b op cWhere
a, b or c
represents operands like names, constants or compiler generated temporaries and
op
represents the operator
Ex:
a := (-c * b) + (-c * d)Three-address code is as follows: t1:= -c
t2:= b*t1
t3:= -c
t4:= d * t3
t5:= t2+ t4
a:= t5
PROGRAM:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void pm();
void plus();
void divi();
int i,ch,j,l,addr=100;
char ex[10], expression[10], exp1[10], result[10], id1[5], op[5], id2[5];
int main() {
while(1) {
printf("\n1.Assignment\n2.Arithmetic\n3.Relational\n4.Exit\nEnter the choice:");
scanf("%d", &ch);
switch(ch) {
case 1:
printf("\nEnter the expression with assignment operator:");
scanf("%s", expression);
l = strlen(expression);
result[0]='\0';
i = 0;
while(expression[i] != '=') i++;
strncat(result, expression, i);
reverse_string(expression);
exp1[0]='\0';
strncat(exp1, expression, l - (i + 1));
reverse_string(exp1);
printf("Three address code:\ntemp=%s\n%s=temp\n", exp1, result);
break;
case 2:
printf("\nEnter the expression with arithmetic operator:");
scanf("%s", ex);
strcpy(expression, ex);
l = strlen(expression);
exp1[0]='\0';
for(i = 0; i < l; i++) {
if(expression[i] == '+' || expression[i] == '-') {
if(expression[i+2] == '/' || expression[i+2] == '*') {
pm();
break;
}
else {
plus();
break;
}
}
else if(expression[i] == '/' || expression[i] == '*') {
divi();
break;
}
}
break;
case 3:
printf("Enter the expression with relational operator");
scanf("%s%s%s", id1, op, id2);
if(((strcmp(op,"<")==0)||(strcmp(op,">")==0)||(strcmp(op,"<=")==0)||
(strcmp(op,">=")==0)||(strcmp(op,"==")==0)||(strcmp(op,"!=")==0))==0)
printf("Expression is error");
else
{
printf("\n%d\tif %s%s%s goto %d", addr, id1, op, id2, addr + 3);
addr++;
printf("\n%d\t T:=0", addr);
addr++;
printf("\n%d\t goto %d", addr, addr + 2);
addr++;
printf("\n%d\t T:=1", addr);
}
break;
case 4:
exit(0);
}
}
}
void pm() {
reverse_string(expression);
j = l - i - 1;
strncat(exp1, expression, j);
reverse_string(exp1);
printf("Three address code:\ntemp=%s\nresult=%c%ctemp\n", exp1, expression[j + 1],
expression[j]);
}
void divi() {
strncat(exp1, expression, i + 2);
printf("Three address code:\ntemp=%s\nresult=temp%c%c\n", exp1, expression[i + 2],
expression[i + 3]);
}
void plus() {
strncat(exp1, expression, i + 2);
printf("Three address code:\ntemp=%s\nresult=temp%c%c\n", exp1, expression[i + 2],
expression[i + 3]);
}
Output: 1.Assignment
2.Arithmetic
3.Relational
4.Exit
Enter the choice:1
Enter the expression with assignment operator:
Experiment:4
AIM: code to Implement SLR Parser
#include<stdio.h>
#include<string.h>
int axn[][6][2]={
{{100,5},{-1,-1},{-1,-1},{100,4},{-1,-1},{-1,-1}},
{{-1,-1},{100,6},{-1,-1},{-1,-1},{-1,-1},{102,102}},
{{-1,-1},{101,2},{100,7},{-1,-1},{101,2},{101,2}},
{{-1,-1},{101,4},{101,4},{-1,-1},{101,4},{101,4}},
{{100,5},{-1,-1},{-1,-1},{100,4},{-1,-1},{-1,-1}},
{{-1,-1},{101,6},{101,6},{-1,-1},{101,6},{101,6}},
{{100,5},{-1,-1},{-1,-1},{100,4},{-1,-1},{-1,-1}},
{{100,5},{-1,-1},{-1,-1},{100,4},{-1,-1},{-1,-1}},
{{-1,-1},{100,6},{-1,-1},{-1,-1},{100,1},{-1,-1}},
{{-1,-1},{101,1},{100,7},{-1,-1},{101,1},{101,1}},
{{-1,-1},{101,3},{101,3},{-1,-1},{101,3},{101,3}},
{{-1,-1},{101,5},{101,5},{-1,-1},{101,5},{101,5}}
};//Axn Table
int gotot[12][3]={1,2,3,-1,-1,-1,-1,-1,-1,-1,-1,-1,8,2,3,-1,-1,-1,
int a[10];
char b[10];
int top=-1,btop=-1,i;
if(top<9)
a[++top]=k;
}
{
if(btop<9)
b[++btop]=k;
}
char TOS()
{
return a[top];
}
void pop()
{
if(top>=0)
top--;
}
void popb()
{
if(btop>=0)
b[btop--]='\0';
}
void display()
{
for(i=0;i<=top;i++)
printf("%d%c",a[i],b[i]);
}
{
int l;
printf("\t\t");
for(l=m;p[l]!='\0';l++)
printf("%c",p[l]);
printf("\n");
}
void error()
{
printf("Syntax Error");
}
{
int len,k,ad;
char src,*dest;
switch(p)
{
case 1:dest="E+T";
src='E';
break;
case 2:dest="T";
src='E';
break;
case 3:dest="T*F";
src='T';
break;
case 4:dest="F";
src='T';
break;
case 5:dest="(E)";
src='F';
break;
case 6:dest="i";
src='F';
break;
default:dest="\0";
src='\0';
break;
}
for(k=0;k<strlen(dest);k++)
{
pop();
popb();
}
pushb(src);
switch(src)
{
case 'E':ad=0;
break;
case 'T':ad=1;
break;
case 'F':ad=2;
break;
default: ad=-1;
break;
}
push(gotot[TOS()][ad]);
}
int main()
{
int j,st,ic;
char ip[20]="\0",an;
// clrscr();
+
scanf("%s",ip);
push(0);
display();
printf("\t%s\n",ip);
for(j=0;ip[j]!='\0';)
{
st=TOS();
an=ip[j];
if(an>='a'&&an<='z') ic=0;
else {
error();
break;
}
if(axn[st][ic][0]==100)
{
pushb(an);
push(axn[st][ic][1]);
display();
j++;
display1(ip,j);
}
if(axn[st][ic][0]==101)
{
reduce(axn[st][ic][1]);
display();
display1(ip,j);
}
if(axn[st][ic][1]==102)
{
// getch();
break;
}
/* else
{
break;
}*/
}
return 0;
}
Output:
Enter any String
a+a*a$
0 a+a*a$
0a5 +a*a$
0F3 +a*a$
0T2 +a*a$
0E1 +a*a$
0E1+6 a*a$
0E1+6a5 *a$
0E1+6F3 *a$
0E1+6T9 *a$
0E1+6T9*7 a$
0E1+6T9*7a5 $
0E1+6T9*7F10 $
0E1+6T9 $
0E1 $