0% found this document useful (0 votes)
13 views17 pages

Experiment No 3,4

Uploaded by

Aryan bond
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views17 pages

Experiment No 3,4

Uploaded by

Aryan bond
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

EXPERIMENT NO-3

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];

void reverse_string(char* str) {


int length = strlen(str);
for (int i = 0; i < length / 2; i++) {
char temp = str[i];
str[i] = str[length - i - 1];
str[length - i - 1] = temp;
}
}

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,

-1,9,3,-1,-1,10,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}; //GoTo table

int a[10];

char b[10];

int top=-1,btop=-1,i;

void push(int k)


{

if(top<9)

a[++top]=k;

}

void pushb(char 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]);

}

void display1(char p[],int m) //Displays The Present Input String

{

int l;

printf("\t\t");

for(l=m;p[l]!='\0';l++)

printf("%c",p[l]);

printf("\n");
}

void error()

{

printf("Syntax Error");

}

void reduce(int p)

{

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();

printf("Enter any String\n");

+

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 if(an=='+') ic=1;

else if(an=='*') ic=2;

else if(an=='(') ic=3;

else if(an==')') ic=4;

else if(an=='$') ic=5;

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)
{

printf("Given String is accepted \n");

// getch();

break;

}

/* else

{

printf("Given String is rejected \n");

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 $

Given String is accepted

You might also like