3

在解析过程中,我需要在语义操作中设置一些属性(因为它们是从被解析的数据派生的,我想避免global变量和依赖BOOST_FUSION_ADAPT_STRUCT以及我的代码应该是通用的,以便我可以将它用于多个类型)。如果我使用多个传入的变量,qi::phrase_parse 我会得到很长的编译错误列表。我非常需要帮助:-)

#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>
#include <climits>

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 1 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 2 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;         
    }
    //Test case 3 works fine
    {
        auto iter( input.begin() );
        auto last( input.end() );
        int val1=33;
        int val2=0;
        rc = qi::phrase_parse( iter, last,
                qi::attr(1)>>qi::attr(2),
                ascii::space, val1,val2 ) && iter==last;
        if( rc )
            std::cout << "val1=" << val1 <<
                         " val2=" << val2 << std::endl;
    }

    return 0;
}

我从中获取了“自定义” my_phrase_parse ,cv_and_he但它破坏了我想要运行的完整测试用例的编译:

template<typename T,typename R>
void testParser( R rule )
{
    for ( const auto input : std::vector< std::string >{ "5 1.0 2.0 3.0 4.0 5.0", "1 1.0", "0" , "", "2 3 ab" } )
    {
        bool rc;
        T maxValue;
        T minValue;
        auto iter( input.begin() );
        auto last( input.end() );
        std::vector< T > v;
        rc = my_phrase_parse( iter, last,
            qi::eps[
                     ph::at_c<0>(qi::_val)=std::numeric_limits<T>::max(),
                     ph::at_c<1>(qi::_val)=std::numeric_limits<T>::min()
                   ]
            >> -( qi::omit[ qi::int_] 
            >> *rule[ ph::if_(ph::at_c<0>(qi::_val)>qi::_1)[ ph::at_c<0>(qi::_val)=qi::_1 ],
                      ph::if_(ph::at_c<1>(qi::_val)<qi::_1)[ ph::at_c<1>(qi::_val)=qi::_1 ]
                    ] )
                ,ascii::space, minValue, maxValue,v ) && iter==last;
        std::cout << ( rc ? "ok :`" : "err:`" ) << input << "` -> ";
        if( rc )
        {
            std::cout << "min=" << minValue << " max=" << maxValue << "\t";
            std::copy( v.begin(), v.end(), std::ostream_iterator<T>( std::cout," " ));
        }
        else
            std::cout << *iter;
        std::cout << std::endl;
    }
}
...
    testParser<double>( qi::double_ );
4

3 回答 3

1

这是workaround我最初的问题:

#define BOOST_RESULT_OF_USE_DECLTYPE
#define BOOST_SPIRIT_USE_PHOENIX_V3

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

#include <iostream>
#include <algorithm>
#include <iterator>
#include <string>
#include <climits>

namespace qi = boost::spirit::qi;
namespace ph = boost::phoenix;
namespace ascii = boost::spirit::ascii;

template<typename T,typename R>
void testParser( R rule )
{
    for ( const auto &input : std::vector< std::string >{ "5 1.0 2.0 3.0 4.0 5.0" } )
    {
        bool rc=false;
        T maxValue, minValue;
        auto iter( input.begin() ), last( input.end() );
        std::vector< T > data;

        qi::rule< decltype(iter),std::vector<T>( T&, T& ),qi::space_type > mrule;

        mrule %=
            qi::eps[ qi::_r1=std::numeric_limits<T>::max(),
                     qi::_r2=std::numeric_limits<T>::min() ] >>
             -( qi::omit[ qi::int_[ ph::reserve( qi::_val,qi::_1 ) ] ]
                >> *rule[ ph::if_(qi::_r1>qi::_1)[ qi::_r1=qi::_1 ],
                          ph::if_(qi::_r2<qi::_1)[ qi::_r2=qi::_1 ]
                        ] );

        rc = qi::phrase_parse( iter, last, mrule( ph::ref(minValue), ph::ref(maxValue) ), qi::space, data ) && iter==last;

    std::cout << ( rc ? "ok :`" : "err:`" ) << input << "` -> ";
    if( rc )
    {
        std::cout << "min=" << minValue << " max=" << maxValue << "\t";
        std::copy( data.begin(), data.end(), std::ostream_iterator<T>( std::cout," " ));
        }
        else
            std::cout << *iter;
        std::cout << std::endl;
    }
}

