-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlexer.cpp
More file actions
222 lines (214 loc) · 6.4 KB
/
lexer.cpp
File metadata and controls
222 lines (214 loc) · 6.4 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
#include <iostream>
#include <fstream>
#include <vector>
#include <set>
#include <cctype>
using namespace std;
set<pair<int, string>> se;
vector<string> keywords = {"auto", "double", "int", "struct", "break", "else", "long", "switch",
"case", "enum", "register", "typedef", "char", "extern", "return", "union",
"const", "float", "short", "unsigned", "continue", "for", "signed", "void",
"default", "goto", "sizeof", "volatile", "do", "if", "while", "static"}; // 关键词
vector<char> delimiters = {'(', ')', '{', '}', '[', ']', ';', ','}; // 分界符
vector<string> operators = {"+", "-", "*", "/", "=", "%", "++", "--", "==", "!=", "<=", ">=", "<", ">", "&&",
"||", "!", "&", "|", "^", "~", ">>", "<<", ".", "::"}; // 运算符
string map[5] = {"关键字", "标识符", "常数", "运算符", "分界符"}; // 映射
bool isKeyword(const string &token) // 判断是不是关键字
{
for (const auto &keyword : keywords)
{
if (keyword == token)
return true;
}
return false;
}
bool isDelimiter(char ch) // 判断是不是分界符
{
for (const auto &delimiter : delimiters)
{
if (delimiter == ch)
return true;
}
return false;
}
bool isOperator(const string &token) // 判断是不是运算符
{
for (const auto &op : operators)
{
if (op == token)
return true;
}
return false;
}
bool isIdentifier(const string &token) // 判断是不是标识符
{
if (!isalpha(token[0]) && token[0] != '_')
return false;
for (char ch : token)
{
if (!isalnum(ch) && ch != '_')
return false;
}
return true;
}
bool isConstantNumber(const string &token)
{
bool decimalPoint = false;
for (char ch : token)
{
if (!isdigit(ch) && ch != 'e' && ch != '-')
{
if (ch == '.' && !decimalPoint)
{
decimalPoint = true;
}
else
{
return false;
}
}
}
return true;
}
bool isConstantString(const string &token)
{
for (const auto &keyword : keywords)
{
if (keyword == token)
return false;
}
return true;
}
int main()
{
ifstream ifs;
ifs.open("code.txt", ios::in);
if (ifs.is_open())
{
cout << "success to open the txt!" << endl;
for (int i = 0; i < 5; i++)
cout << i + 1 << " : " << map[i] << endl;
string buf, sentence;
while (getline(ifs, buf))
sentence += buf;
string token; // 保存当前解析的单词
int i = 0; // 源代码遍历索引
bool correct = false;
while (i < sentence.length())
{
char ch = sentence[i];
correct = false;
// 跳过空格和换行符
if (ch == ' ' || ch == '\t' || ch == '\n')
{
++i;
continue;
}
// 处理分界符
if (isDelimiter(ch))
{
cout << "(5, " << ch << ")" << endl;
correct = true;
++i;
continue;
}
// 处理运算符
string op, temp;
bool two = false;
op += ch;
temp += sentence[i + 1];
if (isOperator(temp))
{
op += sentence[++i];
two = true;
}
if (isOperator(op))
{
cout << "(4, " << op << ")" << endl;
correct = true;
++i;
continue;
}
else
{
if (two)
{
op = {ch};
--i;
if (isOperator(op))
{
cout << "(4, " << op << ")" << endl;
correct = true;
++i;
continue;
}
}
}
// 处理标识符或关键字
if (isalpha(ch) || ch == '_')
{
token += ch;
while (++i < sentence.length() && isalnum(sentence[i]))
{
token += sentence[i];
}
if (isKeyword(token))
{
cout << "(1, " << token << ")" << endl;
correct = true;
}
else if (isIdentifier(token))
{
cout << "(2, " << token << ")" << endl;
correct = true;
}
token.clear();
continue;
}
// 处理数字常量
if (isdigit(ch) || ch == '-')
{
token += ch;
while (++i < sentence.length() && (isdigit(sentence[i]) || sentence[i] == '.' || sentence[i] == 'e'))
{
token += sentence[i];
}
if (isConstantNumber(token))
{
cout << "(3, " << token << ")" << endl;
correct = true;
}
token.clear();
continue;
}
// 处理字符or字符串常量
if (ch == '\"')
{
token += ch;
while (++i < sentence.length() && sentence[i] != '\"')
{
token += sentence[i];
}
token += "\"";
if (isConstantString(token))
{
cout << "(3, " << token << ")" << endl;
correct = true;
}
token.clear();
++i;
continue;
}
if (!correct)
{
token.clear();
++i;
}
}
}
else
cout << "failed to open the txt!" << endl;
ifs.close();
system("pause");
return 0;
}