使用 Spirit x3,如何控制在每个不同的输入上调用哪个解析器?
Using Spirit x3, how to control which parser is called on each different input?
我正在使用提升精神x3,但不清楚一点。我有一个文件,其中有非常不同和重复的行。前几行可能是注释。接下来的 1000 行可能是坐标,接下来的 1000 行可能是 int 的列表,等等......
我的问题是如何识别该行并知道该行使用哪个解析器。 例如,这里有两个解析器函数....
template <typename Iterator>
bool parse_ints(Iterator first, Iterator last, std::vector<int>& v)
{
using x3::int_;
using x3::phrase_parse;
using x3::_attr;
using ascii::space;
auto push_back = [&](auto& ctx){ v.push_back(_attr(ctx)); };
bool r = phrase_parse(first, last,
(
int_[push_back]
>> *(',' >> int_[push_back])
)
,
space);
if (first != last)
return false;
return r;
}
template <typename Iterator>
bool parse_doubles(Iterator first, Iterator last, std::vector<double>& v)
{
using x3::double_;
using x3::phrase_parse;
using x3::_attr;
using ascii::space;
auto push_back = [&](auto& ctx){ v.push_back(_attr(ctx)); };
bool r = phrase_parse(first, last,
(
double_[push_back]
>> *(',' >> double_[push_back])
)
,
space);
if (first != last) // fail if we did not get a full match
return false;
return r;
}
这是输入的方式(istringstream的大小为数百MB)。在 while 循环中,我想知道要调用哪个解析器。
int main()
{
istringstream str(my.data());
while (getline(str, line)) {
// based on the format of 'line', how does one know which
// parser to call?
}
}
你的语法可以描述整个输入。所以你可以说
auto input = *headerComment
>> points
>> edges;
您可以简单地定义的地方
auto headerComment = '#' >> *(char_ - eol) >> eol;
例如
auto points = skip(blank) [
*(point >> eol)
];
auto edges = skip(blank) [
*(edge >> eol)
];
point = int_ >> int_ >> int_; // assuming x y z
edge = int_ >> int_; // assuming from to
这假定输入格式是明确的。 也就是说,如果点被x y
,它就不起作用,因为它当时无法与边缘区分开来。
相关文章:
- 使用 pybind11 调用 Python 函数时避免复制输入数据
- C++ 如何根据作为输入传递的参数调用一个构造函数或另一个构造函数?
- 使用多个输入向量中值的笛卡尔乘积调用 lambda
- 程序在调用函数时跳过用户输入
- 从 C++ 调用 GNU 绘图,无需用户输入
- GoogleMock:如何验证输入参数的调用次数?
- 对需要双精度数组和结构作为输入C++ DLL 函数的 C# 调用
- 使用 Spirit x3,如何控制在每个不同的输入上调用哪个解析器?
- 如何使用低级系统调用从 stdin 和输入文件中获取字节数
- 是否可以使用输入作为对字符串的调用
- 干净取消在输入/输出调用中被阻止的 std::线程
- 功能调用不起作用,但控制台仍允许输入
- 我可以在输入BSTR变体上调用variantChangeType吗?
- 实现递归函数,避免由 C++ 中 include 的循环调用(没有 #pragma 一次)引起的无限循环输入
- 为什么在同一输入上调用std::getline()n次会产生第n个标记化
- 功能调用,该函数从文本文件-C 检查输入正确性
- 在多线程中调用带有输入参数的函数
- 析构函数的调用.和输入输出过载
- C++自定义用户输入调用变量
- 基于字符串输入调用函数