-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStep3Scanner.c
More file actions
829 lines (721 loc) · 25.2 KB
/
Copy pathStep3Scanner.c
File metadata and controls
829 lines (721 loc) · 25.2 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
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
/*
************************************************************
* COMPILERS COURSE - Algonquin College
* Code version: Fall, 2025
* Author: Alex Vu and Yassine Amraoui
* Professors: Paulo Sousa
************************************************************
"\t=-------------------------------------------------------------------=\n",
"\t| COMPILERS - ALGONQUIN COLLEGE (S25) |\n",
"\t=-------------------------------------------------------------------=\n",
"\t @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ \n",
"\t @@ @@ \n",
"\t @@ %&@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@ @@ \n",
"\t @@ @%% (@@@@@@@@@ @ @ @@ \n",
"\t @@ @& @ @ @ @ @ @@ \n",
"\t @@ @ @ % / / @@@@@@ @ @@ \n",
"\t @@ & @ @ @@ @ @@ \n",
"\t @@ @/ @*@ @ @ @ @ @@ \n",
"\t @@ @@@@ @@ @ @ @ @@ \n",
"\t @@ /@@ @@@ @ @ @@ \n",
"\t @@ @ / / @@ @ @ @@ \n",
"\t @@ @ @@ /@/ @@@ @ @ @@ \n",
"\t @@ @@@@@@@@@@@@@@@ @ @@ \n",
"\t @@ @@ \n",
"\t @@ S T R U C T U R A L @@ \n",
"\t @@ By Alex Vu and Yassine Amraoui @@ \n",
"\t @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ \n",
"\t \n",
"\t[COMPILER SCRIPT ..................................................]\n",*/
/*
************************************************************
* File name: Scanner.c
* Compiler: MS Visual Studio 2022
* Course: CST 8152 – Compilers, Lab Section: [011, 012]
* Assignment: A22, A32.
* Date: May 01 2024
* Purpose: This file contains all functionalities from Scanner.
* Function list: (...).
************************************************************
*/
/* TO_DO: Adjust the function header */
/* The #define _CRT_SECURE_NO_WARNINGS should be used in MS Visual Studio projects
* to suppress the warnings about using "unsafe" functions like fopen()
* and standard sting library functions defined in string.h.
* The define does not have any effect in Borland compiler projects.
*/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h> /* standard input / output */
#include <ctype.h> /* conversion functions */
#include <stdlib.h> /* standard library functions and constants */
#include <string.h> /* string functions */
#include <limits.h> /* integer types constants */
#include <float.h> /* floating-point types constants */
/* #define NDEBUG to suppress assert() call */
#include <assert.h> /* assert() prototype */
/* project header files */
/* Toggle debug printing */
#define DEBUG_SCANNER 0 /* set to 1 to enable debug logs */
#if DEBUG_SCANNER
#define DBG(...) fprintf(stderr, __VA_ARGS__)
#else
#define DBG(...) ((void)0)
#endif
#ifndef COMPILERS_H_
#include "Compilers.h"
#endif
#ifndef BUFFER_H_
#include "Step2Reader.h"
#endif
#ifndef SCANNER_H_
#include "Step3Scanner.h"
#endif
/*
----------------------------------------------------------------
TO_DO: Global vars definitions
----------------------------------------------------------------
*/
/* Global objects - variables */
/* This buffer is used as a repository for string literals. */
extern BufferPointer stringLiteralTable; /* String literal table */
struct_int line; /* Current line number of the source code */
extern struct_int errorNumber; /* Defined in platy_st.c - run-time error number */
extern struct_int stateType[NUM_STATES];
extern struct_strg keywordTable[KWT_SIZE];
extern PTR_ACCFUN finalStateTable[NUM_STATES];
extern struct_int transitionTable[NUM_STATES][CHAR_CLASSES];
/* Local(file) global objects - variables */
static BufferPointer lexemeBuffer; /* Pointer to temporary lexeme buffer */
static BufferPointer sourceBuffer; /* Pointer to input source buffer */
struct_int numScannerErrors = 0;
ScannerData scData = { 0 };
/*
************************************************************
* Intitializes scanner
* This function initializes the scanner using defensive programming.
***********************************************************
*/
/* ------------------------------------------------------------------------
* startScanner()
* Initializes the scanner before tokenization.
* Defensive programming is applied to validate input buffer and reset data.
* ------------------------------------------------------------------------
*/
struct_int startScanner(BufferPointer psc_buf) {
if (psc_buf == NULL) {
fprintf(stderr, "Error: Source buffer is NULL in startScanner().\n");
return EXIT_FAILURE;
}
/* Defensive: Initialize histogram safely */
for (struct_int i = 0; i < NUM_TOKENS; i++) {
scData.scanHistogram[i] = 0;
}
/* Basic scanner initialization */
readerRecover(psc_buf);
readerClear(stringLiteralTable);
line = 1;
sourceBuffer = psc_buf;
return EXIT_SUCCESS;
}
/* ------------------------------------------------------------------------
* tokenizer()
* Reads characters from the buffer and classifies them into tokens.
* This version includes defensive programming for stability.
* ------------------------------------------------------------------------
*/
Token tokenizer(struct_void) {
Token currentToken = { 0 }; /* initialize all members to 0 */
struct_char c = 0; /* input symbol */
struct_int state = 0; /* initial FSM state */
struct_int lexStart = 0;
struct_int lexEnd = 0;
struct_int lexLength = 0;
struct_int i = 0;
struct_strg lexeme = NULL; /* current lexeme string */
/* Defensive: Ensure sourceBuffer is initialized */
if (sourceBuffer == NULL) {
fprintf(stderr, "Error: Source buffer is NULL in tokenizer().\n");
return currentToken;
}
/* Defensive: Allocate memory for lexeme and validate */
while (1) {
c = readerGetChar(sourceBuffer);
DBG("[tok] c='%c' (0x%02X) line=%d pos=%d\n",
(c >= 32 && c < 127 ? c : '?'),
(unsigned char)c,
line,
readerGetPosRead(sourceBuffer));
/* Defensive: Check for invalid character values */
if (c == SCANNER_ERROR) {
fprintf(stderr, "Reader error: Invalid character read.\n");
break;
}
if ((struct_byte)c > 255) {
fprintf(stderr, "Warning: Invalid character code (%d).\n", c);
continue; /* skip invalid input */
}
/* ================================================================
* PART 1 — Token-driven scanner (direct token recognition)
* ================================================================ */
switch ((unsigned char)c) {
case SPC_CHR:
case TAB_CHR:
case '\r':
continue;
case NWL_CHR:
line++;
continue;
case LPR_CHR:
currentToken.code = LPR_T;
scData.scanHistogram[currentToken.code]++;
return currentToken;
case EOS_CHR:
currentToken.code = SEOF_T;
currentToken.attribute.seofType = SEOF_0;
scData.scanHistogram[currentToken.code]++;
return currentToken;
case RPR_CHR:
currentToken.code = RPR_T;
scData.scanHistogram[currentToken.code]++;
return currentToken;
case PRD_CHR:
currentToken.code = DOT_T;
scData.scanHistogram[currentToken.code]++;
return currentToken;
case '\'': { /* single-quoted string literal */
Token t = { 0 };
struct_char ch;
t.attribute.contentString = readerGetPosWrte(stringLiteralTable);
while ((ch = readerGetChar(sourceBuffer)) != '\'' &&
ch != EOS_CHR && ch != EOF_CHR) {
if (ch == NWL_CHR) line++;
readerAddChar(stringLiteralTable, ch);
}
readerAddChar(stringLiteralTable, EOS_CHR);
t.code = STR_T;
scData.scanHistogram[t.code]++;
return t;
}
case '-': { /* support '->' as separator (preserve lexeme) */
struct_char nextCh = readerGetChar(sourceBuffer);
if (nextCh == '>') {
Token t = { 0 };
t.code = SMC_T;
strcpy_s(t.attribute.idLexeme, sizeof(t.attribute.idLexeme), "->");
scData.scanHistogram[t.code]++;
return t;
}
readerRetract(sourceBuffer);
/* let DFA handle standalone '-' */
break;
}
case EOF_CHR:
currentToken.code = SEOF_T;
scData.scanHistogram[currentToken.code]++;
currentToken.attribute.seofType = SEOF_255;
return currentToken;
case COM_CHR: { /* line comment: consume to NL/SEOF */
struct_char ch2;
while ((ch2 = readerGetChar(sourceBuffer)) != NWL_CHR &&
ch2 != EOS_CHR && ch2 != EOF_CHR) {
}
if (ch2 == NWL_CHR) line++;
Token t = { 0 };
t.code = CMT_T;
scData.scanHistogram[t.code]++;
return t;
}
default:
/* ================================================================
* PART 2 — DFA-based pattern recognition (finite-state scanner)
* ================================================================ */
state = 0; /* start state */
/* mark the start of the lexeme at the char we just read in Part I */
lexStart = readerGetPosRead(sourceBuffer) - 1;
readerSetMark(sourceBuffer, lexStart);
DBG("[dfa] start state=%d at=%d\n", state, lexStart);
/* PART 2 — DFA-based */
int col = nextClass(c); /* map char to class/column */
state = nextState(state, col);
lexStart = readerGetPosRead(sourceBuffer) - 1;
readerSetMark(sourceBuffer, lexStart);
int pos = 0;
int lastValidState = state; /* Track last valid state before error */
while (state < NUM_STATES && stateType[state] == NOFS) {
lastValidState = state; /* Remember this state */
c = readerGetChar(sourceBuffer);
col = nextClass(c);
state = nextState(state, col);
pos++;
// Check for error states before accessing stateType
if (state >= NUM_STATES) {
break;
}
// Different limits for different contexts
// State 3 = inside string, allow much longer lexemes
// State 1 = inside identifier, use VID_LEN limit
struct_int maxLen = (state == 3) ? 512 : (VID_LEN * 2);
if (pos > maxLen) {
fprintf(stderr, "Error: Scanner exceeded max lexeme length (state=%d, pos=%d).\n", state, pos);
break;
}
}
// Handle error states (EN, EW, FS)
int alreadyRetracted = 0;
if (state >= NUM_STATES) {
if (state == EW && pos > 0) {
readerRetract(sourceBuffer);
alreadyRetracted = 1;
}
// Map to appropriate accepting state based on what we were building
if (lastValidState == 1) {
state = 2; /* Accept as identifier */
} else if (lastValidState == 6 || lastValidState == 7) {
state = 8; /* Accept as number */
} else {
state = 19; /* Error state */
}
}
if (!alreadyRetracted && state < NUM_STATES && stateType[state] == FSWR && pos > 0) {
readerRetract(sourceBuffer);
}
lexEnd = readerGetPosRead(sourceBuffer);
lexLength = lexEnd - lexStart;
/* Defensive: validate lexLength */
if (lexLength <= 0) {
/* We must advance the reader by one char to avoid looping on the same spot */
readerRestore(sourceBuffer); /* go back to the mark (lexStart) */
c = readerGetChar(sourceBuffer); /* consume exactly one char */
/* produce a 1-char error token for what we just consumed */
static char bad[2] = { 0, 0 };
bad[0] = (char)(unsigned char)c;
currentToken = funcERR(bad);
return currentToken;
}
BufferPointer lb = readerCreate((struct_int)(lexLength + 2), READER_DEFAULT_FACTOR);
readerRestore(sourceBuffer);
for (i = 0; i < lexLength; ++i) {
struct_char ch = readerGetChar(sourceBuffer);
if (!readerAddChar(lb, ch)) break;
}
readerAddChar(lb, READER_TERMINATOR);
lexeme = readerGetContent(lb, 0);
if (state >= 0 && state < NUM_STATES && finalStateTable[state]) {
currentToken = finalStateTable[state](lexeme);
}
else {
currentToken = funcERR(lexeme);
}
readerFree(lb);
return currentToken;
}
/* Defensive: validate token code before histogram update */
if (currentToken.code >= 0 && currentToken.code < NUM_TOKENS) {
scData.scanHistogram[currentToken.code]++;
}
else {
fprintf(stderr, "Warning: Invalid token code (%d).\n", currentToken.code);
}
/* Token recognized → return safely */
if (currentToken.code != 0) {
return currentToken;
}
} // while
return currentToken;
}
static struct_int nextState(struct_int state, struct_int col) {
if (state < 0 || state >= NUM_STATES) return FS;
if (col < 0 || col >= CHAR_CLASSES) return FS;
return transitionTable[state][col];
}
/*
************************************************************
* Get Next Token Class
* Create a function to return the column number in the transition table:
* Considering an input char c, you can identify the "class".
* For instance, a letter should return the column for letters, etc.
***********************************************************
*/
/* TO_DO: Use your column configuration */
/* Adjust the logic to return next column in TT */
static struct_int nextClass(struct_char c) {
/* Lowercase letters → L */
if (c >= 'a' && c <= 'z')
return 0;
/* Digits → D */
if (c >= '0' && c <= '9')
return 1;
/* Uppercase letters OR underscore → U */
if ((c >= 'A' && c <= 'Z') || c == '_')
return 2;
switch (c) {
case '"': return 3; /* Q */
case ' ': case '\t': case '\r': return 4; /* S (WSN) */
case EOS_CHR: return 4; /* treat SEOF like WSN */
case '%': return 5; /* PC */
case ':': return 6; /* CO */
case '>': return 7; /* GT */
case '=': return 8; /* EQ */
case '.': return 9; /* DT */
case '(': return 10; /* LP */
case ')': return 11; /* RP */
case ',': return 12; /* CM */
case ';': return 13; /* SM */
case '-': return 14; /* MN */
case '\\': return 15; /* BS */
case QES_CHR: return 16; /* QM */
case '\n': return 17; /* NL (note: index moved by one) */
default: return 18; /* O */
}
}
/*
************************************************************
* Acceptance State Function COM
* Function responsible to identify COM (comments).
***********************************************************
*/
/* TO_DO: Adjust the function for IL */
Token funcCMT(struct_strg lexeme) {
Token currentToken = { 0 };
struct_int i = 0, len = (struct_int)strlen(lexeme);
currentToken.attribute.contentString = readerGetPosWrte(stringLiteralTable);
for (i = 1; i < len - 1; i++) {
if (lexeme[i] == NWL_CHR)
line++;
}
currentToken.code = CMT_T;
scData.scanHistogram[currentToken.code]++;
return currentToken;
}
/*
************************************************************
* Acceptance State Function IL
* Function responsible to identify IL (integer literals).
* - It is necessary respect the limit (ex: 2-byte integer in C).
* - In the case of larger lexemes, error shoul be returned.
* - Only first ERR_LEN characters are accepted and eventually,
* additional three dots (...) should be put in the output.
***********************************************************
*/
/* TO_DO: Adjust the function for IL */
Token funcIL(struct_strg lexeme) {
Token currentToken = { 0 };
struct_long tlong;
if (lexeme[0] != EOS_CHR && strlen(lexeme) > NUM_LEN) {
currentToken = (*finalStateTable[EN])(lexeme); // too long → error
}
else {
tlong = atol(lexeme);
if (tlong >= 0 && tlong <= SHRT_MAX) {
currentToken.code = NUM_T;
scData.scanHistogram[currentToken.code]++;
currentToken.attribute.intValue = (struct_int)tlong;
}
else {
currentToken = (*finalStateTable[EN])(lexeme); // out of range
}
}
return currentToken;
}
/*
************************************************************
* Acceptance State Function ID
* In this function, the pattern for IDs must be recognized.
* Since keywords obey the same pattern, is required to test if
* the current lexeme matches with KW from language.
* - Remember to respect the limit defined for lexemes (VID_LEN) and
* set the lexeme to the corresponding attribute (vidLexeme).
* Remember to end each token with the \0.
* - Suggestion: Use "strncpy" function.
***********************************************************
*/
/* TO_DO: Adjust the function for ID */
Token funcID(struct_strg lexeme) {
Token token = { 0 };
unsigned char c0 = (unsigned char)lexeme[0];
strncpy(token.attribute.idLexeme, lexeme, VID_LEN);
token.attribute.idLexeme[VID_LEN] = EOS_CHR;
/* Check if it's a variable (uppercase first char) */
if (isupper(c0)) {
token.code = VAR_T;
scData.scanHistogram[token.code]++;
return token;
}
/* Check if it's a keyword */
for (struct_int j = 0; j < KWT_SIZE; j++) {
if (strcmp(lexeme, keywordTable[j]) == 0) {
token.code = KWD_T;
scData.scanHistogram[token.code]++;
return token;
}
}
/* ================================================================
* Lookahead to distinguish FCT_T (fact/predicate) from ATOM_T
* After the DFA processes "foo", it retracts before the '('
* So we can peek ahead to see if '(' follows
* ================================================================ */
struct_int savedPos = readerGetPosRead(sourceBuffer);
struct_char nextCh = readerGetChar(sourceBuffer);
/* Restore position so the '(' will be read by tokenizer next time */
readerSetPos(sourceBuffer, savedPos);
/* Skip any whitespace to find the actual next token */
struct_int tempPos = savedPos;
while (nextCh == ' ' || nextCh == '\t' || nextCh == '\r') {
tempPos = readerGetPosRead(sourceBuffer);
nextCh = readerGetChar(sourceBuffer);
}
/* Restore to original position */
readerSetPos(sourceBuffer, savedPos);
/* If next meaningful character is '(', this is a fact/predicate */
if (nextCh == '(') {
token.code = FCT_T;
scData.scanHistogram[token.code]++;
return token;
}
/* Otherwise it's an atom (constant) */
token.code = ATOM_T;
scData.scanHistogram[token.code]++;
return token;
}
/*
************************************************************
* Acceptance State Function SL
* Function responsible to identify SL (string literals).
* - The lexeme must be stored in the String Literal Table
* (stringLiteralTable). You need to include the literals in
* this structure, using offsets. Remember to include \0 to
* separate the lexemes. Remember also to incremente the line.
***********************************************************
*/
/* TO_DO: Adjust the function for SL */
/*
************************************************************
* Acceptance State Function Error
* Function responsible to deal with ERR token.
* - This function uses the errLexeme, respecting the limit given
* by ERR_LEN. If necessary, use three dots (...) to use the
* limit defined. The error lexeme contains line terminators,
* so remember to increment line.
***********************************************************
*/
/* TO_DO: Adjust the function for Errors */
Token funcERR(struct_strg lexeme) {
Token currentToken = { 0 };
struct_int i = 0, len = (struct_int)strlen(lexeme);
if (len > ERR_LEN) {
strncpy(currentToken.attribute.errLexeme, lexeme, ERR_LEN - 3);
currentToken.attribute.errLexeme[ERR_LEN - 3] = EOS_CHR;
strcat(currentToken.attribute.errLexeme, "...");
}
else {
strcpy(currentToken.attribute.errLexeme, lexeme);
}
for (i = 0; i < len; i++) {
if (lexeme[i] == NWL_CHR)
line++;
}
currentToken.code = ERR_T;
scData.scanHistogram[currentToken.code]++;
return currentToken;
}
/*
************************************************************
* Step3Scanner.c - Updated implementation for CST 8152
* Author: Alex Vu and Yassine Amraoui
************************************************************
*/
/* ------------------------------------------------------------------------
* Acceptance State Functions for tokens
* ------------------------------------------------------------------------
*/
Token funcNUM(struct_strg lexeme) {
Token t = { 0 };
if (strchr(lexeme, '.') != NULL) {
char* endp = NULL;
double dv = strtod(lexeme, &endp);
if (endp && *endp == EOS_CHR) {
t.code = NUM_T;
t.attribute.floatValue = (struct_real)dv;
scData.scanHistogram[t.code]++;
return t;
}
return funcERR(lexeme);
}
else {
if (lexeme[0] != EOS_CHR && strlen(lexeme) > NUM_LEN)
return (*finalStateTable[EN])(lexeme);
struct_long iv = atol(lexeme);
if (iv >= 0 && iv <= SHRT_MAX) {
t.code = NUM_T;
t.attribute.intValue = (struct_int)iv;
scData.scanHistogram[t.code]++;
return t;
}
return funcERR(lexeme);
}
}
Token funcGTE(struct_strg lexeme) {
Token currentToken = { 0 };
currentToken.code = GTE_T;
scData.scanHistogram[currentToken.code]++;
return currentToken;
}
Token funcEQ(struct_strg lexeme) {
Token currentToken = { 0 };
currentToken.code = EQ_T;
scData.scanHistogram[currentToken.code]++;
return currentToken;
}
Token funcDOT(struct_strg lexeme) {
Token currentToken = { 0 };
currentToken.code = DOT_T;
scData.scanHistogram[currentToken.code]++;
return currentToken;
}
Token funcLPR(struct_strg lexeme) {
Token currentToken = { 0 };
currentToken.code = LPR_T;
scData.scanHistogram[currentToken.code]++;
return currentToken;
}
Token funcRPR(struct_strg lexeme) {
Token currentToken = { 0 };
currentToken.code = RPR_T;
scData.scanHistogram[currentToken.code]++;
return currentToken;
}
Token funcCOM(struct_strg lexeme) {
Token currentToken = { 0 };
currentToken.code = COM_T;
scData.scanHistogram[currentToken.code]++;
return currentToken;
}
Token funcSMC(struct_strg lexeme) {
Token currentToken = { 0 };
currentToken.code = SMC_T;
scData.scanHistogram[currentToken.code]++;
return currentToken;
}
/* funcErr, funcIL, funcSL, funcKEY already implemented as shown before */
/*
************************************************************
* The function prints the token returned by the scanner
***********************************************************
*/
/*
************************************************************
* printToken - Prints information about a scanner token
************************************************************
*/
struct_void printToken(Token t) {
switch (t.code) {
case RTE_T:
printf("RTE_T\t\t%s", t.attribute.errLexeme);
if (errorNumber != 0) {
printf("%d", errorNumber);
exit(errorNumber);
}
printf("\n");
break;
case ERR_T:
printf("ERR_T\t\t%s\n", t.attribute.errLexeme);
break;
case SEOF_T:
printf("SEOF_T\t\t%d\n", t.attribute.seofType);
break;
case STR_T:
printf("STR_T\t\t%d\t ", (struct_int)t.attribute.contentString);
printf("%s\n", readerGetContent(stringLiteralTable,
(struct_int)t.attribute.contentString));
break;
case LPR_T: printf("LPR_T\n"); break;
case RPR_T: printf("RPR_T\n"); break;
case CMT_T:
printf("CMT_T\n");
break;
case ATOM_T:
printf("ATOM_T\t\t%s\n", t.attribute.idLexeme);
break;
case NUM_T:
if (t.attribute.floatValue != 0.0 || t.attribute.intValue == 0) {
printf("NUM_T\t\t%g\n", (double)t.attribute.floatValue);
}
else {
printf("NUM_T\t\t%d\n", (int)t.attribute.intValue);
}
break;
case VAR_T:
printf("VAR_T\t\t%s\n", t.attribute.idLexeme);
break;
case IMPL_T: printf("IMPL_T\n"); break;
case GTE_T: printf("GTE_T\n"); break;
case EQ_T: printf("EQ_T\n"); break;
case DOT_T: printf("DOT_T\n"); break;
case COM_T: printf("COM_T\n"); break;
case SMC_T: printf("SMC_T\n"); break;
case FCT_T: printf("FCT_T\t\t%s\n", t.attribute.idLexeme); break;
case KWD_T: printf("KWD_T\t\t%s\n", t.attribute.idLexeme); break;
case QRY_T: printf("QRY_T\n"); break;
default:
printf("Scanner error: invalid token code: %d\n", t.code);
break;
}
}
/*
************************************************************
* The function prints statistics of tokens
* Param:
* - Scanner data
* Return:
* - Void (procedure)
***********************************************************
*/
struct_void printScannerData(ScannerData scData) {
/* Print Scanner statistics */
printf("Statistics:\n");
printf("----------------------------------\n");
int cont = 0;
for (cont = 0; cont < NUM_TOKENS; cont++) {
if (scData.scanHistogram[cont] > 0)
printf("%s%s%s%d%s", "Token[", tokenStrTable[cont], "]=", scData.scanHistogram[cont], "\n");
}
printf("----------------------------------\n");
}
Token funcIQ(struct_strg lexeme) {
Token t = { 0 };
if (lexeme[0] == '?') {
t.code = QRY_T;
scData.scanHistogram[t.code]++;
}
else { /* ':' */
t.code = IMPL_T;
scData.scanHistogram[t.code]++;
}
return t;
}
Token funcSTR(struct_strg lexeme) {
Token currentToken = { 0 };
struct_int i = 0, len = (struct_int)strlen(lexeme);
currentToken.attribute.contentString = readerGetPosWrte(stringLiteralTable);
for (i = 1; i < len - 1; i++) {
if (lexeme[i] == NWL_CHR)
line++;
if (!readerAddChar(stringLiteralTable, lexeme[i])) {
currentToken.code = RTE_T;
scData.scanHistogram[currentToken.code]++;
strcpy(currentToken.attribute.errLexeme, "Run Time Error:");
errorNumber = RTE_CODE;
return currentToken;
}
}
if (!readerAddChar(stringLiteralTable, EOS_CHR)) {
currentToken.code = RTE_T;
scData.scanHistogram[currentToken.code]++;
strcpy(currentToken.attribute.errLexeme, "Run Time Error:");
errorNumber = RTE_CODE;
return currentToken;
}
currentToken.code = STR_T;
scData.scanHistogram[currentToken.code]++;
return currentToken;
}