The question is easy to formulate. I have a recursive rule, I do not know the synthesized attribute type for the rule, but I also have a few more questions about the inner workings.
It looks to me that the return type is variant<tuple<return_type_of_a, SEQ>, tuple<return_type_of_b, SEQ>>
where SEQ
is a recursive rule and a
and b
are terminals:
rule<class myrule, ??> SEQ = a >> SEQ | b >> SEQ;
The following is not accepted because the rule is recursive, and I cannot figure out the return type exactly:
rule<class myrule, decltype (a>> SEQ | b >> SEQ)> seq = a >> seq | b >> seq;
- must I know the return type of the recursive rule?
- it looks like there must be some type of type nesting, this is natural to recursion, but if it is not flattened it is impossible to calculate the return type at compile-time. So how is the type for a recursive rule calculated at compile-time? there is any kind of flattening?
- what should be the synthesized of the rule above?
- would it help to refactor the rule to:
rule<class myrule, ??> SEQ = (a | b) >> SEQ;
Thanks for your help.
About
First and foremost: your grammar is strictly circular and never parses: it will infinitely recurse the rule, until mismatch. I'm going to assume you wanted something like:
(Note how not all branches recurse unconditionally).
Yes. The tutorial samples should probably show this.
Sample
Let's say we have a very very simple grammar like
We create an AST to reflect that:
The Rules
Because the
expr
rule is going to be recursive, we have to declare it before its definition:Let's imagine it's already defined, we would write the sub expressions like:
All that's left is the recursive rule itself:
BOOST_SPIRIT_DEFINE
That's all.
DEMO TIME
Let's add some test cases:
Live On Coliru
Prints
And optionally (
#define BOOST_SPIRIT_X3_DEBUG
)Live On Coliru