-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscanner.cpp
More file actions
executable file
·568 lines (556 loc) · 14.7 KB
/
scanner.cpp
File metadata and controls
executable file
·568 lines (556 loc) · 14.7 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
#include "scanner.h"
#include <algorithm>
const Token &Scanner::get() const
{
return current_;
}
bool Scanner::operator ==(int t)
{
return current_.type() == t;
}
bool Scanner::operator !=(int t)
{
return current_.type() != t;
}
int Scanner::is_eof() const
{
return feof(input_);
}
void Scanner::error(const std::string &mes, const Token &token1, const Token &token2, int code)
{
switch (code)
{
case 0:
output_ << "Error at line " << token1.get_line() << ", col " << token1.get_col();
output_ << ": " << mes << "-- \"" << token1.get_string().c_str() << "\"" << std::endl;
break;
case 1:
output_ << "Error at line " << token2.get_line() << ": Expected " << "\"";
output_ << token1.get_string().c_str() << "\" but was \"" << token2.get_string().c_str() << "\"";
break;
}
exit(0);
}
void Scanner::type_match_error(const std::string &t1, const std::string &t2, size_t line)
{
output_ << "Error at line " << line << ": impossible type conversion from " << t2 << " to " << t1;
exit(0);
}
void Scanner::require_token(LexemeType t, const std::string &s)
{
if (*this != t)
error("Expected ", Token(t, s), get(), 1);
next();
}
LexemeType set_type_keyword(const std::string &s)
{
if (s == "and")
return and_op;
if (s == "or")
return or_op;
if (s == "xor")
return xor_op;
if (s == "div")
return div_op;
if (s == "mod")
return mod_op;
if (s == "not")
return not_op;
if (s == "begin")
return begin_stmt;
if (s == "end")
return end_stmt;
if (s == "if")
return if_stmt;
if (s == "for")
return for_stmt;
if (s == "while")
return while_stmt;
if (s == "repeat")
return repeat_stmt;
if (s == "write")
return write_stmt;
if (s == "writeln")
return writeln_stmt;
if (s == "read")
return read_stmt;
if (s == "readln")
return readln_stmt;
if (s == "break")
return break_stmt;
if (s == "continue")
return continue_stmt;
if (s == "do")
return do_stmt;
if (s == "until")
return until_stmt;
if (s == "then")
return then_stmt;
if (s == "else")
return else_stmt;
if (s == "to")
return to_stmt;
if (s == "downto")
return downto_stmt;
if (s == "var")
return var_decl;
if (s == "type")
return type_decl;
if (s == "function")
return function_decl;
if (s == "procedure")
return procedure_decl;
if (s == "integer")
return integer_decl;
if (s == "double")
return double_decl;
if (s == "array")
return array_decl;
if (s == "of")
return of_decl;
if (s == "record")
return rec_decl;
return id;
}
const Token &Scanner::output(LexemeType type, std::string &chars, bool isread)
{
std::transform(chars.begin(), chars.end(), chars.begin(), ::tolower);
isread_ = isread;
state_ = start;
int t = (type == id || type == inum || type == dnum || type == and_op || type == or_op || type == xor_op ||
(type == div_op && chars != "/") || type == mod_op || type == not_op) ? 1 : 0;
size_t column = t ? col_ - chars.size() - 1 : col_ - chars.size();
if (type == id)
type = set_type_keyword(chars);
return current_ = Token(type, chars, line_, column);
}
const Token &Scanner::next()
{
std::string chars;
char local_symbol;
LexemeType t = id;
while (!feof(input_))
{
if (!isread_)
{
++col_;
symbol_ = fgetc(input_);
}
isread_ = false;
switch (state_)
{
case start:
{
if (symbol_ == ' ' || symbol_ == '\n' || symbol_ == '\t')
{
if (symbol_ == '\n')
{
++line_;
col_ = 1;
}
state_ = start;
}
else if (!feof(input_))
{
isread_ = true;
state_ = id_1;
}
break;
}
case id_1:
{
if (isalpha(symbol_))
state_ = id_2;
else
{
isread_ = true;
state_ = numDigit_1;
}
break;
}
case id_2:
{
if (isdigit(symbol_) || isalpha(symbol_) || symbol_ == '_')
state_ = id_2;
else
{
isread_ = true;
return output(id, chars, true);
}
break;
}
case numDigit_1:
{
if (isdigit(symbol_))
state_ = numDigit_2;
else
{
isread_ = true;
state_ = equal_st;
}
break;
}
case numDigit_2:
{
if (isdigit(symbol_))
state_ = numDigit_2;
else if (symbol_ == '.')
state_ = numDot;
else
return output(inum, chars, true);
break;
}
case numDot:
{
if (isdigit(symbol_))
state_ = numDigitAfterDot;
else if(symbol_ == '.')
{
symbol_ = chars[chars.length() - 1];
chars[chars.length() - 1] = '\0';
_fseeki64(input_, -1, SEEK_CUR);
return output(inum, chars, true);
}
else
error("Dot after int number", Token(error_lex, ""));
break;
}
case numDigitAfterDot:
{
if (isdigit(symbol_))
state_ = numDigitAfterDot;
else if (symbol_ == 'e' || symbol_ == 'E')
state_ = numExponent;
else
return output(dnum, chars, true);
break;
}
case numExponent:
{
if(symbol_ == '+' || symbol_ == '-')
state_ = numSign;
else if (isdigit(symbol_))
state_ = numDigitAfterExp;
else
return output(dnum, chars, true);
break;
}
case numSign:
{
if (isdigit(symbol_))
state_ = numDigitAfterExp;
else
return output(dnum, chars, true);
break;
}
case numDigitAfterExp:
{
if (isdigit(symbol_))
state_ = numDigitAfterExp;
else
return output(dnum, chars, true);
break;
}
case equal_st:
{
if (symbol_ == '=')
{
chars += '=';
return output(equal, chars, false);
}
isread_ = true;
state_ = greaterOperator_1;
break;
}
case greaterOperator_1:
{
if (symbol_ == '>')
state_ = greaterOperator_2;
else
{
isread_ = true;
state_ = smallerOperator_1;
}
break;
}
case greaterOperator_2:
{
if (symbol_ == '=')
chars += '=';
else
isread_ = true;
return output(chars.size() == 2 ? greater_equal : greater, chars, isread_);
break;
}
case smallerOperator_1:
{
if (symbol_ == '<')
state_ = smallerOperator_2;
else
{
isread_ = true;
state_ = comment;
}
break;
}
case smallerOperator_2:
{
t = id;
if (symbol_ == '=')
{
chars += '=';
t = lesser_equal;
}
else if (symbol_ == '>')
{
chars += '>';
t = not_equal;
}
else
isread_ = true;
return output(t == id ? lesser : t, chars, isread_);
break;
}
case assignmentOperator:
{
if (symbol_ == '=')
{
chars += '=';
return output(assignment, chars, false);
}
--col_;
isread_ = true;
_fseeki64(input_, -1, SEEK_CUR);
symbol_ = local_symbol;
state_ = divider;
break;
}
case comment:
{
if (symbol_ == '{')
state_ = commentCurlyBrackets;
else if (symbol_ == '(')
state_ = commentRoundBracketBegin;
else if (symbol_ == '/')
state_ = slashComment;
else
{
isread_ = true;
state_ = arithmeticOperator;
}
break;
}
case commentCurlyBrackets:
{
if(symbol_ != '}')
{
size_t l = line_, c = col_ - 2;
local_symbol = fgetc(input_);
while (local_symbol != '}')
{
if (feof(input_))
error("found unclosed comment ", Token(error_lex, "{", l, c));
local_symbol = fgetc(input_);
++col_;
}
}
symbol_ = ' ';
isread_ = true;
state_ = start;
break;
}
case commentRoundBracketBegin:
{
if (symbol_ == '*')
state_ = commentRoundBracketEnd;
else
{
--col_;
_fseeki64(input_, -1, SEEK_CUR);
symbol_ = chars[chars.length() - 1];
isread_ = true;
state_ = divider;
}
break;
}
case commentRoundBracketEnd:
{
char local_chars[2];
size_t l = line_, c = col_ - 3;
local_chars[0] = symbol_;
local_chars[1] = fgetc(input_);
++col_;
if (local_chars[0] == '\n')
++line_;
if (local_chars[1] == '\n')
++line_;
while (local_chars[0] != '*' || local_chars[1] != ')')
{
if(feof(input_))
error("found unclosed comment", Token(error_lex, "(*", l, c));
local_chars[0] = local_chars[1];
local_chars[1] = fgetc(input_);
++col_;
if (local_chars[1] == '\n')
++line_;
}
state_ = start;
break;
}
case slashComment:
{
if (symbol_ == '/')
{
local_symbol = fgetc(input_);
while(local_symbol != '\n' && !feof(input_))
{
local_symbol = fgetc(input_);
++col_;
}
++line_;
state_ = start;
}
else
{
--col_;
_fseeki64(input_, -1, SEEK_CUR);
symbol_ = chars[chars.length() - 1];
isread_ = true;
state_ = arithmeticOperator;
}
break;
}
case arithmeticOperator:
{
if (symbol_ == '+' || symbol_ == '-' || symbol_ == '*' || symbol_ == '/')
{
chars += symbol_;
t = id;
switch (symbol_)
{
case '+':
{
t = plus_op;
break;
}
case '-':
{
t = minus_op;
break;
}
case '*':
{
t = mul_op;
break;
}
case '/':
{
t = div_op;
break;
}
}
return output(t, chars, false);
}
if (symbol_ == ':')
{
local_symbol = symbol_;
state_ = assignmentOperator;
}
else
{
isread_ = true;
state_ = divider;
}
break;
}
case divider:
{
if (symbol_ == '[' || symbol_ == ']' || symbol_ == '(' || symbol_ == ')' ||
symbol_ == ';' || symbol_ == '.' || symbol_ == ',' || symbol_ == ':')
{
chars += symbol_;
t = id;
switch(symbol_)
{
case '[':
{
t = left_square_paren;
break;
}
case ']':
{
t = right_square_paren;
break;
}
case '(':
{
t = left_round_paren;
break;
}
case ')':
{
t = right_round_paren;
break;
}
case ';':
{
t = semicolon;
break;
}
case '.':
{
t = dot;
break;
}
case ',':
{
t = comma;
break;
}
case ':':
{
t = colon;
break;
}
}
return output(t, chars, false);
}
isread_ = true;
state_ = quotation;
break;
}
case quotation:
{
if (symbol_ == '\"' || symbol_ == '\'')
{
size_t l = line_, c = col_ - 2;
local_symbol = fgetc(input_);
++col_;
chars += symbol_;
while (local_symbol != '\"' && local_symbol != '\'')
{
if (feof(input_))
error("Found unclosed quotation ", Token(strng, &symbol_, l, c));
chars += local_symbol;
local_symbol = fgetc(input_);
++col_;
}
chars += symbol_;
return output(strng, chars, false);
}
state_ = errorSymbol;
break;
}
case errorSymbol:
{
error("Undefined symbol ", Token(error_lex, &symbol_));
break;
}
}
if (isread_)
chars = "";
else
chars += symbol_;
}
return current_ = Token(error_lex, "", 0);
}