-
Notifications
You must be signed in to change notification settings - Fork 1
/
scanner.c
508 lines (450 loc) · 12.5 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
/* scanner.c
*
* Token scanner (in literature also called symbol scanner)
*
* A program consist of a sequence of tokens. A token is a group of one or
* more characters which have a special meaning in the programming language.
* The scanner reads a program character by character (by using the 'reader'
* object) and converts these into tokens.
*
* Object 'scanner' is the API to the token scanner. Only one scanner object
* exists. For its definition see scanner.h.
*
* The next token is read by calling 'scanner.next'. On return variable
* 'scanner.token' contains the token and 'scanner.string' - if applicable - the
* identifier, the number, the character or the string. In all other cases it
* contains an empty string ("").
*
* 1994 K.W.E. de Lange
*/
#include <assert.h>
#include <string.h>
#include <ctype.h>
#include "identifier.h"
#include "scanner.h"
#include "reader.h"
#include "error.h"
/* Table containing all language keywords and their corresponding tokens.
*/
static struct {
char *keyword;
token_t token;
} keywordTable[] = { /* Note: keyword strings must be sorted alphabetically */
{ "and", AND },
{ "break", BREAK },
{ "char", DEFCHAR },
{ "continue", CONTINUE },
{ "def", DEFFUNC },
{ "do", DO },
{ "else", ELSE },
{ "float", DEFFLOAT },
{ "for", FOR },
{ "if", IF },
{ "import", IMPORT },
{ "in", IN },
{ "input", INPUT },
{ "int", DEFINT },
{ "list", DEFLIST},
{ "or", OR },
{ "pass", PASS },
{ "print", PRINT },
{ "return", RETURN },
{ "str", DEFSTR },
{ "while", WHILE }
};
/* Forward declarations.
*/
static token_t read_next_token(char *buffer);
static token_t read_identifier(char *buffer);
static token_t read_character(char *buffer);
static token_t read_string(char *buffer);
static token_t read_number(char *buffer);
/* API: Initialize scanner object 'sc'.
*/
static void scanner_init(struct scanner *sc)
{
assert(sc != NULL);
/* load the function addresses from the global scanner */
*sc = scanner;
/* reset all object variables to their initial states */
sc->token = UNKNOWN;
sc->peeked = 0;
sc->at_bol = true;
sc->string[0] = 0;
}
/* API: Save the global scanner state in sc.
*/
static void scanner_save(struct scanner *sc)
{
assert(sc != NULL);
*sc = scanner;
}
/* API: Load the global scanner state from sc.
*/
static void scanner_jump(struct scanner *sc)
{
assert(sc != NULL);
scanner = *sc;
}
/* API: Read the next token.
*
* return token read
*
* If previously a peek was executed then return the peeked token.
*/
static token_t next_token(void)
{
if (scanner.peeked == 0)
scanner.token = read_next_token(scanner.string);
else {
scanner.token = scanner.peeked;
scanner.peeked = 0;
}
debug_printf(DEBUGTOKEN, "\ntoken : %s %s", \
tokenName(scanner.token), scanner.string);
return scanner.token;
}
/* API: Look at the next token, without actually considering it read.
*
* return peeked token
*
* Only a single peek is possible, you cannot look more then 1 token ahead.
*/
static token_t peek_token(void)
{
if (scanner.peeked == 0)
scanner.peeked = read_next_token(scanner.string);
return scanner.peeked;
}
/* Read the next token.
*
* buffer pointer to buffer containing the token which was read
* return obecttype which was read
*
* After reading 'buffer' contains:
* the identifier if token == IDENTIFIER
* the number if token == INTEGER or FLOAT
* the string if token == STRING
* the character if token == CHAR
* and an empty string ("") for all other tokens
*/
static token_t read_next_token(char *buffer)
{
char ch;
assert(buffer != NULL);
buffer[0] = 0;
/* Determine the level of indentation. If it has increased compared to the
* previous line then token is INDENT. Has it decreased then check if it
* was equal to the previous (smaller) indentation. If so then the token
* is DEDENT, else there is an indentation error.
* If the indentation has not changed then continue reading the next token.
*/
while (scanner.at_bol == true) {
int col = 0;
scanner.at_bol = false;
/* determine the indentation */
while (1) {
ch = reader.nextch();
if (ch == ' ')
col++;
else if (ch == '\t')
col = (col / config.tabsize + 1) * config.tabsize;
else
break;
} /* col = column-nr of first character which is not tab or space */
/* ignore empty lines or comment only lines */
if (ch == '#')
while (ch != '\n' && ch != EOF)
ch = reader.nextch();
if (ch == '\n') {
scanner.at_bol = true;
continue;
} else if (ch == EOF) {
col = 0; /* do we need more DEDENTs? */
if (col == local->indentation[local->indentlevel])
return ENDMARKER;
} else
reader.pushch(ch);
if (col == local->indentation[local->indentlevel])
break; /* indentation has not changed */
else if (col > local->indentation[local->indentlevel]) {
if (local->indentlevel == MAXINDENT)
error(SyntaxError, "max indentation level reached");
local->indentation[++local->indentlevel] = col;
return INDENT;
} else { /* col < local->indentation[local->level] */
if (--local->indentlevel < 0)
error(SyntaxError, "inconsistent use of TAB and space in identation");
if (col != local->indentation[local->indentlevel]) {
scanner.at_bol = true; /* not yet at old indentation level */
reader.to_bol();
}
return DEDENT;
}
}
/* skip spaces */
do {
ch = reader.nextch();
} while (ch == ' ' || ch == '\t');
/* skip comments */
if (ch == '#')
while (ch != '\n' && ch != EOF)
ch = reader.nextch();
/* check for end of line or end of file */
if (ch == '\n') {
scanner.at_bol = true;
return NEWLINE;
} else if (ch == EOF)
return ENDMARKER;
if (isdigit(ch)) {
reader.pushch(ch);
return read_number(buffer);
} else if (isalpha(ch)) {
reader.pushch(ch);
return read_identifier(buffer);
} else {
switch (ch) {
case '\'': return read_character(buffer);
case '\"': return read_string(buffer);
case EOF : return ENDMARKER;
case '(' : return LPAR;
case ')' : return RPAR;
case '[' : return LSQB;
case ']' : return RSQB;
case ',' : return COMMA;
case '.' : return DOT;
case ':' : return COLON;
case '*' : if (reader.peekch() == '=') {
reader.nextch();
return STAREQUAL;
} else
return STAR;
case '%' : if (reader.peekch() == '=') {
reader.nextch();
return PERCENTEQUAL;
} else
return PERCENT;
case '+' : if (reader.peekch() == '=') {
reader.nextch();
return PLUSEQUAL;
} else
return PLUS;
case '-' : if (reader.peekch() == '=') {
reader.nextch();
return MINUSEQUAL;
} else
return MINUS;
case '/' : if (reader.peekch() == '=') {
reader.nextch();
return SLASHEQUAL;
} else
return SLASH;
case '!' : if (reader.peekch() == '=') {
reader.nextch();
return NOTEQUAL;
} else
return NOT;
case '=' : if (reader.peekch() == '=') {
reader.nextch();
return EQEQUAL;
} else
return EQUAL;
case '<' : if (reader.peekch() == '=') {
reader.nextch();
return LESSEQUAL;
} else if (reader.peekch() == '>') {
reader.nextch();
return NOTEQUAL;
} else
return LESS;
case '>' : if (reader.peekch() == '=') {
reader.nextch();
return GREATEREQUAL;
} else
return GREATER;
default : return UNKNOWN;
}
}
}
/* Read a string.
*
* string pointer to a buffer where the string will be stored
* return objecttype which was read (by definition STR_T)
*
* Strings are surrounded by double quotes. Escape sequences are recognized.
* Examples: "abc" "xyz\n" ""
*/
static token_t read_string(char *string)
{
char ch;
int count = 0;
while (1) {
ch = reader.nextch();
if (ch != EOF && ch != '\"') {
if (ch == '\\')
switch (reader.peekch()) {
case '0' : reader.nextch(); ch = '\0'; break;
case 'a' : reader.nextch(); ch = '\a'; break;
case 'b' : reader.nextch(); ch = '\b'; break;
case 'f' : reader.nextch(); ch = '\f'; break;
case 'n' : reader.nextch(); ch = '\n'; break;
case 'r' : reader.nextch(); ch = '\r'; break;
case 't' : reader.nextch(); ch = '\t'; break;
case 'v' : reader.nextch(); ch = '\v'; break;
case '\\': reader.nextch(); ch = '\\'; break;
case '\'': reader.nextch(); ch = '\''; break;
case '\"': reader.nextch(); ch = '\"'; break;
}
if (count < BUFSIZE)
string[count++]= ch;
} else {
string[count] = 0;
break;
}
}
return STR_T;
}
/* Read an integer or a floating point number.
*
* number pointer to buffer with string representation of the number read
* return objecttype which was read (INT_T or FLOAT_T)
*
* Scientific notation (e, E) is recognized.
* Examples: 2 2. 0.2 2.0 1E+2 1E2 1E-2 0.1e+2
*/
static token_t read_number(char *number)
{
char ch;
int dot = 0;
int exp = 0;
int count = 0;
while (1) {
ch = reader.nextch();
if (ch != EOF && (isdigit(ch) || ch == '.')) {
if (ch == '.') {
if (++dot > 1)
error(ValueError, "multiple decimal points");
}
if (count < BUFSIZE)
number[count++] = ch;
} else { /* check for scientific notation */
if (ch == 'e' || ch == 'E') {
exp = 1;
if (count < BUFSIZE)
number[count++] = ch;
ch = reader.nextch();
if (ch == '-' || ch == '+') {
if (count < BUFSIZE)
number[count++] = ch;
ch = reader.nextch();
}
if (!isdigit(ch))
error(ValueError, "missing exponent");
while (ch != EOF && isdigit(ch)) {
if (count < BUFSIZE)
number[count++] = ch;
ch = reader.nextch();
}
}
number[count] = 0;
reader.pushch(ch);
break;
}
}
if (dot == 1 || exp == 1)
return FLOAT_T;
return INT_T;
}
/* Read a name and check whether it is a keyword or an identifier.
*
* name pointer to buffer with keyword or identifier
* return keyword token (or IDENTIFIER in case of an identifier)
*
* A name consist of digits, letters and underscores, and must start with
* a letter.
*/
static token_t read_identifier(char *name)
{
char ch;
int count = 0, l, h, m, d;
while (1) {
ch = reader.nextch();
if (ch != EOF && (isalnum(ch) || ch == '_')) {
if (count < BUFSIZE)
name[count++] = ch;
} else {
name[count] = 0;
reader.pushch(ch);
break;
}
}
l = 0, h = (int)(sizeof keywordTable / sizeof keywordTable[0]) - 1;
while (l <= h) {
m = (l + h) / 2;
d = strcmp(&name[0], keywordTable[m].keyword);
if (d < 0)
h = m - 1;
if (d > 0)
l = m + 1;
if (d == 0)
break;
};
if (d == 0) {
name[0] = 0;
return keywordTable[m].token;
} else
return IDENTIFIER;
}
/* Read a character constant. This can be a single letter or an escape sequence.
*
* c pointer to buffer with the character read
* return objecttype which was read (by definition CHAR_T)
*
* A character constant is surrounded by single quotes.
* Examples: 'a' '\n'
*/
static token_t read_character(char *c)
{
char ch;
ch = reader.nextch();
if (ch == '\\') { /* is an escape sequence */
ch = reader.nextch();
switch (ch) {
case '0' : c[0] = '\0'; break;
case 'a' : c[0] = '\a'; break;
case 'b' : c[0] = '\b'; break;
case 'f' : c[0] = '\f'; break;
case 'n' : c[0] = '\n'; break;
case 'r' : c[0] = '\r'; break;
case 't' : c[0] = '\t'; break;
case 'v' : c[0] = '\v'; break;
case '\\': c[0] = '\\'; break;
case '\'': c[0] = '\''; break;
case '\"': c[0] = '\"'; break;
default : error(SyntaxError, "unknown escape sequence: %c", ch);
}
} else { /* not an escape sequence */
if (ch == '\'' || ch == EOF)
error(SyntaxError, "empty character constant");
else
c[0] = ch;
}
ch = reader.nextch();
if (ch != '\'')
error(SyntaxError, "to many characters in character constant");
c[1] = 0;
return CHAR_T;
}
/* Token scanner API and data, including the initial settings.
*/
Scanner scanner = {
.token = UNKNOWN,
.peeked = 0,
.at_bol = true,
.string[0] = 0,
.next = next_token,
.peek = peek_token,
.init = scanner_init,
.save = scanner_save,
.jump = scanner_jump
};