-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner.c
265 lines (243 loc) · 6.54 KB
/
scanner.c
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
// scanner.c
#include <stdio.h>
#include <ctype.h>
#include "str.h"
#include "scanner.h"
FILE *source;
void setSourceFile(FILE *file)
{
source = file;
}
int is_keyword(string *str, int fce)
{
if (strCmpConstStr(str, "begin") == 0)
return BEGIN;
if (strCmpConstStr(str, "boolean") == 0)
return BOOLEAN;
if (strCmpConstStr(str, "copy") == 0)
return COPY;
if (strCmpConstStr(str, "do") == 0)
return DO;
if (strCmpConstStr(str, "else") == 0)
return ELSE;
if (strCmpConstStr(str, "end") == 0)
return END;
if (strCmpConstStr(str, "false") == 0)
return FALSE;
if (strCmpConstStr(str, "find") == 0)
return FIND;
if (strCmpConstStr(str, "forward") == 0)
return FORWARD;
if (strCmpConstStr(str, "function") == 0)
return FUNCTION;
if (strCmpConstStr(str, "if") == 0)
return IF;
if (strCmpConstStr(str, "integer") == 0)
return INTEGER;
if (strCmpConstStr(str, "length") == 0)
return LENGTH;
if (strCmpConstStr(str, "readln") == 0)
return READLN;
if (strCmpConstStr(str, "real") == 0)
return REAL;
if (strCmpConstStr(str, "sort") == 0)
return SORT;
if (strCmpConstStr(str, "string") == 0)
return STRING;
if (strCmpConstStr(str, "then") == 0)
return THEN;
if (strCmpConstStr(str, "true") == 0)
return TRUE;
if (strCmpConstStr(str, "var") == 0)
return VAR;
if (strCmpConstStr(str, "while") == 0)
return WHILE;
if (strCmpConstStr(str, "write") == 0)
return WRITE;
if (fce)
return IDENTIFIKATORF;
return IDENTIFIKATOR;
}
int getNextToken(string *attr)
{
int c, state = 0;
string pomEsc;
strClear(attr);
strClear(&pomEsc);
while (1)
{
c = getc(source);
switch (state)
{
case 0: // pocatecni stav
{
if (isspace(c))
state = 0; // ignorovani bilych mist
else if (c == '{')
state = 1; // komentar
else if (isalpha(c))
{
strAddChar(attr, c);
state = 2; // zacatek identifikatoru nebo klic. slova
}
else if (isdigit(c))
{
strAddChar(attr, c);
state = 3; // cislo int ci double
}
else if (c == ':')
{
state = 4; // samostatna dvojtecka nebo operator prirazeni
}
else if (c == '<')
state = 5; // mensi, mensi nebo rovno
else if (c == '>')
state = 6; // vetsi, vetsi nebo rovno
else if (c == '\'')
state = 8; // zacatek retezce
else if (c == '+')
return PLUS;
else if (c == '-')
return MINUS;
else if (c == '*')
return KRAT;
else if (c == '/')
return DELENO;
else if (c == '=')
return ROVNO;
else if (c == ';')
return STREDNIK;
else if (c == ',')
return CARKA;
else if (c == '.')
return TECKA;
else if (c == '(')
return LEVAZAVORKA;
else if (c == ')')
return PRAVAZAVORKA;
else if (c == EOF)
return KONECSOUBORU;
else
return LEXIKALNICHYBA;
break;
}
case 1: // komentar
{
if (c == '}')
state = 0;
else if (c == EOF)
return LEXIKALNICHYBA;
break;
}
case 2: // alfanumericky znak (id nebo klic. slovo)
{
if (isalnum(c) || c == '_')
strAddChar(attr, c);
else
{
ungetc(c, source);
return is_keyword(attr, c == '(');
}
break;
}
case 3: // libovolne cislo
{
if (isdigit(c))
strAddChar(attr, c);
else if (c == '.')
{
strAddChar(attr, c);
state = 7;
}
else
{
ungetc(c, source);
return KONST_INTEGER;
}
break;
}
case 4: // prirazeni
{
if (c == '=')
return PRIRAZENI;
else
{
ungetc(c, source);
return DVOJTECKA;
}
}
case 5: // mensi, mensi nebo rovno, nerovno
{
if (c == '=')
return MENSIROVNO;
else if (c == '>')
return NEROVNO;
else
{
ungetc(c, source);
return MENSI;
}
}
case 6: // vetsi, vetsi nebo rovno
{
if (c == '=')
return VETSIROVNO;
else
{
ungetc(c, source);
return VETSI;
}
}
case 7: // zakladni realne cislo
{
if (isdigit(c))
strAddChar(attr, c);
else
return KONST_REAL;
break;
}
case 8: // retezec
{
if (c == '\'')
state = 9;
else if (c == EOF || c == '\n')
return LEXIKALNICHYBA;
else
strAddChar(attr, c);
break;
}
case 9: // apostrof v retezci, escape sekvence nebo konec retezce
{
if (c == '\'')
{
strAddChar(attr, c);
state = 8; // apostrof v retezci
}
else if (c == '#')
state = 10; // escape sekvence
else
return KONST_STRING; // konec retezce
break;
}
case 10: // escape sekvence
{
if (isdigit(c))
strAddChar(&pomEsc, c);
else if (c == '\'')
{
if (strlen(pomEsc.str) == 0)
return LEXIKALNICHYBA; // Zadne cislo escape sekvence
int escape = atoi(pomEsc.str);
if (esc < 1 || esc > 255)
return LEXIKALNICHYBA; // Escape sekvence neni znak
strAddChar(attr, escape);
strClear(&pomEsc);
state = 8; // pokracovani retezce
}
else
return LEXIKALNICHYBA;
break;
}
}
}
}