-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlittle.cpp
More file actions
499 lines (438 loc) · 11.5 KB
/
little.cpp
File metadata and controls
499 lines (438 loc) · 11.5 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
#include <cstdlib>
#include <cctype>
#include <fstream>
#include <iostream>
#include <map>
using namespace std;
enum symbol { NONEsym,
PROGRAMsym,
IFsym,
ELSEsym,
BEGINsym,
ENDsym,
WHILEsym,
IDENTsym,
NUMBERsym,
OPERATORsym,
PRINTsym,
PRINTLNsym,
INPUTsym,
OUTPUTsym };
char lookahead;
ifstream *input_stream;
int row = 1;
bool recording = false;
string record_buffer = "";
bool reproducing = false;
string reproduce_buffer = "";
symbol token;
string value;
int num_value;
map<string, symbol> keywords;
map<string, int> variables;
void abort(string error_message)
{
cerr << endl;
cerr << "Error: " << error_message << "." << endl;
cerr << "[row " << row << "]" << endl;
exit(1);
}
void expected(string expected_message)
{
cerr << expected_message << " expected." << endl;
cerr << "[row " << row << "]" << endl;
exit(1);
}
void next_char()
{
if (reproducing)
{
lookahead = reproduce_buffer[0];
reproduce_buffer.erase(0, 1);
} else
{
lookahead = static_cast<char>(input_stream->get());
if (lookahead == '\n') ++row;
}
if (recording) record_buffer += lookahead;
if (lookahead == '#') while (lookahead != '\n') next_char();
}
void skip_white()
{
while (isspace(lookahead) && (lookahead != '\n')) next_char();
}
void skip_white_newline()
{
while (isspace(lookahead)) next_char();
}
void match(char to_match)
{
if (to_match == lookahead)
{
next_char();
skip_white();
} else expected(string("\"") + to_match + "\"");
}
bool isaddop(char c)
{
return ((c == '+') || (c == '-'));
}
bool ismulop(char c)
{
return ((c == '*') || (c == '/')) || (c == '%');
}
bool isbop(char c)
{
return ((c == '|') || (c == '&'));
}
bool isrop(char c)
{
return ((c == '=') ||
(c == '<') ||
(c == '>'));
}
bool isop(char c)
{
return ((c == '+') ||
(c == '-') ||
(c == '*') ||
(c == '/') ||
(c == '%') ||
(c == '(') ||
(c == ')') ||
(c == '<') ||
(c == '>') ||
(c == '|') ||
(c == '&') ||
(c == '!') ||
(c == '<') ||
(c == '>') ||
(c == '='));
}
symbol get_keyword(string name)
{
map<string, symbol>::iterator i = keywords.find(name);
return i != keywords.end() ? i->second : NONEsym;
}
void get_name_string()
{
value = "";
if (lookahead!='"') expected("string");
next_char();
while (lookahead!='"')
{
value += (char)(lookahead);
next_char();
}
skip_white();
next_char();
skip_white();
}
void get_name()
{
value = "";
if (!isalpha(lookahead)) expected("Name");
while (isalnum(lookahead) || (lookahead == '_'))
{
value += (char)toupper(lookahead);
next_char();
}
symbol k = get_keyword(value);
if (k == NONEsym) token = IDENTsym;
else token = k;
skip_white();
}
void get_number()
{
value = "";
num_value = 0;
if (!isdigit(lookahead)) expected("Integer");
while (isdigit(lookahead))
{
value += lookahead;
num_value = num_value * 10 + lookahead - '0';
next_char();
}
token = NUMBERsym;
skip_white();
}
void get_operator()
{
value = "";
if (!isop(lookahead)) expected("Operator");
while (isop(lookahead))
{
value += lookahead;
next_char();
}
token = OPERATORsym;
skip_white();
}
void scan()
{
skip_white_newline();
if (isalpha(lookahead)) get_name();
else if (isdigit(lookahead)) get_number();
else if (isop(lookahead)) get_operator();
else abort("Unexpected token: '" + value + "'.");
skip_white();
}
int get_var(string name)
{
map<string, int>::iterator i = variables.find(name);
if (i == variables.end()) abort(name + " not found.");
return i->second;
}
int relation();
int factor()
{
int result;
if (lookahead == '(')
{
match('(');
result = relation();
match(')');
}
else if (lookahead == '!')
{
match('!');
return !factor();
}
else
{
scan();
if (token == NUMBERsym) result = num_value;
else result = get_var(value);
}
return result;
}
int term()
{
int result = factor();
while (ismulop(lookahead))
{
get_operator();
if (value == "*") result *= factor();
else if (value == "/") result /= factor();
else if (value == "%") result %= factor();
}
return result;
}
int expression()
{
int result;
if (isaddop(lookahead)) result = 0;
else result = term();
while (isaddop(lookahead))
{
get_operator();
if (value == "+") result += term();
else if (value == "-") result -= term();
}
return result;
}
int b_expression() {
int result = expression();
while (isbop(lookahead))
{
get_operator();
if (value == "|") result = expression() || result;
else if (value == "&") result = expression() && result;
}
return result;
}
int relation()
{
int result = b_expression();
while (isrop(lookahead))
{
get_operator();
if (value == "<") result = result < b_expression();
else if (value == ">") result = result > b_expression();
else if (value == "<=") result = result <= b_expression();
else if (value == ">=") result = result >= b_expression();
else if (value == "==") result = result == b_expression();
else if (value == "<>") result = result != b_expression();
}
return result;
}
void assignment()
{
string var = value;
match('=');
variables[var] = relation();
}
void input()
{
get_name();
cout << "INPUT(" << value << "): ";
int n;
cin >> n;
variables[value] = n;
}
void output()
{
get_name();
cout << "ans = " << value << " -> " << get_var(value) << endl;
}
void print()
{
string nl="\\n";
skip_white();
//cout << "# ";
if (lookahead=='"')
{
get_name_string();
if (value.compare(nl) == 0)
{
cout << endl;
}
else
{
//cout << "[" << value << "]";
cout << value;
}
}
else
{
get_name();
cout << value << "= " << get_var(value) << " ";
//cout << endl;
}
/*
* something more to print
*/
if (lookahead==',')
{
next_char();
print();
}
}
void println()
{
print();
cout << endl;
}
void avoid_internal_blocks()
{
int count = -1;
while ((token != ENDsym) || (count > 0))
{
if (token == BEGINsym) ++count;
else if (token == ENDsym) --count;
skip_white_newline();
scan();
}
}
void block();
void conditional()
{
if (token != IFsym) expected("If");
else
{
int truth = relation();
if (truth) block();
else avoid_internal_blocks();
skip_white_newline();
get_name();
if (token == ELSEsym)
{
if (truth) avoid_internal_blocks();
else block();
skip_white_newline();
get_name();
}
}
}
void repetition_exec(string saved)
{
int truth = 1;
while (truth)
{
string old_buffer = reproduce_buffer;
reproduce_buffer = saved;
reproducing = true;
next_char();
truth = relation();
if (truth) block();
else avoid_internal_blocks();
if (old_buffer == "") reproducing = false;
reproduce_buffer = old_buffer;
}
}
void repetition()
{
if (token != WHILEsym) expected("While");
else {
record_buffer = "";
recording = true;
record_buffer = lookahead + record_buffer;
int truth = relation();
avoid_internal_blocks();
recording = false;
if (truth) repetition_exec(record_buffer);
}
}
void block()
{
skip_white_newline();
get_name();
if (token != BEGINsym) expected("Begin");
bool ifsym;
while (token != ENDsym)
{
ifsym = false;
switch (token)
{
case BEGINsym: break;
case IFsym: conditional(); ifsym = true; break;
case WHILEsym: repetition(); break;
case PRINTsym: print(); break;
case PRINTLNsym: println(); break;
case OUTPUTsym: output(); break;
case INPUTsym: input(); break;
case IDENTsym: assignment(); break;
default: abort("\"" + value + "\": undefined symbol.");
}
if(!ifsym) scan();
}
}
void program() {
skip_white_newline();
next_char();
get_name();
if (token != PROGRAMsym) expected("Program");
get_name();
if (token != IDENTsym) abort("Incorrect program name");
cout << "Running " << value << endl << endl;
block();
cout << endl << "Bye bye... Thank you for using me!" << endl;
}
int main(int argc, char *argv[])
{
if (argc == 2)
{
input_stream = new ifstream();
input_stream->open(argv[1]);
if (input_stream->fail())
{
delete input_stream;
abort("Unable to open input file.");
}
}
else abort("You must specify the input file.");
keywords["PROGRAM"] = PROGRAMsym;
keywords["IF"] = IFsym;
keywords["ELSE"] = ELSEsym;
keywords["BEGIN"] = BEGINsym;
keywords["END"] = ENDsym;
keywords["WHILE"] = WHILEsym;
keywords["INPUT"] = INPUTsym;
keywords["OUTPUT"] = OUTPUTsym;
keywords["PRINT"] = PRINTsym;
keywords["PRINTLN"] = PRINTLNsym;
program();
input_stream->close();
delete input_stream;
return 0;
}