-
Notifications
You must be signed in to change notification settings - Fork 0
/
lex.cpp
265 lines (254 loc) · 7.95 KB
/
lex.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
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
#include "lex.h"
#include "error.h"
Scanner::Scanner(const char *fname)
{
file = fopen(fname, "r");
if(!file)
{
Error::showError(FILE_OPEN_ERROR);
exit(0);
//这里需要错误处理
}
rowNum = 1; colNum = 0;
//nowChar = lastChar = '';
nowPos = lineNum = 0;
}
Scanner::~Scanner()
{
if(file)
{
Error::showError(FILE_OPEN_ERROR);
exit(0);
//这里需要错误处理
}
}
char Scanner::scan()
{
if(!file) return 0;
if(nowPos == lineNum) //需要新读一行
{
lineNum = fread(charArray, 1, BUFFERLEN, file);
if(!lineNum) //文件读完了
{
//printf("+++++++++++++\n");
charArray[0] = -1;
}
nowPos = 0;
}
char ch = charArray[nowPos ++];
if(ch == -1)
{
fclose(file);
file = NULL;
}
else if(ch == '\n')
{
rowNum ++;
colNum = 0;
}
else colNum ++;
return ch;
}
std::pair<int, int> Scanner::getRowCol()
{
return std::make_pair(rowNum, colNum);
}
Lex::Lex(Scanner &sc1): sc(sc1)
{
ch = 0;
token = NULL;
}
Lex::~Lex()
{
delete token; //释放内存
}
void Lex::debug()
{
printf("now char is %c", ch);
system("pause");
}
bool Lex::scan(char need = 0)
{
ch = sc.scan();
if(!need) return true;
if(need == ch)
{
ch = sc.scan();
return true;
}
return false;
}
Token *Lex::getToken()
{
while(ch != -1)
{
if(token)
{
//debug();
delete token; //释放空间
token = NULL;
}
//scan();
//忽略空白符
while(ch == 0 || ch == ' ' || ch == '\n' || ch == '\t') scan();
//标识符
if(ch == '_' || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
{
string s = "";
while(ch == '_' || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) s += ch, scan();
int k = key.getTag(s);
if(k) token = new Token((enum Tag)k); //是一个关键字
else token = new Id(s); //一个普通id
}
//数字
else if(ch >= '0' && ch <= '9')
{
//debug();
int x = 0;
while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0', scan();
token = new Num(x);
}
//字符
else if(ch == '\'')
{
scan();
char c = -1;
if(ch == -1 || ch == '\n' || ch == '\'')
{
Error::showError(ILLEGAL_CHARACTER);
exit(0);
//这里需要错误处理
}
else if(ch == '\\')
{
scan();
if(ch == -1 || ch == '\n')
{
Error::showError(ILLEGAL_CHARACTER);
exit(0);
//这里需要错误处理
}
else if(ch == 'n') c = '\n';
else if(ch == '\\') c = '\\';
else if(ch == 't') c = '\t';
else if(ch == '0') c = '\0';
else if(ch == '\'') c = '\'';
else c = ch;
scan();
if(ch != '\'')
{
Error::showError(ILLEGAL_CHARACTER);
exit(0);
//这里需要错误处理
}
else token = new Char(c), scan(); //读取下一个字符
}
else
{
c = ch;
scan();
if(ch != '\'')
{
Error::showError(ILLEGAL_CHARACTER);
exit(0);
//这里需要错误处理
}
else token = new Char(c), scan(); //读取下一个字符
}
}
//字符串
else if(ch == '\"')
{
scan();
string s = "";
while(ch != '\"')
{
if(ch == '\n' || ch == -1)
{
Error::showError(ILLEGAL_STRING);
exit(0);
//这里需要错误处理
}
else if(ch == '\\')
{
scan();
if(ch == '\n' || ch == -1)
{
Error::showError(ILLEGAL_STRING);
exit(0);
//这里需要错误处理
}
else if(ch == 'n') s += '\n';
else if(ch == '\\') s += '\\';
else if(ch == 't') s += '\t';
else if(ch == '0') s += '\0';
else if(ch == '\"') s += '\"';
}
else s += ch;
scan();
}
token = new Str(s);
scan(); //读取下一个字符
}
//界符
else if(ch == '#') //暂时忽略宏定义
{
while(ch != '\n' && ch != -1) scan();
token = new Token(ERR); //返回一个空?可能需要继续处理
}
else if(ch == '+') token = new Token(scan('+') ? INC : ADD);
else if(ch == '-') token = new Token(scan('-') ? DEC : SUB);
else if(ch == '%') token = new Token(MOD), scan();
else if(ch == '*') token = new Token(MUL), scan();
else if(ch == '/') //处理除法或者注释
{
scan();
if(ch == '/') //单行注释
{
while(ch != '\n' && ch != -1) scan();
token = new Token(ERR); //返回一个空?可能需要继续处理
}
else if(ch == '*') //多行注释
{
char lastCh;
scan();
lastCh = ch;
while(lastCh != '*' || ch != '/')
{
//debug();
if(ch == -1)
{
Error::showError(ILLEGAL_COMMENT);
exit(0);
//这里需要错误处理
}
else lastCh = ch, scan();
}
token = new Token(ERR), scan(); //返回一个空?可能需要继续处理
}
else token = new Token(DIV), scan();
}
else if(ch == '&') token = new Token(scan('&') ? AAND : AND);
else if(ch == '|') token = new Token(scan('|') ? OOR : OR);
else if(ch == '!') token = new Token(scan('=') ? NEQU : NNOT);
else if(ch == '>') token = new Token(scan('=') ? GE : GT);
else if(ch == '<') token = new Token(scan('=') ? LE : LT);
else if(ch == '=') token = new Token(scan('=') ? EQU : ASSIGN);
else if(ch == '^') token = new Token(XOR), scan();
else if(ch == '~') token = new Token(NOT), scan();
else if(ch == ',') token = new Token(COMMA), scan();
else if(ch == ':') token = new Token(COLON), scan();
else if(ch == ';') token = new Token(SEMICON), scan();
else if(ch == '(') token = new Token(LPAREN), scan();
else if(ch == ')') token = new Token(RPAREN), scan();
else if(ch == '[') token = new Token(LBRACKET), scan();
else if(ch == ']') token = new Token(RBRACKET), scan();
else if(ch == '{') token = new Token(LBRACE), scan();
else if(ch == '}') token = new Token(RBRACE), scan();
else token = new Token(ERR), scan(); //读到其他未知字符 暂时忽略
if(token != NULL && token->tag != ERR) return token; //返回一个有效的token
}
//debug();
if(token) delete token; //释放空间
return token = new Token(END); //文件结束
}