-
Notifications
You must be signed in to change notification settings - Fork 0
/
tester.c
4366 lines (3970 loc) · 166 KB
/
tester.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
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <stdbool.h>
#include<time.h>
#define MAX_RULES 65
#define HASH_TABLE_SIZE 181
#define MAX_LEN 30
#define MAX_BRANCH 10
#define NUM_NT 57
#define NUM_T 59
#define NUM_KEYWORD 30
#define KEYWORD_HASH_TABLE_SIZE 59
#define SYMBOL_TABLE_SIZE 30
typedef enum
{
T_RNUM, T_NUM,T_ID, T_INTEGER,T_REAL,T_BOOLEAN,T_OF,T_ARRAY,T_START,T_END,T_DECLARE,T_MODULE,T_DRIVER,
T_PROGRAM,T_GET_VALUE,T_PRINT,T_USE,T_WITH,T_PARAMETERS,T_TRUE,T_FALSE,T_TAKES,T_INPUT,
T_RETURNS,T_AND,T_OR,T_FOR,T_IN,T_SWITCH,T_CASE,T_BREAK,T_DEFAULT,T_WHILE,
T_PLUS,T_MINUS,T_DIV,T_MUL,T_LT,T_LE,T_GE,T_GT,T_EQ,T_NE,T_DEF,T_COLON,T_RANGEOP,
T_SEMICOL,T_COMMA,T_ASSIGNOP,T_SQBO,T_SQBC,T_BO,T_BC,T_COMMENTMARK,T_DRIVERDEF,
T_DRIVERENDDEF, T_EPS, T_DOLLAR, T_ENDDEF
}tokenizer;
typedef enum{
NT_program, NT_moduleDeclarations, NT_moduleDeclaration, NT_otherModules, NT_driverModule, NT_module,
NT_ret, NT_input_plist, NT_N1, NT_output_plist, NT_N2, NT_dataType, NT_range_arrays, NT_type, NT_moduleDef,
NT_statements, NT_statement, NT_ioStmt, NT_var, NT_var_id_num, NT_boolconstt, NT_whichID,
NT_simpleStmt, NT_assignmentStmt, NT_whichStmt, NT_lvalueIDStmt, NT_lvalueARRStmt, NT_index,
NT_moduleReuseStmt, NT_optional, NT_idList, NT_N3, NT_expression, NT_u1, NT_new_NT, NT_unary_op, NT_arithmeticOrBooleanExpr,
NT_N7, NT_AnyTerm, NT_N8, NT_arithmeticExpr, NT_N4, NT_op1, NT_term, NT_N5, NT_op2, NT_factor, NT_logicalOp, NT_relationalOp,
NT_declareStmt, NT_conditionalStmt, NT_caseStmts, NT_N9, NT_value, NT_default, NT_iterativeStmt, NT_range
}NonTerminals;
typedef struct
{
char lexeme[30];
tokenizer Token;
int line_number;
// type var_type;
}tokenInfo;
typedef union{
tokenizer t;
NonTerminals nt;
}Symbol;
typedef struct {
int level;
int line_number;
int enum_value;
char str[30];
bool is_terminal;
}ASTdata_info;
struct ast{
struct ast *left_child;
struct ast *right_child;
struct ast *forward;
struct ast *parent;
ASTdata_info data;
};
typedef struct ast ASTNode;
struct node{
Symbol s;
int is_terminal; //-1 for not keyword
struct node *next;
};
// have to add a boolean is_keyword.
typedef struct node NODE;
typedef struct{
//stores the head of the hash table of that row
struct node *head;
//size of that row
int size;
}mappingTable[HASH_TABLE_SIZE];
mappingTable m;
struct keywordNode{
int keyword_pos;
struct keywordNode *next;
};
typedef struct keywordNode KEYWORD_NODE;
typedef struct{
struct keywordNode *head;
int size;
}keywordTable[KEYWORD_HASH_TABLE_SIZE];
keywordTable keys;
int first_done[NUM_NT];
unsigned long long int First[NUM_NT];
unsigned long long int Follow[NUM_NT];
int eps_enum = 56;
//Structure for the rhs of a production rule
struct rhs_node{
Symbol s;
int is_terminal;
struct rhs_node *next;
struct rhs_node *prev;
};
//Just rename it
typedef struct rhs_node RHS_NODE;
//Stores the grammar production rules (line by line)
typedef struct{
NonTerminals sym;
RHS_NODE *head[MAX_BRANCH];
RHS_NODE *tail[MAX_BRANCH];
int num_of_branch; // IMP: If there is only 1 branch in Grammar NT_Rule then num_of_branch = 0 ie it is zero indexed
}Grammar[MAX_RULES];
typedef struct{
int NT_rule ,branch_no ,isnonempty,count;
} PARSE_TABLE[NUM_NT][NUM_T];
struct tree_node{
struct tree_node *next;//for sibling
struct tree_node *parent;
struct tree_node *child;//corresponding to first child
int branch_no;
int level;
int line_number;
int enum_value;
char str[30];
bool is_terminal;
};
typedef struct tree_node tree_node;
struct stack_node{
tree_node* data;
struct stack_node* next;
};
typedef struct stack_node stack_node;
struct parseTree{
tree_node *root;
tree_node *current;
// int no_of_level;
};
typedef struct parseTree parseTree;
typedef enum{INTEGER, BOOLEAN, REAL} datatype;
int current_offset;
struct variable
{
datatype dt;
};
struct array
{
datatype dt;
bool is_static;
int s_idx, e_idx;
};
struct struct_type{
int is_array; //0:variable, 1:array
union
{
struct variable v;
struct array a;
}t;
};
//Function parameters
struct parameter
{
struct struct_type type;
struct parameter *next;
};
typedef struct variable variable;
typedef struct array array;
typedef struct struct_type type;
typedef struct parameter parameter;
//Function Symbol table
struct func_hash_entry{
char lexeme[21];
int declare_lno;
int define_lno;
bool called;
// int lno; //First come first
// funcStatus status;
struct parameter *inputList;
struct parameter *outputList;
struct var_st *child;//link to variable symbol table
struct func_hash_entry *next;
};
struct func_st{
int size;
struct func_hash_entry*head;
}func_st_root[SYMBOL_TABLE_SIZE];
typedef struct func_st_entry func_st_entry;
typedef struct func_hash_entry func_hash_entry;
//Variable Symbol table
struct var_hash_entry{
char lexeme[21];
int lno;
int flag; //0: input, 1: output, 2:local, 3:second_declaration and in input_list
int width;
int offset;
struct struct_type type;
struct var_hash_entry *next;
};
struct var_st{
int lno;
int depth;
struct var_hash_table_struct{
int size;
struct var_hash_entry*head;
}var_hash_table[SYMBOL_TABLE_SIZE];
struct var_st *next;
struct var_st *child;
int tag; //0: function ST, 1:Variable ST //Parent type
union st_type{
struct func_hash_entry *f_parent;
struct var_st *v_parent;
} parent;
struct var_st *root_var_st;
};
typedef struct var_hash_entry var_hash_entry;
typedef struct var_st var_st;
Grammar g;
PARSE_TABLE tb;
stack_node *head=NULL;
int counter=0;
unsigned long hash(char *sym);
unsigned long keyword_hash(char *sym);
void populate_table();
void populate_keyword_table();
NODE string_to_enum(char* input);
NODE is_keyword(char *input);
tokenInfo* tokenize(char *str);
void getstream();
tokenInfo* getNextToken();
void removeComments(char *testcaseFile, char *cleanFile);
void populate_grammar(char * file_name);
void disp_forward();
void disp_backward();
void first(int nt_enum);
//void calculate_first();
unsigned long long int follow_set();
//void calculate_follow_set();
void ComputeFirstAndFollowSets();
void disp_first();
void disp_follow();
//void fill_parse_table();
void createParseTable();
void print_parse_table();
tree_node* initialize();
void push(stack_node* temp_node);
tree_node* peek();
tree_node* pop();
void display_stack();
//parseTree* create_tree();
parseTree* parseInputSourceCode();
int getBytes(ASTNode *node);
type getType(ASTNode *node);
void initialise_var_hash_table(struct var_hash_table_struct* table);
void initialise_func_st_root();
unsigned long id_hash(char *sym);
void insert_var_hash(var_hash_entry* entry, var_st* table);
void insert_func_hash(func_hash_entry* entry);
func_hash_entry* find_func_hash(char* lex);
var_hash_entry* find_var(char* lex, int lno, var_st* table);
int check_var(char* lex, var_st* table);
void fill_symbol_table(ASTNode *current, var_st *current_var_st,char for_string[10][21],int size_for_string);
//void semantic_check(ASTNode *current, var_st *current_var_st);
//void TraverseTree_st(ASTNode *current, var_st *current_var_st,var_st *prev_child_var_st);
void semantic_check(ASTNode *current, var_st *current_var_st,var_st *prev_child_var_st);
void print_var_st();
void print_st();
void declaration_varst(var_st *table,FILE*f);
int check_operator(ASTNode *ast_node);
void make_code(ASTNode* ast_node , var_st* table , FILE *f);
void operator_processor(ASTNode *ast_node , var_st *table , FILE *f, int operator);
void expr_code_writer(ASTNode *ast_node , var_st *table , FILE *f , int child_flag , int operator);
void terminals_handler(ASTNode *ast_node , var_st *table , FILE *f);
int is_left_child(ASTNode *node);
char *lexeme_generator(char * lexeme , int lno);
void printParseTree(tree_node *curr_node, FILE *outfile);
char buffer1[256];
char buffer2[256];
char lexeme_buffer[20]; //lexeme that is passed to tokenizer
int eof_buffer = 0; //buffer that has EOF
int eof_marker = -1; //position of EOF in the buffer
int curr_buffer = 1; //active buffer
int buffer_itr = 0; //buffer iterator
bool lex_error=false;
bool comment_flag=false; //true means that comment is ongoing
bool comment_flag_flag=false;
bool buffer1_empty = true;
bool buffer2_empty = true;
int lineNumber = 1;
extern FILE *fp;
bool delimiter_flag = false;
bool error_long_id = false;
bool error_long_id_print = false;
bool file_finished=false;
int err_count = 0;
datatype type_flag;
//type_flag update.
int cmp_count = 0;
//have to make it global as it gets locally chutiyapa
char temp_lexeme[50];
char *terminal_map[NUM_T] = {
"RNUM","NUM","ID","INTEGER","REAL","BOOLEAN","OF","ARRAY","START","END","DECLARE","MODULE","DRIVER",
"PROGRAM","GET_VALUE","PRINT","USE","WITH","PARAMETERS","TRUE","FALSE","TAKES","INPUT",
"RETURNS","AND","OR","FOR","IN","SWITCH","CASE","BREAK","DEFAULT","WHILE",
"PLUS","MINUS","DIV","MUL","LT","LE","GE","GT","EQ","NE","DEF","COLON","RANGEOP",
"SEMICOL","COMMA","ASSIGNOP","SQBO","SQBC","BO","BC","COMMENTMARK","DRIVERDEF","DRIVERENDDEF",
"EPS", "DOLLAR", "ENDDEF"
};
char *non_terminal_map[NUM_NT] = {
"program", "moduleDeclarations", "moduleDeclaration", "otherModules", "driverModule", "module",
"ret", "input_plist", "N1", "output_plist", "N2", "dataType", "range_arrays", "type", "moduleDef",
"statements", "statement", "ioStmt", "var", "var_id_num", "boolconstt", "whichID",
"simpleStmt", "assignmentStmt", "whichStmt", "lvalueIDStmt", "lvalueARRStmt", "index",
"moduleReuseStmt", "optional", "idList", "N3", "expression", "u1", "new_NT", "unary_op", "arithmeticOrBooleanExpr",
"N7", "AnyTerm", "N8", "arithmeticExpr", "N4", "op1", "term", "N5", "op2", "factor", "logicalOp", "relationalOp",
"declareStmt", "conditionalStmt", "caseStmts", "N9", "value", "default", "iterativeStmt", "range"
};
char *keyword_map[NUM_KEYWORD] = {
"integer", "real", "boolean", "of", "array", "start", "end", "declare", "module", "driver", "program",
"get_value", "print", "use", "with", "parameters", "true", "false", "takes", "input",
"returns", "AND", "OR", "for", "in", "switch", "case", "break", "default", "while"
};
int ptree_nodes = 0, ast_nodes = 0;
unsigned long hash(char *sym){
const int p = 53;
unsigned long hash = 0;
unsigned long power = 1;
int i = 0;
while(sym[i] != '\0')
{
hash = (hash + (sym[i] - 'a' +1) * power) % HASH_TABLE_SIZE;
power= (power *p) % HASH_TABLE_SIZE;
i++;
}
return hash;
}
unsigned long keyword_hash(char *sym){
const int p = 53; // because both lower and uppercase letters are present
unsigned long hash = 0;
unsigned long power = 1;
int i = 0;
while(sym[i] != '\0')
{
hash = (hash + (sym[i] - 'a' +1) * power) % KEYWORD_HASH_TABLE_SIZE;
power= (power *p) % KEYWORD_HASH_TABLE_SIZE;
i++;
}
return hash;
}
void populate_table(){
char temp1[MAX_LEN], temp2[MAX_LEN];
//Inserting terminals into hash table
int ct1 = 0, ct2 = 0;
for(int term = 0; term < NUM_T ; term++)
{
unsigned long pos_in_table = hash(terminal_map[term]);
NODE *n = (NODE *)malloc(sizeof(NODE));
n->is_terminal = 1;
n->next = m[pos_in_table].head;
(n->s).t = ct1;
m[pos_in_table].head = n;
m[pos_in_table].size++;
ct1++;
}
for(int non_term = 0; non_term < NUM_NT ; non_term++)
{
unsigned long pos_in_table = hash(non_terminal_map[non_term]);
NODE *n = (NODE *)malloc(sizeof(NODE));
n->is_terminal = 0;
n->next = m[pos_in_table].head;
(n->s).nt = (NonTerminals)ct2;
m[pos_in_table].head = n;
m[pos_in_table].size++;
ct2++;
}
}
void populate_keyword_table(){
for(int keyword_no = 0; keyword_no < NUM_KEYWORD; keyword_no ++){
unsigned long pos_in_table = keyword_hash(keyword_map[keyword_no]);
KEYWORD_NODE *the_keyword = (KEYWORD_NODE *)malloc(sizeof(KEYWORD_NODE));
the_keyword->keyword_pos = keyword_no;
the_keyword->next = keys[pos_in_table].head;
keys[pos_in_table].head = the_keyword;
keys[pos_in_table].size++;
}
}
NODE string_to_enum(char* input)
{
Symbol s1;
NODE h1;
unsigned long pos = hash(input);
NODE *h = m[pos].head;
while(h!=NULL) //loop over all collisions
{
if(h->is_terminal)
{
if(strcmp(input ,terminal_map[(h->s).t]) == 0)
{
h1.is_terminal = 1;
h1.s = h->s;
return h1;
}
}
else
{
if(strcmp(input ,non_terminal_map[(h->s).nt]) == 0)
{
h1.is_terminal = 0;
h1.s = h->s;
return h1;
}
}
h = h->next;
}
return h1;
}
NODE is_keyword(char *input){
char getting_token[30];
unsigned long pos = keyword_hash(input);
KEYWORD_NODE *h = keys[pos].head;
NODE ans;
int found = 0;
while(h != NULL){
if(strcmp(input, keyword_map[h->keyword_pos]) == 0){
int i = 0;
while(input[i] != '\0'){
getting_token[i] = input[i];
if(input[i] >= 'a' && input[i] <= 'z'){
getting_token[i] = (input[i] - 32);
}
i++;
}
getting_token[i] = '\0';
ans = string_to_enum(getting_token);
return ans;
}
h = h->next;
}
ans.is_terminal = -1;
return ans;
}
tokenInfo* tokenize(char *str){//str denotes the line for which i have to give the token
int i=0;//important variable
// i has to be maintained for next token
char ch;//parsing the string and used in switch
lex_error=false; //true when lexecal error is encountered
comment_flag_flag = false; //true only when the lexeme is "**"
delimiter_flag = false;
tokenInfo *ans = (tokenInfo*)malloc(sizeof(tokenInfo));//the one I will be returning in get_Token()
int k=0; //initialsing lexeme to null
for(k=0; k<30;k++){
ans->lexeme[k] = '\0';
}
ch=str[0];
ans->line_number=lineNumber;
while(ch!='\0'){
if(ch=='\n'){
lineNumber++;
ans->line_number=lineNumber;
i++;
ch = str[i];
delimiter_flag=true;
error_long_id = false;
break;
}
else if(ch==' ' || ch=='\t'){
i++;
ch = str[i];
delimiter_flag=true;
error_long_id = false;
break;
}
else if((ch>='a' && ch<='z') || (ch>='A' && ch<='Z') || (error_long_id == true && ((ch == '_' ) || (ch>='0' && ch<='9')))){
char temp_lex[30];
int k;
for(k=0;k<30;k++)
temp_lex[k]='\0';
temp_lex[0]=ch;
int j=1;
i++;
ch=str[i];
while((ch>='a' && ch<='z') || (ch>='A' && ch<='Z') || (ch>='0' && ch<='9') || (ch == '_')){
temp_lex[j]=ch;
j++;
if(j==21){
error_long_id = true;
j=20;
break;
}
i++;
ch=str[i];
}
for(k=0;k<j;k++){
ans->lexeme[k]=temp_lex[k];
}
ans->lexeme[k]='\0';
if(error_long_id){
lex_error=true;
break;
}
if(j>20){
strcpy(ans->lexeme,temp_lex);
lex_error=true;
}
NODE keyword_info;
keyword_info = is_keyword(ans->lexeme);
if(keyword_info.is_terminal!=-1){
ans->Token = keyword_info.s.t;
}
else{
ans->Token=T_ID;
}
}
else if(ch>='0' && ch<='9'){
char temp_lex[30];
int k;
for(k=0;k<30;k++)
temp_lex[k]='\0';
temp_lex[0]=ch;
int j=1;
i++;
ch=str[i];
while(ch>='0' && ch<='9'){
temp_lex[j]=ch;
j++;
i++;
ch=str[i];
}
if(ch=='.'){
temp_lex[j]=ch;
i++;
j++;
ch=str[i];
if(ch>='0' && ch<='9'){
temp_lex[j]=ch;
j++;
i++;
ch=str[i];
while(ch>='0' && ch<='9'){
temp_lex[j]=ch;
i++;
j++;
ch=str[i];
}
if(ch=='e' || ch=='E'){
temp_lex[j]=ch;
j++;
i++;
ch=str[i];
if(ch>='0' && ch<='9'){
temp_lex[j]=ch;
j++;i++;
ch=str[i];
while(ch>='0' && ch<='9'){
temp_lex[j]=ch;
j++;i++;
ch=str[i];
}
strcpy(ans->lexeme,temp_lex);
ans->Token=T_RNUM;
}
else{
//+-
if(ch=='+' || ch=='-'){
temp_lex[j]=ch;
j++;i++;
ch=str[i];
if(ch>='0' && ch<='9'){
temp_lex[j]=ch;
j++;i++;
ch=str[i];
while(ch>='0' && ch<='9'){
temp_lex[j]=ch;
j++;i++;
ch=str[i];
}
strcpy(ans->lexeme,temp_lex);
ans->Token=T_RNUM;
}
else{
strcpy(ans->lexeme,temp_lex);
lex_error=true;
}
}
else{
strcpy(ans->lexeme,temp_lex);
lex_error=true;
}
}
}
else{
strcpy(ans->lexeme,temp_lex);
ans->Token=T_RNUM;
}
}
else{
if(ch=='.'){
int k=0;
for(k=0;k<j-1;k++)
ans->lexeme[k]=temp_lex[k];
ans->Token=T_NUM;
i=i-1;
}
else{
strcpy(ans->lexeme,temp_lex);
lex_error=true;
}
}
}
else{
strcpy(ans->lexeme,temp_lex);
ans->Token=T_NUM;
}
}
else if(!((ch>='a' && ch<='z') || (ch>='A' && ch<='Z') || (ch>='0' && ch<='9'))){
error_long_id = false;
switch(ch){
case '+':{
strcpy(ans->lexeme,"+");
ans->Token=T_PLUS;
i+=1;
break;
}
case '-':{
strcpy(ans->lexeme,"-");
ans->Token=T_MINUS;
i+=1;
break;
}
case ';':{
strcpy(ans->lexeme,";");
ans->Token=T_SEMICOL;
i+=1;
break;
}
case ',':{
strcpy(ans->lexeme,",");
ans->Token=T_COMMA;
i+=1;
break;
}
case '=':{
if(i+1<strlen(str) && str[i+1]=='='){
strcpy(ans->lexeme,"==");
ans->Token=T_EQ;
i+=2;
}
else
{
strcpy(ans->lexeme,"=");
lex_error=true;
i+=1;
}
break;
}
case '<':{
if(i+1<strlen(str) && str[i+1]=='<'){
if(i+2<strlen(str) && str[i+2]=='<'){
strcpy(ans->lexeme,"<<<");
ans->Token=T_DRIVERDEF;
i+=3;
}
else{
strcpy(ans->lexeme,"<<");
ans->Token=T_DEF;
i+=2;
}
}
else if(i+1<strlen(str) && str[i+1]=='='){
strcpy(ans->lexeme,"<=");
ans->Token=T_LE;
i+=2;
}
else{
strcpy(ans->lexeme,"<");
ans->Token=T_LT;
i+=1;
}
break;
}
case '>':{
if(i+1<strlen(str) && str[i+1]=='>'){
if(i+2<strlen(str) && str[i+2]=='>'){
strcpy(ans->lexeme,">>>");
ans->Token=T_DRIVERENDDEF;
i+=3;
}
else{
strcpy(ans->lexeme,">>");
ans->Token=T_ENDDEF;
i+=2;
}
}
else if(i+1<strlen(str) && str[i+1]=='='){
strcpy(ans->lexeme,">=");
ans->Token=T_GE;
i+=2;
}
else{
strcpy(ans->lexeme,">");
ans->Token=T_GT;
i+=1;
}
break;
}
case '*':{
if(i+1<strlen(str) && str[i+1]!='*'){
strcpy(ans->lexeme,"*");
ans->Token=T_MUL;
i+=1;
}
else if(i+1<strlen(str) && str[i+1]=='*'){
i+=2;
comment_flag_flag = true;
if(comment_flag==false){
comment_flag = true;
}
else{
comment_flag = false;
}
}
break;
}
case ':':{
if(i+1<strlen(str) && str[i+1]=='='){
strcpy(ans->lexeme,":=");
ans->Token=T_ASSIGNOP;
i+=2;
}
else{
strcpy(ans->lexeme,":");
ans->Token=T_COLON;
i+=1;
}
break;
}
case '.':{
if(i+1<strlen(str) && str[i+1]=='.'){
strcpy(ans->lexeme,"..");
ans->Token=T_RANGEOP;
i+=2;
}
else{
strcpy(ans->lexeme,".");
lex_error=true;
i+=1;
}
break;
}
case '!':{
if(i+1<strlen(str) && str[i+1]=='='){
strcpy(ans->lexeme,"!=");
ans->Token=T_NE;
i+=2;
}
else{
strcpy(ans->lexeme,"!");
lex_error=true;
i+=1;
}
break;
}
case ']':{
strcpy(ans->lexeme,"]");
ans->Token=T_SQBC;
i+=1;
break;
}
case '[':{
strcpy(ans->lexeme,"[");
ans->Token=T_SQBO;
i+=1;
break;
}
case '(':{
strcpy(ans->lexeme,"(");
ans->Token=T_BO;
i+=1;
break;
}
case ')':{
strcpy(ans->lexeme,")");
ans->Token=T_BC;
i+=1;
break;
}
case '/':{
strcpy(ans->lexeme,"/");
ans->Token=T_DIV;
i+=1;
break;
}
default:{
sprintf(ans->lexeme,"%c",ch);
lex_error=true;
i+=1;
break;
}
}
}
break;
}
buffer_itr += i;
return(ans);
}
void getstream(){
char ch;
if(buffer1_empty == true && eof_buffer==0){
int itr = 0;
while (itr<256 && (ch = fgetc(fp)) != EOF)
{
buffer1[itr] = ch;
itr++;
}
if(itr!=256){
eof_marker = itr;
eof_buffer = 1;
}
buffer1_empty = false;
}
if(buffer2_empty == true && eof_buffer==0){
int itr = 0;
while (itr<256 && (ch = fgetc(fp)) != EOF)
{
buffer2[itr] = ch;
itr++;
}
if(itr!=256){
eof_marker = itr;
eof_buffer = 2;
}
buffer2_empty = false;
}
if(buffer_itr > 256-1){
buffer_itr-=256;
if(curr_buffer == 1){
curr_buffer = 2;
buffer1_empty = true;
}
else if(curr_buffer == 2){
curr_buffer =1;
buffer2_empty = true;
}
}
if(curr_buffer == 1){
int i=0;
while(i<21){
if((eof_buffer == 1 && (buffer_itr+i>=eof_marker)) || (eof_buffer == 2 && (buffer_itr+i-256>=eof_marker))){
lexeme_buffer[i] = '\0';
}
else if(buffer_itr+i<256){
lexeme_buffer[i] = buffer1[buffer_itr+i];
}
else{
lexeme_buffer[i] = buffer2[buffer_itr+i-256];
}
i++;
}
}
else if(curr_buffer == 2){
int i=0;
while(i<21){
if((eof_buffer == 2 && (buffer_itr+i>=eof_marker)) || (eof_buffer == 1 && (buffer_itr+i-256>=eof_marker))){
lexeme_buffer[i] = '\0';
}
else if(buffer_itr+i<256){
lexeme_buffer[i] = buffer2[buffer_itr+i];
}
else{
lexeme_buffer[i] = buffer1[buffer_itr+i-256];
}
i++;
}
}
}
tokenInfo* getNextToken(){
tokenInfo *a = (tokenInfo *)malloc(sizeof(tokenInfo));
if(curr_buffer == eof_buffer && buffer_itr>=eof_marker){
if(comment_flag==true){
printf("\nLexical Error because end of comment not found");
}
else{
printf("\nEOF reached");
}
printf("\nTerminating Lexer\n");
file_finished = true;
a->Token = T_DOLLAR;
strcpy(a->lexeme, "DOLLAR");
}
else{
getstream();
a = tokenize(lexeme_buffer); //NOTE: line number will be taken care of in anubhav
if(comment_flag==true || comment_flag_flag==true || delimiter_flag==true){
a = getNextToken();
}
else if(lex_error){
if(error_long_id==true){
if(error_long_id_print == false){
error_long_id_print = true;
printf("\nLine Number %d Lexeme Error Lexeme %s",a->line_number,a->lexeme);
err_count+=1;
}
else{
printf("%s",a->lexeme);
}
}
else{
error_long_id_print = false;
printf("\nLine Number %d Lexeme Error Lexeme %s",a->line_number,a->lexeme);
err_count+=1;
}
a = getNextToken();
}
else{
return a;
}
}