属性组合在Boost :: spirit中不起作用

时间:2012-09-02 18:22:43

标签: c++ boost boost-spirit

documentation表示如果同时使用A和B,则两个合成规则(a>> b)的属性应为元组。

假设我尝试读出这样一个元组的第一个属性。但它失败了:
(我尝试将解析后的整数存储在'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;
}

写作:

foo_ = ((boost::spirit::qi::int_ >> boost::spirit::qi::float_)
                     [boost::phoenix::ref(i) = boost::spirit::_1]);
只要A是int,

就会一直有效 来回更改类型和编写自定义规则表明,无论B是什么,(a&gt;&gt; b)的属性将始终为A.

甚至

2 个答案:

答案 0 :(得分:2)

我想你想这样做:

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 ];

如果您真的想使用序列,请尝试:

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) ]

或者使用帮助规则:

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) ]
    ;

所有三个版本都在 http://liveworkspace.org/code/518f2bd03e1fed7ed734d62071a88eab

进行编译

答案 1 :(得分:1)

如果你编译它:

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;
}

你应该在结果错误文章中看到属性类型是一个元组:

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>        ]

(使用visual studio 2008编译)

不能直接解决我们的问题,但至少你知道它确实是fusion::vector<int,float>