-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
952 lines (737 loc) · 23.9 KB
/
main.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
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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdbool.h>
//hashtable implementation
#define TABLE_SIZE 128
typedef struct {
char *key;
long long int value;
} element;
typedef struct {
element *elements;
int size;
} table;
table *Hashtable;
int hash_function(char *key);
void init_table(table *table);
void insert(table *table, char *key, long long int value);
int is_balanced(char *str);
int is_valid_function(char *str);
int check_function(char *line, char *function_name);
int is_valid_operator(char *line, char *operator);
/**
* function that checks whether an input string has balanced parantheses or not.
* @param input str
* @return balance value
*/
int is_balanced(char *str) {
int len = strlen(str);
char stack[len];
int top = -1;
for (int i = 0; i < len; i++) {
if (str[i] == '(') {
stack[++top] = str[i];
} else if (str[i] == ')') {
if (top == -1) {
return 0;
} else if (str[i] == ')' && stack[top] == '(') {
top--;
} else {
return 0;
}
}
}
return (top == -1);
}
/**
* Hashing function
* @param key
* @return hash value
*/
int hash_function(char *key) {
int hash = 0;
int length = strlen(key);
for (int i = 0; i < length; i++) {
hash = hash + key[i];
}
return hash % TABLE_SIZE;
}
/** Function that initializes our hash table
* @param table
*/
void init_table(table *table) {
table->elements = (element *) calloc(TABLE_SIZE, sizeof(element));
table->size = TABLE_SIZE;
for (int i = 0; i < TABLE_SIZE; i++) {
table->elements[i].key = NULL;
table->elements[i].value = 0;
}
}
/**
* Function that adds an element to our hash table.
* @param table hashtable
* @param key key to be inserted
* @param value value to be inserted
*/
void insert(table *table, char *key, long long int value) {
int index = hash_function(key);
int i = index;
while (table->elements[i].key != NULL && strcmp(table->elements[i].key, "") != 0) {
if (strcmp(table->elements[i].key, key) == 0) {
table->elements[i].value = value;
return;
}
i = (i + 1) % table->size;
if (i == index) {
return;
}
}
table->elements[i].key = strdup(key);
table->elements[i].value = value;
}
/**
* Function that fetches a key's value from the table.
* @param table
* @param key
* @return key's value
*/
long long int lookup(table *table, char *key) {
int index = hash_function(key);
int i = index;
while (table->elements[i].key != NULL) {
if (strcmp(table->elements[i].key, key) == 0) {
return table->elements[i].value;
}
i = (i + 1) % table->size;
if (i == index) {
return 0;
}
}
return 0;
}
int error = 0;
int MAX_EXPR_LEN = 257;
typedef enum {
NONE,
NUM, //all numbers
OP, //+,-,&,|,*
IDENT, //identifiers,initally zero
FUNC_CALL, //for all functions in the format function(arg1,arg2)
LPAR,
RPAR,
COMMA,
ASSIGN,
AND,
OR
} TokenType;
typedef struct {
TokenType type;
char *value; //string value of the token
char *function_name; // Name of function
} Token;
Token tokens[257];
int current_index = 0;
int current_token_type;
int *num_tokens = 0;
int old;
int numtoken;
int numofpost;
Token create_token(int type, char *value);
/**
* function that forms tokens from an input string.
* i.e the tokenizer / lexer
* @param input string
* @param tokens the array that will store the tokens
* @param num_tokens
* @return
*/
Token *tokenize(char *input, Token tokens[], int *num_tokens) {
int i = 0; //current array location,incremented after each process
char curr_char = input[i];
while (curr_char != '\0' && curr_char != '%') {
if (isdigit(curr_char)) {
char *start = input + i; //start of the number (ex. 5 for 5674)
while (isdigit(curr_char)) { //current is a digit
i++;
curr_char = input[i];
}
int length = input + i - start;
char *token_str = (char *) malloc(length + 1);
strncpy(token_str, start, length);
token_str[length] = '\0'; //last character of the number,so the number's tokenized length is one more
tokens[*num_tokens] = create_token(NUM, token_str);
(*num_tokens)++;
}
else if (isalpha(curr_char)) {
char *start = input + i;
while (isalnum(curr_char) && curr_char != ' ') {
i++;
curr_char = input[i];
}
int length = input + i - start;
char *token_str = (char *) malloc(length + 1);
strncpy(token_str, start, length);
token_str[length] = '\0';
if (strcmp(token_str, "ls") == 0 || strcmp(token_str, "rs") == 0 ||
strcmp(token_str, "xor") == 0 || strcmp(token_str, "lr") == 0 ||
strcmp(token_str, "rr") == 0 || strcmp(token_str, "not") == 0) {
tokens[*num_tokens] = create_token(FUNC_CALL, token_str);
}
else {
tokens[*num_tokens] = create_token(IDENT, token_str);
}
(*num_tokens)++;
}
else if (curr_char == ',') {
char *token_str = (char *) malloc(2);
token_str[0] = curr_char;
token_str[1] = '\0';
tokens[*num_tokens] = create_token(COMMA, token_str);
(*num_tokens)++;
i++;
curr_char = input[i];
}
else if (curr_char == '=') {
char *token_str = (char *) malloc(2);
token_str[0] = curr_char;
token_str[1] = '\0';
tokens[*num_tokens] = create_token(ASSIGN, token_str);
(*num_tokens)++;
i++;
curr_char = input[i];
}
else if (curr_char == '+' || curr_char == '-' || curr_char == '*') {
char *token_str = (char *) malloc(2);
token_str[0] = curr_char;
token_str[1] = '\0';
tokens[*num_tokens] = create_token(OP, token_str);
(*num_tokens)++;
i++;
curr_char = input[i];
}
else if (curr_char == '(') {
char *token_str = (char *) malloc(2);
token_str[0] = curr_char;
token_str[1] = '\0';
tokens[*num_tokens] = create_token(LPAR, token_str);
(*num_tokens)++;
i++;
curr_char = input[i];
}
else if (curr_char == ')') {
char *token_str = (char *) malloc(2);
token_str[0] = curr_char;
token_str[1] = '\0';
tokens[*num_tokens] = create_token(RPAR, token_str);
(*num_tokens)++;
i++;
curr_char = input[i];
} else if (curr_char == '&') {
char *token_str = (char *) malloc(2);
token_str[0] = curr_char;
token_str[1] = '\0';
tokens[*num_tokens] = create_token(AND, token_str);
(*num_tokens)++;
i++;
curr_char = input[i];
}
else if (curr_char == '|') {
char *token_str = (char *) malloc(2);
token_str[0] = curr_char;
token_str[1] = '\0';
tokens[*num_tokens] = create_token(OR, token_str);
(*num_tokens)++;
i++;
curr_char = input[i];
}
else {
i++;
curr_char = input[i];
}
}
for (int i = 0; i < *num_tokens; i++) {
numtoken = *num_tokens;
}
return tokens;
}
/**
* function that creates a new token
* @param type token's type
* @param value token's value
* @return Token struct
*/
Token create_token(int type, char *value) {
Token token;
token.type = type;
token.value = value;
return token;
}
/**
* Function that decides on the precedence of a token.
* This method is used to decide our postfix structure.
* @param opearator
* @return int precedence
*/
int precedence(char *op) {
if (strcmp(op, "=") == 0)
return 7;
else if (strcmp(op, "(") == 0 || strcmp(op, ")") == 0)
return 0;
else if (strcmp(op, ",") == 0)
return 1;
else if (strcmp(op, "xor") == 0 || strcmp(op, "not") == 0 || strcmp(op, "ls") == 0 || strcmp(op, "rs") == 0 ||
strcmp(op, "lr") == 0 || strcmp(op, "rr") == 0)
return 5;
else if (strcmp(op, "*") == 0) {
return 4;
}
else if (strcmp(op, "+") == 0 || strcmp(op, "-") == 0)
return 3;
else if (strcmp(op, "&") == 0)
return 2;
else if (strcmp(op, "|") == 0)
return 1;
else
return -1;
}
/**
* checks whether all chars are alphabetic characters
* @param str input
* @return whether the string is all alphabetic
*/
int allAlpha(char *str) {
int i = 0;
while (str[i]) {
if (!isalpha(str[i])) {
return 0;
}
i++;
}
return 1;
}
/**
* Function that converts infix expression to postfix
* @param tokens array of tokens returned by the tokenizer function
* @param postfix expression
*/
void infix_to_postfix(Token *tokens, Token *postfix) {
Token stack[MAX_EXPR_LEN];
int top = -1;
int i, j;
for (i = 0, j = 0; i < numtoken; i++) {
// If the current character is an operand, add it to the postfix expression
if (tokens[i].type != 4 && (isdigit((int) *(tokens[i].value)) || allAlpha((tokens[i].value)))) {
postfix[j++].value = (tokens[i].value);
}
// If the current character is an operator, add it to the stack
else if (strcmp(tokens[i].value, "(") == 0) {
stack[++top].value = (tokens[i].value); // add everything after "(" until you encounter ")"
}
else if (strcmp(tokens[i].value, ")") == 0) {
// Pop operators off the stack and add them to the postfix expression until a ")" is encountered
while (top >= 0 && strcmp(stack[top].value, "(") != 0) {
postfix[j++].value = stack[top--].value;
}
// Trash the left parenthesis
top--;
}
else {
if(tokens[i].type==4 && strcmp(tokens[i+1].value, "(") != 0){
error=1;
}
// Pop operators off the stack and add them to the postfix expression until an operator with lower precedence is encountered
while (top >= 0 && precedence(stack[top].value) >= precedence(tokens[i].value)) {
if (strcmp(stack[top].value, ",") == 0) {
top--; //skip the comma and get to the function
continue;
}
postfix[j++].value = stack[top--].value;
}
// Push the current operator onto the stack
stack[++top].value = tokens[i].value;
}
}
// Pop any remaining operators off the stack and add them to the postfix expression
while (top >= 0) {
postfix[j++].value = stack[top--].value;
}
// Add null terminator to the end of the postfix expression
postfix[j].value = '\0';
numofpost = j;
}
/**
* Function that evaluates the postfix expression.
* @param postfix expression
* @return long long int, result of the expression
*/
long long int evaluate_postfix(Token *postfix) {
int stack[MAX_EXPR_LEN];
int top = -1;
int i;
for (i = 0; i < numofpost; i++) {
// If the current character is a number, push it onto the stack
if (isdigit(*postfix[i].value)) {
long long int operand = atoi(postfix[i].value);
stack[++top] = operand;
} else {
int op1, op2;
long long int result;
char op[32] = ""; //string to store operators,function names or variable names to print out result of that variable
int j = 0;
strcpy(op, postfix[i].value);
//evaluation steps
if (strcmp(op, "xor") == 0) {
op1 = (stack[top--]);
op2 = (stack[top--]);
result = op1 ^ op2;
stack[++top] = result;
} else if (strcmp(op, "not") == 0) {
op1 = stack[top--];
result = ~op1;
stack[++top] = result;
} else if (strcmp(op, "*") == 0) {
op1 = stack[top--];
op2 = stack[top--];
result = op2 * op1;
stack[++top] = result;
} else if (strcmp(op, "+") == 0) {
op1 = stack[top--];
op2 = stack[top--];
result = op2 + op1;
stack[++top] = result;
} else if (strcmp(op, "-") == 0) {
op1 = stack[top--];
op2 = stack[top--];
result = op2 - op1;
stack[++top] = result;
} else if (strcmp(op, "&") == 0) {
op1 = stack[top--];
op2 = stack[top--];
result = op2 & op1;
stack[++top] = result;
} else if (strcmp(op, "|") == 0) {
op1 = stack[top--];
op2 = stack[top--];
result = op2 | op1;
stack[++top] = result;
} else if (strcmp(op, "lr") == 0) {
op1 = stack[top--];
op2 = stack[top--];
result = (op2 << op1) | (op2 >> (sizeof(op2) * 8 - op1));
stack[++top] = result;
} else if (strcmp(op, "rr") == 0) {
op1 = stack[top--];
op2 = stack[top--];
result = (op2 >> op1) | (op2 << (sizeof(op2) * 8 - op1));
stack[++top] = result;
} else if (strcmp(op, "ls") == 0) {
op1 = stack[top--];
op2 = stack[top--];
result = (op2 << op1);
stack[++top] = result;
} else if (strcmp(op, "rs") == 0) {
op1 = stack[top--];
op2 = stack[top--];
result = (op2 >> op1);
stack[++top] = result;
}
else if (isalpha(*postfix[i].value) && strcmp(postfix[i].value, op) ==0) {
//if not a function name, this is a variable. Thus fetch the value.
result = lookup(Hashtable,op);
stack[++top] = result;
} else if (strcmp(op, ",") == 0) {
//skip
continue;
} else {
error = 1;
break;
}
}
}
if (top != 0) {
error = 1;
}
return stack[top];
}
/**
* Function that strips whitespaces from a string.
* @param str
* @return str without whitespaces.
*/
char *trim(char *str) {
char *end;
// Trim leading whitespace
while (isspace((unsigned char) *str)) {
str++;
}
if (*str == 0) { // All spaces?
return str;
}
// Trim trailing whitespace
end = str + strlen(str) - 1;
while (end > str && isspace((unsigned char) *end)) {
end--;
}
// Write new null terminator
*(end + 1) = '\0';
return str;
}
/**
* Function that checks whether a function is in the correct form within the input string.
* For example: xor(3,4) valid and xor((3,4)) invalid.
* @param str input string
* @return validness of the function use.
*/
int is_valid_function(char *str) {
int degree = 0;
int validness = 0;
for (int i = 0; i < strlen(str); i++) {
//increase the degree when you encounter a left paranthesis.
if (str[i] == '(') {
degree++;
}
//decrease the degree when you encounter a left paranthesis.
else if (str[i] == ')') {
degree--;
}
else if (str[i] == ',' && degree == 1) {
//if there is a comma bw 2 first degree parantheses, function syntax is correct.
validness = 1;
}
}
return validness;
}
/**
* Function that checks whether the whole input string has a valid use for a particular fuction.
* This is done by iterating through the whole string.
* @param line to be iterated
* @param function_name function name to be checked
* @return validness of a function's use in the whole string.
*/
int check_function(char *line, char *function_name) {
char copyline[strlen(line)];
strcpy(copyline, line);
char *func_pos = strstr(copyline, function_name);
int namesize = strlen(function_name);
int broken_function = 0;
while (func_pos != NULL) {
func_pos = strstr(func_pos, function_name);
if (func_pos != NULL) {
func_pos = func_pos + namesize; // second part is the value
int valid_function = is_valid_function(func_pos);
if (valid_function == 1) {
func_pos = strstr(func_pos, function_name);
continue;
} else {
broken_function = 1;
break;
}
}
}
if (broken_function) {
return 0;
} else {
return 1;
}
}
/**
* Function that checks whether the whole input string has a valid use for a particular operator.
* This is done by iterating through the whole string.
* for example, 3-4 valid but 3(-)4 is invalid.
* @param line to be iterated
* @param op_name operator to be checked
* @return validness of a operators use in the whole string.
*/
int is_valid_operator(char *line, char *op_name) {
char copyline[strlen(line)];
strcpy(copyline, line);
char *op_pos = strstr(copyline, op_name);
int namesize = 1;
int broken_op = 0;
while (op_pos != NULL) {
op_pos = strstr(op_pos, op_name);
if (op_pos != NULL) {
op_pos--; //include the paranthesis
if (*(op_pos) == '(' || *(op_pos + 2) == ')') {
broken_op = 1;
break;
} else {
op_pos += 3;
op_pos = strstr(op_pos, op_name);
continue;
}
}
}
if (broken_op) {
return 0; //not valid :(
} else {
return 1;
}
}
int main() {
Hashtable = (table *) malloc(sizeof(table));
init_table(Hashtable);
char line[257] = ""; //input line will be stored in here
printf(">");
//start taking inputs
while (fgets(line, sizeof(line), stdin) !=NULL) {
//blankline inputs
if (strcmp(line, "\n") == 0 || strcmp(line, " \n") == 0 || strcmp(line, "\t\n") == 0) {
printf(">");
continue;
}
//erroneous inputs
//1)empty paranthesis
char *paranthesis_pos = strstr(line, "()");
if (paranthesis_pos != NULL) {
printf("Error!\n");
printf(">");
continue;
}
//2)lines starting with operations. i.e unary cases like +1+b
else if (line[0] == '+' || line[0] == '-' || line[0] == '*' || line[0] == '&' || line[0] == '|') {
printf("Error!\n");
printf(">");
continue;
}
char *commentPos = strchr(line, '%');
if (commentPos != NULL) {
*commentPos = '\0'; //trim the expression to the % part included \0
}
//3)unbalanced paranthesis expressions
else if (!is_balanced(line)) {
printf("Error!\n");
printf(">");
continue;
}
//4) broken function calls. We need to evalute the whole string for all the 5 functions.
else if (!check_function(line, "xor")) {
printf("Error!\n");
printf(">");
continue;
} else if (!check_function(line, "ls")) {
printf("Error!\n");
printf(">");
continue;
} else if (!check_function(line, "rs")) {
printf("Error!\n");
printf(">");
continue;
} else if (!check_function(line, "rr")) {
printf("Error!\n");
printf(">");
continue;
} else if (!check_function(line, "lr")) {
printf("Error!\n");
printf(">");
continue;
}
//5) we won't allow operators inside paranthesis like 3(+)4
else if (!is_valid_operator(line, "+")) {
printf("Error!\n");
printf(">");
continue;
} else if (!is_valid_operator(line, "-")) {
printf("Error!\n");
printf(">");
continue;
} else if (!is_valid_operator(line, "&")) {
printf("Error!\n");
printf(">");
continue;
} else if (!is_valid_operator(line, "*")) {
printf("Error!\n");
printf(">");
continue;
} else if (!is_valid_operator(line, "|")) {
printf("Error!\n");
printf(">");
continue;
}
//ASSIGNMENT CHECK
char *pos = strchr(line, '=');
if (pos != NULL) { // if there is an assignment statement
*pos = '\0'; // replace = with \0
char *variable = line; // first part is the variable
char *value = pos + 1; // second part is the value
variable = trim(variable);
value = trim(value);
//reserved keywords cannot be variable names.
if (!allAlpha(variable)) {
printf("Error!\n");
printf(">");
continue;
} else if (strcmp("xor", variable) == 0) {
printf("Error!\n");
printf(">");
continue;
} else if (strcmp("ls", variable) == 0) {
printf("Error!\n");
printf(">");
continue;
} else if (strcmp("rs", variable) == 0) {
printf("Error!\n");
printf(">");
continue;
} else if (strcmp("rr", variable) == 0) {
printf("Error!\n");
printf(">");
continue;
} else if (strcmp("lr", variable) == 0) {
printf("Error!\n");
printf(">");
continue;
} else if (strcmp("not", variable) == 0) {
printf("Error!\n");
printf(">");
continue;
}
//No error so far, we can evaluate the right-hand side of the assignment. i.e the expression part.
Token postfixx[257];
Token tokens[257];
int numtok = 0;
tokenize(value, tokens, &numtok);
infix_to_postfix(tokens, postfixx);
//If there has been an error in tokenization.
if (error == 1) {
printf("Error!\n");
printf(">");
error = 0; //reset
continue;
}
long long int res = evaluate_postfix(postfixx);
//add the result to the hashtable.
insert(Hashtable, variable, res);
printf(">");
continue;
}
//EXPRESSION EVALUATION
else { //normal expression input exists. no assignment statement
Token postfixx[257];
Token tokens[257];
int numtok = 0;
tokenize(line, tokens, &numtok);
//comment inputs
if (numtok == 0) {
printf(">");
error = 1;
continue;
}
infix_to_postfix(tokens, postfixx);
long long int res = evaluate_postfix(postfixx);
if (error == 1) {
printf("Error!\n");
printf(">");
error = 0; //reset
continue;
}
if (!error) {
printf("%d\n", res);
}
printf(">");
continue;
}
}
}