Domanda

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!

È stato utile?

Soluzione

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top