-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_three_address_code.c
101 lines (101 loc) · 2.9 KB
/
generate_three_address_code.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <stdio.h>
#include <string.h>
void pm();
void plus();
void div();
int i, ch, j, l, addr = 100;
char ex[10], exp[10], exp1[10], exp2[10], id1[5], op[5], id2[5];
void main()
{
clrscr();
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", exp);
l = strlen(exp);
exp2[0] = '\0';
i = 0;
while (exp[i] != '=')
{
i++;
}
strncat(exp2, exp, i);
strrev(exp);
exp1[0] = '\0';
strncat(exp1, exp, l - (i + 1));
strrev(exp1);
printf("Three address code:\ntemp=%s\n%s=temp\n", exp1, exp2);
break;
case 2:
printf("\nEnter the expression with arithmetic operator:");
scanf("%s", ex);
strcpy(exp, ex);
l = strlen(exp);
exp1[0] = '\0';
for (i = 0; i < l; i++)
{
if (exp[i] == '+' || exp[i] == '-')
{
if (exp[i + 2] == '/' || exp[i + 2] == '*')
{
pm();
break;
}
else
{
plus();
break;
}
}
else if (exp[i] == '/' || exp[i] == '*')
{
div();
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()
{
strrev(exp);
j = l - i - 1;
strncat(exp1, exp, j);
strrev(exp1);
printf("Three address code:\ntemp=%s\ntemp1=%c%ctemp\n", exp1, exp[j + 1], exp[j]);
}
void div()
{
strncat(exp1, exp, i + 2);
printf("Three address code:\ntemp=%s\ntemp1=temp%c%c\n", exp1, exp[i + 2], exp[i + 3]);
}
void plus()
{
strncat(exp1, exp, i + 2);
printf("Three address code:\ntemp=%s\ntemp1=temp%c%c\n", exp1, exp[i + 2], exp[i + 3]);
}