#include <iostream>
using namespace std;
#include <string>
struct Token {
std::string type; // 词法单元类型,如"IDENTIFIER", "SEPARATOR"
std::string value; // 词法单元的值
};
#include <vector>
#include <cctype>
pair<bool, string> keywordIdentify(string identifier) {
if (identifier == "int")
return pair<bool, string>(1, "关键字int");
else if (identifier == "main")
return pair<bool, string>(1, "主程序入口main");
else if (identifier == "return")
return pair<bool, string>(1, "关键字return");
else {
return pair<bool, string>(0, "");
}
}
vector<Token> tokenize(const std::string &input) {
vector<Token> tokens;
size_t pos = 0;
while (pos < input.size()) {
if (isspace(input[pos])) { // 跳过空白字符
++pos;
} else if (isalpha(input[pos]) || input[pos] == '_') { // 标识符
string identifier;
while (pos < input.size() && (isalnum(input[pos]) || input[pos] == '_')) {
identifier += input[pos++];
}
pair<bool, string> res = keywordIdentify(identifier);
if (res.first) {
tokens.push_back({res.second, identifier});
} else
tokens.push_back({"标识符", identifier});
} else { // 其他字符视为分隔符处理
tokens.push_back({"分隔符", string(1, input[pos++])});
}
}
return tokens;
}
int main() {
string input = "int main() { int __a = 5; return 0; }";
auto tokens = tokenize(input);
for (const auto &token : tokens) {
std::cout << "Type: " << token.type << ", Value: '" << token.value << "'\n";
}
return 0;
}
这一个词法分析器简单地读取标准输入,然后用了两个动态数组分别存放分隔符和标识符。
还用了一个辅助函数,用于在标识符里识别关键字。