1

The documentation says that the attribute of two composited rules (a >> b) should be tuple if both A and B are used.

Assuming this I tried to read out the first attribute of such a tuple. But it fails:
(I try to store the parsed integer in 'i')

#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_object.hpp>

template <typename ForwardIterator> class TestGrammar
: public boost::spirit::qi::grammar<ForwardIterator, boost::spirit::ascii::space_type>
{
    boost::spirit::qi::rule<ForwardIterator, boost::spirit::ascii::space_type> foo_;
public:
    TestGrammar( void ) : TestGrammar::base_type( foo_ )
    {
        int i;
        foo_ = ((boost::spirit::qi::int_ >> boost::spirit::qi::float_)
                      [boost::phoenix::ref(i) = boost::phoenix::at_c<0>(boost::spirit::_1)]);
    }
};

int main( void )
{
    TestGrammar<std::string::iterator> g;

    return 0;
}

Writing:

foo_ = ((boost::spirit::qi::int_ >> boost::spirit::qi::float_)
                     [boost::phoenix::ref(i) = boost::spirit::_1]);

will always work though as long as A is an int.
Changing types back and forth and writing custom rules revealed that the attribute of (a >> b) will always be A regardless of what B is.

Even

iolo
  • 1,090
  • 1
  • 9
  • 20

2 Answers2

2

I guess you wanted to do this:

int i;
float f;
foo_ = (boost::spirit::qi::int_ >> boost::spirit::qi::float_)
             [ boost::phoenix::ref(i) = boost::spirit::_1,
               boost::phoenix::ref(f) = boost::spirit::_2 ];

If you really wanted to use the sequence, try:

foo_ = qi::attr_cast<>(qi::int_ >> qi::float_)
                [ boost::phoenix::ref(i) = phx::at_c<0>(qi::_1),
                  boost::phoenix::ref(f) = phx::at_c<1>(qi::_1) ]

Or with a helper rule:

helper = qi::int_ >> qi::float_;
foo_ = helper
                [ boost::phoenix::ref(i) = phx::at_c<0>(qi::_1),
                  boost::phoenix::ref(f) = phx::at_c<1>(qi::_1) ]
    ;

All three versions compiling at http://liveworkspace.org/code/518f2bd03e1fed7ed734d62071a88eab

sehe
  • 374,641
  • 47
  • 450
  • 633
1

If you compile this:

struct TestDelegate
{
    template <typename T, typename U, typename V>
    void operator()(T const& t, U const& u, V const& v) const {
        bar;
    }
};

template <typename ForwardIterator> class TestGrammar
: public boost::spirit::qi::grammar<ForwardIterator, boost::spirit::ascii::space_type>
{
    boost::spirit::qi::rule<ForwardIterator, boost::spirit::ascii::space_type> foo_;
public:
    TestGrammar( void ) : TestGrammar::base_type( foo_ )
    {
        //int i;
        TestDelegate test_delegate;
        //foo_ = ((boost::spirit::qi::int_ >> boost::spirit::qi::float_)
        //              [boost::phoenix::ref(i) = boost::phoenix::at_c<0>(boost::spirit::_1)]);
        foo_ = ((boost::spirit::qi::int_ >> boost::spirit::qi::float_)
                      [test_delegate]);
    }
};

int main( void )
{
    TestGrammar<std::string::iterator> g;

    return 0;
}

You should see in the resulting error essay that the attribute type is a tuple:

1>e:\work\test\spiritprob\spiritprob\spiritprob.cpp(14) : error C2065: 'bar' : undeclared identifier
1>        e:\boost\boost_1_44_0\boost_1_44_0\boost\spirit\home\support\action_dispatch.hpp(29) : see reference to function template instantiation 'void TestDelegate::operator ()<Attribute,Context,bool>(const T &,const U &,const V &) const' being compiled
1>        with
1>        [
1>            Attribute=boost::fusion::vector2<int,float>,
1>            Context=boost::spirit::context<boost::fusion::cons<boost::fusion::unused_type &,boost::fusion::nil>,boost::fusion::vector0<>>,
1>            T=boost::fusion::vector2<int,float>,
1>            U=boost::spirit::context<boost::fusion::cons<boost::fusion::unused_type &,boost::fusion::nil>,boost::fusion::vector0<>>,
1>            V=bool
1>        ]

(compiled with visual studio 2008)

Doesn't directly solve our problem, but at least you know it does come out as a fusion::vector<int,float>

Pete
  • 4,784
  • 26
  • 33
  • Though I had to change the 'bar' expression to get my compiler to print out the type you are right. But then there seems to be a problem with '_1' instead... I will investigate that. – iolo Sep 02 '12 at 19:53
  • I think your problem is that at the point of instantiation of at_c,the type is the type of the place-holder '_1', and not a sequence. Maybe write a function object to handle it or something. – Pete Sep 02 '12 at 21:40
  • I provided three alternatives that work. Also, related: [Detecting the parameter types in a Spirit semantic action](http://stackoverflow.com/questions/9404189/detecting-the-parameter-types-in-a-spirit-semantic-action) – sehe Sep 02 '12 at 23:14