gpt4 book ai didi

c++ - 为传递到 qi::phrase_parse 的表达式设置语义 Action 的属性

转载 作者:太空狗 更新时间:2023-10-29 21:45:03 25 4
gpt4 key购买 nike

在解析期间,我有一些属性只需要在语义操作中设置(因为它们是从正在解析的数据派生的,我想避免 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;
}

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

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

最佳答案

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

回避此问题的一种方法是创建一个规则,该规则具有一个 fusion::vector 属性,该规则引用了您在调用 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;
}

关于c++ - 为传递到 qi::phrase_parse 的表达式设置语义 Action 的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18429974/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com