-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscanner.c
523 lines (490 loc) · 20 KB
/
scanner.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
/**
* @project IFJ17
* @file scanner.c
* @author Jan Bartosek (xbarto92)
* @author Petr Šopf (xsopfp00)
* @brief Returns "tokens" that represent each word or char from stdin to the parser
*/
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include "scanner.h"
#include "error_codes.h"
#include "symtable.h"
//array of all the keywords for an easy return implementation
char *keyWords[] = {"as", "declare", "dim", "do", "double", "else", "end",
"function", "if", "input", "integer", "loop", "print",
"return", "scope", "string", "then", "while", "and",
"boolean", "continue", "elseif", "exit", "false", "for", "next",
"not", "or", "shared", "static", "true"};
unsigned line = 1; // Line Counter (for err messages, exp.: The syntax error is on line 5)
/**
* @copydoc tokenInit
*/
int tokenInit(token *T) {
string attr;
if (strInit(&attr) == ERROR_INTERNAL) {
return ERROR_INTERNAL;
}
T->value = attr;
T->line = line;
return 0;
}
/**
* @copydoc tokenFree
*/
void tokenFree(token *T) {
strFree(&T->value);
}
//TODO Every calling of printErrAndExit should have as the first argument ERROR_SCANNER but it doesn't work for any reason. Temporary replaced by 1.
/**
* @copydoc getNextToken
*/
token getNextToken() {
token T; //TODO - CALL function strFree(attr) !!
if (tokenInit(&T) == ERROR_INTERNAL) {
printErrAndExit(ERROR_INTERNAL, "There is a memory error while allocating token structure.");
T.lexem = ERROR_INTERNAL;
return T;
}
int state = 0;
int c;
bool decimal = false;
bool decimal_e = false;
bool button = false;
while (1) {
c = getchar();
switch (state) {
case 0: // The beginning
if (button || c ==
'\n') { // If there are multiple EOLs, returns only one and increments line counter with every EOL
button = false;
line++;
while (isspace(c)) {
c = getchar();
if (c == '\n') {
line++;
}
}
if (c == 39) {
state = 1;
break;
} else if (c == '/') {
c = getchar();
if (c == 39) {
state = 3;
button = true;
break;
} else {
ungetc(c, stdin);
}
}
ungetc(c, stdin);
T.lexem = EOL_ENUM;
return T;
} else if (c == EOF) {
T.lexem = EOF;
return T;
} else if (c == 39) { // It's an one-line comment
state = 1;
} else if (c == '/') { // It's a multi-line comment or division
state = 2;
} else if (isspace(c)) { // It's a white space (ignore that!)
break;
} else if (isalpha(c) || c == '_') { // It's an ID or a keyword
strAddChar(&T.value, tolower(c));
state = 4;
} else if (isdigit(c)) { // It's a number
strAddChar(&T.value, c);
state = 5;
} else if (c == '!') { // It's a string
c = getchar();
if (c == 34) { // ASCII 34 == "
state = 6;
} else {
printErrAndExit(ERROR_SCANNER,
"on line: %d - The '!' was given and the following should be '\"' but %c was given",
T.line, c);
return T;
}
} else if (c == '+') { // It's a plus
c = getchar();
while (isspace(c)) {
if (c == EOL || c == EOF) {
ungetc(c, stdin);
T.lexem = PLUS;
return T;
}
c = getchar();
}
if (c == '=') {
T.lexem = PLUS_ASSIGNMENT;
return T;
}
ungetc(c, stdin);
T.lexem = PLUS;
return T;
} else if (c == '-') { // It's minus or decrement
c = getchar();
while (isspace(c)) {
if (c == EOL || c == EOF) {
ungetc(c, stdin);
T.lexem = MINUS;
return T;
}
c = getchar();
}
if (c == '=') {
T.lexem = MINUS_ASSIGNMENT;
return T;
}
ungetc(c, stdin);
T.lexem = MINUS;
return T;
} else if (c == '*') {
c = getchar();
while (isspace(c)) {
if (c == EOL || c == EOF) {
ungetc(c, stdin);
T.lexem = MULTIPLY;
return T;
}
c = getchar();
}
if (c == '=') {
T.lexem = MULTIPLY_ASSIGNMENT;
return T;
}
ungetc(c, stdin);
T.lexem = MULTIPLY;
return T;
} else if (c == 92) {
c = getchar();
while (isspace(c)) {
if (c == EOL || c == EOF) {
ungetc(c, stdin);
T.lexem = BACKSLASH;
return T;
}
c = getchar();
}
if (c == '=') {
T.lexem = BACKSLASH_ASSIGNMENT;
return T;
}
ungetc(c, stdin);
T.lexem = BACKSLASH;
return T;
} else if (c == '(') {
T.lexem = BRACKET_LEFT;
return T;
} else if (c == ')') {
T.lexem = BRACKET_RIGHT;
return T;
} else if (c == ',') {
T.lexem = COMMA;
return T;
} else if (c == ';') {
T.lexem = SEMICOLON;
return T;
} else if (c == '=') {
T.lexem = ASSIGNMENT;
return T;
} else if (c == '<') {
c = getchar();
while (isspace(c)) {
if (c == EOL || c == EOF) {
ungetc(c, stdin);
T.lexem = LESS;
return T;
}
c = getchar();
}
if (c == '=') {
T.lexem = LESS_EQUAL;
return T;
} else if (c == '>') {
T.lexem = NOT_EQUAL;
return T;
} else {
ungetc(c, stdin);
T.lexem = LESS;
return T;
}
} else if (c == '>') {
c = getchar();
while (isspace(c)) {
if (c == EOL || c == EOF) {
ungetc(c, stdin);
T.lexem = GREATER;
return T;
}
c = getchar();
}
if (c == '=') {
T.lexem = GREATER_EQUAL;
return T;
} else {
ungetc(c, stdin);
T.lexem = GREATER;
return T;
}
} else if (c == '&') {
c = getchar();
if (c == 'B' || c == 'b') {
strAddChar(&T.value, '0');
state = 7;
} else if (c == 'O' || c == 'o') {
strAddChar(&T.value, '0');
state = 8;
} else if (c == 'H' || c == 'h') {
strAddChar(&T.value, '0');
state = 9;
} else {
printErrAndExit(ERROR_SCANNER, "on line: %d - Character %c was given but B, O or H was expected", T.line, c);
return T;
}
} else {
printErrAndExit(ERROR_SCANNER, "on line: %d - Unknown character was given: %c", T.line, c);
return T;
}
break;
case 1: //one-Line Comment
if (c == EOL) {
state = 0;
ungetc(c, stdin);
} else if (c == EOF) {
T.lexem = EOF;
return T;
}
break;
case 2: // Multi-Line Comment or Division
if (c == 39) { // ASCII 39 == '
state = 3;
break;
} else {
while (isspace(c)) {
if (c == EOL || c == EOF) {
ungetc(c, stdin);
T.lexem = DIVISION;
return T;
}
c = getchar();
}
if (c == '=') {
T.lexem = DIVISION_ASSIGNMENT;
return T;
}
ungetc(c, stdin);
T.lexem = DIVISION;
return T;
}
case 3: // Multi-Line Comment
if (c == 39) {
c = getchar();
if (c == '/') {
state = 0;
}
} else if (c == EOF) {
printErrAndExit(ERROR_SCANNER, "on line: %d - Never-Ending Multi-Line Comment", T.line);
return T;
}
break;
case 4: // ID or Keyword
if (isalnum(c) || c == '_') {
strAddChar(&T.value, tolower(c));
} else {
ungetc(c, stdin);
for (unsigned int i = 0; i < sizeof(keyWords) / sizeof(char *); i++) {
if (strcmp((&T.value)->str, keyWords[i]) == 0) {
T.lexem = 30 + i;
return T;
}
}
if (T.value.length > MAX_ID_LENGTH) {
T.value.str[MAX_ID_LENGTH] = '\0';
}
T.lexem = ID;
return T;
}
break;
case 5: // It's a Number
if (isdigit(c)) {
strAddChar(&T.value, c);
} else if (c == '.') {
if (decimal || decimal_e) {
printErrAndExit(ERROR_SCANNER,
"on line: %d - Wrong format of a decimal number. Multiple dots were used.",
T.line);
return T;
}
strAddChar(&T.value, c);
c = getchar();
if (isdigit(c)) {
strAddChar(&T.value, c);
} else {
printErrAndExit(ERROR_SCANNER,
"on line: %d - Wrong format of a decimal number. A number is required after a dot, but %c was given",
T.line, c);
return T;
}
decimal = true;
} else if (c == 'e' || c == 'E') {
if (decimal_e) {
printErrAndExit(ERROR_SCANNER,
"on line: %d - Wrong format of a decimal number. Multiple exponent expressions were used.",
T.line);
return T;
}
decimal_e = true;
strAddChar(&T.value, c);
c = getchar();
if (isdigit(c) || c == '+' || c == '-') {
if (c == '+' || c == '-') {
char next = getchar();
if (!isdigit(next)) {
printErrAndExit(ERROR_SCANNER,
"on line: %d - Wrong exponent sequence was given.",
T.line);
}
ungetc(next, stdin);
}
strAddChar(&T.value, c);
} else {
printErrAndExit(ERROR_SCANNER,
"on line: %d - Wrong format of a decimal number. A number or '+' or '-' is required after an exponent expression, but %c was given",
T.line, c);
return T;
}
} else {
ungetc(c, stdin);
if (decimal || decimal_e) {
T.lexem = DECIMAL_NUMBER;
return T;
}
T.lexem = NUMBER;
return T;
}
break;
case 6: // String
if (c == EOL || c == EOF) {
printErrAndExit(ERROR_SCANNER,
"on line: %d - Wrong string format. '\"' is required to end the string but EOL or EOF was given",
T.line);
return T;
} else if (c == 34) {
T.lexem = STRING_EXPRESSION;
return T;
}
int ascii = (int) c;
if (ascii <= 31) {
printErrAndExit(ERROR_SCANNER, "on line: %d - All standalone characters in string must have bigger ASCII value then 31!",
T.line);
}
else if (ascii == 92) {
char next = getchar();
if (next == '"') {
strAddChar(&T.value, '"');
} else if (next == 'n') {
strAddChar(&T.value, (char) 92);
strAddChar(&T.value, '0');
strAddChar(&T.value, '1');
strAddChar(&T.value, '0');
} else if (next == 't') {
strAddChar(&T.value, (char) 92);
strAddChar(&T.value, '0');
strAddChar(&T.value, '0');
strAddChar(&T.value, '9');
} else if (next == (char) 92) {
strAddChar(&T.value, (char) 92);
strAddChar(&T.value, '0');
strAddChar(&T.value, '9');
strAddChar(&T.value, '2');
} else if (next >= '0' && next <= '9') {
if (next > '2') {
printErrAndExit(ERROR_SCANNER, "on line: %d - Given escape character is out of range",
T.line);
}
char second = getchar();
if (second >= '0' && second <= '9') {
if (next == '2' && second > '5') {
printErrAndExit(ERROR_SCANNER, "on line: %d - Given escape character is out of range",
T.line);
}
char third = getchar();
if (third >= '0' && third <= '9') {
if ((next == '2' && second == '5' && third > '5') || (next == '0' && second == '0' && third == '0')) {
printErrAndExit(ERROR_SCANNER, "on line: %d - Given escape character is out of range",
T.line);
}
strAddChar(&T.value, (char) 92);
strAddChar(&T.value, next);
strAddChar(&T.value, second);
strAddChar(&T.value, third);
} else {
printErrAndExit(ERROR_SCANNER, "on line: %d - Wrong escape sequence was given",
T.line);
}
} else {
printErrAndExit(ERROR_SCANNER, "on line: %d - Wrong escape sequence was given",
T.line);
}
} else {
printErrAndExit(ERROR_SCANNER, "on line: %d - Wrong escape sequence was given",
T.line);
}
} else if ((ascii >= 0 && ascii <= 32) || ascii == 35) {
strAddChar(&T.value, (char) 92);
strAddChar(&T.value, '0');
if (ascii < 10) {
strAddChar(&T.value, '0');
} else {
int asciiNext = ascii / 10;
strAddChar(&T.value, (char) (asciiNext + 48));
}
ascii = ascii % 10;
strAddChar(&T.value, (char) (ascii + 48));
} else {
strAddChar(&T.value, c);
}
break;
case 7: // Binary number
if (c == '0' || c == '1') {
strAddChar(&T.value, c);
break;
} else {
ungetc(c, stdin);
int val = strtol(T.value.str, 0, 2);
sprintf(T.value.str, "%d", val);
T.lexem = NUMBER;
return T;
}
case 8: // Octal number
if (isdigit(c) && c != '8' && c != '9') {
strAddChar(&T.value, c);
break;
} else {
ungetc(c, stdin);
int val = strtol(T.value.str, 0, 8);
sprintf(T.value.str, "%d", val);
T.lexem = NUMBER;
return T;
}
case 9: // Hex number
if (isdigit(c) || c == 'a' || c == 'A' || c == 'b' || c == 'B' || c == 'c' || c == 'C' ||
c == 'd' || c == 'D' || c == 'e' || c == 'E' || c == 'f' || c == 'F') {
strAddChar(&T.value, c);
break;
} else {
ungetc(c, stdin);
int val = strtol(T.value.str, 0, 16);
strSizeUp(&T.value);
sprintf(T.value.str, "%d", val);
T.lexem = NUMBER;
return T;
}
}
}
}