1

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?

Community
  • 1
  • 1
fhw72
  • 1,066
  • 1
  • 11
  • 19
  • If you include a SSCCE people are gonna have a much better time guessing the errors. Because they might not be helpful to you, but the absense is usually even less helpful. You happen to have me here, but most people will just ignore these questions. And with good reason. – sehe Mar 31 '15 at 12:33
  • You're right and I just omitted a full fledged example as I guessed that this is certainly something trivial I'm missing this time. But I'll follow your suggestion and be more detailed next time. Thanks for pointing that out! – fhw72 Mar 31 '15 at 13:04

1 Answers1

1

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")?

Community
  • 1
  • 1
sehe
  • 374,641
  • 47
  • 450
  • 633
  • Once again you shed much light on the issue... thanks alot! I really wonder if you've ever considered wrinting a cookbook about Spirit... seriously! – fhw72 Mar 31 '15 at 12:48
  • If it pays :) I might be interested if an initiative already exists – sehe Mar 31 '15 at 12:51
  • Indeed I'd prefer to put the parsing of the fields in the ast. But I'm not familiar how to properly design the ast even for such a simple use case. My problem is: Some of the identifiers are followed by another 2nd identifier and some others are not... but if you have some useful hints or links I'd happily dig deeper! ps: I'd buy your book...promised! – fhw72 Mar 31 '15 at 12:52