I am working on a parser with boost spirit x3. I finished the grammar and the parser parses as expected. Now I want to add error handling, so I have to add expectation points to my grammar.
My question is when exactly can I use the expectation operator >
instead of the "followed by" operator >>
?
Can I only use it like a > b
if a >> b
never happens in another part of the grammar?

- 4,798
- 6
- 25
- 53
1 Answers
The expectation operator essentially disables back-tracking. If b
must always follow a
, it is a good time to use an expectation point : >
. If there is some combination of logic that can result in an a
followed by something else, you should not use expectation but instead >>
.
If you have alternatives in your grammar, you will want to pay special attention that you have not defeated valid back tracks.
For example, if you are writing a language parser that requires the conditional expression of an if statement to be in parenthesis, a valid grammar might include:
if_statement = lit("if") > '(' > statement > ')';
if
is a keyword and it must be followed by a (
. Perhaps there is whitespace between the if
and (
but the keyword if
must be followed by a (
.
(Note: the reality is that the grammar is a little more complex than that. For if
to be a keyword it can't just match some token that starts with the letter i and f)
You can use a > b
in your grammar if you know at the point that the rule is encountered an a
must always be followed by a b
. You might have a a >> b
somewhere else in the overall grammar.

- 221
- 1
- 4
-
Worth noting that mixing `>` and `>>` can complicate automatic attribute propagation and transformation quite a bit (in relation to the literal question _"When exactly can I use the expectation operator?"_). The problem is that `vector3
` can become e.g. `vector2 – sehe Aug 15 '16 at 00:43, double>` in such cases -
1@sehe I don't have much experience with X3, and so sadly I can't say for sure, but I seem to remember Joel de Guzman saying that this problem, which gave lots of headaches in Qi, is already solved in X3. PS: [Found the link to the statement](http://boost.2283326.n4.nabble.com/Unexpected-behavior-using-versus-tp4662087p4662153.html). – llonesmiz Aug 15 '16 at 06:36