-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathyeniodev.c
More file actions
577 lines (500 loc) · 18.7 KB
/
Copy pathyeniodev.c
File metadata and controls
577 lines (500 loc) · 18.7 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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_EXPR_SIZE 256
//TokenType enum is used for classification of the token types.
typedef enum {
TOKEN_TYPE_NULLL,
TOKEN_TYPE_NUMBER,
TOKEN_TYPE_OPERATOR,
TOKEN_TYPE_FUNCTION,
TOKEN_TYPE_VARIABLE,
TOKEN_TYPE_PARENTHESES,
TOKEN_TYPE_COMMA
} TokenType;
//Token struct
typedef struct {
TokenType type;
char value[257];
} Token;
// The aim of checkerToken array is to check if the expression should return "Error!" or not. It contains the raw-expression that is splitted by tokens.
Token checkerToken[257];
// The postfix expression is kept in arrToken.
Token arrToken[256+1];
// Lookup arrays are for memorization of variables. Lookup array records the input as string and lookup_2 records integer value of that string.
char lookup[257][257];
long long lookup_2[257];
// a is the index of arrToken
int a=0;
// b is the index of checkerToken
int b=0;
// this is flag that we use in possible error places. This is not a specific flag
int bigCheck=0;
// indicates the precedence of operators
int precedence(char operator)
{
switch (operator) {
case '+':
case '-':
return 1;
case '*':
return 2;
case '&':
case '|':
return 0;
default:
return -1;
}
}
// indicates operators
int isOperator(char ch)
{
return (ch == '+' || ch == '-' || ch == '*' || ch == '&' || ch == '|');
}
// converts infix epression to postfix expression
int infixToPostfix(char* infix)
{
a=0;
bigCheck=0;
//initializing arrToken and checkerToken
for(int i=0;i<257;i++){
arrToken[i].type = TOKEN_TYPE_NULLL;
checkerToken[i].type = TOKEN_TYPE_NULLL;
strcpy(arrToken[i].value,"");
strcpy(checkerToken[i].value,"");
}
// stackToken includes operators, functions and parantheses
Token stackToken[256+1];
// funcStack shows which function is the last one
Token funcStack[257];
// index for funcStack
int funcCounter=0;
int i=0, j=0;
int len = strlen(infix);
int top = -1;
//iterating through chars of infix expression
while(i<len-1){
//skipping blankspaces
if (infix[i] == ' ' || infix[i] == '\t'){
i++;
continue;
}
Token token;
//checking if an incoming char is digit and if so tokenizes it. Then, adding this token to arrToken and checkerToken.
if (isdigit(infix[i])) {
token.type = TOKEN_TYPE_NUMBER;
int k = 0;
while(isdigit(infix[i])){
token.value[k]=infix[i];
k++;
i++;
}
token.value[k] = '\0';
arrToken[a++] = token;
checkerToken[b++] = token;
}
//checking if an incoming char is alphabetic and if so tokenizes it. Then, adding this token to arrToken and checkerToken.
else if (isalpha(infix[i])) {
int k = 0;
while(isalpha(infix[i])){
token.value[k]=infix[i];
k++;
i++;
}
token.value[k] = '\0';
//checking if the token is function or not, then adding into funcStack.
if(strcmp(token.value, "xor") == 0 || strcmp(token.value, "ls") == 0||strcmp(token.value, "rs") == 0||strcmp(token.value, "lr") == 0||strcmp(token.value, "rr") == 0||strcmp(token.value, "not") == 0 ){
token.type = TOKEN_TYPE_FUNCTION;
funcStack[funcCounter++] = token;
}else{ // if it is not a function, then it must be a variable.
token.type = TOKEN_TYPE_VARIABLE;
arrToken[a++] = token;
}
checkerToken[b++] = token;
}
//if the incoming char is comma, initially tokenizes it, then looks at the last function and add the operator of it to the stackToken.
// This is important part for the placement of the function (except not) operator to stack and postfix.
else if(infix[i] == ','){
token.type = TOKEN_TYPE_COMMA;
token.value[0] = ',';
checkerToken[b++] = token;
if(funcCounter==0){
bigCheck=1;
break;
}
if(strcmp(funcStack[funcCounter-1].value,"xor")==0){
while (top > -1 && precedence(stackToken[top].value[0]) >= precedence('^') && stackToken[top].type != TOKEN_TYPE_PARENTHESES){
arrToken[a++] = stackToken[top--];
}
stackToken[++top].type = TOKEN_TYPE_OPERATOR;
strcpy(stackToken[top].value,"^");
}else if(strcmp(funcStack[funcCounter-1].value,"ls")==0){
while (top > -1 && precedence(stackToken[top].value[0]) >= -1 && stackToken[top].type != TOKEN_TYPE_PARENTHESES){
arrToken[a++] = stackToken[top--];
}
stackToken[++top].type = TOKEN_TYPE_OPERATOR;
strcpy(stackToken[top].value,"<<");
}else if(strcmp(funcStack[funcCounter-1].value,"rs")==0){
while (top > -1 && precedence(stackToken[top].value[0]) >= -1 && stackToken[top].type != TOKEN_TYPE_PARENTHESES){
arrToken[a++] = stackToken[top--];
}
stackToken[++top].type = TOKEN_TYPE_OPERATOR;
strcpy(stackToken[top].value,">>");
}else if(strcmp(funcStack[funcCounter-1].value,"lr")==0){
while (top > -1 && precedence(stackToken[top].value[0]) >= -1 && stackToken[top].type != TOKEN_TYPE_PARENTHESES){
arrToken[a++] = stackToken[top--];
}
stackToken[++top].type = TOKEN_TYPE_OPERATOR;
strcpy(stackToken[top].value,"lr");
}else if(strcmp(funcStack[funcCounter-1].value,"rr")==0){
while (top > -1 && precedence(stackToken[top].value[0]) >= -1 && stackToken[top].type != TOKEN_TYPE_PARENTHESES){
arrToken[a++] = stackToken[top--];
}
stackToken[++top].type = TOKEN_TYPE_OPERATOR;
strcpy(stackToken[top].value,"rr");
}
funcCounter--;
i++;
}
// if the character is '(' push it in the stackToken
else if (infix[i] == '(') {
token.type = TOKEN_TYPE_PARENTHESES;
token.value[0]=infix[i];
i++;
token.value[i] = '\0';
stackToken[++top]=token;
checkerToken[b++]=token;
//if the last function is "not", execute not operation for postfix
if(strcmp(funcStack[funcCounter-1].value,"not")==0){
stackToken[++top].type = TOKEN_TYPE_OPERATOR;
strcpy(stackToken[top].value,"~");
funcCounter--;
}
}
// if the character is ')'
// pop the stack and add it to the strcmp(stackToken[top].value[0], '(')!=0
// output string until empty or '(' found
else if (infix[i] == ')') {
checkerToken[b].type=TOKEN_TYPE_PARENTHESES;
strcpy(checkerToken[b++].value,")");
while (top > -1 && stackToken[top].type != TOKEN_TYPE_PARENTHESES ){
arrToken[a++] = stackToken[top--];
}
if (top > -1 && stackToken[top].type != TOKEN_TYPE_PARENTHESES ){
bigCheck=1;
}
else{
top--;
}
i++;
}
// checking if the char is operator
// push it to the stack after considering the precedence
else if (isOperator(infix[i])) {
token.type = TOKEN_TYPE_OPERATOR;
token.value[0] = infix[i];
token.value[1] = '\0';
checkerToken[b++] = token;
while (top > -1
&& precedence(stackToken[top].value[0])
>= precedence(infix[i]))
arrToken[a++] = stackToken[top--];
stackToken[++top] = token;
i++;
}
else{
bigCheck=1;
break;
}
}
// checking if the parantheses are balanced
while (top > -1) {
if (stackToken[top].type == TOKEN_TYPE_PARENTHESES) {
bigCheck=1;
}
arrToken[a++] = stackToken[top--];
}
return 0;
}
// big function for checking errors
int checkFunc(){
// initializing parameters or flags to be used
int isEqualParantheses=0;
int num=0;
int count_func_with_comma=0;
int comma_count=0;
for(int k=0;k<b;k++){
// two seperate number cannot be placed next to each other
if(checkerToken[k].type == TOKEN_TYPE_NUMBER){
if(checkerToken[k+1].type == TOKEN_TYPE_NUMBER){
return 1;
}else if(checkerToken[k+1].type == TOKEN_TYPE_VARIABLE){
return 1;
}
}
// two seperate variable cannot be placed next to each other
if(checkerToken[k].type == TOKEN_TYPE_VARIABLE){
if(checkerToken[k+1].type == TOKEN_TYPE_NUMBER){
return 1;
}else if(checkerToken[k+1].type == TOKEN_TYPE_VARIABLE){
return 1;
}
}
// two operator number cannot be placed next to each other
if(checkerToken[k].type == TOKEN_TYPE_OPERATOR){
if(checkerToken[k+1].type == TOKEN_TYPE_OPERATOR){
return 1;
}
}
//if the token is function
//the next token should be left parantheses
if(checkerToken[k].type == TOKEN_TYPE_FUNCTION){
if(checkerToken[k+1].value[0] != '('){
return 1;
}
if(strcmp(checkerToken[k].value,"not")!=0){
count_func_with_comma++;
}
num=0;
}
if(checkerToken[k].type == TOKEN_TYPE_PARENTHESES){
if(checkerToken[k].value[0] == '('){
isEqualParantheses++;
}
if(checkerToken[k].value[0] == ')'){
isEqualParantheses--;
// checking if the last function has more than one comma or not
// checking whether the part from the comma to last function is balanced
}
}
//if it is comma
// checking whether the part from the last function to the comma is balanced
if(checkerToken[k].type == TOKEN_TYPE_COMMA){
comma_count++;
}
}
// checking if the parantheses are balanced
if(isEqualParantheses != 0){
return 1;
}
// checking if the comma count is the same with the functions other than "not"
// since "not" does not have comma
if(comma_count!=count_func_with_comma){
return 1;
}
return 0;
}
// This function calculates the postfix notation coming from infixToPostfix function
long long evaluatePostfix()
{
// index for tstack
int toptstack=0;
// initializing stack to evaluate postfix
Token tstack[256];
for(int l=0;l<257;l++){
tstack[l].type = TOKEN_TYPE_NULLL;
strcpy(tstack[l].value,"");
}
//iterating in postfix notation
for (int i = 0; i< a; i++)
{
// if the type of the token is operator, then pop the last two element from tstak
// evaluate the result with these values and operator
// then, push this result back to the tstack
if(arrToken[i].type == TOKEN_TYPE_OPERATOR){
char op_val[257];
strcpy(op_val, arrToken[i].value);
Token tok1 = tstack[--toptstack];
long long val1=0;
if (tok1.type == TOKEN_TYPE_NUMBER){
sscanf(tok1.value,"%lld",&val1);
}
long long outcome=0;
if(strcmp(op_val,"~")==0){
outcome = ~val1;
}else{
Token tok2 = tstack[--toptstack];
long long val2=0;
if (tok2.type == TOKEN_TYPE_NUMBER){
sscanf(tok2.value,"%lld",&val2);
}
if(strcmp(op_val,"+")==0){
outcome = val1+val2;
}else if(strcmp(op_val,"-")==0){
outcome = val2-val1;
}else if(strcmp(op_val,"*")==0){
outcome = val1*val2;
}else if(strcmp(op_val,"&")==0){
outcome = val1&val2;
}else if(strcmp(op_val,"|")==0){
outcome = val1|val2;
}else if(strcmp(op_val,"^")==0){
outcome = val1^val2;
}else if(strcmp(op_val,">>")==0){
outcome = val2>>val1;
}else if(strcmp(op_val,"<<")==0){
outcome = val2<<val1;
}else if(strcmp(op_val,"lr")==0){
outcome = (val2 << val1%64) | (val2 >> (64-val1)%64);
}else if(strcmp(op_val,"rr")==0){
outcome = (val2 >> val1%64) | (val2 << (64-val1)%64);
}
}
tstack[toptstack].type=TOKEN_TYPE_NUMBER ;
sprintf(tstack[toptstack].value,"%lld",outcome);
toptstack++;
// if the type of the token is number
// directly push it to the tstack
}else if(arrToken[i].type == TOKEN_TYPE_NUMBER){
tstack[toptstack].type = arrToken[i].type;
strcpy(tstack[toptstack].value,arrToken[i].value);
toptstack++;
// if the type of the token is variable
// look at the lookup tables to find the int value of that variable
// push this int value to the tstack
}else if(arrToken[i].type == TOKEN_TYPE_VARIABLE){
long long val_var=0;
for(int m=0;m<257;m++){
if(strcmp(lookup[m], "")!=0 && strcmp(lookup[m], arrToken[i].value)==0){
val_var = lookup_2[m];
break;
}
}
tstack[toptstack].type = TOKEN_TYPE_NUMBER;
sprintf(tstack[toptstack].value,"%lld",val_var);
toptstack++;
}
}
long long res=0;
sscanf(tstack[toptstack-1].value,"%lld",&res);
return res;
}
//function for checking if the given variable is in the lookup array
int isInsideLookup(char arr[]){
int ind_look=0;
for(int m=0;m<128;m++){
if(strcmp(lookup[m], "")!=0 && strcmp(lookup[m], arr)==0){
return m;
break;
}
}
return -1;
}
int main()
{
//initializing the lookup arrays
for(int k=0;k<257;k++){
strcpy(lookup[k],"");
lookup_2[k]=0;
}
//index for lookup arrays
int index_of_lookup=0;
char line[256 +1] = "";
printf("> ");
while(fgets(line,sizeof(line),stdin)){
if(line==NULL){
break;
}
char line2[256+1]="";
// if the line contains '%'
// discard the part after %
if(strchr(line,'%') != NULL){
int ii=0;
while(line[ii]!='%'){
line2[ii]=line[ii];
ii++;
}
line2[ii] = '\n';
memset(line,'\0',sizeof(line));
strcpy(line,line2);
}
// This part enables to the code to continue if the input is blank line
int spaceFlag=1;
for(int j=0;line[j]!='\n';j++){
if(!isspace(line[j])){
spaceFlag=0;
}
}
if(spaceFlag==1){
printf("> ");
continue;
}
// if the line does not include '='
// execute the infixToPostfix and evaluatePostfix functions respectively.
if(strchr(line,'=') == NULL){
infixToPostfix(line);
int check_number = checkFunc();
if(check_number==1 || bigCheck==1){
printf("Error!\n");
printf("> ");
continue;
}
long long ans = 0;
ans = evaluatePostfix();
printf("%lld\n",ans);
}else{
// splitting the line by =
char temp[257];
strcpy(temp,line);
char* token = strtok(temp, "=");
char* value = strtok(NULL, "=");
char token_array[257] = "";
// checking if the left side of = has any errors
int hasError = 0;
int my_flag=0;
int i=0;
while(token[i]!='\0'){
if (isspace(token[i])) {
// skip whitespace characters
i++;
continue;
}
if (!isalpha(token[i])){
hasError=1;
i++;
}else {
if (my_flag == 1){
hasError=1;
}
int k = 0;
my_flag=1;
while(isalpha(token[i])){
token_array[k]=token[i];
k++;
i++;
}
token_array[k] = '\0';
}
}
if(hasError == 1){
printf("Error!\n");
printf("> ");
continue;
}
int ifInside = isInsideLookup(token_array);
// calculate the right side by sending it to the infixToPostfix and evaluatePostfix functions respectively.
infixToPostfix(value);
int check_number = checkFunc();
if(check_number==1 || bigCheck==1){
printf("Error!\n");
printf("> ");
continue;
}
long long ans=0;
ans = evaluatePostfix();
// if the variable is not in lookup array
// assign the variable and its value to lookup arrays separately
if(ifInside==-1){
strcpy(lookup[index_of_lookup],token_array);
lookup_2[index_of_lookup] = ans;
index_of_lookup++;
}else{
// just change the value of it
lookup_2[ifInside] = ans;
}
}
printf("> ");
}
return 0;
}