-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
393 lines (306 loc) · 10.2 KB
/
Copy pathmain.c
File metadata and controls
393 lines (306 loc) · 10.2 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <stdbool.h>
#include <math.h>
typedef enum {
TOKEN_NUMBER,
TOKEN_OPERATOR,
TOKEN_LPAREN,
TOKEN_RPAREN,
TOKEN_END
} TokenType;
typedef struct {
TokenType type;
union {
double number;
char operator;
} value;
} Token;
#define STACK_SIZE 256
typedef struct {
Token items[STACK_SIZE];
int top;
} TokenStack;
void init_stack(TokenStack *s) {
s->top = -1;
}
bool is_empty(TokenStack *s) {
return s->top == -1;
}
bool push(TokenStack *s, Token t) {
if (s->top >= STACK_SIZE - 1) {
fprintf(stderr, "Error: tokens full\n");
return false;
}
s->items[++(s->top)] = t;
return true;
}
Token pop(TokenStack *s) {
if (is_empty(s)) {
fprintf(stderr, "Error: Empty token list\n");
Token error_token = {.type = TOKEN_END};
return error_token;
}
return s->items[(s->top)--];
}
Token peek(TokenStack *s) {
if (is_empty(s)) {
fprintf(stderr, "Error: Empty token list\n");
Token error_token = {.type = TOKEN_END};
return error_token;
}
return s->items[s->top];
}
Token *tokenize(const char *input, int *token_count) {
Token *tokens = malloc(512 * sizeof(Token));
if (!tokens) {
perror("Failed to alloc memory to tokens");
return NULL;
}
int capacity = 512;
int pos = 0;
const char *current = input;
while (*current) {
if (pos >= capacity - 1) {
capacity *= 2;
Token *new_tokens = realloc(tokens, capacity * sizeof(Token));
if (!new_tokens) {
perror("Failed relocating token memory");
free(tokens);
return NULL;
}
tokens = new_tokens;
}
if (isdigit(*current) || (*current == '.' && isdigit(current[1]))) {
char *end;
tokens[pos].type = TOKEN_NUMBER;
tokens[pos].value.number = strtod(current, &end);
current = end;
pos++;
} else if (strchr("+-*/", *current)) {
tokens[pos].type = TOKEN_OPERATOR;
tokens[pos].value.operator = *current;
current++;
pos++;
} else if (*current == '(') {
tokens[pos].type = TOKEN_LPAREN;
tokens[pos].value.operator = '(';
current++;
pos++;
} else if (*current == ')') {
tokens[pos].type = TOKEN_RPAREN;
tokens[pos].value.operator = ')';
current++;
pos++;
} else if (isspace(*current)) {
current++;
} else {
fprintf(stderr, "Unknow Char at input: %c\n", *current);
free(tokens);
return NULL;
}
}
tokens[pos].type = TOKEN_END;
*token_count = pos;
return tokens;
}
int get_precedence(char op) {
switch (op) {
case '+':
case '-': return 1;
case '*':
case '/': return 2;
default: return 0;
}
}
bool infix_to_rpn(Token *infix, int infix_len, Token *rpn_output, int *rpn_len) {
TokenStack operator_stack;
init_stack(&operator_stack);
int rpn_idx = 0;
for (int i = 0; i < infix_len; ++i) {
Token current_token = infix[i];
switch (current_token.type) {
case TOKEN_NUMBER:
rpn_output[rpn_idx++] = current_token;
break;
case TOKEN_OPERATOR:
while (!is_empty(&operator_stack) &&
peek(&operator_stack).type == TOKEN_OPERATOR &&
get_precedence(peek(&operator_stack).value.operator) >= get_precedence(current_token.value.operator))
{
rpn_output[rpn_idx++] = pop(&operator_stack);
}
if (!push(&operator_stack, current_token)) return false;
break;
case TOKEN_LPAREN:
if (!push(&operator_stack, current_token)) return false;
break;
case TOKEN_RPAREN:
while (!is_empty(&operator_stack) && peek(&operator_stack).type != TOKEN_LPAREN) {
rpn_output[rpn_idx++] = pop(&operator_stack);
}
if (is_empty(&operator_stack) || peek(&operator_stack).type != TOKEN_LPAREN) {
fprintf(stderr, "Error: unbalenced parenthesis \n");
return false;
}
pop(&operator_stack);
break;
default:
break;
}
if (rpn_idx >= 512 - 1) {
fprintf(stderr, "Erro: Output buffer\n");
return false;
}
}
while (!is_empty(&operator_stack)) {
Token top_token = peek(&operator_stack);
if (top_token.type == TOKEN_LPAREN) {
fprintf(stderr, "Erro: Error: unbalenced parenthesis\n");
return false;
}
rpn_output[rpn_idx++] = pop(&operator_stack);
if (rpn_idx >= 512 - 1) {
fprintf(stderr, "Erro: Output buffer\\n");
return false;
}
}
rpn_output[rpn_idx].type = TOKEN_END;
*rpn_len = rpn_idx;
return true;
}
typedef struct {
double items[STACK_SIZE];
int top;
} DoubleStack;
void init_double_stack(DoubleStack *s) { s->top = -1; }
bool is_double_empty(DoubleStack *s) { return s->top == -1; }
bool push_double(DoubleStack *s, double val) {
if (s->top >= STACK_SIZE - 1) return false;
s->items[++(s->top)] = val;
return true;
}
double pop_double(DoubleStack *s) {
if (is_double_empty(s)) return NAN;
return s->items[(s->top)--];
}
bool evaluate_rpn(Token *rpn_tokens, int rpn_len, double *result) {
DoubleStack operand_stack;
init_double_stack(&operand_stack);
for (int i = 0; i < rpn_len; ++i) {
Token token = rpn_tokens[i];
if (token.type == TOKEN_NUMBER) {
if (!push_double(&operand_stack, token.value.number)) {
fprintf(stderr, "Error: operand pile at evaluation\n");
return false;
}
} else if (token.type == TOKEN_OPERATOR) {
double right_operand = pop_double(&operand_stack);
double left_operand = pop_double(&operand_stack);
if (isnan(right_operand) || isnan(left_operand)) {
fprintf(stderr, "Error: missing operands '%c'\n", token.value.operator);
return false;
}
double current_result;
switch (token.value.operator) {
case '+': current_result = left_operand + right_operand; break;
case '-': current_result = left_operand - right_operand; break;
case '*': current_result = left_operand * right_operand; break;
case '/':
if (right_operand == 0.0) {
fprintf(stderr, "Error: Division by zero\n");
return false;
}
current_result = left_operand / right_operand;
break;
default:
fprintf(stderr, "Unknow Operator: %c\n", token.value.operator);
return false;
}
if (!push_double(&operand_stack, current_result)) {
fprintf(stderr, "Erroe: operand pile at evaluation\n");
return false;
}
}
}
double final_result = pop_double(&operand_stack);
if (isnan(final_result) || !is_double_empty(&operand_stack)) {
fprintf(stderr, "Erro: Invalid RPN expression\n");
return false;
}
*result = final_result;
return true;
}
void generate_assembly_rpn(Token *rpn_tokens, int rpn_len) {
for (int i = 0; i < rpn_len; ++i) {
Token token = rpn_tokens[i];
if (token.type == TOKEN_NUMBER) {
printf("PUSH %d\n", (int)token.value.number);
} else if (token.type == TOKEN_OPERATOR) {
switch (token.value.operator) {
case '+': printf("ADD\n"); break;
case '-': printf("SUB\n"); break;
case '*': printf("MUL\n"); break;
case '/': printf("DIV\n"); break;
default:
fprintf(stderr, "Unknow opertor at assembly generation %c\n", token.value.operator);
break;
}
}
}
printf("End of assembly parsing\n");
}
int main() {
char input[512];
printf("Enter expression (ex: 3 + 4 * (2 - 1) / 5): ");
if (!fgets(input, sizeof(input), stdin)) {
fprintf(stderr, "Error reading input\n");
return 1;
}
input[strcspn(input, "\n")] = '\0';
if (strlen(input) == 0) {
printf("Empty input.\n");
return 0;
}
int token_count;
Token *infix_tokens = tokenize(input, &token_count);
if (!infix_tokens) {
fprintf(stderr, "Filed on tokenization.\n");
return 1;
}
Token *rpn_tokens = malloc((token_count + 1) * sizeof(Token));
if (!rpn_tokens) {
perror("Failed to alloc RPN Memory");
free(infix_tokens);
return 1;
}
int rpn_len;
if (infix_to_rpn(infix_tokens, token_count, rpn_tokens, &rpn_len)) {
printf("Tokenized infix expression: ");
for(int i=0; i<token_count; ++i) {
if(infix_tokens[i].type == TOKEN_NUMBER) printf("%.2f ", infix_tokens[i].value.number);
else printf("%c ", infix_tokens[i].value.operator);
}
printf("\n");
printf("RPN expressions: ");
for(int i=0; i<rpn_len; ++i) {
if(rpn_tokens[i].type == TOKEN_NUMBER) printf("%.2f ", rpn_tokens[i].value.number);
else printf("%c ", rpn_tokens[i].value.operator);
}
printf("\n");
double result;
if (evaluate_rpn(rpn_tokens, rpn_len, &result)) {
printf("Result: %f\n", result);
generate_assembly_rpn(rpn_tokens, rpn_len);
} else {
fprintf(stderr, "Error during RPN validation\n");
}
} else {
fprintf(stderr, "Error converting to RPN\n");
}
free(infix_tokens);
free(rpn_tokens);
return 0;
}