-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExp6.cpp
178 lines (159 loc) · 4.62 KB
/
Exp6.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
// Write a program to implement Lexical Analyzer for the source code stored in a file.
#include <iostream>
#include <vector>
#include <fstream>
#include <algorithm>
#include <iomanip>
using namespace std;
vector<string> keywords = {
"alignas", "alignof", "asm", "auto", "bool", "break", "case", "catch",
"char", "char16_t", "char32_t", "class", "const", "constexpr", "const_cast", "continue",
"decltype", "default", "delete", "do", "double", "dynamic_cast", "else", "enum",
"explicit", "export", "extern", "false", "float", "for", "friend", "goto",
"if", "inline", "int", "long", "mutable", "namespace", "new", "noexcept",
"nullptr", "operator", "private", "protected", "public", "register",
"reinterpret_cast", "return", "short", "signed", "sizeof", "static", "static_assert",
"static_cast", "struct", "switch", "template", "this", "thread_local", "throw",
"true", "try", "typedef", "typeid", "typename", "union", "unsigned", "using",
"virtual", "void", "volatile", "wchar_t", "while", "and", "and_eq", "bitand",
"bitor", "compl", "not", "not_eq", "or", "or_eq", "xor", "xor_eq"};
bool binary_search(vector<string>::iterator itr1, vector<string>::iterator itr2, string str)
{
if (itr1 >= itr2)
return false;
auto mid = itr1 + (itr2 - itr1) / 2;
if (*mid == str)
return true;
if (*mid < str)
return binary_search(mid + 1, itr2, str);
return binary_search(itr1, mid - 1, str);
}
bool isDelimiter(char c)
{
return !isalpha(c) and !isdigit(c) and c != '_';
}
bool isKeyword(string &str)
{
if (str.length() == 0)
return false;
return binary_search(keywords.begin(), keywords.end(), str);
}
bool isIdentifier(string &str)
{
if (str.length() == 0)
return false;
if (!isalpha(str[0]) and str[0] != '_')
return false;
for (char c : str)
{
if (!isalpha(c) and !isdigit(c) and c != '_')
return false;
}
return true;
}
bool isOperator(char ch)
{
vector<char> operators = {'+', '-', '*', '/', '%', '>', '<', '='};
return (find(operators.begin(), operators.end(), ch) != operators.end());
}
bool isInteger(string str)
{
if (str.length() == 0)
return false;
vector<char> valids = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
for (int i = 0; i < str.length(); i++)
{
char c = str[i];
if (find(valids.begin(), valids.end(), c) == valids.end() || (c == '-' and i > 0))
return false;
}
return true;
}
bool isRealNumber(string str)
{
if (str.length() == 0)
return false;
vector<char> valids = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
bool hasDecimal = false;
for (int i = 0; i < str.length(); i++)
{
char c = str[i];
if (find(valids.begin(), valids.end(), c) == valids.end() and c != '.' || (c == '-' and i > 0))
return false;
if (c == '.')
hasDecimal = true;
}
return hasDecimal;
}
vector<vector<string>> detectTokens(string &str)
{
vector<vector<string>> tokenList;
int tokens = 0;
int n = str.length();
int left = 0, right = 0;
while (right <= n)
{
if (!isDelimiter(str[right]))
{
right++;
}
if (isDelimiter(str[right]) and left == right)
{
if (isOperator(str[right]) == true)
{
tokenList.push_back({string(1, str[right]), "Operator"});
}
right++;
left = right;
}
else if (isDelimiter(str[right]) and left != right || (right == str.length() and left != right))
{
string subStr = str.substr(left, right - left);
if (isKeyword(subStr))
{
tokenList.push_back({subStr, "Keyword"});
}
else if (isInteger(subStr))
{
tokenList.push_back({subStr, "Integer"});
}
else if (isRealNumber(subStr))
{
tokenList.push_back({subStr, "Real Number"});
}
else if (isIdentifier(subStr))
{
tokenList.push_back({subStr, "Identifier"});
}
left = right;
}
}
return tokenList;
}
string readFile()
{
string file, str, temp;
cout << "Enter program file: ";
cin >> file;
fstream fin(file);
while (getline(fin, temp))
{
str += temp + '\n';
}
return str;
}
void printTokenList(vector<vector<string>> &tokenList)
{
cout << setw(20) << "TOKEN" << setw(20) << "TOKEN TYPE" << endl;
for (auto v : tokenList)
{
cout << setw(20) << v[0] << setw(20) << v[1] << endl;
}
}
int main()
{
string str = readFile();
cout << "Tokens in the program are:\n\n";
vector<vector<string>> tokenList = detectTokens(str);
printTokenList(tokenList);
}