Questions about the sequence operator Spirit.Qi and semantic actions

I have some questions about the sequence operator and semantic actions in Spirit Qi.

I am trying to define a grammar rule for a floating point number that takes metric prefixes (u, m, k, M, etc.), as well as the normal form of the exponent.

  rule<Iterator, std::string()> sign = char_("+-") [ _val = _1 ];
  rule<Iterator, std::string()> exp = char_("eE") >> -sign >> +digit;
  rule<Iterator, std::string()> suffix = char_("yzafpnumkKMGTPEZY") [ _val = _1 ];
  rule<Iterator, std::string()> mantissa = ((*digit >> char_('.') >> +digit) | (+digit >> char_('.') >> *digit));
  rule<Iterator, std::string()> unsigned_floating = (mantissa >> -(exp | suffix) | +digit >> (exp | suffix));
  rule<Iterator, std::string()> floating = -sign >> unsigned_floating;

Question 1: Why do I need to add semantic action to the rule signabove? Not charconvertible to std::string?

Question 2: Why compilation fails when I try to combine the last two rules as follows:

  rule<Iterator, std::string()> floating = -sign >> (mantissa >> -(exp | suffix) | +digit >> (exp | suffix));

3: , , floating double double. , , ?

4: floating 2, - _2 ?

Edit:

, :

placeholder _2 ?

  rule<Iterator, std::string()> floating = (-sign >> (mantissa >> -(exp | suffix) | +digit >> (exp | suffix))) [ _2 ];

!

+2
1

-, -. .

1. ? char std::string?

Erm, no char . . .

2: , :

rule<Iterator, std::string()> floating = -sign >> 
              (mantissa >> -(exp | suffix) | +digit >> (exp | suffix));

. -

vector2<optional<string>, variant<
      vector2<string, optional<string> >,
      vector2<std::vector<char>, optional<string> > >

(. , ). , , . qi::as<> . *** qi::as_string:

floating = qi::as_string [ -sign >> (mantissa >> -(exp | suffix) | 
                                     +digit >> (exp | suffix)) ] 

3. , , double. , , ?

qi::as_string , qi::raw:

floating = qi::raw [ -sign >> (mantissa >> -(exp | suffix) | 
                               +digit >> (exp | suffix)) ] 
       [ _val = parse_float(_1, _2) ];

, .

4. , 2, placeholder _2 - ?

, - , - . :


--

Qi builtin real_parser<> template, . , , .

real_ parser , . . NaN ?.

RealPolicies :

Expression                 | Semantics 
===========================+=============================================================================
RP::allow_leading_dot      | Allow leading dot. 
RP::allow_trailing_dot     | Allow trailing dot. 
RP::expect_dot             | Require a dot. 
RP::parse_sign(f, l)       | Parse the prefix sign (e.g. '-'). Return true if successful, otherwise false. 
RP::parse_n(f, l, n)       | Parse the integer at the left of the decimal point. Return true if successful, otherwise false. If successful, place the result into n. 
RP::parse_dot(f, l)        | Parse the decimal point. Return true if successful, otherwise false. 
RP::parse_frac_n(f, l, n)  | Parse the fraction after the decimal point. Return true if successful, otherwise false. If successful, place the result into n. 
RP::parse_exp(f, l)        | Parse the exponent prefix (e.g. 'e'). Return true if successful, otherwise false. 
RP::parse_exp_n(f, l, n)   | Parse the actual exponent. Return true if successful, otherwise false. If successful, place the result into n. 
RP::parse_nan(f, l, n)     | Parse a NaN. Return true if successful, otherwise false. If successful, place the result into n. 
RP::parse_inf(f, l, n)     | Parse an Inf. Return true if successful, otherwise false. If successful, place the result into n

. , .

+2

Source: https://habr.com/ru/post/1663131/


All Articles