-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlexer.c
More file actions
606 lines (513 loc) · 15.4 KB
/
lexer.c
File metadata and controls
606 lines (513 loc) · 15.4 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
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
// BATCH NUMBER - 57
// Authors:-
// Rishabh Singh 2012B4A7691P f2012691@pilani.bits-pilani.ac.in
// Anchit Jain 2012B3A7570P f2012570@pilani.bits-pilani.ac.in
#include "lexerDef.h"
int NUM_KEYWORD = 0;
void insertInTrie1(trieNode1 *root,char *st,int id)
{
int j ;
int len = strlen(st),i;
trieNode1 *head = root;
trieNode1 *x;
for(i = 0;i < len;i++)
{
if(head->child[st[i]-'$'] == NULL)
{
x = (trieNode1 *)malloc(sizeof(trieNode1));
for(j = 0;j < 26;j++)
{
x->child[j] = NULL;
}
x->num = st[i]-'$';
head->child[st[i]-'$'] = x;
head = head->child[st[i]-'$'];
}
else
{
head = head->child[st[i]-'$'];
}
}
head->isleaf = 1;
head->id = id;
}
int findInTrie1(trieNode1 *root,char *st)
{
if(root == NULL)
return -1;
int len = strlen(st),i;
for(i = 0;i < len;i++)
{
if(root->child[st[i]-'$'] == NULL)
return -1;
root = root->child[st[i]-'$'];
}
if(root->isleaf)
return root->id;
else
return -1;
}
// Returns a new DFA
DFA *getNewDFA()
{
DFA *dfa = NULL;
dfa = malloc(sizeof(DFA));
if (dfa==NULL)
{
fprintf(stderr, "Failed to allocate memory for DFA\n");
return dfa;
}
// Initially the DFA has no states and current state is undefined
dfa->start_state = 0;
dfa->total_states = NULL;
dfa->cur_state = 0;
return dfa;
}
// Checks if a state is final or not.
int final_or_not(DFA *dfa, int cur_state)
{
return dfa->total_states[cur_state].is_final;
}
// Checks whether inp is extendable with cur_state.
int extendable_or_not(DFA *dfa, int cur_state, char inp)
{
return (dfa->total_states[cur_state].next_state[(int) inp])!=NULL;
}
// Check if a string st is keyword from hash_keyword_token.
int check_hash(char *st)
{
int i;
for (i = 0; i < NUM_KEYWORD; i++)
{
if (!strcmp(st, hash_keyword_token[i]))
return 1;
}
return 0;
}
// Set the number of states for the dfa and initialize the next_state pointer of every state to NULL.
void setNumStates(DFA *dfa, int numStates)
{
if (dfa==NULL)
{
fprintf(stderr, "Error Initialize dfa");
exit(-1);
}
int i, j;
dfa->num_states = numStates;
dfa->total_states = (STATE *) malloc(sizeof(STATE)*numStates);
for (i = 0; i < numStates; i++)
{
dfa->total_states[i].state_number = i;
dfa->total_states[i].is_final = 0;
for (j = 0; j < LIMIT; j++)
{
dfa->total_states[i].next_state[j] = NULL;
}
}
}
/* Inserts transition into DFA.
* If inptype == 0 i.e single character than input is ascii value of actual input.
* If inptype == 1 i.e character range than input is index of that range in mappings array.
*/
void transition(int input, int start, int end, int inptype, DFA *dfa, CHAR_RANGE *mappings)
{
if (inptype==0)
{
dfa->total_states[start].next_state[input] = &(dfa->total_states[end]);
}
else
{
int i;
for (i = mappings[input - 48].start; i <= mappings[input - 48].end; i++)
{
dfa->total_states[start].next_state[i] = &(dfa->total_states[end]);
}
}
}
// Return state number of next state after consuming inp on curr_st.
int get_transition(DFA *dfa, int curr_st, char inp)
{
STATE *state_ptr = dfa->total_states[curr_st].next_state[(int) inp];
if (state_ptr==NULL)
return 0;
return state_ptr->state_number;
}
// Populate the DFA structure by reading from filename containing DFA information in prescribed format.
DFA *populateDfa(char *filename)
{
DFA *dfa;
dfa = getNewDFA();
FILE *fp;
fp = fopen(filename, "r");
if (fp==NULL)
{
fprintf(stderr, "%s file not found", filename);
exit(-1);
}
char buffer[1000];
int numStates, numFinalStates, numMapping, i, m;
fscanf(fp, "%s %d", buffer, &numStates);
setNumStates(dfa, numStates);
fscanf(fp, "%s %d", buffer, &numFinalStates);
//To skip lines
fgets(buffer, sizeof(buffer), fp);
fgets(buffer, sizeof(buffer), fp);
fgets(buffer, sizeof(buffer), fp);
hashFinalToken = (char **) malloc(sizeof(char *)*numStates);
for (i = 0; i < numFinalStates; i++)
{
bzero(buffer, sizeof(buffer));
fscanf(fp, "%d %s", &m, buffer);
hashFinalToken[m] = (char *) malloc(sizeof(char)*TOKEN_LEN);
strcpy(hashFinalToken[m], buffer);
dfa->total_states[m].is_final = 1;
}
fscanf(fp, "%s %d", buffer, &numMapping);
CHAR_RANGE mappings[numMapping];
fgets(buffer, sizeof(buffer), fp);
fgets(buffer, sizeof(buffer), fp);
fgets(buffer, sizeof(buffer), fp);
for (i = 0; i < numMapping; i++)
{
bzero(buffer, sizeof(buffer));
fscanf(fp, "%d %d %d %s", &mappings[i].start, &mappings[i].end, &mappings[i].id, buffer);
}
fscanf(fp, "%s %d", buffer, &NUM_KEYWORD);
hash_keyword_token = (char **) malloc(sizeof(char *)*NUM_KEYWORD);
for (i = 0; i < NUM_KEYWORD; i++)
{
hash_keyword_token[i] = (char *) malloc(sizeof(char)*30);
fscanf(fp, "%s", hash_keyword_token[i]);
}
fgets(buffer, sizeof(buffer), fp);
fgets(buffer, sizeof(buffer), fp);
fgets(buffer, sizeof(buffer), fp);
int curr_st, next, inp_type, x;
char inp[2];
while (fscanf(fp, "%d %d %s %d", &curr_st, &inp_type, inp, &next)!=-1)
{
// no transition from curr_st
if (inp_type==2)
{
bzero(buffer, sizeof(buffer));
continue;
}
// checking for 10 i.e. /n
if (strlen(inp) > 1)
{
x = atoi(inp);
}
else
{
// getting ascii value
x = (int) inp[0];
}
bzero(inp, 2);
transition(x, curr_st, next, inp_type, dfa, mappings);
}
return dfa;
}
//Returns buff after reading blocksize from file pointed by fp.
int getNextStream(FILE *fp, char *buff, int blocksize)
{
return (int) fread(buff, sizeof(char), (size_t) blocksize - 1, fp);
}
//Returns token id of the state identified by state_num.
char *get_token_id(char *st, int state_num)
{
char *token_id;
//check if keyword
if (check_hash(st)==1)
{
if (!strcmp(st, "_main"))
{
token_id = (char *) malloc(sizeof(char)*(strlen(st) + 2));
strcpy(token_id, "TK_MAIN");
}
else
{
token_id = (char *) malloc(sizeof(char)*(strlen(st) + 3));
int i;
for (i = 0; i < strlen(st); i++)
st[i] = (char) toupper(st[i]);
sprintf(token_id, "TK_%s", st);
}
}
else
{
token_id = (char *) malloc(sizeof(char)*(strlen(hashFinalToken[state_num])));
strcpy(token_id, hashFinalToken[state_num]);
}
return token_id;
}
//Checks if c is delimiter or not
int check_delimiter(char c)
{
if (c=='\n' || c==' ' || c=='\t' || c=='\r')
return 1;
return 0;
}
//Creates string usign buff from start to end position.
char *make_string(char *buff, int start, int end)
{
char *st = (char *) malloc(sizeof(char)*(end - start + 2));
bzero(st, sizeof(st));
int i;
for (i = start; i <= end; i++)
st[i - start] = buff[i];
st[i - start] = '\0';
return st;
}
//Returns TOKEN_INFO structure after populating it with information
TOKEN_INFO *create_token(char *st, int prev_state, int line_num)
{
TOKEN_INFO *token = (TOKEN_INFO *) malloc(sizeof(TOKEN_INFO));
token->line_number = line_num;
token->token_name = (char *) malloc(sizeof(char)*strlen(st));
strcpy(token->token_name, st);
char *token_id = get_token_id(st, prev_state);
token->token_id = (char *) malloc(sizeof(char)*strlen(token_id));
strcpy(token->token_id, token_id);
return token;
}
//Returns tokens one after one.
TOKEN_INFO *getNextToken(FILE *fp, FILE *fp_error, FILE *fp_comment, int reset)
{
static int blocksize ;
// int blocksize = 3;
static char *buff[2];
static int exit_flag = 0, first_run = 0, first_initialization = 0;
static DFA *dfa;
int curr_length = 0;
static int stream_len = 0;
static int line_num = 1, continue_flag = 0;
static int start = 0, forward = 0, prev_final = 0, curr_extendable;
static char *prev_string = NULL;
static int buff_curr_ind;
char *st;
if(reset)
{
if(first_initialization)
{
bzero(buff[0], sizeof(buff[0]));
bzero(buff[1], sizeof(buff[1]));
}
// fseek(fp, 0, SEEK_SET);
// fseek(fp_error, 0, SEEK_SET);
// fseek(fp_comment, 0, SEEK_SET);
exit_flag = 0;
first_run = 0;
curr_length = 0;
line_num = 1;
continue_flag = 0;
start = 0;
forward = 0;
prev_final = 0;
curr_extendable = 0;
prev_string = NULL;
buff_curr_ind = 0;
return NULL;
}
if(!first_initialization)
{
first_initialization = 1;
//Getting system blocksize.
struct stat fi;
stat("/", &fi);
blocksize = (int) fi.st_blksize;
buff[0] = (char *) malloc(sizeof(char)*blocksize);
buff[1] = (char *) malloc(sizeof(char)*blocksize);
dfa = populateDfa("dfa.txt");
}
//When this function is called for first time.
if (!first_run)
{
first_run = 1;
stream_len = getNextStream(fp, buff[0], blocksize);
buff_curr_ind = 0;
buff[buff_curr_ind][blocksize - 1] = '\0';
}
//Whenever this function is called dfa would start from start state i.e 0
dfa->cur_state = 0;
if (exit_flag==1)
{
TOKEN_INFO *t=(TOKEN_INFO *)malloc(sizeof(TOKEN_INFO));
t->token_id=(char*)malloc(sizeof("$"));
strcpy(t->token_id,"$");
t->token_name=(char*)malloc(sizeof("$"));
strcpy(t->token_name,"$");
t->line_number = line_num;
return t;
}
while (stream_len!=0)
{
// printf("%s\n", buff[0]);
for (; forward < blocksize - 1; forward++)
{
curr_extendable = extendable_or_not(dfa, dfa->cur_state, buff[buff_curr_ind][forward]);
if (curr_extendable)
{
curr_length++;
dfa->cur_state = get_transition(dfa, dfa->cur_state, buff[buff_curr_ind][forward]);
// save current consumed input(s) in prev_String which would be used by inputs coming in next buffer read.
if (forward==blocksize - 2)
{
// if already prev_string is there
if (prev_string!=NULL && strlen(prev_string))
{
char *s = prev_string;
prev_string = (char *) malloc(sizeof(char)*(strlen(prev_string) + (forward - start + 2)));
sprintf(prev_string, "%s%s", s, make_string(buff[buff_curr_ind], start, forward));
}
else
prev_string = make_string(buff[buff_curr_ind], start, forward);
continue_flag = 1;
}
if (buff[buff_curr_ind][forward]=='\n')
line_num++;
continue;
}
//Checking when dfa is at non start state i.e some input(s) have been consumed.
if (dfa->cur_state!=0)
{
prev_final = final_or_not(dfa, dfa->cur_state);
//if last input in consumed input(s) is at final.
if (prev_final)
{
// if inter buffer inputs are present.
if (continue_flag)
{
st = (char *) malloc(sizeof(char)*(strlen(prev_string) + forward - start + 1));
sprintf(st, "%s%s", prev_string, make_string(buff[buff_curr_ind], start, forward - 1));
bzero(prev_string, sizeof(prev_string));
continue_flag = 0;
}
else
{
st = make_string(buff[buff_curr_ind], start, forward - 1);
}
//checking for /n because it would be a comment
if (dfa->cur_state!=17)
{
int rm = dfa->cur_state;
//checking if input is of type TK_FUNID and length > 30
if ((rm==13 || rm==14 || rm==15) && curr_length > 30)
{
if(print_syntax_error)
fprintf(stdout,
"ERROR_1: FunctionId at line<%d>:<%s> is longer than prescribed length of 30 characters\n",
line_num,
st);
error1 = 1;
}
else if ((rm==5 || rm==6 || rm==7) && curr_length > 20) //checking if input is of type TK_ID and length > 20
{
if(print_syntax_error)
fprintf(stdout,
"ERROR_1: Identifier at line<%d>:<%s> is longer than prescribed length of 20 characters\n",
line_num,
st);
error1 = 1;
}
else
{
start = forward;
return create_token(st, dfa->cur_state, line_num);
}
}
else
{
//fprintf(fp_comment, "%s\n", st);
}
// free(st);
// free(prev_string);
curr_length = 0;
}
else
{
//Since last consumed input is at non final state this means it is error.
if (continue_flag)
{
st = (char *) malloc(sizeof(char)*(strlen(prev_string) + forward - start + 1));
sprintf(st, "%s%s", prev_string, make_string(buff[buff_curr_ind], start, forward - 1));
bzero(prev_string, sizeof(prev_string));
continue_flag = 0;
}
else
{
st = make_string(buff[buff_curr_ind], start, forward - 1);
}
if(print_syntax_error)
fprintf(stdout, "ERROR_3: Unknown pattern <%s> at line <%d>\n", st, line_num);
error1 = 1;
}
dfa->cur_state = 0;
start = forward;
//Condition when at boundary of buffer and current input is valid input.
if (forward==blocksize - 2 && (!check_delimiter(buff[buff_curr_ind][forward]) && !buff[buff_curr_ind][forward]=='\0'))
{
if (extendable_or_not(dfa, dfa->cur_state, buff[buff_curr_ind][forward]))
{
dfa->cur_state = get_transition(dfa, dfa->cur_state, buff[buff_curr_ind][forward]);
prev_string = make_string(buff[buff_curr_ind], start, forward);
continue_flag = 1;
}
forward += 1;
}
forward -= 1;
}
else // when dfa is at start state i.e previous consumed input was delimiter or invalid input.
{
if (check_delimiter(buff[buff_curr_ind][forward]))
{
if (buff[buff_curr_ind][forward]=='\n')
line_num++;
start = forward + 1;
continue;
}
//when EOF has been reached.
if (stream_len < blocksize && forward >= stream_len)
{
exit_flag = 1;
if (final_or_not(dfa, dfa->cur_state))
{
bzero(buff[buff_curr_ind], sizeof(buff[buff_curr_ind]));
return create_token(make_string(buff[buff_curr_ind], start, forward - 1), dfa->cur_state, line_num);
}
break;
}
if(print_syntax_error)
fprintf(stdout, "ERROR_2: Unknown symbol <%s> at line <%d>\n", make_string(buff[buff_curr_ind], start, forward),
line_num);
error1 = 1;
start = forward + 1;
continue;
}
}
buff_curr_ind = !buff_curr_ind;
//Reload buffer again.
bzero(buff[buff_curr_ind], sizeof(buff[buff_curr_ind]));
stream_len = getNextStream(fp, buff[buff_curr_ind], blocksize);
buff[buff_curr_ind][blocksize - 1] = '\0';
start = 0;
forward = 0;
if (exit_flag==1)
break;
}
// if prev_string is present this means it hasnt been saved yet.
if (prev_string!=NULL && strlen(prev_string) > 0 && final_or_not(dfa, dfa->cur_state))
{
exit_flag = 1;
char *x;
x = (char *) malloc(sizeof(char)*strlen(prev_string));
bzero(prev_string, sizeof(prev_string));
return create_token(x, dfa->cur_state, line_num);
}
TOKEN_INFO *t=(TOKEN_INFO *)malloc(sizeof(TOKEN_INFO));
t->token_id=(char*)malloc(sizeof("$"));
strcpy(t->token_id,"$");
t->token_name=(char*)malloc(sizeof("$"));
strcpy(t->token_name,"$");
t->line_number = line_num;
return t;
}