-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLexicalAnalysis38.cpp
More file actions
479 lines (469 loc) · 19.1 KB
/
LexicalAnalysis38.cpp
File metadata and controls
479 lines (469 loc) · 19.1 KB
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/**
"Lexical Analysis"
Compiler Design Laboratory
Md. Farhan Sadique
Student ID: 130238
**/
/**
Input: Source program with or without space after each lexeme. There can be comments in input. Input needs to be a valid program. Input ends when 'eof' is given after a newline.
Output: List of Lexemes with their token name and attribute value and symbol table.
**C++11 required to compile the program**
**/
#include<iostream>
#include<string>
#include<vector>
using namespace std;
void checkToken();
void checkSingleToken();
void tokensCalculation();
void symbolTableCalculation();
bool checkKeyword(string str);
bool checkOperator(string str);
bool checkSymbol(string str);
bool isNumber(string str);
string keywords[] = { "bool", "char", "int", "long", "float", "double", "void", "return", "if", "else", "while", "for",
"break", "switch", "case", "cin", "cout","string"
};
string operators[] = { "+", "-", "*", "/", "++", "--", "=", "+=", "-=", "*=", "/=", "==", "!=", ">", "<", ">=", "<=", "&&", "||", "<<", ">>"
};
string symbols[] = { "(", ")", "{", "}", ";","," };
string program = ""; //for containing the input program as a single string.
string lexeme = ""; //for maintaining operation with each lexeme.
string x; //for storing each line of input program
vector<string> lexemes; //for saving the lexemes.
vector<string> tokenName; //for saving the token name of the lexemes
vector<string> attributeValue; //for saving the attribute value of the lexemes
vector<string> symbolTableSymbol; //for saving the symbols of the symbol table
vector<string> symbolTableDataType; //for saving the data type of the symbols
int lexemeFound = 0;
string tempLexeme[2]; //this program starts processing of lexeme when 2 lexemes are found. So, a array of size=2 is taken for saving previous 2 lexemes.
string tempId = "";
string singleCharLexeme = "";
int flag = 0;
int main()
{
cout<<"Source Program:"<<endl;
/**input starts**/
while (getline(cin, x)) //takes a string input with/without spaces. When newline is given, body of the loop executes.
{
int commentIndex = -1; //for saving starting index of a comment like this //......
int commentStartIndex = -1; //for saving starting index of a comment like this /*......*/
int commentEndIndex = -1; //for saving end index of a comment like this /*......*/
if (x == "eof") //When eof is given as a input, input ends.
break;
//starting index of comment calculation. comment like //.....
for (int i = 0; i<x.size() - 1; i++)
{
if (x[i] == '/' && x[i + 1] == '/')
{
commentIndex = i; //if comment is present in current line(x), than the starting index of the comment is saved.
break;
}
}
//starting and end index of comment calculation. comment like /*........*/
for (int i = 0; i<x.size() - 1; i++)
{
if (x[i] == '/' && x[i + 1] == '*')
{
commentStartIndex = i; //if comment is present in current line(x), than the starting index of the comment is saved. /*...*/
for (int j = i + 2; x.size() - 1; j++)
{
if (x[j] == '*' && x[j + 1] == '/')
{
commentEndIndex = j + 1; //end index of the comment is saved. /*....*/
i = x.size(); //i becomes the end index of the current program line(x). Because of ending outer for loop.
break;
}
}
}
}
//comment found starting with // and it's removal process
if (commentIndex != -1)
{
for (int j = 0; j<commentIndex; j++)
{
program += x[j]; //current line is added to program until the starting index of comment(//...) begin.
}
}
//comment found starting with /* and ending with */ and it's removal process
else if (commentStartIndex != -1)
{
for (int j = 0; j<commentStartIndex; j++)
{
program += x[j]; //current line is added to program until the starting index of comment(/*...*/) begin.
}
if (commentEndIndex != -1)
{
for (int j = commentEndIndex + 1; j<x.size(); j++)
{
program += x[j]; //current line is added to program from the [end index of comment(/*...*/)+1] to the last index of the line.
}
}
//comment started with /* but there is no end of the comment with */
else
{
cout << "Compile Error!";
}
}
//no comment in the line, so total line taken
else
{
program += x;
}
program += " "; //space added among the lines instead of newline.
}
/**input ends**/
/**token calculation starts**/
for (int i = 0; i<program.length(); i++)
{
//removing extra spaces if there is
if (program[i] == ' ') //When a space is found, that means a lexeme is found
{
while (program[i] == ' ' && i != program.length()) //loop for removing extra space
{
i++; //i is forwarded until consecutive spaces is found in program.
}
//Now i is at a position where a new lexeme starts
i--; //i is decremented because in for loop definition i is incremented again. Now i defines a space.
}
//no space, single character is added to lexeme and singleCharLexeme
else
{
singleCharLexeme += program[i]; //singleCharLexeme contains only a single character.
lexeme += program[i]; //lexeme contains the whole lexeme.
}
//checking the lexeme(immediately after a identifier) for separating the identifier as a different lexeme
if ((checkKeyword(singleCharLexeme) || checkOperator(singleCharLexeme) || checkSymbol(singleCharLexeme)) && lexeme.size()>1)
{
for (int x = 0; x<lexeme.size() - 1; x++)
{
tempId += lexeme[x]; //lexeme(identifier) is added to tempId without the last character(which is a new lexeme)
}
flag = 1; //flag turns to 1 to indicate a lexeme(identifier) is found.
i--; //i is decremented for getting the last character(new lexeme after the identifier) again in next turn of the loop.
lexeme = tempId; //tempId(identifier) is added as a lexeme
tempId="";
}
singleCharLexeme = ""; //empty the singleCharLexeme for taking next character
//if a lexeme is found
if (checkKeyword(lexeme) || checkOperator(lexeme) || checkSymbol(lexeme) || flag == 1)
{
flag = 0;
//if number of found lexemes is less than 2, than the current lexeme added to templexeme array.
if (lexemeFound<2)
{
tempLexeme[lexemeFound] = lexeme;
lexemeFound++;
}
//if number of found lexemes is equal to 2, than the previous 2 lexemes are checked for token calculation
else
{
checkToken(); //checking if the lexeme is a keyword or operator or symbol or identifier or number.
}
lexeme = ""; //empty the lexeme for taking next lexeme
}
}
//after calculating all lexemes, there can be some lexemes in templexeme array. Because in previous loop token is calculated only when lexemeFound>2.
if (lexemeFound>0)
{
//one lexeme remains
if(lexemeFound == 1)
{
checkSingleToken(); //checking if the lexeme is a keyword or operator or symbol or identifier or number.
}
//2 lexeme remains
else if (lexemeFound == 2)
{
checkToken(); //checking if the lexeme is a keyword or operator or symbol or identifier or number.
}
//last lexeme of the 2 lexeme
if (lexemeFound == 2)
{
checkSingleToken(); //checking if the lexeme is a keyword or operator or symbol or identifier or number.
}
}
symbolTableCalculation(); //symbol table for output
tokensCalculation(); //Tokens for output
/**Output**/
cout <<"\n\nTokens:"<<endl;
cout <<"\nLexemes-Token Name-Attribute Value\n"<<endl;
for (int i = 0; i<lexemes.size(); i++)
{
cout << lexemes[i] << " - " << tokenName[i] << " - " << attributeValue[i]<<endl;
}
cout <<"\n\nSymbol Table:"<<endl;
cout <<"\nSymbol-Token-DataType-Pointer to symbol table entry\n"<<endl;
for (int i = 0; i<symbolTableSymbol.size(); i++)
{
cout << symbolTableSymbol[i] << " - " << "id" << " - " << symbolTableDataType[i]<<" - "<<i<<endl;
}
return 0;
}
//function for checking a lexeme to calculate token
void checkToken()
{
//if the previous 2 lexemes combinedly generate a lexeme like 2 lexemes '+' and '+' combinedly makes '++'(a new lexeme).
if (checkKeyword(tempLexeme[0] + tempLexeme[1]))
{
lexemes.push_back(tempLexeme[0] + tempLexeme[1]);
tokenName.push_back("keyword");
lexemeFound = 1; //'lexeme' variable contains a new lexeme, so lexemeFound turns to 1.
tempLexeme[0] = lexeme; //The new lexeme is added to tempLexeme array at index=0.
}
else if (checkOperator(tempLexeme[0] + tempLexeme[1]))
{
lexemes.push_back(tempLexeme[0] + tempLexeme[1]);
tokenName.push_back("operator");
lexemeFound = 1;
tempLexeme[0] = lexeme;
}
else if (checkSymbol(tempLexeme[0] + tempLexeme[1]))
{
lexemes.push_back(tempLexeme[0] + tempLexeme[1]);
tokenName.push_back("special symbol");
lexemeFound = 1;
tempLexeme[0] = lexeme;
}
//if among the previous 2 lexemes, first lexeme is a valid lexeme itself without combining with the 2nd lexeme.
else if (checkKeyword(tempLexeme[0]))
{
lexemes.push_back(tempLexeme[0]);
tokenName.push_back("keyword");
lexemeFound = 2; //templexeme contains a lexeme which is not calculated yet and 'lexeme' variable contains a new lexeme. So, it is 2.
tempLexeme[0] = tempLexeme[1]; //lexeme at 0 index is calculated, so lexeme at 1 index goes to 0 index
tempLexeme[1] = lexeme; //new lexeme is added to index=1
}
else if (checkOperator(tempLexeme[0]))
{
lexemes.push_back(tempLexeme[0]);
tokenName.push_back("operator");
lexemeFound = 2;
tempLexeme[0] = tempLexeme[1];
tempLexeme[1] = lexeme;
}
else if (checkSymbol(tempLexeme[0]))
{
lexemes.push_back(tempLexeme[0]);
tokenName.push_back("special symbol");
lexemeFound = 2;
tempLexeme[0] = tempLexeme[1];
tempLexeme[1] = lexeme;
}
else if (isNumber(tempLexeme[0]))
{
lexemes.push_back(tempLexeme[0]);
tokenName.push_back("number");
lexemeFound = 2;
tempLexeme[0] = tempLexeme[1];
tempLexeme[1] = lexeme;
}
else
{
lexemes.push_back(tempLexeme[0]);
tokenName.push_back("id");
lexemeFound = 2;
tempLexeme[0] = tempLexeme[1];
tempLexeme[1] = lexeme;
}
}
//function for checking lexeme which is not a combining one(like 2 lexemes '+' and '+' combinedly makes '++'(a new lexeme))
void checkSingleToken()
{
if (checkKeyword(tempLexeme[0]))
{
lexemes.push_back(tempLexeme[0]);
tokenName.push_back("keyword");
}
else if (checkOperator(tempLexeme[0]))
{
lexemes.push_back(tempLexeme[0]);
tokenName.push_back("operator");
}
else if (checkSymbol(tempLexeme[0]))
{
lexemes.push_back(tempLexeme[0]);
tokenName.push_back("special symbol");
}
else if (isNumber(tempLexeme[0]))
{
lexemes.push_back(tempLexeme[0]);
tokenName.push_back("number");
}
else
{
lexemes.push_back(tempLexeme[0]);
tokenName.push_back("id");
}
}
//function for checking if a lexeme is a keyword
bool checkKeyword(string str)
{
for (string key : keywords) //foreach loop runs for every value of 'keywords' array.[it exists in C++11]
{
if (key == str)
{
return true;
}
}
return false;
}
//function for checking if a lexeme is a operator
bool checkOperator(string str)
{
for (string key : operators)
{
if (key == str)
{
return true;
}
}
return false;
}
//function for checking if a lexeme is a symbol
bool checkSymbol(string str)
{
for (string key : symbols)
{
if (key == str)
{
return true;
}
}
return false;
}
//function for checking if a lexeme is a number
bool isNumber(string str)
{
int length = str.length();
int k = 0;
for (char c : str) //foreach loop to check if every element of the lexeme(str) is digit.
{
if (isdigit(c)) //checking if the current character is a digit.
k++; //if the current character is a digit, then variable 'k' counts it.
else if (c == '.') //checking if the current character is a '.'.
k++; //if the current character is a '.', then variable 'k' counts it.
}
if (k == length) //if every character of the lexeme is digit or '.', than k is equal to the length of the lexeme(str).
return true;
return false;
}
//function for symbol table calculation
void symbolTableCalculation()
{
for(int i=0; i<lexemes.size(); i++)
{
flag = 0;
if(tokenName[i] == "id")
{
//checking if current id is previously found or not
for(int j=0; j<symbolTableSymbol.size(); j++)
{
if(lexemes[i] == symbolTableSymbol[j])
flag = 1;
}
//if current id is not found previously.
if(flag == 0)
{
symbolTableSymbol.push_back(lexemes[i]);
//if it is not a function name. There is a '(' after a function name and before a function name there is a 'keyword'
if(lexemes[i+1] != "(" && tokenName[i-1]=="keyword")
{
symbolTableDataType.push_back(lexemes[i-1]); //a variable's data type is it's previous lexeme (int temp; where int is the data type of temp)
}
//variable separated by comma
else if(lexemes[i-1] == ",")
{
symbolTableDataType.push_back(symbolTableDataType[symbolTableDataType.size()-1]); //the previous variable's data type = current variable's data type.
}
//it is a function name
else
{
symbolTableDataType.push_back("_"); //function name has no data type.
}
}
}
}
}
//function for Tokens calculation
void tokensCalculation()
{
for(int i=0; i<lexemes.size(); i++)
{
if(tokenName[i] == "keyword")
{
tokenName[i] = lexemes[i]; //if it is a keyword, than it's tokenName is itself.
attributeValue.push_back(" - "); //if it is a keyword, than it does not have a attributeValue.
}
else if(tokenName[i] == "number")
{
attributeValue.push_back("constant"); //if it is a number, than it's tokenName is number and attributeValue is constant.
}
else if(tokenName[i] == "id")
{
if(lexemes[i+1] != "(") //if it is an variable, than it's tokenName is id and attributeValue is "pointer to symbol table entry".
attributeValue.push_back("pointer to symbol table entry");
else //if it is a function name, than it's tokenName is id and attributeValue is empty.
attributeValue.push_back(" - ");
}
else if(tokenName[i] == "operator") //giving every operator it's own name as attribute value.
{
if(lexemes[i] == "+")
attributeValue.push_back("addition");
else if(lexemes[i] == "-")
attributeValue.push_back("subtraction");
else if(lexemes[i] == "*")
attributeValue.push_back("multiplication");
else if(lexemes[i] == "/")
attributeValue.push_back("division");
else if(lexemes[i] == "++")
attributeValue.push_back("increment");
else if(lexemes[i] == "--")
attributeValue.push_back("decrement");
else if(lexemes[i] == "=")
attributeValue.push_back("assignment");
else if(lexemes[i] == "+=")
attributeValue.push_back("addition and assignment");
else if(lexemes[i] == "-=")
attributeValue.push_back("subtraction and assignment");
else if(lexemes[i] == "*=")
attributeValue.push_back("multiplication and assignment");
else if(lexemes[i] == "/=")
attributeValue.push_back("division and assignment");
else if(lexemes[i] == "==")
attributeValue.push_back("Relation Operator");
else if(lexemes[i] == "!=")
attributeValue.push_back("Relation Operator");
else if(lexemes[i] == ">")
attributeValue.push_back("Relation Operator");
else if(lexemes[i] == "<")
attributeValue.push_back("Relation Operator");
else if(lexemes[i] == ">=")
attributeValue.push_back("Relation Operator");
else if(lexemes[i] == "<=")
attributeValue.push_back("Relation Operator");
else if(lexemes[i] == "&&")
attributeValue.push_back("Relation Operator");
else if(lexemes[i] == "||")
attributeValue.push_back("Relation Operator");
else if(lexemes[i] == "<<")
attributeValue.push_back("Stream extraction operator");
else if(lexemes[i] == ">>")
attributeValue.push_back("Stream insertion operator");
}
else if(tokenName[i] == "special symbol") //giving every symbol it's own name as attribute value.
{
if(lexemes[i] == "(")
attributeValue.push_back("opening braces");
else if(lexemes[i] == ")")
attributeValue.push_back("closing braces");
else if(lexemes[i] == "{")
attributeValue.push_back("left curly braces");
else if(lexemes[i] == "}")
attributeValue.push_back("right curly braces");
else if(lexemes[i] == ";")
attributeValue.push_back("semicolon");
else if(lexemes[i] == ",")
attributeValue.push_back("comma");
}
}
}