-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlexycal.cpp
executable file
·678 lines (634 loc) · 22.1 KB
/
lexycal.cpp
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
#include "ctype.h"
#include "stdbool.h"
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "token.h"
#include "symbol_table.h"
#include "lexycal.h"
int currentState = INITIAL_STATE;
int currentInput;
int currentLine = 1;
int currentColumn = 0;
char *lexemeBuffer = nullptr;
char *lastLexemeFound = nullptr;
int lexemeLength = 0;
int lexemeBufferSize = 0;
char const *tokens[] = {"ENDOFILE", "LT", "LE", "EQ", "NE", "GT", "GE", "IF", "BOOL", "ELSE", "ID",
"INT", "FLOAT", "COMMA", "LPARENT", "RPARENT", "ASSIGN", "SEMICOLON",
"WHILE", "LBRACKET", "RBRACKET", "SWITCH", "BREAK", "RETURN", "PRINT",
"READLN", "THROW", "TRY", "CATCH", "CASE", "LITERAL", "TRUE", "FALSE",
"ADDRESS", "STAR", "DOT", "LBRACE", "RBRACE", "NOT", "CHAR", "QUOTE",
"SIMPLEQUOTE", "BACKSLASH", "COLON", "PLUS", "MINUS", "PIPE", "SLASH",
"PERCENT", "AND", "OR", "POINTER", "TYPEDEF", "STRUCT", "NUMINT",
"NUMFLOAT", "LITERALCHAR"};
ReservedWordsTable reservedWordsTable;
LiteralsTable literalsTable;
IdsTable identifiersTable;
NumIntTable numIntTable;
NumFloatTable numFloatTable;
FILE *filePointer;
LiteralsTable get_literals_table() {
return literalsTable;
}
IdsTable get_identifiers_table() {
return identifiersTable;
}
int lexical_analyzer_getLine() {
return currentLine;
}
/**
* Set the currentInput to the next char in input stream,
* updating the lexeme buffer with the new one and handling the column count
*/
void get_next_char() {
if (lexemeLength == lexemeBufferSize) {
lexemeBufferSize += BUFFER_SIZE;
lexemeBuffer = (char *) realloc(lexemeBuffer, lexemeBufferSize);
}
static char buf[IO_BUFFER_SIZE];
static char *bufp = buf;
static int n = 0;
/* Buffer is empty */
if (n == 0) {
n = fgets(buf, IO_BUFFER_SIZE, filePointer) != nullptr ? strlen(buf) : 0;
bufp = buf;
}
char next_input = (--n >= 0) ? *bufp++ : EOF;
lexemeBuffer[lexemeLength++] = (currentInput = next_input);
currentColumn++;
}
/**
* Clear the dirty entries from lexeme buffer
*/
void clear_lexeme() {
memset(lexemeBuffer, 0, lexemeBufferSize);
lexemeBuffer[0] = (char) currentInput;
lexemeLength = 1;
}
/**
* Get next input and set automaton state
* @param state target automaton state
*/
void get_next_char_and_go_to(int state) {
get_next_char();
currentState = state;
}
/**
* Set automaton state keeping the current input
* @param state target automaton state
*/
void go_to_state(int state) {
currentState = state;
}
/**
* Remove last char from buffer (usually it belongs to the next token lexeme)
*/
void remove_last_char_from_lexeme() {
lexemeBuffer[lexemeLength - 1] = '\0';
}
/**
* Handles a found token and restart automaton to get next
* @param token
* @return found token and lexeme
*/
//int found_token_and_restart(int token)
int found_token_and_restart(int token) {
remove_last_char_from_lexeme();
lastLexemeFound = (char *) malloc(strlen(lexemeBuffer) + 1); //Save the buffer
strcpy(lastLexemeFound, lexemeBuffer);
clear_lexeme();
go_to_state(0);
return token;
}
/**
* Handles a found token, but get next input before it
* @param token
* @return found token
*/
int found_token_and_get_next_input(int token) {
get_next_char();
return found_token_and_restart(token);
}
/**
* Handles a found token, but it determines the token type, if it is a reserved word
* @return found token
*/
int found_token_and_check_for_reserved_word() {
remove_last_char_from_lexeme();
// int token = reservedWordsTable.cSearch(lexemeBuffer);
ReservedTokenSymbol *token = reservedWordsTable.cSearch(lexemeBuffer);
if (token != nullptr) {
return found_token_and_restart(token->getTokenID());
} else {
identifiersTable.cInsert(lexemeBuffer);
return found_token_and_restart(ID);
}
}
/**
* Handles a literal found token, with the correct add in symbolaa table.
* @param token
* @return found token
*/
//int found_literal_and_restart(int token)
int found_literal_and_restart(int token) {
remove_last_char_from_lexeme(); //remove the char from next token
literalsTable.cInsert(lexemeBuffer);
return found_token_and_restart(token);
}
int found_numInt_and_restart(int token) {
numIntTable.cInsert(lexemeBuffer);
return found_token_and_restart(token);
}
int found_numFloat_and_restart(int token) {
numFloatTable.cInsert(lexemeBuffer);
return found_token_and_restart(token);
}
/**
* [MANDATORY] Initialize the analyzer module, create the symtable for reserved words and
* prepare the input
*/
void lexical_analyzer_init(FILE *fp) {
filePointer = fp;
reservedWordsTable.cInsert(IF, "if");
reservedWordsTable.cInsert(ELSE, "else");
reservedWordsTable.cInsert(TRY, "try");
reservedWordsTable.cInsert(CATCH, "catch");
reservedWordsTable.cInsert(INT, "int");
reservedWordsTable.cInsert(FLOAT, "float");
reservedWordsTable.cInsert(BOOL, "bool");
reservedWordsTable.cInsert(RETURN, "return");
reservedWordsTable.cInsert(WHILE, "while");
reservedWordsTable.cInsert(BREAK, "break");
reservedWordsTable.cInsert(SWITCH, "switch");
reservedWordsTable.cInsert(PRINT, "print");
reservedWordsTable.cInsert(READLN, "readln");
reservedWordsTable.cInsert(CASE, "case");
reservedWordsTable.cInsert(THROW, "throw");
reservedWordsTable.cInsert(TRUE, "true");
reservedWordsTable.cInsert(FALSE, "false");
reservedWordsTable.cInsert(CHAR, "char");
reservedWordsTable.cInsert(TYPEDEF, "typedef");
reservedWordsTable.cInsert(STRUCT, "struct");
get_next_char();
}
bool is_letter(char c) {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}
bool is_digit(char c) {
return c >= '0' && c <= '9';
}
/**
* Handles the fail state
* @param reason
*/
void fail(const char *reason) {
fprintf(stderr, "[LEXICAL ERROR] %s at %d:%d\n", reason, currentLine, currentColumn - 1);
clear_lexeme();
}
void handle_next_line() {
currentLine++;
currentColumn = 1;
}
/**
* Terminates the analyzer instance
*/
void lexical_analyzer_dispose() {
free(lexemeBuffer);
free(lastLexemeFound);
}
/**
* Search the input for the next token. At end of input stream, it must return
* ENDOFFILE token.
* @return found token
*/
int lexical_analyzer_next_token() {
while (true) {
switch (currentState) {
case INITIAL_STATE:
if (currentInput == '\n') {
handle_next_line();
}
if (isspace(currentInput)) {
get_next_char_and_go_to(INITIAL_STATE);
clear_lexeme();
} else if (is_letter(currentInput)) {
get_next_char_and_go_to(13);
} else if (is_digit(currentInput)) {
get_next_char_and_go_to(19);
} else
switch (currentInput) {
case '<':
get_next_char_and_go_to(1);
break;
case '=':
get_next_char_and_go_to(4);
break;
case '>':
get_next_char_and_go_to(5);
break;
case '!':
get_next_char_and_go_to(10);
break;
case '.':
get_next_char_and_go_to(14);
break;
case '+':
get_next_char_and_go_to(27);
break;
case ')':
get_next_char_and_go_to(28);
break;
case '-':
get_next_char_and_go_to(29);
break;
case '(':
get_next_char_and_go_to(30);
break;
case '{':
get_next_char_and_go_to(31);
break;
case '}':
get_next_char_and_go_to(32);
break;
case '[':
get_next_char_and_go_to(33);
break;
case ']':
get_next_char_and_go_to(34);
break;
case ',':
get_next_char_and_go_to(35);
break;
case ';':
get_next_char_and_go_to(36);
break;
case '"':
get_next_char_and_go_to(37);
break;
case '\'':
get_next_char_and_go_to(38);
break;
case '*':
get_next_char_and_go_to(39);
break;
case '|':
get_next_char_and_go_to(43);
break;
case '&':
get_next_char_and_go_to(46);
break;
case '/':
get_next_char_and_go_to(49);
break;
case ':':
get_next_char_and_go_to(97);
break;
case EOF:
case ENDOFFILE:
go_to_state(54);
break;
default:
get_next_char_and_go_to(18);
}
break;
case 1:
switch (currentInput) {
case '=':
go_to_state(2);
break;
default:
go_to_state(3);
break;
}
break;
case 2:
return found_token_and_get_next_input(LE); //found LE
case 3:
return found_token_and_restart(LT); //found LT
case 4:
switch (currentInput) {
case '=':
get_next_char_and_go_to(6);
break;
default:
go_to_state(7);
break;
}
break;
case 5:
switch (currentInput) {
case '=':
get_next_char_and_go_to(8);
break;
default:
go_to_state(9);
break;
}
break;
case 6:
return found_token_and_get_next_input(EQ); //found EQ
case 7:
return found_token_and_restart(ASSIGN); //found assign
case 8:
return found_token_and_get_next_input(GE); //found GE
case 9:
return found_token_and_restart(GT); //found GT
case 10:
switch (currentInput) {
case '=':
get_next_char_and_go_to(11);
break;
default:
go_to_state(12);
break;
}
break;
case 11:
return found_token_and_get_next_input(NE); //found NE
case 12:
return found_token_and_restart(NOT); //found NOT
case 13:
if (is_letter(currentInput) || is_digit(currentInput)) {
get_next_char_and_go_to(13);
} else {
go_to_state(15);
}
break;
case 14:
if (is_digit(currentInput)) {
get_next_char_and_go_to(55);
} else {
go_to_state(53);
}
break;
case 15:
return found_token_and_check_for_reserved_word();
case 17: //Unexpected EOF fail state
fail("Unexpected end of file");
go_to_state(54);
break;
case 18: //Unexpected char fail state
fail("Unexpected character");
go_to_state(INITIAL_STATE);
break;
case 19:
if (is_digit(currentInput)) {
get_next_char_and_go_to(19);
} else if (currentInput == '.') {
get_next_char_and_go_to(25);
} else if (currentInput == 'e' || currentInput == 'E') {
get_next_char_and_go_to(20);
} else if (is_letter(currentInput)) {
go_to_state(94);
} else {
go_to_state(24);
}
break;
case 20:
if (is_digit(currentInput)) {
get_next_char_and_go_to(22);
} else if (currentInput == '+' || currentInput == '-') {
get_next_char_and_go_to(21);
} else {
go_to_state(93);
}
break;
case 21:
if (is_digit(currentInput)) {
get_next_char_and_go_to(22);
} else {
go_to_state(92);
}
break;
case 22:
if (is_digit(currentInput)) {
get_next_char_and_go_to(22);
} else {
go_to_state(23);
}
break;
case 23:
return found_numFloat_and_restart(NUMFLOAT); //found NUMFLOAT
case 24:
return found_numInt_and_restart(NUMINT); //found NUMINT
case 25:
if (is_digit(currentInput)) {
get_next_char_and_go_to(26);
} else if (is_letter(currentInput)) {
go_to_state(91);
} else {
go_to_state(23);
}
break;
case 26:
if (is_digit(currentInput)) {
get_next_char_and_go_to(26);
} else if (currentInput == 'E' || currentInput == 'e') {
get_next_char_and_go_to(20);
} else if (is_letter(currentInput)) {
go_to_state(91);
} else {
go_to_state(23);
}
break;
case 27:
return found_token_and_restart(PLUS); //found PLUS
case 28:
return found_token_and_restart(RPARENT); //found RPARENT
case 29:
if (currentInput == '>') {
get_next_char_and_go_to(56);
} else {
go_to_state(57);
}
break;
case 30:
return found_token_and_restart(LPARENT); //found LPARENT
case 31:
return found_token_and_restart(LBRACE); //found LBREACE
case 32:
return found_token_and_restart(RBRACE); //found RBRACE
case 33:
return found_token_and_restart(LBRACKET); //found LBRACKET
case 34:
return found_token_and_restart(RBRACKET); //found RBRACKET
case 35:
return found_token_and_restart(COMMA); //found COMMA
case 36:
return found_token_and_restart(SEMICOLON); //found SEMICOLON
case 37:
switch (currentInput) {
case '"':
get_next_char_and_go_to(40);
break;
case '\\':
get_next_char_and_go_to(41);
break;
case EOF:
case ENDOFFILE:
go_to_state(17);
break;
case '\n':
handle_next_line();
default:
get_next_char_and_go_to(37);
break;
}
break;
case 38:
switch (currentInput) {
case '\\':
get_next_char_and_go_to(99);
break;
default:
get_next_char_and_go_to(42);
break;
}
break;
case 39:
return found_token_and_restart(STAR); //found STAR
case 40:
return found_literal_and_restart(LITERAL); //found LITERAL
case 41:
get_next_char_and_go_to(37);
break;
case 42:
switch (currentInput) {
case '\'':
get_next_char_and_go_to(98);
break;
case EOF:
case ENDOFFILE:
go_to_state(17);
break;
default:
go_to_state(96);
break;
}
break;
case 43:
if (currentInput == '|')
get_next_char_and_go_to(44);
else
go_to_state(45);
break;
case 44:
return found_token_and_restart(OR);
case 45:
return found_token_and_restart(PIPE);
case 46:
if (currentInput == '&')
get_next_char_and_go_to(47);
else
go_to_state(48);
break;
case 47:
return found_token_and_restart(AND);
case 48:
return found_token_and_restart(ADDRESS);
case 49:
switch (currentInput) {
case '*':
get_next_char_and_go_to(51);
break;
default:
go_to_state(50);
break;
}
break;
case 50:
return found_token_and_restart(SLASH);
case 51:
switch (currentInput) {
case '*':
get_next_char_and_go_to(52);
break;
case EOF:
case ENDOFFILE:
go_to_state(17);
break;
case '\n':
handle_next_line();
default:
get_next_char_and_go_to(51);
break;
}
break;
case 52:
switch (currentInput) {
case '/':
get_next_char_and_go_to(INITIAL_STATE);
break;
case EOF:
case ENDOFFILE:
go_to_state(17);
break;
default:
get_next_char_and_go_to(51);
break;
}
break;
case 53:
return found_token_and_restart(DOT); //found DOT
case 54:
return found_token_and_restart(ENDOFFILE); //found EOF
case 55:
if (is_digit(currentInput)) {
get_next_char_and_go_to(55);
} else if (currentInput == 'e' || currentInput == 'E') {
get_next_char_and_go_to(20);
} else if (is_letter(currentInput)) {
go_to_state(18);
} else
go_to_state(23);
break;
case 56:
return found_token_and_restart(POINTER); //found POINTER
case 57:
return found_token_and_restart(MINUS); //found MINUS
case 91: //Unexpected float number before the ID = fail state
fail("The ID can't start with float numbers");
go_to_state(INITIAL_STATE);
break;
case 92: //Unexpected char after the E or e = fail state
fail("Missing the power function value on the scientific notation");
go_to_state(INITIAL_STATE);
break;
case 93: //Unexpected char after the E or e = fail state
fail("Unexpected character after the scientific notation");
go_to_state(INITIAL_STATE);
break;
case 94: //Unexpected int number before the ID = fail state
fail("The ID can't start with int numbers");
go_to_state(INITIAL_STATE);
break;
// case 95: //Number fail state : change ',' to '.'
// fail("Missing . character. Maybe change ',' to '.' ");
// go_to_state(INITIAL_STATE);
// break;
case 96: //Missing ' character fail state
fail("Missing \' character");
go_to_state(INITIAL_STATE);
break;
case 97:
return found_token_and_restart(COLON);
case 98:
return found_literal_and_restart(LITERALCHAR); //found LITERAL
case 99:
get_next_char_and_go_to(42);
break;
}
}
}
char *lexical_analyzer_last_lexeme() {
return lastLexemeFound;
}
/**
* Convert token numerical IDs to a textual represent
* @param id Kind of token
* @return Textual value of token
*/
char const *token_id_to_name(int id) {
return tokens[id];
}