int main( int argc, char**argv )
{
    testParser<double>( qi::double_ );
    return 0;
}
于 2013-08-25T23:02:51.747 回答
1

Spirit 使用的Phoenix 占位符允许您从规则的上下文中操作重要信息。当您在调用中直接在表达式中使用它们时,parse或者phrase_parse没有规则,因此没有上下文。在版本 Boost 1.47.0 之前不起作用,并且为了使这种行为一致,对这些函数的单参数版本进行了修复,但显然不适用于可变参数版本。

回避此问题的一种方法是创建一个规则,该规则具有fusion::vector对您在调用中使用的类型的引用的属性 a phrase_parse

编辑:删除“my_phrase_parse”,因为我不确定它是否正确

#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;
namespace fusion = boost::fusion;

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; 
    }
    //You can use a rule
    {
        auto iter( input.begin() );
        auto last( input.end() );
        int val1=33;
        int val2=0;

        qi::rule<decltype(iter),fusion::vector<int&, int&>(),ascii::space_type> parser=qi::eps[ ph::at_c<0>(qi::_val)=1,ph::at_c<1>(qi::_val)=2 ];

        rc = qi::phrase_parse( iter, last, 
                parser,
                ascii::space, val1,val2 ) && iter==last;
        if( rc )
            std::cout << "val1=" << val1 << 
                         " val2=" << val2 << std::endl;      
    }

    return 0;
}

编辑 2:添加了另一种方法来解决您要在编辑中解决的问题

#define BOOST_RESULT_OF_USE_DECLTYPE
#define BOOST_SPIRIT_USE_PHOENIX_V3

#include <boost/spirit/include/qi_core.hpp>
#include <boost/spirit/include/qi_omit.hpp>

#include <iostream>
#include <string>
#include <climits>

namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;

template <typename T>
struct min_max_set
{
    min_max_set():min(std::numeric_limits<T>::max()),max(std::numeric_limits<T>::min()),set(){}
    T min;
    T max;
    std::vector<T> set;
};

namespace boost{ namespace spirit { namespace traits
{
    template <typename T>
    struct is_container<min_max_set<T>>
        : boost::mpl::true_
    {};

    template <typename T>
    struct container_value<min_max_set<T>>
    {
        typedef T type;
    };

    template <typename T>
    struct push_back_container<min_max_set<T>,T>
    {
        static bool call(min_max_set<T>& cont, const T& val)
        {
            if(cont.min>val)
                cont.min=val;
            if(cont.max<val)
                cont.max=val;
            cont.set.push_back(val);
            return true;
        }
    };

}}}

template<typename T,typename R>
void testParser( R rule )
{
    for ( const auto input : std::vector< std::string >{ "5 1.0 2.0 3.0 4.0 5.0", "1 1.0", "0" , "", "2 3 ab" } )
    {
        bool rc;

        auto iter( input.begin() );
        auto last( input.end() );
        min_max_set<T> accum;   

        rc = qi::phrase_parse( iter, last,
                qi::omit[qi::int_] >> *rule
                ,ascii::space, accum ) && iter==last;
        std::cout << ( rc ? "ok :`" : "err:`" ) << input << "` -> ";
        if( rc )
        {
            std::cout << "min=" << accum.min << " max=" << accum.max << "\t";
            std::copy( accum.set.begin(), accum.set.end(), std::ostream_iterator<T>( std::cout," " ));
        }
        else
            std::cout << *iter;
        std::cout << std::endl;
    }
}


int main( int argc, char**argv )
{
    testParser<double>(qi::double_);
    return 0;
}
于 2013-08-25T15:10:11.557 回答
0

我希望我正确地看到了问题,但即使这有点不同,您也可以毫不费力地存储多个值:

struct data {
  int _a;
  int _b;
  data(int a, int b) : _a(a), _b(b) {};
};

并构造它:

qi::eps[qi::_val = phx::construct<data>(1, 2)]
于 2013-08-25T21:40:52.637 回答