-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscan.cpp
213 lines (204 loc) · 6.76 KB
/
scan.cpp
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
/* Simple ad-hoc scanner for the calculator language.
Dies on lexical errors.
Michael L. Scott, 2008-2022.
*/
#include "scan.hpp"
#include <cctype>
#include <iostream>
#include <tuple>
#include <string>
using std::cerr;
using std::cin;
using std::endl;
using std::hex;
using std::make_tuple;
using std::string;
tuple<token, string> scanner::scan() {
string token_image;
while (isspace(c)) {
c = cin.get();
}
if (c == EOF)
return make_tuple(t_eof, "$$");
if (isalpha(c)) {
do {
token_image += c;
c = cin.get();
} while (isalpha(c) || isdigit(c) || c == '_');
if (token_image == "read")
return make_tuple(t_read, "read");
else if (token_image == "write")
return make_tuple(t_write, "write");
else if (token_image == "int")
return make_tuple(t_int, "int");
else if (token_image == "real")
return make_tuple(t_real, "real");
else if (token_image == "if")
return make_tuple(t_if, "if");
else if (token_image == "then")
return make_tuple(t_then, "then");
else if (token_image == "while")
return make_tuple(t_while, "while");
else if (token_image == "do")
return make_tuple(t_do, "do");
else if (token_image == "trunc")
return make_tuple(t_trunc, "trunc");
else if (token_image == "end")
return make_tuple(t_end, "end");
else if (token_image == "float")
return make_tuple(t_float, "float");
else
return make_tuple(t_id, token_image);
} else if (c == '.') {
do {
token_image += c;
c = cin.get();
} while (isdigit(c));
if (c == 'e') {
token_image += c;
c = cin.get();
if (c == '+' || c == '-') {
token_image += c;
c = cin.get();
}
if (!isdigit(c)) {
CalculatorErrorDetect = true;
cerr << "Lexical ERROR: expected digit after 'e' or 'e+' or 'e-', got '" << char(c) << "' (0x" << hex << c << ")\n";
return scan();
}
do {
token_image += c;
c = cin.get();
} while (isdigit(c));
return make_tuple(t_rnum, token_image);
} else if (c == ';' || c == ' ') {
return make_tuple(t_rnum, token_image);
} else {
CalculatorErrorDetect = true;
cerr << "Lexical ERROR: expected digit after '.', got '" << char(c) << "' (0x" << hex << c << ")\n";
return scan();
}
}
else if (isdigit(c)) {
do {
token_image += c;
c = cin.get();
} while (isdigit(c));
if (c == '.') {
do {
token_image += c;
c = cin.get();
} while (isdigit(c));
if (c == 'e') {
token_image += c;
c = cin.get();
if (c == '+' || c == '-') {
token_image += c;
c = cin.get();
}
if (!isdigit(c)) {
CalculatorErrorDetect = true;
cerr << "Lexical ERROR: expected digit after 'e' or 'e+' or 'e-', got '" << char(c) << "' (0x" << hex << c << ")\n";
return scan();
}
do {
token_image += c;
c = cin.get();
} while (isdigit(c));
return make_tuple(t_rnum, token_image);
} else if (c == ';' || c == ' ') {
return make_tuple(t_rnum, token_image);
} else {
CalculatorErrorDetect = true;
cerr << "Lexical ERROR: expected digit after '.', got '" << char(c) << "' (0x" << hex << c << ")\n";
return scan();
}
} else {
return make_tuple(t_inum, token_image);
}
}
else
switch (c) {
case ':':
c = cin.get();
if (c != '=') {
CalculatorErrorDetect = true;
cerr << "Lexical ERROR: expected '=' after ':', got '" << c << "' (0x" << hex << c << ")\n";
return scan();
}
c = cin.get();
return make_tuple(t_gets, ":=");
break;
case '=':
c = cin.get();
if (c != '=') {
CalculatorErrorDetect = true;
cerr << "Lexical ERROR: expected '=' after '=', got '" << c << "' (0x" << hex << c << ")\n";
return scan();
}
c = cin.get();
return make_tuple(t_equal, "==");
break;
case '>':
c = cin.get();
switch (c) {
case '=':
c = cin.get();
return make_tuple(t_largerEqual, ">=");
case ' ':
c = cin.get();
return make_tuple(t_larger, ">");
default:
c = cin.get();
CalculatorErrorDetect = true;
cerr << "Lexical ERROR: expected '=' after '>', got '" << c << "' (0x" << hex << c << ")\n";
return scan();
}
case '<':
c = cin.get();
switch (c) {
case '=':
c = cin.get();
return make_tuple(t_smallerEqual, "<=");
case '>':
c = cin.get();
return make_tuple(t_notEqual, "<>");
case ' ':
c = cin.get();
return make_tuple(t_smaller, "<");
default:
c = cin.get();
CalculatorErrorDetect = true;
cerr << "Lexical ERROR: expected '=' or '>' after '<', got '" << c << "' (0x" << hex << c << ")\n";
return scan();
}
break;
case '+':
c = cin.get();
return make_tuple(t_add, "+");
case '-':
c = cin.get();
return make_tuple(t_sub, "-");
case '*':
c = cin.get();
return make_tuple(t_mul, "*");
case '/':
c = cin.get();
return make_tuple(t_div, "/");
case '(':
c = cin.get();
return make_tuple(t_lparen, "(");
case ')':
c = cin.get();
return make_tuple(t_rparen, ")");
case ';':
c = cin.get();
return make_tuple(t_semicolon, ";");
default:
CalculatorErrorDetect = true;
cerr << "Lexical ERROR: unexpected character '" << char(c) << "' (0x" << hex << c << ")\n";
c = cin.get();
return scan();
}
exit(1);
}