Sunday, 25 August 2013

Setting attributes from semantic action

Setting attributes from semantic action

During parsing I have a few attributes that I need to set in semantic
action only ( as they are derived from data being parsed and I want to
avoid global variables ). If I use more than one variable passed into
qi::phrase_parse I get very long list of compilation errors. I need a help
badly :-)
#define BOOST_RESULT_OF_USE_DECLTYPE
#define BOOST_SPIRIT_USE_PHOENIX_V3
#include <boost/function.hpp>
#include <boost/phoenix/fusion.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/spirit/include/qi.hpp>
#include <iostream>
#include <string>
namespace qi = boost::spirit::qi;
namespace ph = boost::phoenix;
namespace ascii = boost::spirit::ascii;
int main( int argc, char**argv )
{
bool rc;
std::string input("");
//Test case works fine
{
auto iter( input.begin() );
auto last( input.end() );
int val1=33;
rc = qi::phrase_parse( iter, last, qi::eps[ qi::_val=11 ] ,
ascii::space, val1 ) && iter==last;
if( rc )
std::cout << "val1=" << val1 << std::endl;
}
//Test case does not compile
{
auto iter( input.begin() );
auto last( input.end() );
int val1=33;
int val2=0;
rc = qi::phrase_parse( iter, last,
qi::eps[ ph::at_c<0>(qi::_val)=1,ph::at_c<1>(qi::_val)=2 ],
ascii::space, val1,val2 ) && iter==last;
if( rc )
std::cout << "val1=" << val1 <<
"val2=" << val2 << std::endl;
}
return 0;
}

No comments:

Post a Comment