forked from zameel7/CD-Lab
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathintermediate.c
119 lines (100 loc) · 3.99 KB
/
intermediate.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <stdio.h> // Include the standard input/output library
#include <stdlib.h> // Include the standard library
#include <string.h> // Include the string library
// Global variables
int i = 1, j = 0, tmpch = 90; // i, j, no are counters, tmpch is a temporary character
char str[100], left[15], right[15]; // str is the input string, left and right are substrings
// Function prototypes
void findopr(); // Function to find operators in the string
void explore(); // Function to explore the string and generate intermediate code
void fleft(int); // Function to find the left part of an expression
void fright(int); // Function to find the right part of an expression
// Structure to hold the position and operator of an expression
struct exp
{
int pos; // Position of the operator in the string
char op; // The operator
} k[15]; // Array of expressions
// Main function
int main()
{
printf("\t\tINTERMEDIATE CODE GENERATION\n\n"); // Print title
printf("Enter the Expression :"); // Prompt for input
scanf("%s", str); // Read the input string
printf("The intermediate code:\n"); // Print title for output
findopr(); // Find the operators in the string
explore(); // Generate the intermediate code
}
// Function to loop through the string and find a specific operator
void oprloop(char opr)
{
for (i = 0; str[i] != '\0'; i++) // Loop through the string
if (str[i] == opr) // If the current character is the operator
{
k[j].pos = i; // Store the position
k[j++].op = opr; // Store the operator and increment the counter
}
}
// Function to find all operators in the string
void findopr()
{
oprloop(':'); // Find :
oprloop('/'); // Find /
oprloop('*'); // Find *
oprloop('+'); // Find +
oprloop('-'); // Find -
}
// Function to generate the intermediate code
void explore()
{
i = 1;
while (k[i].op != '\0') // Loop through the expressions
{
fleft(k[i].pos); // Find the left part of the expression
fright(k[i].pos); // Find the right part of the expression
str[k[i].pos] = tmpch--; // Replace the operator with a temporary character
printf("\t%c := %s%c%s\t\t\n", str[k[i].pos], left, k[i].op, right); // Print the intermediate code
i++; // Increment the counter
}
fright(-1); // Find the right part of the last expression
fleft(strlen(str)); // Find the left part of the last expression
printf("\t%s := %s\n", right, left); // Print the final result
getchar();
exit(0);
}
// Function to find the left part of an expression
void fleft(int x)
{
int w = 0; // Initialize counters
x--; // Decrement the position
// Loop through the string backwards until an operator or the start of the string is found
while (x != -1 && str[x] != '+' && str[x] != '*' && str[x] != '=' && str[x] != '\0' && str[x] != '-' && str[x] != '/' && str[x] != ':')
{
if (str[x] != '$') // If the current character is not $
{
left[w++] = str[x]; // Add the character to the left string and increment the counter
left[w] = '\0'; // End the string
str[x] = '$'; // Replace the character in the string with $
return;
}
x--; // Decrement the position
}
}
// Function to find the right part of an expression
void fright(int x)
{
int w = 0; // Initialize counters
x++; // Increment the position
// Loop through the string forwards until an operator or the end of the string is found
while (x != -1 && str[x] != '+' && str[x] != '*' && str[x] != '=' && str[x] != '\0' && str[x] != '-' && str[x] != '/' && str[x] != ':')
{
if (str[x] != '$') // If the current character is not $
{
right[w++] = str[x]; // Add the character to the right string and increment the counter
right[w] = '\0'; // End the string
str[x] = '$'; // Replace the character in the string with $
return;
}
x++; // Increment the position
}
}