-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanning.h
341 lines (279 loc) · 5.57 KB
/
scanning.h
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
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
#include <stdlib.h>
using namespace std;
typedef enum {
NONE=0, IDENT, NUMBER,
BEGIN, CALL, CONST, DO, ELSE, END, FOR, IF, ODD,
PROCEDURE, PROGRAM, THEN, TO, VAR, WHILE,
PLUS, MINUS, TIMES, SLASH, EQU, NEQ, LSS, LEQ, GRT, GEQ, LPARENT, RPARENT, LBRACK, RBRACK, PERIOD, COMMA, SEMICOLON, ASSIGN, PERCENT,COMMENT
} TokenType;
string key_words[] = {"BEGIN", "CALL", "CONST", "DO", "ELSE", "END", "FOR", "IF", "ODD",
"PROCEDURE", "PROGRAM", "THEN", "TO", "VAR", "WHILE"};
string type_words[] = {"NONE", "IDENT", "NUMBER",
"BEGIN", "CALL", "CONST", "DO", "ELSE", "END", "FOR", "IF", "ODD",
"PROCEDURE", "PROGRAM", "THEN", "TO", "VAR", "WHILE",
"+", "-", "*", "/", "==", "<>", "<", "<=", ">", ">=", "(",
")", "[", "]", ".", ",", ";", ":=", "%","COMMENT"};
int key_words_len = 15;
int line=1,column=0;
char ch = ' ';
int max_num = 9;
int max_ident = 9;
vector<TokenType> tokens; //chua danh sach cac token cua chuong trinh
vector<string> idents; // danh sach cac ten bien
vector<int> numbers; // sanh sach so
vector<int> columns;
vector<int> lines;
char getCh(ifstream& sfile);
int check_letter(char ch);
int check_digit(char ch);
TokenType test_ident(string s);
TokenType test_number(string s);
TokenType getToken(ifstream& sfile);
void show_token();
void get_tokens(char* file_name);
char getCh(ifstream& sfile){
char c;
if(!sfile.eof()){
sfile>>c;
column++;
// if(c == '\n'){
// cout<<" "<<endl;
// }
/*
while(c == '\n' && !sfile.eof()){
line++;
column=0;
sfile>>c;
}
*/
}
else{
return 0;
}
char upper_c =toupper(c);
// cout<<upper_c<<" ";
return upper_c;
}
int check_letter(char ch){
// cout<<"letter: "<<ch<<endl;
return isalpha(ch);
}
int check_digit(char ch){
return isdigit(ch);
}
TokenType test_ident(string s){
int i=0;
for (i = 0;i<key_words_len;i++){
if(s.compare(key_words[i]) == 0){
TokenType type = static_cast<TokenType>(i+3);;
return type;
}
}
// kiem tra do dai xau
string s_cut;
if(s.length() > max_ident){
for(i=0;i<max_ident;i++){ // cat lay 9 ky tu
s_cut+=s[i];
}
idents.push_back(s_cut);
}
else{
idents.push_back(s);
}
return IDENT;
}
TokenType test_number(string s){
int value = 0;
int i= 0;
int L = s.length();
if(L>max_num){
cout<<"\n Loi: So qua lon"<<" dong: "<<line<<" cot: "<<column<<endl;
exit(EXIT_SUCCESS);
return NONE;
}
if(check_digit(s[i])){
value = value*10 + s[i]-48;
i=i+1;
while(check_digit(s[i])){
value = value*10 + s[i]-48;
i = i + 1;
}
if(i==L) {
numbers.push_back(value);
return NUMBER;
}
}
return NONE;
}
TokenType getToken(ifstream& sfile){
get_token:
while(ch == ' ' || ch==9 || ch =='\n'){
if(ch=='\n'){
line++;
column=0;
}
ch = getCh(sfile);
}
string s;
if(check_letter(ch)){
s+=ch;
ch = getCh(sfile);
// cout<<"ident"<<ch<<endl;
while(check_letter(ch) || check_digit(ch)){
s+=ch;
ch = getCh(sfile);
};
return test_ident(s);
}
else if(check_digit(ch)){
s+=ch;
ch = getCh(sfile);
while(check_digit(ch)){
s+=ch;
ch = getCh(sfile);
};
return test_number(s);
}
else if(ch == '<'){
ch = getCh(sfile);
if(ch == '>'){
ch = getCh(sfile);
return NEQ;
}
else if(ch == '='){
ch = getCh(sfile);
return LEQ;
}
else{
return LSS;
}
}
else if(ch == '>'){
ch = getCh(sfile);
if(ch == '='){
ch = getCh(sfile);
return GEQ;
}
else{
return GRT;
}
}
else if(ch == ':'){
ch = getCh(sfile);
if(ch == '='){
ch = getCh(sfile);
return ASSIGN;
}
}
else if(ch == '+'){
ch = getCh(sfile);
return PLUS;
}
else if(ch == '-'){
ch = getCh(sfile);
return MINUS;
}
else if(ch == '*'){
ch = getCh(sfile);
return TIMES;
}
else if(ch == '/'){
ch = getCh(sfile);
return SLASH;
}
else if(ch == '%'){
ch = getCh(sfile);
return PERCENT;
}
else if(ch == '='){
ch = getCh(sfile);
return EQU;
}
else if(ch == '('){
ch = getCh(sfile);
if(ch == '*'){
comment:
ch = getCh(sfile);
while(ch != '*'){
ch = getCh(sfile);
}
ch = getCh(sfile);
if(ch == ')'){
ch = getCh(sfile);
// cout<<"Het comment"<<endl;
goto get_token;
}
else{
goto comment;
}
}
return LPARENT;
}
else if(ch == ')'){
ch = getCh(sfile);
return RPARENT;
}
else if(ch == '['){
ch = getCh(sfile);
return LBRACK;
}
else if(ch == ']'){
ch = getCh(sfile);
return RBRACK;
}
else if(ch == '.'){
ch = getCh(sfile);
return PERIOD;
}
else if(ch == ','){
ch = getCh(sfile);
return COMMA;
}
else if(ch == ';'){
ch = getCh(sfile);
// cout<<"SEMICOLON"<<endl;
return SEMICOLON;
}
else{
// cout<<"het "<<(int)ch<<endl;
if(!sfile.eof()){
cout<<"Xuat hien ky tu la hoac file bi loi"<<" dong: "<<line<<" cot: "<<column<<endl;
exit(EXIT_SUCCESS);
}
return NONE;
}
}
void show_token(){
int n_number = 0;
int n_ident = 0;
int l = tokens.size();
for(int i=0;i<l;i++){
if(tokens[i] == IDENT){
cout<<type_words[(int) tokens[i]]<<" ( "<<idents[n_ident]<<" )"<<endl;
n_ident++;
}
else if(tokens[i] == NUMBER){
cout<<type_words[(int) tokens[i]]<<" ( "<<numbers[n_number]<<" )"<<endl;
n_number++;
}
else{
cout<<type_words[(int) tokens[i]]<<endl;
}
}
}
void get_tokens(char* file_name){
ifstream sfile;
sfile.open(file_name,ios_base::in);
sfile.unsetf(ios_base::skipws);
TokenType token;
do{
token = getToken(sfile);
tokens.push_back(token);
lines.push_back(line);
columns.push_back(column);
}while(token!=NONE);
}