forked from Jenin82/programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
icg1.c
89 lines (81 loc) · 1.9 KB
/
icg1.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
#include <stdio.h>
#include <string.h>
int i = 1, j = 0, no = 0, tmpch = 90;
char str[100], left[15], right[15];
struct exp {
int pos;
char op;
} k[15];
void findopr();
void explore();
void fleft(int);
void fright(int);
int main() {
printf("\t\tINTERMEDIATE CODE GENERATION\n\n");
printf("Enter the Expression :");
scanf("%s", str);
printf("The intermediate code:\t\tExpression\n");
findopr();
explore();
return 0;
}
void findopr() {
char operators[] = "=/*+-";
for (i = 0; str[i] != '\0'; i++) {
for (int opIndex = 0; operators[opIndex] != '\0'; opIndex++) {
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);
} else {
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++;
}
}