3

I am trying to learn boost::spirit to parse a grammar, but am having trouble understanding exactly how to use the library.

Basically, if the parser hits "Test" in the input stream, I'd like to emit 5 as the return value. (Later, I'd actually like to emit a pair, with a string member "Test" and a value member 5.

Here is what I have so far:

template <typename Iterator>
struct testgrammar
    : public grammar<Iterator, unsigned int()>
{
public:

    testgrammar() : testgrammar::base_type(start)
    {
        start = std::wstring(L"Test") [ _val = 5 ] ;
    }

    virtual ~testgrammar(){};

    rule<Iterator, unsigned int()> start;

};

In main():

std::wstring strIn = L"Test";
std::wstring::const_iterator itBegin = strIn.begin();
std::wstring::const_iterator itEnd = strIn.end();
unsigned int nData = 0;
bool bRes = parse(itBegin, itEnd, avxgrammar<std::wstring::const_iterator>(), nData);

However, I'm doing it wrong, because the compiler emits the following error:

error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const boost::spirit::_val_type' (or there is no acceptable conversion)

My question is why can't I assign to _val? How should I emit such a struct from the semantic action?

Edit Maybe I should clarify the end goal. I want to parse a text file into pairs of Token/Literal values.

Thank you in advance for your help!

sehe
  • 374,641
  • 47
  • 450
  • 633
namezero
  • 2,203
  • 3
  • 24
  • 37

1 Answers1

3

To assign to qi::_val you probably just need to

#include <boost/spirit/include/phoenix.hpp>

or minimally phoenix_operator.hpp.


Regarding the goal of your exercise I think you'll want to read about

sehe
  • 374,641
  • 47
  • 450
  • 633
  • Thanks for the reply. I had tried including phoenix, but that changes the compiler error to this: error C2679: binary '[' : no operator found which takes a right-hand operand of type 'boost::phoenix::actor' – namezero Aug 15 '13 at 21:02
  • 2
    Darn, I overlooked the obvious earlier:`std::wstring` is not a parser expression template, so it won't take a semantic action for sure. Use `qi::string` or perhaps the one from `boost::spirit::standard_wide` for great good! _(quoting details from memory sure to extremely limited Internet access)_ – sehe Aug 15 '13 at 22:39