Exagon Exagon - 1 year ago 95
C++ Question

When exactly can I use the expectation operator?

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
a >> b
never happens in another part of the grammar?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download