-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.c
512 lines (478 loc) · 16.4 KB
/
lexer.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
// GROUP - 30
// ID: 2020A7PS0094P Name: Arya Veer Singh Chauhan
// ID: 2020A7PS0049P Name: Madhav Madhusoodanan
// ID: 2020A7PS0016P Name: Ruchika Sarkar
// ID: 2020A7PS0984P Name: Utsav Goel
// ID: 2020A7PS0102P Name: Hardik Jain
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include "lexerDef.h"
int lineCount = 1;
void populateTwinBuffer(TwinBuffer *TB){
// call this function whenever TB needs to be filled
int length = fread(TB->buffer[TB->currentForward], sizeof(char), TB->SIZE, TB->fp);
if(length != TB->SIZE) TB->buffer[TB->currentForward][length] = EOF;
}
TwinBuffer* initializeTwinBuffer(char* fname, int bufferSize){
TwinBuffer* TB = (TwinBuffer*) malloc(sizeof(TwinBuffer));
TB->fp = fopen(fname,"r");
TB->SIZE = bufferSize;
TB->lexemeBegin = 0;
TB->forward = 0;
TB->currentLexemeBegin = 0;
TB->currentForward = 0;
TB->buffer[0] = malloc(bufferSize);
TB->buffer[1] = malloc(bufferSize);
populateTwinBuffer(TB);
lineCount = 1;
return TB;
}
void cleanTwinBuffer(TwinBuffer* TB){
free(TB->buffer[0]);
free(TB->buffer[1]);
}
void incrementForward(TwinBuffer *TB){
// if forward reached end of buffer, fill up the other buffer and put forward to zero
if(TB->forward == TB->SIZE - 1) {
TB->forward = 0;
TB->currentForward = 1 - TB->currentForward;
populateTwinBuffer(TB);
return;
}
else TB->forward++;
// else simply increment forward
}
void decrementForward(TwinBuffer *TB){
// if forward reached end of buffer, fill up the other buffer and put forward to zero
if(TB->forward == 0) {
TB->forward = TB->SIZE - 1;
TB->currentForward = 1 - TB->currentForward;
return;
}
else TB->forward--;
// else simply increment forward
}
short findLexemeLength(TwinBuffer *TB){
// Major case 1: when the forward is equal to or ahead of lexemeBegin
if(TB->currentForward != TB->currentLexemeBegin) return TB->SIZE + TB->forward - TB->lexemeBegin;
// Major case 2: when the forward is behind lexemeBegin
else return TB->forward - TB->lexemeBegin;
}
// not exported
char moveLexemeBegin(TwinBuffer *TB){
if(TB->lexemeBegin == TB->SIZE - 1) {
char lexemeBeginCharacter = TB->buffer[TB->currentLexemeBegin][TB->SIZE - 1];
TB->lexemeBegin = 0;
TB->currentLexemeBegin = 1 - TB->currentLexemeBegin;
return lexemeBeginCharacter;
}
else return TB->buffer[TB->currentLexemeBegin][ TB->lexemeBegin++];
}
/* MAKE SURE THAT LEXEME_BEGIN IS AT A NON-WHITESPACE CHARACTER */
char* extractLexeme(TwinBuffer *TB){
short length = findLexemeLength(TB);
short i = 0;
for (i = 0; i < length; i++){
ReturnLexeme[i] = moveLexemeBegin(TB);
}
ReturnLexeme[i] = '\0';
// printf("lexeme ------> %s\n", ReturnLexeme);
return ReturnLexeme;
}
char getCharacterAtForward(TwinBuffer *TB){
return TB->buffer[TB->currentForward][TB->forward];
}
short isFloat(char* number){
short n = strlen(number);
for(short i=0;i<n;i++){
if(number[i] == '.'){
return 1;
}
}
return 0;
}
/* @param
TWINBUFFER TB;
int line;
*/
void skipComment(TwinBuffer *TB,short toPrint){
char* input = extractLexeme(TB);
if(toPrint == 1)printf("Skipped Comment\n");
}
LEXEME* tokenizeEOF(TwinBuffer *TB,short line){
char* input = extractLexeme(TB);
LEXEME* lex = (LEXEME*) malloc(sizeof(LEXEME));
lex->token = EOF_TOKEN;
lex->lineNo = line;
return lex;
}
int hashCode(char* str){
int num=2;
int k=214;
int product=0;
for(int i=0;i<strlen(str);i++){
product=product*num+str[i];
}
product=product%k;
return product;
}
LEXEME* tokenize(TwinBuffer *TB,short int line,short toPrint){
char* input = extractLexeme(TB);
if(input[0] == ' ' || input[0] == '\n' || input[0] == '\t' || input[0] == EOF) return NULL; // If a white space is there do not tokenize it
LEXEME* lex = (LEXEME*) malloc(sizeof(LEXEME));
lex->lexemedata = (union lexemeData*) malloc(sizeof(union lexemeData));
lex->lineNo = line;
/*
Checking For Int or Float
*/
if(input[0] >= '0' && input[0] <= '9'){
if(isFloat(input)){
char* endWord;
lex->lexemedata->floatData = strtod(input,&endWord);
lex->token = RNUM_TOKEN;
if(toPrint == 1)printf("LINE NO: %d LEXEME: %lf TOKEN: %s\n",line,lex->lexemedata->floatData,"RNUM");
}
else{
lex->lexemedata->intData = atoi(input);
lex->token = NUM_TOKEN;
if(toPrint == 1)printf("LINE NO: %d LEXEME: %d TOKEN: %s\n",line,lex->lexemedata->intData,"NUM");
}
return lex;
}
lex->lexemedata->data = input;
// printf("%s\n",input);
short found = 0;
int hc = hashCode(input);
// printf(TOKENS_STRING[hc])
if(strcmp(input,TOKENS_STRING[hc]) == 0){
lex->token = (TOKENS) hc;
found = 1;
if(toPrint == 1)printf("LINE NO: %d LEXEME: %s TOKEN: %s\n",line,input,TERMINALS_STRINGS[hc]);
// break;
}
if(found == 0){
lex->token = IDENTIFIER_TOKEN;
if(toPrint == 1)printf("LINE NO: %d LEXEME: %s TOKEN: %s\n",line,input,"ID");
}
return lex;
}
LEXEME* simulateDFA(TwinBuffer *TB,short toPrint){
char* error;
short int state = 0;
char c;
short charCount;
char errorChar;
char* errorString;
LEXEME* lex;
while(1){
switch(state){
case -1:
errorChar = getCharacterAtForward(TB);
errorString = extractLexeme(TB);
printf("\n\x1B[1m\033[31m LEXICAL ERROR OCCURED AT LINE %hi, \"%s\" , INVALID CHARACTER '%c'\n \033[0m\033[0m\n",lineCount,errorString,c);
state = 0;
break;
case 0:
c = getCharacterAtForward(TB);
if(c == '+') state = 1;
else if (c == '-') state = 2;
else if (c == '/') state = 3;
else if (c == ';') state = 4;
else if (c == ',') state = 5;
else if (c == '[') state = 6;
else if (c == ']') state = 7;
else if (c == '(') state = 8;
else if (c == ')') state = 9;
else if (c == '!') state = 10;
else if (c == '=') state = 12;
else if (c == '.') state = 14;
else if (c == '*') state = 16;
else if (c == ':') state = 20;
else if (c >= '0' && c <= '9') state = 22;
else if ((c >= 'a' && c <= 'z') || (c >='A' && c <= 'Z') || (c == '_')) {
state = 28;
charCount = 1;
}
else if (c == '>') state = 29;
else if (c == '<') state = 33;
else if (c == ' ') state = 37;
else if (c == '\t') state = 38;
else if (c == '\n') {
state = 39;
(lineCount)++;
break;
}
else if (c == EOF || c == '\0') state = 40;
else {
incrementForward(TB);
state = -1;
}
break;
case 1:
incrementForward(TB);
lex = tokenize(TB,lineCount,toPrint);
return lex;
break;
case 2:
incrementForward(TB);
lex = tokenize(TB,lineCount,toPrint);
return lex;
break;
case 3:
incrementForward(TB);
lex = tokenize(TB,lineCount,toPrint);
return lex;
break;
case 4:
incrementForward(TB);
lex = tokenize(TB,lineCount,toPrint);
return lex;
break;
case 5:
incrementForward(TB);
lex = tokenize(TB,lineCount,toPrint);
return lex;
break;
case 6:
incrementForward(TB);
lex = tokenize(TB,lineCount,toPrint);
return lex;
break;
case 7:
incrementForward(TB);
lex = tokenize(TB,lineCount,toPrint);
return lex;
break;
case 8:
incrementForward(TB);
lex = tokenize(TB,lineCount,toPrint);
return lex;
break;
case 9:
incrementForward(TB);
lex = tokenize(TB,lineCount,toPrint);
return lex;
break;
case 10:
incrementForward(TB);
c = getCharacterAtForward(TB);
if(c == '=') state = 11;
else state = -1;
break;
case 11:
incrementForward(TB);
lex = tokenize(TB,lineCount,toPrint);
return lex;
break;
case 12:
incrementForward(TB);
c = getCharacterAtForward(TB);
if(c == '=') state = 13;
else state = -1;
break;
case 13:
incrementForward(TB);
lex = tokenize(TB,lineCount,toPrint);
return lex;
break;
case 14:
incrementForward(TB);
c = getCharacterAtForward(TB);
if(c == '.') state = 15;
else state = -1;
break;
case 15:
incrementForward(TB);
lex = tokenize(TB,lineCount,toPrint);
return lex;
break;
case 16:
incrementForward(TB);
c = getCharacterAtForward(TB);
if(c == '*') state = 17;
else{
lex = tokenize(TB,lineCount,toPrint);
return lex;
}
break;
case 17:
incrementForward(TB);
c = getCharacterAtForward(TB);
if(c == '*') state = 18;
else if(c == EOF) return tokenizeEOF(TB,lineCount);
else if(c == '\n') ++lineCount;
else state = 17;
break;
case 18:
incrementForward(TB);
c = getCharacterAtForward(TB);
if(c == '*') state = 19;
else state = 17;
break;
case 19:
incrementForward(TB);
skipComment(TB,toPrint);
state=0;
break;
case 20:
incrementForward(TB);
c = getCharacterAtForward(TB);
if(c == '=') state = 21;
else{
lex = tokenize(TB,lineCount,toPrint);
return lex;
}
break;
case 21:
incrementForward(TB);
lex = tokenize(TB,lineCount,toPrint);
return lex;
break;
case 22:
incrementForward(TB);
c = getCharacterAtForward(TB);
if(c == '.') state = 23;
else if(c >= '0' && c <= '9') state = 22;
else{
lex = tokenize(TB,lineCount,toPrint);
return lex;
}
break;
case 23:
incrementForward(TB);
c = getCharacterAtForward(TB);
if(c >= '0' && c <= '9') state = 24;
else if(c == '.'){
decrementForward(TB);
lex = tokenize(TB,lineCount,toPrint);
return lex;
}
else state = -1;
break;
case 24:
incrementForward(TB);
c = getCharacterAtForward(TB);
if(c >= '0' && c <= '9') state = 24;
else if (c == 'e' || c == 'E') state = 25;
else{
lex = tokenize(TB,lineCount,toPrint);
return lex;
}
break;
case 25:
incrementForward(TB);
c = getCharacterAtForward(TB);
if(c == '+' || c == '-') state = 26;
else if(c >= '0' && c <= '9') state = 27;
else state = -1;
break;
case 26:
incrementForward(TB);
c = getCharacterAtForward(TB);
if(c >= '0' && c <= '9') state = 27;
else state = -1;
break;
case 27:
incrementForward(TB);
c = getCharacterAtForward(TB);
if(c >= '0' && c <= '9') state = 27;
else{
lex = tokenize(TB,lineCount,toPrint);
return lex;
}
break;
case 28:
charCount++;
incrementForward(TB);
c = getCharacterAtForward(TB);
if((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || (c == '_')) state = 28;
else {
if(charCount > 20){
errorString = extractLexeme(TB);
printf("\n\x1B[1m\033[31m LEXICAL ERROR OCCURED AT LINE %hi, \"%s\" , VARIABLE LENGTH CAN NOT BE MORE THAN 20\n \033[0m\033[0m\n",lineCount,errorString);
state = 0;
break;
}
lex = tokenize(TB,lineCount,toPrint);
return lex;
}
break;
case 29:
incrementForward(TB);
c = getCharacterAtForward(TB);
if(c == '=') state = 30;
else if(c == '>') state = 31;
else{
lex = tokenize(TB,lineCount,toPrint);
return lex;
}
break;
case 30:
incrementForward(TB);
lex = tokenize(TB,lineCount,toPrint);
return lex;
break;
case 31:
incrementForward(TB);
c = getCharacterAtForward(TB);
if(c == '>') state = 32;
else{
lex = tokenize(TB,lineCount,toPrint);
return lex;
}
break;
case 32:
incrementForward(TB);
lex = tokenize(TB,lineCount,toPrint);
return lex;
break;
case 33:
incrementForward(TB);
c = getCharacterAtForward(TB);
if(c == '=') state = 34;
else if(c == '<') state = 35;
else{
lex = tokenize(TB,lineCount,toPrint);
return lex;
}
break;
case 34:
incrementForward(TB);
lex = tokenize(TB,lineCount,toPrint);
return lex;
break;
case 35:
incrementForward(TB);
c = getCharacterAtForward(TB);
if(c == '<') state = 36;
else{
lex = tokenize(TB,lineCount,toPrint);
return lex;
}
break;
case 36:
incrementForward(TB);
lex = tokenize(TB,lineCount,toPrint);
return lex;
break;
case 37:
incrementForward(TB);
lex = tokenize(TB,lineCount,toPrint);
state = 0;
break;
case 38:
incrementForward(TB);
lex = tokenize(TB,lineCount,toPrint);
state = 0;
break;
case 39:
incrementForward(TB);
lex = tokenize(TB,lineCount,toPrint);
state = 0;
break;
case 40:
return tokenizeEOF(TB,lineCount);
}
}
}