Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost spirit: how to parse until we have "->"

I am trying to parse a rule of the form predicate -> action.

My problem is that predicate can be any valid mathematical expression, so it may actually include a minus sign or a greater sign (but we have to disallow them in sequence as that's the token we want to separate predicate from action).

Essentially, I would like predicate to consume all non-spaces until it hits the string "->".

How do I got about doing this?

Is the right approach to fix the line I have commented below or should I better define what a predicate is, in terms of a valid expression, and let the parser fall into "->" when predicate ends, according to that valid expression?

rule %=
  predicate
  >> "->"
  >> action
  ;

predicate %= (+~(qi::char_("-"))); // BAD: works only if no minus sign in predicate
action    %= (+(qi::char_));
like image 273
kfmfe04 Avatar asked Sep 18 '25 12:09

kfmfe04


1 Answers

Rephrase predicate as follows:

predicate = +(qi::char_ - "->"); 
action    = +(qi::char_);

Equivalently,

predicate = +(!qi::lit("->") >> qi::char_); 

should do the same.

See the docs

  • Difference Parser (a - b)
  • Not-Predicate Parser (!a)
like image 146
sehe Avatar answered Sep 21 '25 02:09

sehe