-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlex.cpp
More file actions
357 lines (340 loc) · 11.5 KB
/
Copy pathlex.cpp
File metadata and controls
357 lines (340 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
#include <cstring>
#include <string>
#include <map>
#include <cstdio>
using namespace std;
#include "lex.h"
#include "comerr.h"
/* Abbreviations:
*
* ID: identifier
* KW: keyword
* OP: operator
* DL: delimiter
* CS: constant
* CM: comment
*/
const char *OP_START = "+-*/<>!&|."; // IMPORTANT: "==" should be judged independently,
// in order not to be confused with delimiter "="
const char *DL_START = "=()[],;{}";
const char BLANK_CHAR[] = " \n\t";
// one-to-one correspondent non-datatype keywords and their codes
const int NON_DATATYPE_KEYWORDS_NUM = 5;
const char *(NON_DATATYPE_KEYWORDS[NON_DATATYPE_KEYWORDS_NUM]) = {"if", "else", "do", "while", "return"};
const SymbolType NON_DATATYPE_KEYWORD_CODES[NON_DATATYPE_KEYWORDS_NUM] = {IF, ELSE, DO, WHILE, RETURN};
// one-to-one correspondent datatype keywords and their codes
const int DATATYPE_KEYWORDS_NUM = 4;
const char *(DATATYPE_KEYWORDS[DATATYPE_KEYWORDS_NUM]) = {"int", "float", "bool", "struct"};
const SymbolType DATATYPE_KEYWORD_CODES[DATATYPE_KEYWORDS_NUM] = {INT, FLOAT, BOOL, STRUCT};
const TokenTableEntry templateTokenEntry = {
NONE, // type
0, // index
0, // row
0, // col
NULL // source
#ifdef MATCH_SOURCE
,0, // start
0 // end
#endif
};
const LexicalSymbolTableEntry templateSymbolEntry {false, {0}};
// lexical analysis context
map<string, int> identifierMap;
map<int, int> intConstantMap;
map<double, int> floatConstantMap;
int row, col;
LexicalError err; // error code when consuming a token.
int skipped; // the number of skipped characters. set when the consumer functions return.
// negative when the consumer assumes more characters present.
int consumeIDKW(const char *s, TokenTable &tokenTable, LexicalSymbolTable &symbolTable);
int consumeOP(const char *s, TokenTable &tokenTable);
int consumeDL(const char *s, TokenTable &tokenTable);
int consumeCS(const char *s, TokenTable &tokenTable, LexicalSymbolTable &symbolTable);
int consumeCM(const char *s, TokenTable &tokenTable);
void clearTable(TokenTable &tokenTable, LexicalSymbolTable &symbolTable);
int lexicalAnalyse(const char *s, int l, TokenTable &tokenTable, LexicalSymbolTable &symbolTable) {
identifierMap.clear();
intConstantMap.clear();
floatConstantMap.clear();
clearTable(tokenTable, symbolTable);
symbolTable.push_back(templateSymbolEntry); // index 0 of the symbol table is not used
row = col = 1;
bool errorOccured = false;
int i = 0;
while(i < l) {
// skip blank characters
while(i < l && strchr(BLANK_CHAR, s[i]) != NULL) {
if(s[i] == '\n') {
row++;
col = 1;
} else
col++;
i++;
}
if(i == l) break;
int currentRow = row;
int currentCol = col;
// judge the type of token by its first character
bool isComment = false;
int tokenLength = 0;
if((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z') || s[i] == '_') // identifier or keyword
tokenLength = consumeIDKW(s + i, tokenTable, symbolTable);
else if(s[i] == '/' && s[i + 1] == '*') { // comment (must prior to operator)
tokenLength = consumeCM(s + i, tokenTable);
isComment = true;
} else if(strchr(OP_START, s[i]) != NULL // operator ("==" is judged independently
|| (s[i] == '=' && s[i + 1] == '=')) // to be distinguished with delimiter "=")
tokenLength = consumeOP(s + i, tokenTable);
else if(strchr(DL_START, s[i]) != NULL) // delimiter
tokenLength = consumeDL(s + i, tokenTable);
else if(s[i] >= '0' && s[i] <= '9') // constant
tokenLength = consumeCS(s + i, tokenTable, symbolTable);
else { // error
err = UNRECOGNIZED_CHARACTER;
tokenLength = 0;
skipped = 1; // skip the character
printf(LEXICAL_ERROR_MESSAGE[err], row, col, s[i]);
}
if(tokenLength > 0) {
tokenTable.back().row = currentRow;
tokenTable.back().col = currentCol;
tokenTable.back().source = new char[tokenLength + 1];
strncpy(tokenTable.back().source, s + i, tokenLength);
tokenTable.back().source[tokenLength] = '\0';
}
#ifdef MATCH_SOURCE
if(tokenLength > 0) { // token consumed successfully
tokenTable.back().start = i;
tokenTable.back().end = i + tokenLength;
}
#endif
if(err) errorOccured = true;
int totLen = tokenLength + (skipped > 0 ? skipped : 0);
i += totLen;
if(!isComment) col += totLen; // `row` and `col` is managed by the consumer function
// when consuming comments
}
return errorOccured ? -1 : 0;
}
void clearTable(TokenTable &tokenTable, LexicalSymbolTable &symbolTable) {
for(TokenTable::iterator it = tokenTable.begin(); it != tokenTable.end(); it++) {
if(it->source)
delete it->source;
if(it->type == IDENTIFIER || it->type == COMMENT) {
if(symbolTable[it->index].value.stringValue != NULL) {
delete symbolTable[it->index].value.stringValue;
symbolTable[it->index].value.stringValue = NULL;
}
}
}
tokenTable.clear();
symbolTable.clear();
}
int consumeIDKW(const char *s, TokenTable &tokenTable, LexicalSymbolTable &symbolTable) {
err = LEXICAL_OK;
skipped = 0;
int i;
for(i = 1; s[i]; i++) {
if(!((s[i] >= '0' && s[i] <= '9')
|| (s[i] >= 'a' && s[i] <= 'z')
|| (s[i] >= 'A' && s[i] <= 'Z')
|| s[i] == '_')) {
break;
}
}
char *str = new char[i + 1];
strncpy(str, s, i);
str[i] = '\0';
// judge if it is a non-datatype keyword
for(int j = 0; j < NON_DATATYPE_KEYWORDS_NUM; j++) {
if(strcmp(NON_DATATYPE_KEYWORDS[j], str) == 0) {
tokenTable.push_back(templateTokenEntry);
tokenTable.back().type = NON_DATATYPE_KEYWORD_CODES[j];
delete[] str;
return i;
}
}
// judge if it is a datatype keyword
for(int j = 0; j < DATATYPE_KEYWORDS_NUM; j++) {
if(strcmp(DATATYPE_KEYWORDS[j], str) == 0) {
tokenTable.push_back(templateTokenEntry);
tokenTable.back().type = DATATYPE_KEYWORD_CODES[j];
delete[] str;
return i;
}
}
// now it must be an identifier
tokenTable.push_back(templateTokenEntry);
tokenTable.back().type = IDENTIFIER;
string tmp = string(str);
if(identifierMap[tmp] == 0) {
identifierMap[tmp] = symbolTable.size();
symbolTable.push_back(templateSymbolEntry);
}
tokenTable.back().index = identifierMap[tmp];
symbolTable[tokenTable.back().index].isString = true;
symbolTable[tokenTable.back().index].value.stringValue = str;
return i;
}
int consumeOP(const char *s, TokenTable &tokenTable) {
err = LEXICAL_OK;
skipped = 0;
SymbolType type;
int l = 1;
if(s[0] == '+')
type = PLUS;
else if(s[0] == '-')
type = MINUS;
else if(s[0] == '*')
type = MULTIPLY;
else if(s[0] == '/')
type = DIVIDE;
else if(s[0] == '<') {
if(s[1] == '=') {
type = LESSEQUAL;
l = 2;
} else
type = LESS;
} else if(s[0] == '>') {
if(s[1] == '=') {
type = GREATEREQUAL;
l = 2;
} else
type = GREATER;
} else if(s[0] == '=' && s[1] == '=') {
type = EQUAL;
l = 2;
} else if(s[0] == '!') {
if(s[1] == '=') {
type = NOTEQUAL;
l = 2;
} else
type = NOT;
} else if(s[0] == '&' && s[1] == '&') {
type = AND;
l = 2;
} else if(s[0] == '|' && s[1] == '|') {
type = OR;
l = 2;
} else if(s[0] == '.')
type = DOT;
else {
err = UNRECOGNIZED_OPERATOR;
printf(LEXICAL_ERROR_MESSAGE[err], row, col, s[0]);
skipped = 1;
return 0;
}
tokenTable.push_back(templateTokenEntry);
tokenTable.back().type = type;
return l;
}
int consumeDL(const char *s, TokenTable &tokenTable) {
err = LEXICAL_OK;
skipped = 0;
SymbolType type;
int l = 1;
if(s[0] == '=')
type = ASSIGN;
else if(s[0] == '(')
type = LEFTPAREN;
else if(s[0] == ')')
type = RIGHTPAREN;
else if(s[0] == '[')
type = LEFTBRACKET;
else if(s[0] == ']')
type = RIGHTBRACKET;
else if(s[0] == ',')
type = COMMA;
else if(s[0] == ';')
type = SEMICOLON;
else if(s[0] == '{')
type = LEFTBRACE;
else if(s[0] == '}')
type = RIGHTBRACE;
else {
err = UNKNOWN_ERROR;
printf(LEXICAL_ERROR_MESSAGE[err], row, col);
skipped = 1;
return 0;
}
tokenTable.push_back(templateTokenEntry);
tokenTable.back().type = type;
return l;
}
int consumeCS(const char *s, TokenTable &tokenTable, LexicalSymbolTable &symbolTable) {
err = LEXICAL_OK;
skipped = 0;
int i;
bool isFloat = false;
int intValue = 0;
double floatValue = 0.0;
double scale = 1.0;
intValue = s[0] - '0';
for(i = 1; s[i]; i++) {
if(s[i] == '.') { // judge if it is a float number
isFloat = true;
floatValue = intValue;
} else if(s[i] >= '0' && s[i] <= '9') {
if(isFloat) {
scale *= 0.1;
floatValue += (int)(s[i] - '0') * scale;
} else {
intValue = intValue * 10 + (int)(s[i] - '0');
}
} else {
break;
}
}
if(isFloat && scale == 1.0) { // no numbers after the decimal point
i--;
isFloat = false;
}
tokenTable.push_back(templateTokenEntry);
tokenTable.back().type = CONSTANT;
if(isFloat) {
if(floatConstantMap[floatValue] == 0) {
floatConstantMap[floatValue] = symbolTable.size();
symbolTable.push_back(templateSymbolEntry);
}
tokenTable.back().index = floatConstantMap[floatValue];
symbolTable[tokenTable.back().index].value.numberValue.value.floatValue = floatValue;
} else {
if(intConstantMap[intValue] == 0) {
intConstantMap[intValue] = symbolTable.size();
symbolTable.push_back(templateSymbolEntry);
}
tokenTable.back().index = intConstantMap[intValue];
symbolTable[tokenTable.back().index].value.numberValue.value.intValue = intValue;
}
symbolTable[tokenTable.back().index].isString = false;
symbolTable[tokenTable.back().index].value.numberValue.isFloat = isFloat;
return i;
}
// IMPORTANT: `row` and `col` is managed by the consumer function when consuming comments,
// because comment is the only kind of token which can span multiple lines.
int consumeCM(const char *s, TokenTable &tokenTable) {
err = LEXICAL_OK;
skipped = 0;
int i = 0;
bool matchedFlag = false;
for(i = 2; s[i]; i++) {
if(s[i] == '*' && s[i + 1] == '/') {
matchedFlag = true;
i = i + 2; // the true length of this comment
col += 2;
break;
} else if(s[i] == '\n') {
row++;
col = 1;
} else
col++;
}
if(!matchedFlag) {
err = INCOMPLETE_COMMENT;
printf(LEXICAL_ERROR_MESSAGE[err], row, col);
skipped = -2;
}
tokenTable.push_back(templateTokenEntry);
tokenTable.back().type = COMMENT;
return i;
}