-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexan.cpp
executable file
·436 lines (410 loc) · 10.7 KB
/
lexan.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
#include "lexan.h"
LexElement Lexan::nextToken() {
if (!init)
return LexElement(ERROR);
if ((int)back.size()>0){
LexElement result = back.front();
back.pop();
return result;
}
int state = 0, token;
/* strPos = pocet nactenych znaku identifikatoru */
int i, strPos = 0;
/* Velikost a znamenko exponentu */
int exp = 0, expSign;
/* prave zpracovavany desetinny rad */
double dmsPos = 0.1;
int intValue;
double floatValue;
char charValue[100];
while ( 1 ){
token = fgetc (source);
switch (state){
case 0:
if ( token == '\n')
line++;
if ( token == ' ' || token == '\n' || token == '\t' || token == '\r' || token == 13 )
break;
if ( token == '/' ){
state = 1;
break;
}
if ( token == '0' ){
intValue = 0;
floatValue = 0.0;
state = 13;
break;
}
if ( token == '.' ){
floatValue = 0.0;
dmsPos = 0.1;
state = 12;
break;
}
if ( isdigit(token)){
state = 7;
intValue = token - '0';
floatValue = token - '0';
break;
}
if ( isalpha ( token ) || token == '_' ){
/* pocatek retezce identifikatoru*/
charValue[0] = token;
strPos = 1;
state = 6;
break;
}
/* zacatek retezce */
if (token == '\"'){
state = 17;
strPos = 0;
break;
}
if (token == '+')
return LexElement(PLUS);
if (token == '-')
return LexElement(MINUS);
if (token == '*')
return LexElement(MUL);
if (token == '(')
return LexElement(OPEN_ROUND_BRACKET);
if (token == ')')
return LexElement(CLOSE_ROUND_BRACKET);
if (token == '[')
return LexElement(OPEN_BRACKET);
if (token == ']')
return LexElement(CLOSE_BRACKET);
if (token == '{')
return LexElement(OPEN_BRACE);
if (token == '}')
return LexElement(CLOSE_BRACE);
if (token == ',')
return LexElement(COMMA);
if (token == ';')
return LexElement(SEMICOLON);
if (token == '='){
state = 21;
break;
}
if (token == '!'){
state = 22;
break;
}
if (token == '<'){
state = 23;
break;
}
if (token == '>'){
state = 24;
break;
}
if (token == '|'){
state = 25;
break;
}
if (token == '&'){
state = 26;
break;
}
if ( token == EOF )
return LexElement(END_FILE);
/* nepoznali jsme zacatek zadneho lex. elementu */
return LexElement(ERROR);
/* deleni, radkovy nebo blokovy komentar */
case 1:
if ( token == '*' ){
state = 3;
break;
}
if ( token == '/' )
{
state = 4;
break;
}
/* Lexan jiz nemuze pokracovat zadnym prechodem. Protoze jsme v 'koncovem' */
/* stavu, vratim ezpet nacteny znak a navratova hodnota bude DIV */
ungetc(token,source);
return LexElement(DIV);
/* blokovy komentar */
case 3:
if ( token == '*' ){
state = 5;
break;
}
/* pokud by uprostred komentare nastal EOF */
/* a neosetrili bychom jej, Lexan by se zacyklil */
if ( token == EOF )
return LexElement(ERROR);
if ( token == '\n')
line++;
/* zustavame ve stavu 3 */
break;
/* blokovy komentar */
case 5:
if ( token == '*' )
break;
if ( token == '/' ){
state = 0;
break;
}
if ( token == '\n')
line++;
state = 3;
break;
/* radkovy komentar */
case 4:
if ( token == '\n' ){
line++;
state = 0;
break;
}
if ( token == EOF )
/* kontrola EOF stejne jako ve stavu 3 */
return LexElement(ERROR);
/* zustavame ve stavu 4 */
break;
/* identifikator */
case 6:
if ( isalpha (token) || isdigit (token) || token == '_' ){
/* HINT: podminka zajistuje, ze nevyjedeme mimo pole */
/* navic si rezervujeme 1 pozici pro ukonceni retezce */
/* znakem '\0' */
if ( strPos < (int)sizeof (charValue) - 1 )
charValue [strPos ++] = token;
else
cout << "Pozor max delka identifikatoru je 100 (radek " << line << ")" << endl;
break;
}
/* konec identifikatoru */
ungetc ( token, source );
/* semanticka akce v tomto stavu: doplneni 0 na konec retezce */
/* test, zda retezec neni identifikator */
charValue[strPos] = 0;
for ( i=0; i < nrKeyWords; i ++ )
if ( !strcmp ( charValue, keywords[i].word ) )
return LexElement(keywords[i].keyWord);
/* nenalezli jsme mezi klicovymi slovy -> je to identifikator */
return LexElement(IDENTIFICATOR,charValue);
/* cele nebo desetinne cislo */
case 7:
if ( isdigit(token)){
intValue = intValue * 10 + token - '0';
floatValue = floatValue * 10.0 + token - '0';
break;
}
if ( token == '.' ){
dmsPos = 0.1;
state = 8;
break;
}
if ( token == 'e' || token == 'E' ){
exp = 0;
expSign = 1;
state = 9;
break;
}
/* nalezen integer */
ungetc(token, source);
return LexElement(INTEGER,intValue);
/* desetinne cislo */
case 8:
if ( isdigit(token)){
floatValue += dmsPos * ( token - '0');
dmsPos /= 10.0;
break;
}
if ( token == 'e' || token == 'E'){
exp = 0;
expSign = 1;
state = 9;
break;
}
/* desetinne cislo */
ungetc ( token, source );
return LexElement(DOUBLE,floatValue);
/* exponent pro des. cislo */
case 9:
if ( token == '+' || token == '-' ){
/* znamenko exponentu si zapanmatujeme */
expSign = (token == '-') ? -1 : 1;
state = 10;
break;
}
if ( isdigit(token)){
exp = token - 0x30;
state = 11;
break;
}
/* cislo nemuze koncit znakem 'e' */
return LexElement(ERROR);
/* exponent po znamenku */
case 10:
if ( isdigit(token )){
/* je vyzadovana alespon 1 cifra exponentu */
exp = token - 0x30;
state = 11;
break;
}
return LexElement(ERROR);
/* exponent */
case 11:
if ( isdigit(token)){
/* dalsi cifry exponentu */
exp = 10 * exp + token - 0x30;
break;
}
/* zkompletujeme desetinne cislo z Lex_Float (mantisa), exp a expSign */
ungetc ( token, source );
floatValue *= pow ( 10.0, exp * expSign );
return LexElement(DOUBLE,floatValue);
/* desetinne cislo zacinajici .*/
case 12:
/* alespon 1 cirfa za desetinnou teckou */
if ( isdigit(token)){
state = 8;
floatValue = 0.1 * ( token - '0' );
/* pristi zpracovavany rad jsou setiny */
dmsPos = 0.01;
break;
}
ungetc ( token, source );
return LexElement(DOT);
/* nula, desetinne, oktanove nebo hexa cislo */
case 13:
if ( token == 'x' ){
/* nasleduje hex. cislo */
state = 14;
break;
}
if ( isdigit(token) && token != '8' && token != '9'){
/* oktalove cifry */
intValue = token - '0';
state = 16;
break;
}
ungetc ( token, source );
/* nalezenno cele cislo - nula */
return LexElement(INTEGER,intValue);
/* hexa cislo */
case 14:
if ( isxdigit(token)){
/* hex. cifry prevedeme na velka pismena */
token = toupper(token);
/* musime rozdelit cifry a pismena, 'A' odpovida 10 */
intValue = ( token >= 'A' ) ? token - 'A' + 10 : token - '0';
state = 15;
break;
}
/* alespone 1 hex. cifra */
return LexElement(ERROR);
/* hexa cislo */
case 15:
if (isxdigit(token)){
/* podobne jako ve stavu 14 */
token = toupper(token);
/* nasobeni 16 je ekvivalentni posunu vlevo o 4 mista */
/* pozor: priorita operatoru << je nizsi nez priorita + ! */
intValue = ( intValue << 4 ) + (( token >= 'A' ) ? token - 'A' + 10 : token - '0');
break;
}
ungetc ( token, source );
/* nalezli jsme cele cislo. Vyssi vrstve */
/* (syntaktickemu analyzatoru) je lhostejne, zda bylo zadano jako */
/* desitkove, osmickove nebo sestnactkove. Dulezite je, aby byla */
/* spravne vypoctena hodnota atributu Lex_Int */
return LexElement(INTEGER,intValue);
/* oktanove cislo */
case 16:
if ( isdigit(token) && token != '8' && token != '9'){
/* nasobeni 8 je exvivalentni posunu 0o 3 vlevo */
/* pozor na prioritu + */
intValue = ( intValue << 3 ) + ( token - '0' );
break;
}
ungetc (token, source);
return LexElement(INTEGER,intValue);
/* stringy */
case 17:
if (token == '\\'){
state = 18;
break;
}
if (token == '\"'){
charValue[strPos] = 0;
return LexElement(STRING,charValue);
}
if ( token == EOF || token == '\n')
return LexElement(ERROR);
if ( strPos < (int)sizeof (charValue) - 1 )
charValue [strPos ++] = token;
else
cout << "Pozor max delka retezce je 100 (radek " << line << ")" << endl;
break;
/* escape sekvence */
case 18:
if (token == 'n'){
charValue [strPos ++] = '\n';
state = 17;
break;
}
if (token == 'r'){
charValue [strPos ++] = '\r';
state = 17;
break;
}
if (token == 't'){
charValue [strPos ++] = '\t';
state = 17;
break;
}
if (token == 'a'){
charValue [strPos ++] = '\a';
state = 17;
break;
}
if (token == '\\'){
charValue [strPos ++] = '\\';
state = 17;
break;
}
if (token == '\"'){
charValue [strPos ++] = '\"';
state = 17;
break;
}
return LexElement(ERROR);
case 21:
if (token == '=')
return LexElement(EQUAL);
ungetc ( token, source );
return LexElement(ASIGNMENT);
case 22:
if (token == '=')
return LexElement(NOT_EQUAL);
ungetc ( token, source );
return LexElement(NOT);
case 23:
if (token == '=')
return LexElement(LESSER_OR_EQUAL);
ungetc ( token, source );
return LexElement(LESSER);
case 24:
if (token == '=')
return LexElement(GREATER_OR_EQUAL);
ungetc ( token, source );
return LexElement(GREATER);
case 25:
if (token == '|')
return LexElement(OR);
return LexElement(ERROR);
case 26:
if (token == '&')
return LexElement(AND);
return LexElement(ERROR);
/* neznamy stav */
default:
return LexElement(ERROR);
break;
}
}
}