forked from 61c-teach/sp19-proj1-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokenizer.c
570 lines (545 loc) · 16.5 KB
/
tokenizer.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
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
#include "tokenizer.h"
#include <ctype.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "preprocessor.h"
#include "string-helpers.h"
#include "tokenizer-errors.h"
#include "tokens.h"
#include "utils.h"
// Helper Functions to produce tokens
enum TokenType check_keyword(char* str);
size_t SelectToken(char* buffer,
size_t size,
size_t* linenum,
TokenList** tokens,
char* filename);
/*
Global variable to check if we are in a comment and if so to throw out all
characters until a newline is reached.
Hmmm... This might be helpful!
*/
int IS_COMMENT = 0;
/*
Helper function to determine if a token is a keyword. It takes in a
null terminated string and checks it against all of the keywords.
If it makes a keyword it returns that token type and otherwise
returns the error token type.
*/
enum TokenType check_keyword(char* str) {
char lower_str[strlen(str) + 1];
lower_strcpy(lower_str, str);
if (strcmp(lower_str, "int") == 0) {
return TOKEN_KW_INT;
} else if (strcmp(lower_str, "char") == 0) {
return TOKEN_KW_CHAR;
} else if (strcmp(lower_str, "const") == 0) {
return TOKEN_KW_CONST;
} else if (strcmp(lower_str, "struct") == 0) {
return TOKEN_KW_STRUCT;
} else if (strcmp(lower_str, "bool") == 0) {
return TOKEN_KW_BOOL;
} else if (strcmp(lower_str, "for") == 0) {
return TOKEN_KW_FOR;
} else if (strcmp(lower_str, "continue") == 0) {
return TOKEN_KW_CONTINUE;
} else if (strcmp(lower_str, "break") == 0) {
return TOKEN_KW_BREAK;
} else if (strcmp(lower_str, "if") == 0) {
return TOKEN_KW_IF;
} else if (strcmp(lower_str, "else") == 0) {
return TOKEN_KW_ELSE;
} else if (strcmp(lower_str, "true") == 0) {
return TOKEN_KW_TRUE;
} else if (strcmp(lower_str, "false") == 0) {
return TOKEN_KW_FALSE;
} else if (strcmp(lower_str, "return") == 0) {
return TOKEN_KW_RETURN;
} else if (strcmp(lower_str, "null") == 0) {
return TOKEN_KW_NULL;
} else {
return TOKEN_ERR;
}
}
/*
Takes in a buffer with a given size, a pointer to a linenumber which should be
incremented every time a newline is encountered, a pointer to a list of tokens
and a filename. Removes any unnecessary whitespace. Then it searches based
upon
first character to produce a token if it can find one to append to the tokens
list,
possibly an error if the input does not match any valid tokens. Returns the
number
of characters that were consumed to produce tokens (and thus will not be
reused).
*/
size_t SelectToken(char* buffer,
size_t size,
size_t* linenum,
TokenList** tokens,
char* filename) {
char token_contents[size + 1];
size_t size_read = 0;
/* First remove any leading whitespace. */
while (size_read < size && (is_space(buffer[size_read]))) {
if (buffer[size_read] == '\n') {
(*linenum)++;
}
size_read++;
}
/* Check and make sure you haven't only read in whitespace. */
if (size_read == size) {
return size_read;
}
/*
Token that will be appended to the list of tokens. Assumes that if
a token cannot be produced it will return.
*/
TokenList* node = NULL;
Token* t = NULL;
/* Should now be able to narrow tokens by first character. */
if (buffer[size_read] == '+') { // + and ++
if (size_read + 1 == size) {
return size_read;
}
size_read++;
t = create_token(filename);
t->linenum = *linenum;
if (buffer[size_read] == '+') {
t->type = TOKEN_SYM_PLUSPLUS;
size_read++;
} else {
t->type = TOKEN_SYM_PLUS;
}
} else if (buffer[size_read] == '-') { // -, --, ->
if (size_read + 1 == size) {
return size_read;
}
t = create_token(filename);
t->linenum = *linenum;
if (buffer[size_read + 1] == '>') {
t->type = TOKEN_SYM_ARROW;
size_read += 2;
} else if (buffer[size_read + 1] == '-') {
t->type = TOKEN_SYM_MINUSMINUS;
size_read += 2;
} else {
size_read++;
t->type = TOKEN_SYM_MINUS;
}
} else if (buffer[size_read] == '*') { // *
t = create_token(filename);
t->type = TOKEN_SYM_TIMES;
t->linenum = *linenum;
size_read++;
} else if (buffer[size_read] == '/') { // / and comments
if (size_read + 1 == size) {
return size_read;
}
if (0) {
/* YOUR CODE HERE*/
} else {
size_read++;
t = create_token(filename);
t->type = TOKEN_SYM_SLASH;
t->linenum = *linenum;
}
} else if (buffer[size_read] == '=') { // = and ==
if (size_read + 1 == size) {
return size_read;
}
size_read++;
t = create_token(filename);
t->linenum = *linenum;
if (buffer[size_read] == '=') {
t->type = TOKEN_EQUALEQUAL;
size_read++;
} else {
t->type = TOKEN_EQUAL;
}
} else if (buffer[size_read] == '!') { // ! and !=
if (size_read + 1 == size) {
return size_read;
}
size_read++;
t = create_token(filename);
t->linenum = *linenum;
if (buffer[size_read] == '=') {
size_read++;
t->type = TOKEN_BANGEQUAL;
} else {
t->type = TOKEN_SYM_NOTNOT;
}
} else if (buffer[size_read] == '>') { // > and >=
if (size_read + 1 == size) {
return size_read;
}
size_read++;
t = create_token(filename);
t->linenum = *linenum;
if (buffer[size_read] == '=') {
t->type = TOKEN_GREATEREQUAL;
size_read++;
} else {
t->type = TOKEN_GREATER;
}
} else if (buffer[size_read] == '<') { // < and <=
if (size_read + 1 == size) {
return size_read;
}
size_read++;
t = create_token(filename);
t->linenum = *linenum;
if (buffer[size_read] == '=') {
t->type = TOKEN_LESSEQUAL;
size_read++;
} else {
t->type = TOKEN_LESS;
}
} else if (buffer[size_read] == '.') { // .
size_read++;
t = create_token(filename);
t->linenum = *linenum;
t->type = TOKEN_SYM_DOT;
} else if (buffer[size_read] == '&') { // & and &&
if (size_read + 1 == size) {
return size_read;
}
size_read++;
t = create_token(filename);
t->linenum = *linenum;
if (buffer[size_read] == '&') {
t->type = TOKEN_SYM_ANDAND;
size_read++;
} else {
t->type = TOKEN_SYM_AND;
}
} else if (buffer[size_read] == '|') { // | and ||
if (size_read + 1 == size) {
return size_read;
}
size_read++;
t = create_token(filename);
t->linenum = *linenum;
if (buffer[size_read] == '|') {
t->type = TOKEN_SYM_OROR;
size_read++;
} else {
t->type = TOKEN_SYM_OR;
}
} else if (buffer[size_read] == '^') { // ^
size_read++;
t = create_token(filename);
t->linenum = *linenum;
t->type = TOKEN_SYM_XOR;
} else if (buffer[size_read] == '~') { // ~
size_read++;
t = create_token(filename);
t->linenum = *linenum;
t->type = TOKEN_SYM_NOT;
} else if (buffer[size_read] == '(') { // (
size_read++;
t = create_token(filename);
t->linenum = *linenum;
t->type = TOKEN_SYM_OPENPREN;
} else if (buffer[size_read] == ')') { // )
size_read++;
t = create_token(filename);
t->linenum = *linenum;
t->type = TOKEN_SYM_CLOSEPREN;
} else if (buffer[size_read] == '{') { // {
size_read++;
t = create_token(filename);
t->linenum = *linenum;
t->type = TOKEN_SYM_OPENBRACE;
} else if (buffer[size_read] == '}') { // }
size_read++;
t = create_token(filename);
t->linenum = *linenum;
t->type = TOKEN_SYM_CLOSEBRACE;
} else if (buffer[size_read] == ',') { // ,
size_read++;
t = create_token(filename);
t->linenum = *linenum;
t->type = TOKEN_SYM_COMMA;
} else if (buffer[size_read] == ';') { // ;
size_read++;
t = create_token(filename);
t->linenum = *linenum;
t->type = TOKEN_SYM_SEMICOLON;
} else if (buffer[size_read] == '#') { // #include /s* "string" or error
int match = 1;
int i = 0;
for (i = 0; i < strlen (PREPROCESSOR_PRAGMA) && size_read + i < size; i++) {
if (buffer[size_read + i] != PREPROCESSOR_PRAGMA[i]) {
match = 0;
break;
}
}
size_t str_len = 0;
if (match) {
str_len = strlen(PREPROCESSOR_PRAGMA);
int search = 1;
while (size_read + str_len < size && buffer[size_read + str_len] == ' ') {
size_read++;
}
if (size_read + str_len >= size) {
return size_read;
} else if (buffer[size_read + str_len] == '"') {
str_len++;
while (size_read + str_len < size && search) {
if (buffer[size_read + str_len] == '\\') {
if (size_read + str_len + 1 < size &&
replace_escape_in_string(buffer + size_read + str_len) != -1) {
str_len += 2;
} else {
str_len += 1;
}
} else if (buffer[size_read + str_len] == '"') {
search = 0;
size_read += str_len + 1;
return size_read;
} else if (!isprint(buffer[size_read + str_len])) {
search = 0;
int total = generate_string_error(&t, buffer, size_read, size,
*linenum, filename);
if (total == 0) {
return size_read;
} else {
size_read += total;
}
} else {
str_len += 1;
}
}
} else {
int total = generate_string_error(&t, buffer, size_read, size, *linenum,
filename);
if (total == 0) {
return size_read;
} else {
size_read += total;
}
}
} else {
int total = generate_generic_error(&t, buffer, size_read, size, *linenum,
filename);
if (total == 0) {
return size_read;
} else {
size_read += total;
}
}
if (size_read + str_len >= size) {
return size_read;
}
} else if (buffer[size_read] == '\'') { // characters and some errors
/* YOUR CODE HERE */
/* FIXME IM NOT CORRECT. */
int total =
generate_string_error(&t, buffer, size_read, size, *linenum, filename);
if (total == 0) {
return size_read;
} else {
size_read += total;
}
} else if (buffer[size_read] == '"') { // strings and some errors
size_t str_len = 1;
int search = 1;
while (size_read + str_len < size && search) {
if (buffer[size_read + str_len] == '\\') {
if (size_read + str_len + 1 < size &&
replace_escape_in_string(buffer + size_read + str_len) != -1) {
str_len += 2;
} else {
str_len += 1;
}
} else if (buffer[size_read + str_len] == '"') {
search = 0;
/* Create a string token. */
for (int j = 0; j < str_len - 1; j++) {
token_contents[j] = buffer[size_read + j + 1];
}
token_contents[str_len - 1] = '\0';
size_read += str_len + 1;
t = create_token(filename);
t->linenum = *linenum;
t->data.string =
(char*)malloc(sizeof(char) * strlen(token_contents) + 1);
size_t i = 0;
size_t j = 0;
while (i < strlen(token_contents)) {
int escaped = replace_escape_in_string(token_contents + i);
if (escaped == -1) {
t->data.string[j++] = token_contents[i++];
} else {
t->data.string[j++] = escaped;
i += 2;
}
t->data.string[j] = '\0';
}
t->type = TOKEN_STRING;
} else if (!isprint(buffer[size_read + str_len])) {
search = 0;
int total = generate_string_error(&t, buffer, size_read, size, *linenum,
filename);
if (total == 0) {
return size_read;
} else {
size_read += total;
}
} else {
str_len += 1;
}
}
if (search) {
return size_read;
}
} else if (is_digit(
buffer[size_read])) { // positive integers and some errors
size_t int_len = 1;
int search = 1;
while (size_read + int_len < size && search) {
if (is_digit(buffer[size_read + int_len])) {
int_len++;
} else {
search = 0;
/* Create an int token. Hint: you may find the function strtol helpful
*/
/* YOUR CODE HERE */
/* FIXME IM NOT CORRECT. */
int total = generate_string_error(&t, buffer, size_read, size, *linenum,
filename);
if (total == 0) {
return size_read;
} else {
size_read += total;
}
}
}
if (search) {
return size_read;
}
} else { // Identifiers, keywords, and errors
size_t id_len = 1;
int search = 1;
while (size_read + id_len < size && search) {
if (is_identifier_component(buffer[size_read + id_len])) {
id_len++;
} else {
search = 0;
/* Create a token. */
for (int j = 0; j < id_len; j++) {
token_contents[j] = buffer[size_read + j];
}
token_contents[id_len] = '\0';
enum TokenType type = check_keyword(token_contents);
if (type != TOKEN_ERR) {
t = create_token(filename);
t->linenum = *linenum;
t->type = type;
size_read += id_len;
} else if (0) { // FIX ME
/* Handle identifiers */
/* YOUR CODE HERE */
} else {
/* Errors */
int total = generate_generic_error(&t, buffer, size_read, size,
*linenum, filename);
if (total == 0) {
return size_read;
} else {
size_read += total;
}
}
}
}
if (search) {
return size_read;
}
}
/* Append the Token to the end of the list. */
node = create_list(t);
if (*tokens != NULL) {
(*tokens)->next = node;
}
*tokens = node;
return size_read;
}
/*
Takes in a filename and parses the file contents into a series of tokens.
Returns the list of tokens in the file or NULL if there are no tokens
in the file. Files can have unbounded total input size as well as unbounded
line size (see the spec for grading breakdown if you do not handle unbounded
input sizes). If a file cannot be opened or memory cannot be allocated the
program should exit with a nonzero exit code. It is NEVER acceptable for the
program to SEGFAULT.
*/
TokenList* TokenFile(char* filename) {
FILE* f = fopen(filename, "r");
if (f == NULL) {
fprintf(stderr, "Error: Unable to open file %s\n", filename);
exit(1);
}
char* token_buffer = (char*)malloc(DEFAULT_BUFFERSIZE);
if (token_buffer == NULL) {
allocation_failed();
}
size_t buffer_size = 0;
size_t buffer_offset = 0;
size_t mem_size = DEFAULT_BUFFERSIZE;
size_t linenum = 1;
size_t read_size = 0;
size_t tokens_read = 0;
TokenList* tokens = NULL;
TokenList* tokens_end = tokens;
/*
Read in input. We assume an unbounded line size so we cannot read in an
entire line at a time.
*/
while ((read_size = fread(token_buffer + buffer_size, sizeof(char),
mem_size - buffer_size, f)) != 0) {
buffer_size += read_size;
while ((tokens_read = SelectToken(token_buffer + buffer_offset,
buffer_size - buffer_offset, &linenum,
&tokens_end, filename)) != 0) {
if (tokens == NULL) {
tokens = tokens_end;
}
buffer_offset += tokens_read;
}
/*
Move any remaining elements in the buffer to the front. Note that we
cannot use strcpy because copying inside the same buffer leads to
undefined behavior.
*/
/* Set remaining size to to be that in the offset. */
buffer_size = buffer_size - buffer_offset;
if (buffer_offset != 0) {
for (size_t i = 0; i < buffer_size; i++) {
token_buffer[i] = token_buffer[i + buffer_offset];
}
}
/*
Make any necessary increases to the memory capacity of the buffer.
This is only necessary when buffer_offset == 0, but we will do it
earlier for efficiency.
*/
if (buffer_offset < DEFAULT_BUFFERSIZE / 4) {
mem_size *= 2;
token_buffer = (char*)realloc(token_buffer, mem_size);
if (token_buffer == NULL) {
allocation_failed();
}
}
/* Reset offset. */
buffer_offset = 0;
}
/* There should never be any remaining tokens assuming every file ends in a
* newline. */
free(token_buffer);
fclose(f);
return tokens;
}