fhw72
fhw72

Reputation: 1164

Compile error when adding semantic action to Boost Spirit parser

Thanks to the help from user 'sehe' I'm now at the point where I can compile my ast.

(Please see here: https://stackoverflow.com/a/29301655/1835623 )

Now one of the data fields extracted from JEDEC file I need to parse looks like this:

"12345 0000100101010111010110101011010"

I already built a parser to consume these kind of fields:

std::string input("12345 010101010101010101010");
std::string::iterator st = input.begin();

qi::parse(st, input.end(), qi::ulong_ >> ' ' >> *qi::char_("01"));

Obviously not that complicated. Now my problem is that I want to assign the ulong_ and the binary string to some local variables using a semantic action. This is what I did:

using boost::phoenix::ref;

std::string input("12345 010101010101010101010");
std::string::iterator st = input.begin();

uint32_t idx;
std::string sequence;
qi::parse(st, input.end(),
          qi::ulong_[ref(idx) = qi::_1] >>
          ' ' >>
          *qi::char_("01")[ref(sequence) += qi::_2]);

But unfortunately this doesn't even compile and the error message I get is not helpful (at least to me)? I guess it's something simple... but I'm hopelessly stuck now. :-(

Does someone have an idea what I'm doing wrong?

Upvotes: 1

Views: 79

Answers (1)

sehe
sehe

Reputation: 394054

Two ways:

  1. fix the SA's

    qi::parse(st, input.end(),
              qi::ulong_[ref(idx) = qi::_1] >>
              ' ' >>
              qi::as_string[*qi::char_("01")] [phx::ref(sequence) += qi::_1]);
    

    Notes:

    • it's qi::_1 because the expression the SA attaches to doesn't expose two elements, just 1
    • it's explicitly phx::ref because otherwise ADL¹ will select std::ref (because of std::string)
    • use as_string to coerce the attribute type from std::vector<char>


  2. However, of course, as always: Boost Spirit: "Semantic actions are evil"?

    qi::parse(st, input.end(),
              qi::ulong_ >> ' ' >> *qi::char_("01"),
              idx, sequence
        );
    

    Use the parser API to bind references to attributes.

¹ What is "Argument-Dependent Lookup" (aka ADL, or "Koenig Lookup")?

Upvotes: 1

Related Questions