-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlex.cpp
443 lines (421 loc) · 13.1 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
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
#include "stdafx.h"
//work variables
signed char ch = ' '; //last character, initialized with space
int cc_start = 0; //character counter before the valid token
int cc = 0; //character counter
int lc = 1; //line counter
map<char, int> singletoken;
map<string, int> keyword;
//lexical output variables
int tkntyp; //token type
int tknval; //number value, operator subtype
char tknchar; //char literal
string tknstr; //string literal, identifier name
//Safely get char from src_input. When at EOF, return one more char and throw
//exception at next calling. This function is aimed to handling the tail problem
signed char getcs()
{
static int eofamt = 0;
char c = ' ';
if(*src_input)//there are more chars to read
{
src_input->get(c);
return (c>0)? c: ' ';
}
if(eofamt<2)
{
++eofamt;
return -1;
}
FATAL_ERROR("Source input incomplete.");//fatal error
return ' ';
}
void lex_init()
{
//fill in the table
singletoken[';']=semicln_tk;
singletoken[',']=comma_tk;
singletoken['[']=lbrkt_tk;
singletoken[']']=rbrkt_tk;
singletoken['(']=lprt_tk;
singletoken[')']=rprt_tk;
singletoken['{']=lbrc_tk;
singletoken['}']=rbrc_tk;
singletoken[':']=cln_tk;
keyword[string("const")] = const_tk;
keyword[string("int")] = int_tk;
keyword[string("char")] = char_tk;
keyword[string("void")] = void_tk;
keyword[string("if")] = if_tk;
keyword[string("while")] = while_tk;
keyword[string("switch")] = switch_tk;
keyword[string("case")] = case_tk;
keyword[string("default")] = default_tk;
keyword[string("return")] = return_tk;
//read the first char
ch = getcs(), ++cc;
}
//assuming that ch_before=='/'&&ch=='*'
void jump_multiline_comment()
{
char ch_before = '/'; //the old value of ch
do
{
ch_before = ch;
ch = getcs(), ++cc;
if(ch == '\n')
cc=0, ++lc; //update counter
}while(!(ch_before=='*' && ch=='/'));
ch = getcs(), ++cc;
}
//read string literal. assuming that ch = '"'
bool getstrlit()
{
bool iswarned = false;
tknstr.clear();
while(true)
{
ch = getcs(), ++cc;
if(ch == '\n')
{
ERROR("String cannot get cross over lines.");//TODO: ERROR( string cannot get cross over lines )
return true;
}
if(ch == '"')//end of string
{
ch = getcs(), ++cc;
return true;
}
if(!iswarned && (ch < 32 || ch == 34 || ch >126))
{
iswarned = true;
ERROR("Unsupported char found in the string.");//TODO: WARN( unsupported char );
}
tknstr += ch;
}
}
//read char literal. assuming that ch = '\''
bool getchrlit()
{
tknchar = ' ';
ch = getcs(), ++cc;
if(ch == '\'')
{
ERROR("Empty chracter literal.");//TODO:ERROR(empty character literal)
ch = getcs(), ++cc;
return true;
}
if(ch == '\n')
{
ERROR("Char literal cannot get cross over lines.");//TODO:ERROR( char literal cannot get cross over lines )
return true;
}
tknchar = ch;
if(!(ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '_' || (ch >= 'a' && ch <='z') ||
(ch >= 'A' && ch <='Z') || (ch >= '0' && ch <='9')))
WARNING("This char is out of standard.");//TODO:ERROR( char set isn't allowed)
ch = getcs(), ++cc;
if(ch != '\'' && ch != '\n')
ERROR("Char iteral is too long.");//TODO:ERROR( literal is too long )
while(ch != '\'')
{
if(ch == '\n')
{
ERROR("Char literal cannot get cross over lines.");//TODO:ERROR( char literal cannot get cross over lines )
return true;
}
ch = getcs(), ++cc;
}
ch = getcs(), ++cc;
return true;
}
//read integer literal. assuming that ch is the first digit
bool getintlit()
{
bool isoverflow = false;
tknval = (ch - '0');
ch = getcs(), ++cc;
if(tknval == 0 && ch >= '0' && ch <='9')//number start with 0
ERROR("Number cannot be started with 0.");//TODO:ERROR( number cannot be started with 0
while(ch >= '0' && ch <='9')
{
if(tknval > (INT_MAX-(ch - '0'))/10 )
isoverflow = true;
tknval *= 10;
tknval += (ch - '0');
ch = getcs(), ++cc;
}
if(isoverflow)
WARNING("Integer literal overflows.");
return true;
}
//read identifier and keyword, assuming that ch is the first letter
bool getidentifier()
{
tknstr.clear();
tknstr += ch;
ch = getcs(), ++cc;
while(ch == '_' || (ch >= 'a' && ch <='z') || (ch >= 'A' && ch <='Z') || (ch >= '0' && ch <='9'))
{
tknstr += ch;
ch = getcs(), ++cc;
}
if(keyword.find(tknstr) != keyword.end())//tknstr is keyword
tkntyp = keyword[tknstr];
return true;
}
//Read a token and fill the valid output into output variables,
//pass the EOF exception out.
//One character read before is needed.
void gettoken()
{
while(true)//loop until one valid char is read
{
cc_start = cc;
//jump the white char
if(ch==' ' || ch=='\n' || ch=='\t')
{
if(ch == '\n')
cc=0, ++lc; //update counter
ch = getcs(), ++cc; //read new char
continue;
}
if(ch >= '0' && ch <='9')//number literal
{
tkntyp = intlit_tk;
if(getintlit())
return;
else
continue;
}
//identifier or keyword
if(ch == '_' || (ch >= 'a' && ch <='z') || (ch >= 'A' && ch <='Z'))
{
tkntyp = id_tk;
if(getidentifier())
return;
else
continue;
}
//if ch is a single sign, output it
if(singletoken.find(ch) != singletoken.end()) //ch in the map
{
tkntyp = singletoken[ch];
ch = getcs(), ++cc;
return;
}
switch(ch)
{
case '+':
tkntyp = addsub_tk;
tknval = addop;
ch = getcs(), ++cc;
return;
case '-':
tkntyp = addsub_tk;
tknval = subop;
ch = getcs(), ++cc;
return;
case '*':
tkntyp = multdiv_tk;
tknval = multop;
ch = getcs(), ++cc;
return;
case '/':
ch = getcs(), ++cc;
if(ch == '/')//single line comment
while(ch != '\n')
ch = getcs(), ++cc;
else if(ch == '*')//multi-line comment
jump_multiline_comment();
else//division operator
{
tkntyp = multdiv_tk;
tknval = divop;
return;
}
break;//after jumping comment, continue to read token
case '=':
ch = getcs(), ++cc;
if(ch == '=')//equals
{
tkntyp = rltop_tk;
tknval = eqlop;
ch = getcs(), ++cc;
return;
}
else//assign
{
tkntyp = assign_tk;
return;
}
case '<':
ch = getcs(), ++cc;
if(ch == '=')//less or equal than
{
tkntyp = rltop_tk;
tknval = leop;
ch = getcs(), ++cc;
return;
}
else//less than
{
tkntyp = rltop_tk;
tknval = ltop;
return;
}
case '>':
ch = getcs(), ++cc;
if(ch == '=')//greater or equal than
{
tkntyp = rltop_tk;
tknval = geop;
ch = getcs(), ++cc;
return;
}
else//greater than
{
tkntyp = rltop_tk;
tknval = gtop;
return;
}
case '!':
ch = getcs(), ++cc;
if(ch == '=')//not equal
{
tkntyp = rltop_tk;
tknval = neqlop;
ch = getcs(), ++cc;
return;
}
ERROR("Unrecognized token '!'.");//TODO: ERROR(jumped unrecognized token '!')
break;//jump unrecognized token, continue to read next token
case '"':
tkntyp = strlit_tk;
if(getstrlit())
return;
break;
case '\'':
tkntyp = chrlit_tk;
if(getchrlit())
return;
break;
default:
if(ch<0)
{
tkntyp = eof_tk;
ch = getcs(), ++cc;
return;
}
//TODO:ERROR
ERROR("Unknown char code: "+tostr(int(ch)));
ch = getcs(), ++cc;
}
}
}
//repeatedly call gettoken() and output the token into lex_output.
void lex_dump()
{
int cnt = 0;
ostream &out = *lex_output;
map<int, string> tokentypename;
map<int, string> rltoptypesign;
map<int, char> signtype;
tokentypename[chrlit_tk] = string("CharLiteral");
tokentypename[strlit_tk] = string("StringLit..");
tokentypename[intlit_tk] = string("IntLiteral");
tokentypename[id_tk] = string("Identifier");
tokentypename[const_tk] = string("KW: const");
tokentypename[int_tk] = string("KW: int");
tokentypename[char_tk] = string("KW: char");
tokentypename[void_tk] = string("KW: void");
tokentypename[if_tk] = string("KW: if");
tokentypename[while_tk] = string("KW: while");
tokentypename[switch_tk] = string("KW: switch");
tokentypename[case_tk] = string("KW: case");
tokentypename[default_tk] = string("KW: default");
tokentypename[return_tk] = string("KW: return");
tokentypename[semicln_tk] = string("Semicolon");
tokentypename[comma_tk] = string("Comma");
tokentypename[assign_tk] = string("Assign");
tokentypename[lbrkt_tk] = string("LBracket");
tokentypename[rbrkt_tk] = string("RBracket");
tokentypename[lprt_tk] = string("LeftPar");
tokentypename[rprt_tk] = string("RightPar");
tokentypename[lbrc_tk] = string("LBrace");
tokentypename[rbrc_tk] = string("RBrace");
tokentypename[cln_tk] = string("Colon");
tokentypename[addsub_tk] = string("AddSub");
tokentypename[multdiv_tk] = string("MultDiv");
tokentypename[rltop_tk] = string("RelationOp");
rltoptypesign[ltop] = string("<");
rltoptypesign[leop] = string("<=");
rltoptypesign[gtop] = string(">");
rltoptypesign[geop] = string(">=");
rltoptypesign[neqlop] = string("!=");
rltoptypesign[eqlop] = string("==");
signtype[semicln_tk] = ';';
signtype[comma_tk] = ',';
signtype[assign_tk] = '=';
signtype[lbrkt_tk] = '[';
signtype[rbrkt_tk] = ']';
signtype[lprt_tk] = '(';
signtype[rprt_tk] = ')';
signtype[lbrc_tk] = '{';
signtype[rbrc_tk] = '}';
signtype[cln_tk] = ':';
out << left;
while(tkntyp!=eof_tk)
{
++cnt;
gettoken();
out << setw(4) << lc << setw(4) << cc_start << setw(4) << cc;
switch (tkntyp)
{
case const_tk: case int_tk: case char_tk:
case void_tk: case if_tk: case while_tk:
case case_tk: case default_tk: case return_tk:
case switch_tk:
out << setw(5) << cnt << " " << setw(12) << "Keyword" << " "
<< tknstr << endl;
break;
case id_tk:
out << setw(5) << cnt << " " << setw(12) << tokentypename[tkntyp] << " "
<< tknstr << endl;
break;
case intlit_tk:
out << setw(5) << cnt << " " << setw(12) << tokentypename[tkntyp] << " "
<< tknval << endl;
break;
case chrlit_tk:
out << setw(5) << cnt << " " << setw(12) << tokentypename[tkntyp] << " '"
<< tknchar << "'" << endl;
break;
case strlit_tk:
out << setw(5) << cnt << " " << setw(12) << tokentypename[tkntyp] << " \""
<< tknstr << "\"" << endl;
break;
case semicln_tk: case comma_tk: case assign_tk: case lbrkt_tk:
case rbrkt_tk: case lprt_tk: case rprt_tk: case lbrc_tk:
case rbrc_tk: case cln_tk:
out << setw(5) << cnt << " " << setw(12) << tokentypename[tkntyp] << " "
<< signtype[tkntyp] << endl;
break;
case addsub_tk:
out << setw(5) << cnt << " " << setw(12) << tokentypename[tkntyp] << " "
<< (tknval == addop ? '+':'-') << endl;
break;
case multdiv_tk:
out << setw(5) << cnt << " " << setw(12) << tokentypename[tkntyp] << " "
<< (tknval == addop ? '*' : '/') << endl;
break;
case rltop_tk:
out << setw(5) << cnt << " " << setw(12) << tokentypename[tkntyp] << " "
<< rltoptypesign[tknval] << endl;
break;
case eof_tk:
out << setw(5) << cnt << " " << setw(12) << "EOF" << endl;
break;
}
}
}