编写词法分析器:词法分析器找不到"
Writing a lexer : lexer doesnt find "
我正在编写一种小型编程语言,该语言只有一个关键字为 write
。我没有做解析器,而是勒克斯。它可以正常工作直到到达"
代币。
main.cpp:
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <vector>
using namespace std;
int main(int argc, char* argv[2])
{
char text;
string tok = "";
string str = "";
vector <string> tokens;
fstream inFile(argv[1]);
while (inFile >> noskipws >> text)
{
tok += text;
if (tok == "write")
{
tokens.push_back(tok);
tok = "";
}
else if(tok == """)
{
str += text;
tokens.push_back("String:"+str);
str = "";
tok = "";
tok += text;
}
for (auto const& c : tokens)
std::cout << c << ' ';
}
return 0;
}
test.txt:
write "hello"
输出是:
write write write write write write write ...
而不是
String:hello
我该怎么办?有什么想法吗?
我认为应该是 skipws
而不是 noskipws
,因为当它遇到"写"answers" hello"之间的空间时,您不会处理案例;具体来说,您不清除tok
,因此它仍然认为令牌是一个空格。
相关文章:
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 为什么野牛仍在使用"int yylex(void)",却找不到"int yylex(YYS
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 奇怪的(对我来说)返回声明 - 在谷歌上找不到任何关于它的信息
- 找不到QtResource文件中的文件
- VC++本机单元测试,找不到调试符号
- RegGetValue在当前用户下找不到名称
- 带有 -stdlib=libc++ 的 clang++ 9.0.1 找不到<optional>
- c++找不到具有相同哈希的无序集合元素
- 找不到以下加速库:boost_fiber
- 找不到 FLTK(缺少:FLTK_INCLUDE_DIR)
- 设置 Visual Studio for MPI: 找不到标识符错误
- "assimp/config.h"找不到,但它在文件夹中
- 链接器找不到在虚拟类 c++ 中访问的静态字段的符号
- C++系统找不到指定的文件错误
- FindPackageHandleStandardArgs.cmake:137 的 CMake 错误(消息):找不到 Boost (缺少:正则表达式)(找到合适的版本"1.72.0",
- vcpkg 添加新库,但找不到
- 为什么我的静态分析器找不到潜在的未初始化读取?
- 编写词法分析器:词法分析器找不到"