-
Notifications
You must be signed in to change notification settings - Fork 0
/
LexicalScanner.cpp
345 lines (284 loc) · 7.19 KB
/
LexicalScanner.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
/** This process will take input file as argument and output file with name output.txt will be generated.
% is also included as input symbol. Alos, printf might seem that it is error but in reality it qualifies identifier type.
Also this code will only do only Lexical Analysis, syntax and symentatic analysis are out of ots scope. Tab and space are treates as word seperator **/
#include<iostream>
#include<string>
#include<fstream>
#include<vector>
#include<regex>
using namespace std;
const string lineSeps=" \t";
const string keywords[] = {"void", "int", "float", "if", "else", "for", "return", "then", "main", "class", "and", "not", "or", "get", "put"};
const string specialSymbol[] = {"==", "+", "(", "<>", ")", "<", "=", "{", ">", "/", "}", "<=", "=", "[", ">=", "]", ";", ",", "//", "/*", "*/", "%", "\""};
/** regular expressionis for lex analyzer **/
regex integerExp("[0-9]+");
regex floatExp("[0-9]+\\.[0-9]+");
regex fraction("\\.[0-9]+");
regex idExp("[a-zA-Z]+[0-9|a-zA-Z|_]*");
regex comment("/\\*(.*?)\\*/");
regex alphaNum("[0-9|a-zA-Z|_]*");
/** vector to store result **/
vector<string> identifier_vec;
vector<string> alphaNumeric_vec;
vector<string> number_vec;
vector<string> fraction_vec;
vector<string> error_vec;
vector<string> reserved_vec;
/** check if string is keyword **/
bool isKeyword(string value){
unsigned int i=0;
for( i=0; i<sizeof(keywords)/sizeof(keywords[0]); i++){
if(keywords[i]==value){
return true;
}
}
return false;
}
/** check if sprcial symbol **/
bool isSpecialSymbol(string value){
unsigned int i=0;
for(i=0; i<sizeof(specialSymbol)/sizeof(specialSymbol[0]); i++){
if(specialSymbol[i]==value){
return true;
}
}
return false;
}
/** check if fraction **/
bool isFraction(string value){
if(regex_match(value, fraction))
return true;
else
return false;
}
/** check if alphanumeric ***/
bool isAlphanum(string value){
if(regex_match(value, alphaNum))
return true;
else
return false;
}
/** check if positive integer **/
bool isIntNum(string value){
if(regex_match(value, integerExp))
return true;
else
return false;
}
/** check if float **/
bool isFloatNum(string value){
if(regex_match(value, floatExp))
return true;
else
return false;
}
/** check if identifier **/
bool isID(string value){
if(regex_match(value, idExp))
return true;
else
return false;
}
/** check if special symbol are present **/
bool hasSpecialSymbol(string value, string& symbolFound){
unsigned int i=0;
for(i=0; i<sizeof(specialSymbol)/sizeof(specialSymbol[0]); i++){
if(value.find(specialSymbol[i]) != string::npos){
symbolFound=specialSymbol[i];
return true;
}
}
return false;
}
/** remove comments **/
void removeComments(string& input){
smatch matchedStr;
string temp=input;
if(regex_search(temp, matchedStr, comment)) {
reserved_vec.push_back("/*");
reserved_vec.push_back("*/");
}
input = regex_replace(input, comment, " ");
}
/** analyzer **/
bool analyzeLine(string& input, string tokenizer){
vector<string> tokenOfLine;
string specialSymbol;
bool result=true;
size_t prev = 0, pos;
removeComments(input);
/** break line in tokens **/
while ((pos = input.find_first_of(tokenizer, prev)) != string::npos)
{
if (pos > prev)
tokenOfLine.push_back(input.substr(prev, pos-prev));
prev = pos+1;
}
if (prev < input.length())
tokenOfLine.push_back(input.substr(prev, string::npos));
/*8 for each token check what criteria is qualified and store in vector **/
for(unsigned int i=0; i<tokenOfLine.size(); ++i)
{
/** if token has special symbol along with other characters then create token on basis of this symbol **/
if(hasSpecialSymbol(tokenOfLine[i], specialSymbol)){
result=analyzeLine(tokenOfLine[i], specialSymbol);
if(specialSymbol!="\"")
reserved_vec.push_back(specialSymbol);
}else if(isKeyword(tokenOfLine[i])){
reserved_vec.push_back(tokenOfLine[i]);
}
else if(isSpecialSymbol(tokenOfLine[i])){
reserved_vec.push_back(tokenOfLine[i]);
}
else if(isIntNum(tokenOfLine[i])){
number_vec.push_back(tokenOfLine[i]);
}
else if(isFloatNum(tokenOfLine[i])){
number_vec.push_back(tokenOfLine[i]);
}
else if(isID(tokenOfLine[i])){
identifier_vec.push_back(tokenOfLine[i]);
}
else if(isAlphanum(tokenOfLine[i])){
alphaNumeric_vec.push_back(tokenOfLine[i]);
}else if(isFraction(tokenOfLine[i])){
fraction_vec.push_back(tokenOfLine[i]);
}
else{
error_vec.push_back(tokenOfLine[i]);
}
}
return result;
}
/** check size of each vector and display if any value is present **/
void printResult(ofstream& outFile){
int count=0;
if(reserved_vec.size()>0){
outFile<<"Operators, punctuations, and reserved words:"<<endl;
cout<<"Operators, punctuations, and reserved words:"<<endl;
for(auto token:reserved_vec){
outFile<<token<<" ";
cout<<token<<" ";
count++;
if(count%4==0){
outFile<<endl;
cout<<endl;
count=0;
}
}
outFile<<endl<<endl<<endl;
cout<<endl<<endl<<endl;
}
if(identifier_vec.size()){
outFile<<"id:"<<endl;
cout<<"id:"<<endl;
count=0;
for(auto token:identifier_vec){
outFile<<token<<" ";
cout<<token<<" ";
count++;
if(count%4==0){
outFile<<endl;
cout<<endl;
count=0;
}
}
outFile<<endl<<endl;
cout<<endl<<endl;
}
if(alphaNumeric_vec.size()>0){
outFile<<"alphanum:"<<endl;
cout<<"alphanum:"<<endl;
count=0;
for(auto token:alphaNumeric_vec){
outFile<<token<<" ";
cout<<token<<" ";
count++;
if(count%4==0){
outFile<<endl;
cout<<endl;
count=0;
}
}
outFile<<endl<<endl;
cout<<endl<<endl;
}
if(number_vec.size()>0){
outFile<<"number:"<<endl;
cout<<"number:"<<endl;
count=0;
for(auto token:number_vec){
outFile<<token<<" ";
cout<<token<<" ";
count++;
if(count%4==0){
outFile<<endl;
cout<<endl;
count=0;
}
}
outFile<<endl<<endl;
cout<<endl<<endl;
}
if(fraction_vec.size()>0){
outFile<<"fraction:"<<endl;
cout<<"fraction:"<<endl;
count=0;
for(auto token:fraction_vec){
outFile<<token<<" ";
cout<<token<<" ";
count++;
if(count%4==0){
outFile<<endl;
cout<<endl;
count=0;
}
}
outFile<<endl<<endl;
cout<<endl<<endl;
}
if(error_vec.size()>0){
outFile<<"error token:"<<endl;
cout<<"error token:"<<endl;
count=0;
for(auto token:error_vec){
outFile<<token<<" ";
cout<<token<<" ";
count++;
if(count%4==0){
outFile<<endl;
cout<<endl;
count=0;
}
}
outFile<<endl<<endl;
cout<<endl<<endl;
}
}
int main(int argc, char *argv[]){
string inputLine;
if(argc<2){
cout<<"Fatal error: Input file not provided as argument"<<endl;
return 1;
}
ifstream inputFile(argv[1]);
ofstream outputFile("result.txt");
/** open input file **/
if(!inputFile.is_open()){
cout<<"Fatal error: Unable to open input file"<<endl;
return 1;
}
/** open out put file **/
if(!outputFile.is_open()){
cout<<"Fatal error: Unable to open output file (result.txt)"<<endl;
return 1;
}
/** read each line and process **/
while(getline(inputFile, inputLine)){
analyzeLine(inputLine, lineSeps);
}
/** print result **/
printResult(outputFile);
inputFile.close();
outputFile.close();
}