-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlexer.cpp
More file actions
270 lines (240 loc) · 5.81 KB
/
lexer.cpp
File metadata and controls
270 lines (240 loc) · 5.81 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
#include <stdlib.h>
#include "lexer.h"
#include "token.h"
std::string keywords[] = {"create", "table", "int", "default", "primary",
"key", "insert", "into", "values", "delete", "from", "where", "select"};
Lexer::Lexer() {}
std::vector<Token> Lexer::getToken(std::ifstream& file) {
std::vector<Token> tokens;
std::string line;
std::string tempStr;
// get every lines of input file
while (getline(file, line)) {
// clear the temp string
tempStr = "";
while (line.size() > 0) {
char first = line[0];
// 判断首字符,选择匹配模式
// space, shift
if (first == ' ' || first == '\t' || first == '\r' || first == '\n') {
line = line.erase(0, 1);
}
// number
else if (first >= '0' && first <= '9') {
int len = getDigits(line, tokens);
line = line.erase(0, len);
}
// id or keywords
else if (first == '_' ||
(first >= 'A' && first <= 'Z') ||
(first >= 'a' && first <= 'z')) {
int len = getId_Keyword(line, tokens);
line = line.erase(0, len);
}
// single operator
else if (first == ',' || first == ';' ||
first == '(' || first == ')' ) {
tempStr += first;
Token t("operator", tempStr, -1);
tokens.push_back(t);
line = line.erase(0, 1);
tempStr = tempStr.erase(0, 1);
}
// relational operator
else if (first == '=' || first == '<' || first == '>') {
int len = getOperator(line, tokens);
line = line.erase(0, len);
}
// logical operator
else if (first == '|' || first == '&' || first == '!') {
int len = getLop(line, tokens);
line = line.erase(0, len);
}
// arithmetical operator
else if (first == '+' || first == '-' || first == '/' || first == '*') {
tempStr += first;
Token t("aop", tempStr, -1);
tokens.push_back(t);
line = line.erase(0, 1);
tempStr = tempStr.erase(0, 1);
}
// error string
else {
tempStr += first;
Token t("error", tempStr, -1);
tokens.push_back(t);
line = line.erase(0, 1);
}
}
}
return tokens;
}
// return the length of token
int getDigits(std::string str, std::vector<Token>& tokens) {
std::string tempStr = "";
bool isError = false;
for (int i = 0; i < str.size(); i++) {
if (str[i] >= '0' && str[i] <= '9') {
// add it to token
tempStr += str[i];
}
// digits must be ended with non-alphabet characters
else if (!(str[i] >= 'a' && str[i] <= 'z') &&
!(str[i] >= 'A' && str[i] <= 'Z')) {
break;
}
// must be error
else {
tempStr += str[i];
isError = true;
}
}
if (isError) {
Token t("error", tempStr, -1);
tokens.push_back(t);
}
else {
int number = atoi( tempStr.c_str() );
Token t("num", "", number);
tokens.push_back(t);
}
return tempStr.size();
}
int getId_Keyword(std::string str, std::vector<Token>& tokens) {
std::string tempStr = "";
bool isId = false;
bool isError = false;
for (int i = 0; i < str.size(); i++) {
// underline or digits
if (str[i] == '_' || (str[i] >= '0' && str[i] <= '9')) {
tempStr += str[i];
isId = true;
}
// letters
else if ((str[i] >= 'A' && str[i] <= 'Z') ||
(str[i] >= 'a' && str[i] <= 'z')) {
tempStr += str[i];
}
// end of scanning
else if (!(str[i] >= 'A' && str[i] <= 'Z')
&& !(str[i] >= 'a' && str[i] <= 'z')
&& !(str[i] >= '0' && str[i] <= '9')
&& str[i] != '_') {
break;
}
// error string
else {
tempStr += str[i];
isError = true;
}
}
if (isError) {
Token t("error", tempStr, -1);
tokens.push_back(t);
}
else {
// check whether it's keyword
if (!isId) {
bool isKeyword = false;
// trans it to lower case
std::string str = transLow(tempStr);
for (int i = 0; i < 13; i++) {
if (keywords[i].compare(str) == 0)
isKeyword = true;
}
if (isKeyword) {
Token t("keyword", tempStr, -1);
tokens.push_back(t);
} else {
Token t("id", tempStr, -1);
tokens.push_back(t);
}
}
else {
Token t("id", tempStr, -1);
tokens.push_back(t);
}
}
return tempStr.size();
}
int getOperator(std::string str, std::vector<Token>& tokens) {
std::string tempStr = "";
bool isError = false;
int size = str.size();
for (int i = 0; i < size; i++) {
// token : "=="
if (size > 1 && str[i] == '=' && str[i+1] == '=') {
tempStr = tempStr + str[i] + str[i+1];
}
// token : "<>" or "<="
else if (size > 1 && str[i] == '<' &&
(str[i+1] == '>' || str[i+1] == '=')) {
tempStr = tempStr + str[i] + str[i+1];
}
// token : ">="
else if (size > 1 && str[i] == '>' && str[i+1] == '=') {
tempStr = tempStr + str[i] + str[i+1];
}
// token : "="
else if (str[i] == '=') {
tempStr += str[i];
Token t("operator", tempStr, -1);
tokens.push_back(t);
return tempStr.size();
}
// single op
else {
tempStr += str[i];
}
break;
}
if (isError) {
Token t("error", tempStr, -1);
tokens.push_back(t);
}
else {
Token t("rop", tempStr, -1);
tokens.push_back(t);
}
return tempStr.size();
}
int getLop(std::string str, std::vector<Token>& tokens) {
std::string tempStr = "";
bool isError = false;
int size = str.size();
for (int i = 0; i < size; i++) {
// token : "||"
if (size > 1 && str[i] == '|' && str[i+1] == '|') {
tempStr = tempStr + str[i] + str[i+1];
}
// token : "&&"
else if (size > 1 && str[i] == '&' && str[i+1] == '&') {
tempStr = tempStr + str[i] + str[i+1];
}
// token : "!"
else if (str[i] == '!') {
tempStr = tempStr + str[i];
}
// token : "&&"
else {
isError = true;
}
break;
}
if (isError) {
Token t("error", tempStr, -1);
tokens.push_back(t);
}
else {
Token t("lop", tempStr, -1);
tokens.push_back(t);
}
return tempStr.size();
}
std::string transLow(std::string str) {
for (int i = 0; i < str.size(); i++) {
if (str[i] >= 'A' && str[i] <= 'Z')
str[i] = str[i] + 32;
}
return str;
}