1

I'm trying to port a parser from Spirit V2 to X3. The overall experience is quite good but there are two problems.

The first one is that local variables are gone, which is quite inconvenient to me since I used them quite often to keep track of things. Hence I'm asking for something that would do the job of locals in V2.

The other one is best illustrated with this dummy example: I want to parse a comma separated list of integers into a vector<int>, but it should only parse when the list sums up to zero:

auto const int_list = rule<class int_list, vector<int>>("int_list")
    = int_ % ','
    >> eps(/* How to extract the attribute? */);

I'm stuck with the last checking here as I don't know how to get my hands on the vector<int> the rule is synthesizing.

4

1 回答 1

2

我有同样的发现!

“locals”的诀窍是使用with<>指令。

因为您没有给出使用场景,所以我认为不值得提出示例,尽管您可以搜索我的答案*

第二个技巧是只使用语义操作(可以是 lambda)并赋值_passBoost Spirit X3 无法编译具有可变因子的重复指令也显示了这一点:

auto zerosum = [](auto &ctx) { 
    auto& v = x3::_attr(ctx); 
    _pass(ctx) = std::accumulate(v.begin(), v.end(), 0) == 0;
};
于 2016-02-07T13:32:13.037 回答