boost::spirit::qi 不同的解析器行为

boost::spirit::qi difference parser behavior

我想了解 Qi Difference Parsers 的行为。

像这样:

ruleA = 
  ruleAa
| ruleAb
| ruleAc
;

ruleB =
ruleA - ruleAc
;

我想象解析器会匹配 ruleB,前提是输入匹配 ruleAa 或 ruleAb。换句话说,ruleB 将从 ruleA 中减去一个备选方案 (ruleAc)。这不正确吗?

在我的代码中,我正在执行与上述类似的操作,它可以编译,但似乎并不像我预期的那样运行。我的实际用例涉及其他难以在这里展开的因素。

本质上,我想做的是: 我有一个像 ruleA 这样的规则,其中包含一组备选方案。 这条规则在我的语法中用在了几个不同的地方。但在一个特定的用途中,我需要避免只唤起其中一种选择。差异解析器似乎就是为此目的而设计的,但也许我误解了?

提前致谢!

I was imagining that the parser would match ruleB iff the input matches ruleAa or ruleAb. In other words, ruleB would subtract one alternative (ruleAc) from ruleA. Is this incorrect?

(A|B|C) - C 仅等同于 (A|B) 当且仅当 C 永远不会匹配 (A|B) 会匹配的任何内容。

一个简单的例子,假设:

A = int_;
B = +space;
C = char_;

因为 C 总是匹配 AB 匹配的地方,很明显 (A|B) - C 总是不会匹配。 (A|B|C)-C 也一样。

简而言之 匹配的任何AB不再匹配C something - C.


This rule gets used in a few different places in my grammar. But in one particular use, I need to avoid evoking just one of the alternatives. It seems like the difference parser is intended for that purpose

正如您在上面看到的,情况并非如此。差异解析器根本不/修改/左侧解析器。 (它只是丢弃一些独立于左侧的匹配项)。

我能想到的最好的办法是/只在一个地方使用/(A|B),在另一个地方使用(A|B|C)。真的就是这么简单:说出你的意思.