[英]boost spirit recursive parsing
I have read other topics about recursive parsing, but those solutions are not sufficient for me. 我已经阅读了有关递归解析的其他主题,但是这些解决方案对我来说还不够。
Let's have such a simple parser: 让我们有一个简单的解析器:
struct B;
typedef boost::variant<int, boost::recursive_wrapper<B> > A;
struct B {
B() {
static int l = 0;
cout << "B() " << ++l << endl;
}
A a1;
A a2;
};
// fusion adapt structs ....
typedef std::string::iterator iter;
typedef rule<iter, B()> BRule;
typedef rule<iter, A()> ARule;
ARule a;
BRule b;
a %= b | int_;
b %= a >> lit(',') >> a;
std::string s("5,8,3,1");
iter be = s.begin();
iter en = s.end();
B u;
parse(be, en, b, u);
I want to parse something like 's' string: "5,1,3,9" - this should consist of B element which contains 2 B elements which contain just integers. 我想解析类似's'的字符串:“ 5,1,3,9”-这应该由包含2个仅包含整数的B元素的B元素组成。
It causes - according to the site name - stack overflow. 它导致-根据站点名称-堆栈溢出 When I add parenthises:
当添加括号时:
b %= '(' >> a >> lit(',') >> a >> ')';
std::string s("((5,8),(3,1))");
... everything works fine. ...一切正常。
Is there any possibility to avoid parenthises and use parsers int this manner: 是否有可能避免括号并以这种方式使用解析器:
a %= b ....
b %= a ...
?? ?? Not necessarily such a notation, but for parsing '3,4,5,6' instead of '((3,4),(5,6))'
不一定是这样的符号,而是用于解析'3,4,5,6'而不是'((3,4),(5,6))'
No, you can't do that. 不,你不能那样做。 A PEG parser will simply bounce back and forth between those two rules.
PEG解析器只会在这两个规则之间来回跳动。
Why isn't your parser simply: 为什么解析器不简单:
b %= int_ >> *(lit(',') >> int_);
Alternatively, you'll often see constructs of the form: 另外,您经常会看到以下形式的构造:
expression %= primary >> *(lit(',') >> primary);
primary %= '(' >> expression >> ')' | int_;
You could also use a list parser and write that as: 您还可以使用列表解析器,并将其编写为:
expression %= primary % lit(',');
primary %= '(' >> expression >> ')' | int_;
Of course, better is 当然更好
a %= b % ','
for comma-delimited numbers, but that was just a fake example to show the problem's essence.
As written, the solution is: 如所写,解决方案是:
\n\n a %= b ....a%= b ....\n b %= any_rule_that_consumes_input >> .... a ....
b%= any_rule_that_consumes_input >> .... a ....\n\n
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.