1 Star 0 Fork 0

Leoyby/词法分析器

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
scanner.cpp 1.33 KB
一键复制 编辑 原始数据 按行查看 历史
Leoyby 提交于 2022-12-02 10:09 . init
#include "scanner.h"
#include <cctype>
#include <istream>
ostream& operator<<(ostream &o, const Token &t)
{
o << "( " << t.kind << ", " << t.token << " )";
return o;
}
Scanner::Scanner(const Table &_table):
table(_table){}
vector<Token> Scanner::do_scan(istream &in)
{
vector<Token> res;
int state = 0;
Token tmp = {"", -1};
while (in)
{
string line;
getline(in, line);
line += '\n';
size_t i = 0;
while (i < line.size())
{
const char &c = line[i];
if(state == 0 && isspace(c))
{
++i;
continue;
}
int ne_state = table.state(state, c);
if(ne_state == -1)
{
throw "\e[0;31mError: \e[0m"s + line.substr(0, i) +
"\e[1;31m"s + c + "\e[0m"s + line.substr(i + 1);
}
auto flag = table.entry(state, c);
if(flag.second)
{
tmp.token += c;
++i;
}
if(flag.first == -1)
{
state = ne_state;
}
else
{
tmp.kind = flag.first;
res.push_back(tmp);
tmp.token.clear();
state = 0;
}
}
}
return res;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/Leoyby/lexical-analyzer.git
git@gitee.com:Leoyby/lexical-analyzer.git
Leoyby
lexical-analyzer
词法分析器
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385