-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntermediate_code.cpp
104 lines (96 loc) · 1.93 KB
/
Intermediate_code.cpp
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
102
103
104
#include <stdio.h>
#include <string.h>
#include <process.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];
int main()
{
printf("Enter the expression: ");
scanf("%s", str);
printf("The intermediate code:\t\t Expression\n");
findopr();
explore();
return 0;
}
void findopr()
{
char operators[] = {':', '/', '*', '+', '-'};
for (int opIndex = 0; opIndex < strlen(operators); opIndex++)
{
for (i = 0; str[i] != '\0'; i++)
{
if (str[i] == operators[opIndex])
{
k[j].pos = i;
k[j++].op = operators[opIndex];
}
}
}
}
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);
for (j = 0; j < strlen(str); j++)
{
if (str[j] != '$')
printf("%c", str[j]);
}
printf("\n");
i++;
}
fright(-1);
if (no == 0)
{
fleft(strlen(str));
printf("\t%s := %s", right, left);
return;
}
printf("\t%s := %c", right, str[k[--i].pos]);
}
void fleft(int x)
{
int w = 0, flag = 0;
x--;
while (x != -1 && strchr("+-*/:=", str[x]) == NULL)
{
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 && strchr("+-*/:=", str[x]) == NULL)
{
if (str[x] != '$' && flag == 0)
{
right[w++] = str[x];
right[w] = '\0';
str[x] = '$';
flag = 1;
}
x++;
}
}