-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokenizing.cpp
359 lines (320 loc) · 6.02 KB
/
tokenizing.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
#include <iostream>
#include <fstream>
#include <string.h>
#include <exception>
#include <stdlib.h>
#include <string>
#include <vector>
#include "interpretlib.h"
//vector<string> TS;
int table_identificators::put(const char *buf){
for(int j=1; j<top; j++)
if(!strcmp(buf,p[j].get_name()))
return j;
p[top].set_name(buf);
top++;
return top-1;
}
/* SETTING UP STATIC TABLES */
/* TABLE OF LANGUAGE KEY WORDS */
const char* Scanner::TW[]={
"", //0
"and", //1
"break", //2
"case", //3
"catch", //4
"const", //5
"continue", //6
"debugger", //7
"default", //8
"delete", //9
"do", //10
"else", //11
"finally", //12
"for", //13
"function", //14
"if", //15
"in", //16
"instanceof", //17
"let", //18
"new", //19
"or", //20
"return", //21
"switch", //22
"throw", //23
"try", //24
"typeof", //25
"var", //26
"void", //27
"while", //28
"with", //29
"true", //30
"false",//31
"bool",//32
"alert",//33
"read",//34
NULL
};
/* TABLE OF SPECIAL SIGNS */
const char* Scanner::TD[]={
"", //0
";", //1
",", //2
"{", //3
"}", //4
":", //5
"(", //6
")", //7
"=", //8
"<", //9
">", //10
"+", //11
"-", //12
"*", //13
"/", //14
"<=", //15
"!=", //16
">=", //17
"==", //18
"===",//19
//"\"", //20
//"'", //21
"@", //22
NULL
};
type_of_lexem
Scanner::words [] = {LEX_NULL, LEX_AND, LEX_BREAK,
LEX_CASE, LEX_CATCH, LEX_CONST, LEX_CONTINUE,
LEX_DEBUGGER, LEX_DEFAULT, LEX_DELETE, LEX_DO, LEX_ELSE,
LEX_FINALLY, LEX_FOR, LEX_FUNCTION, LEX_IF, LEX_IN,
LEX_INSTANCEOF, LEX_LET, LEX_NEW, LEX_OR, LEX_RETURN,
LEX_SWITCH, LEX_THROW, LEX_TRY, LEX_TYPEOF, LEX_VAR,
LEX_VOID, LEX_WHILE, LEX_WITH, LEX_TRUE, LEX_FALSE,LEX_BOOL,LEX_ALERT,LEX_READ,LEX_NULL};
type_of_lexem
Scanner::dlms [] = {LEX_NULL, LEX_SEMICOLON, LEX_COMMA, //LEX_ASSIGN,
LEX_LBRACE, LEX_RBRACE, LEX_COLON,
LEX_LPAREN, LEX_RPAREN, LEX_EQ,LEX_LSS, LEX_GTR,
LEX_PLUS, LEX_MINUS, LEX_TIMES, LEX_SLASH, LEX_LEQ,
LEX_NEQ, LEX_GEQ, LEX_DEQ, LEX_TEQ, LEX_STRING, LEX_FIN, LEX_NULL};
vector<string> Scanner::TS;
/* HERE LEX ANALYSATOR BASED ON GRAPH */
Lexem Scanner::get_lex(int *str_num){
int d, j;
bool flg_slash = false;
current_state = H;
do
{
switch ( current_state ){
case H:
if ( c ==' ' || c=='\r' || c =='\t' ) //just skip all spaces
get_char ();
else if(c=='\n'){
(*str_num)++;
get_char();
}
/* ALPHA */ else if (isalpha(c))
{
clear (); //clear the buf
add (); // add char to buf
get_char ();
current_state = IDENT; //alpha | digit
}
/* DIGIT */ else if ( isdigit(c))
{
d = c - '0';
get_char ();
current_state = NUMB;
}
/* HERE NEED TO INSERT string processing "..." and '.'*/
else if (c== '\"') //|| c== '\'')
{
/* After spaces only works*/
clear();
get_char();
current_state = STRING; //need to add new state
}
/*else if(c== '\''){
get_char();
current_state = CHAR;
}*/
/* COM1 */ else if ( c== '/')
{
clear ();
add ();
get_char ();
if (c== '/')
current_state = COM1;
/* COM2 */ else if (c== '*')
current_state = COM2;
else {
flg_slash = true;
current_state = DELIM;
}
}
/* = < > */ else if ( c== '=')
{
clear ();
add ();
get_char ();
current_state = ALE;
}
else if(c== '<' || c== '>'){
clear ();
add ();
get_char ();
current_state = COMP;
}
/* FIN */ else if ( c == '@' ){
return Lexem (LEX_FIN,0);
}
/* ! */ else if ( c == '!' )
{
clear ();
add ();
get_char ();
current_state = NEQ;
}
else{
clear ();
add ();
current_state = DELIM;
}
break;
/* END OF THE FIRST GRAPGH STAGE */
case IDENT:
if(c == '\"' || c=='\''){
clear();
current_state = ERROR;
}
if ( isalpha(c) || isdigit(c) ) // while digit | alpha
{
add ();
get_char ();
}
else if ( (j = look (buf, TW)) ) // buf == keyword
return Lexem ( words[j],j);
else
{
j = TID.put(buf); // if buf == name[j] return j, else push buf to names
return Lexem (LEX_ID,j);
}
break;
case NUMB:
if ( isdigit(c) ) // while digit
{
d = d * 10 + (c - '0');
get_char();
}
else
return Lexem (LEX_NUM,d );
break;
case COM1: // ...
if ( c == '\n') // until end of line
{
(*str_num)++;
get_char ();
current_state = H;
}
else if (c == '@') // ERROR
current_state=ERROR;
else
get_char ();
break;
case COM2: /* ... */
if (c == '*')
{
get_char ();
if (c == '/')
{
get_char();
current_state = H;
}
}
else if (c == '@') //ERROR
current_state = ERROR;
else get_char();
break;
case ALE:
if ( c == '=' )
{
add ();
get_char ();
if(c == '='){
add();
get_char();
j = look ( buf, TD );
return Lexem (dlms[j],j );
}
else{
j = look ( buf, TD );
return Lexem (dlms[j], j );
}
}
else
{
j = look (buf, TD); // buf == sign
return Lexem (dlms[j],j );
}
break;
case COMP:{
if ( c == '=' )
{
add ();
get_char ();
j = look (buf, TD); // buf == sign
return Lexem (dlms[j], j );
}
else
{
j = look (buf, TD); // buf == sign
return Lexem (dlms[j], j );
}
break;
}
case NEQ:
if ( c == '=' )
{
add ();
get_char ();
j = look ( buf, TD );
return Lexem (LEX_NEQ,j );
}
else //ERROR: not !=
current_state =ERROR;
break;
case DELIM:
if ( (j = look(buf, TD)) )
{
if ( !flg_slash ) get_char ();
else flg_slash = false;
return Lexem (dlms[j], j );
}
else // ERROR
current_state = ERROR;
break;
/*STRING*/
case STRING:{
if(c=='\"'){
string str(buf);
add();
get_char();
TS.push_back(str);
return Lexem(LEX_STRING,TS.size());
}
else if(c=='@'){
clear();
current_state = ERROR;
}
else{
add();
get_char();
}
break;
}
case ERROR:{
throw c;
break;
}
} // end of switch
} while ( true );
}