从可选解析器表达式的可变参数列表生成 Spirit 解析器表达式 [英] Generating Spirit parser expressions from a variadic list of alternative parser expressions
问题描述
我正在寻找实现可变参数函数的最简单方法,该方法接受 boost::spirit::qi 规则列表并将列表扩展为格式表达式:rule1 |规则 2 |rule3 |.... 让我们假设规则不合成任何属性.非常感谢您的帮助.
#include #include #include <字符串>#include #include 命名空间 qi = boost::spirit::qi;命名空间 ph = boost::phoenix;命名空间 ascii = boost::spirit::ascii;使用 boost::spirit::qi::phrase_parse;使用 boost::spirit::qi::ascii::space;使用 boost::spirit::iso8859_1::char_;typedef qi::rule<std::string::const_iterator,ascii::space_type>mrule_t;typedef qi::rule<std::string::const_iterator,std::string() >wrule_t;
<小时>
//如何推导出expandBitwise()返回类型?模板T expandBitwise(T& t){返回 t.rule_;}模板T expandBitwise(T& t,Tail& ...tail){返回 t.rule_ |expandBitwise(tail...);}结构体{mrule_t 规则_;模板TStruct( T& rVar,const std::string&name, R& rule ) :rule_( qi::lit( name ) >> rule[ ph::ref( rVar )=qi::_1 ] ){}};模板void mparse(const std::string& line,T& t,Tail& ...tail){std::string::const_iterator f,l;f=line.begin();l=line.end();
<小时>
//我想在这里扩展规则...//if(phrase_parse(f,l,expandBitwise(t,tail...),space ) && f==l)if(phrase_parse(f, l, t.rule_, space ) && f==l )std::cout<<"解析:"<<line<<std::endl;别的std::cout<<"语法错误:"<<line<<std::endl;}int main(){wrule_t rword=+~space;std::string par1,par2,par3,par4;TSstruct r1( par1,"-a", rword );TSstruct r2( par2,"-b", rword );TSstruct r3( par3,"-c", rword );TSstruct r4( par4,"-d", rword );mparse("abc 8.81" ,r1,r2,r3,r4);mparse("-a atoken" ,r1,r2,r3,r4);mparse("-b btoken",r1,r2,r3,r4);mparse("-c ctoken" ,r1,r2,r3,r4);mparse("-d dtoken",r1,r2,r3,r4);返回0;}
感谢您的快速提示!我刚刚试过你的代码,除非我做错了什么......我得到这个输出:
Syntax error:abc 8.81 Parsed:-a atoken Syntax error:-b btoken Syntax error:-c ctoken Syntax error:-d dtoken
– G.Civardi 2 小时前
好吧,所以,我不能不管它:/
结果是涉及未定义行为,因为解析器表达式被传递给expandBitwise
并被复制的方式:Boost Proto表达式模板没有被设计被复制,因为它们可能包含对临时对象的引用,其生命周期在它们包含的完整表达式结束时结束.
有关更多背景信息,请参阅 2 秒内从零加速到 60 MPH!
在使用 rule_.alias()
和 boost::proto::deepcopy
进行长时间(长时间)调整后,我得出了以下解决方案(顺便说一下, 根本不需要辅助函数了):
templatevoid mparse(const std::string& line,Tail& ...tail){自动解析器 = boost::fusion::fold(boost::tie(ph::bind(&TStruct::rule_, arg1)(tail)...),qi::eps(false),deepcopy_(arg2 | arg1));自动 f=begin(line), l=end(line);if( qi::phrase_parse(f, l, parser, ascii::space ) )std::cout <<解析:"<<线<
针对 UB 的保护是 deepcopy_()
调用,它是 boost::proto::deepcopy
的一个简单的多态可调用适配器:
struct DeepCopy{模板struct result { typedef typename boost::proto::result_of::deep_copy::type type;};模板typename 结果::type运算符()(E const& expr) const {返回 boost::proto::deep_copy(expr);}};静态常量 ph::function深拷贝_;
使用这段代码,瞧,输出变成了:
语法错误:abc 8.81未解析:'abc 8.81'解析:-a token解析:-b btoken解析:-c ctoken解析:-d dtoken再见
作为奖励,代码现在允许您使用 Spirit 的内置 debug() 功能(取消注释该行):
<-d><try>abc 8.81</try><失败/></-d><-c><try>abc 8.81</try><失败/></-c><-b><try>abc 8.81</try><失败/></-b><-a><try>abc 8.81</try><失败/></-a>语法错误:abc 8.81未解析:'abc 8.81'
<块引用>
用
测试- 提升 1_54_0
- GCC 4.7.2、4.8.x、Clang 3.2
- 注意
#define
很重要.
完整代码
#define BOOST_RESULT_OF_USE_DECLTYPE#define BOOST_SPIRIT_USE_PHOENIX_V3#include #include #include #include 命名空间 qi = boost::spirit::qi;命名空间 ph = boost::phoenix;命名空间 ascii = boost::spirit::ascii;使用命名空间 ph::arg_names;typedef qi::rule<std::string::const_iterator,ascii::space_type>mrule_t;typedef qi::rule<std::string::const_iterator,std::string() >wrule_t;结构体{mrule_t 规则_;模板TStruct( T& rVar,const std::string&name, R& rule ) :rule_( qi::lit(name) >> rule[ ph::ref(rVar) = qi::_1 ] ){rule_.name(name);//调试(规则_);}};结构体深拷贝{模板struct result { typedef typename boost::proto::result_of::deep_copy::type type;};模板typename 结果::type运算符()(E const& expr) const {返回 boost::proto::deep_copy(expr);}};静态常量 ph::function深拷贝_;模板void mparse(const std::string& line,Tail& ...tail){自动解析器 = boost::fusion::fold(boost::tie(ph::bind(&TStruct::rule_, arg1)(tail)...),qi::eps(false),deepcopy_(arg2 | arg1));自动 f=begin(line), l=end(line);if( qi::phrase_parse(f, l, parser, ascii::space ) )std::cout <<解析:"<<线<
I'm looking for the simplest way to implement variadic function which takes list of boost::spirit::qi rules and expands the list into expression of format: rule1 | rule2 | rule3 |.... Let's assume that the rules synthesize no attribute. Your kind help is very much appreciated.
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <string>
#include <iostream>
#include <boost/spirit/include/phoenix_operator.hpp>
namespace qi = boost::spirit::qi;
namespace ph = boost::phoenix;
namespace ascii = boost::spirit::ascii;
using boost::spirit::qi::phrase_parse;
using boost::spirit::qi::ascii::space;
using boost::spirit::iso8859_1::char_;
typedef qi::rule<std::string::const_iterator,ascii::space_type> mrule_t;
typedef qi::rule< std::string::const_iterator,std::string() > wrule_t;
//How to deduce expandBitwise() return type ?
template<typename T>
T expandBitwise(T& t)
{
return t.rule_;
}
template<typename T,typename ...Tail>
T expandBitwise(T& t,Tail& ...tail)
{
return t.rule_ | expandBitwise(tail...);
}
struct TStruct
{
mrule_t rule_;
template<typename T,typename R>
TStruct( T& rVar,const std::string&name, R& rule ) :
rule_( qi::lit( name ) >> rule[ ph::ref( rVar )=qi::_1 ] )
{}
};
template<typename T,typename ...Tail>
void mparse(const std::string& line,T& t,Tail& ...tail)
{
std::string::const_iterator f,l;
f=line.begin();
l=line.end();
// I would like to expand the rules here ...
//if(phrase_parse(f,l,expandBitwise(t,tail...),space ) && f==l)
if( phrase_parse(f, l, t.rule_, space ) && f==l )
std::cout<<"Parsed:"<<line<<std::endl;
else
std::cout<<"Syntax error:"<<line<<std::endl;
}
int main()
{
wrule_t rword=+~space;
std::string par1,par2,par3,par4;
TStruct r1( par1,"-a", rword );
TStruct r2( par2,"-b", rword );
TStruct r3( par3,"-c", rword );
TStruct r4( par4,"-d", rword );
mparse("abc 8.81" ,r1,r2,r3,r4);
mparse("-a atoken" ,r1,r2,r3,r4);
mparse("-b btoken" ,r1,r2,r3,r4);
mparse("-c ctoken" ,r1,r2,r3,r4);
mparse("-d dtoken" ,r1,r2,r3,r4);
return 0;
}
Thank you for a quick hint! I've just tried your code and unless I do something wrong ... I get this output:
Syntax error:abc 8.81 Parsed:-a atoken Syntax error:-b btoken Syntax error:-c ctoken Syntax error:-d dtoken
– G. Civardi 2 hours ago
Okay, so, I couldn't leave it alone :/
Turns out there was Undefined Behaviour involved, because of the way in which parser expressions were being passed to expandBitwise
and being copied: Boost Proto expression templates weren't designed to be copied as they may contain references to temporaries, whose lifetime ends at the end of their containing full-expression.
See for more background, the discussion at Zero to 60 MPH in 2 seconds!
After a long (long) time of tweaking with rule_.alias()
and boost::proto::deepcopy
I have reached the following solution (which, incidentally, doesn't need a helper function at all, anymore):
template<typename ...Tail>
void mparse(const std::string& line,Tail& ...tail)
{
auto parser = boost::fusion::fold(
boost::tie(ph::bind(&TStruct::rule_, arg1)(tail)...),
qi::eps(false),
deepcopy_(arg2 | arg1)
);
auto f=begin(line), l=end(line);
if( qi::phrase_parse(f, l, parser, ascii::space ) )
std::cout << "Parsed:" << line << std::endl;
else
std::cout << "Syntax error:" << line << std::endl;
if (f!=l)
std::cout << "Remaining unparsed: '" << std::string(f,l) << "'
";
}
The protection against UB is the deepcopy_()
invocation, which is a trivial polymorphic callable adaptor for boost::proto::deepcopy
:
struct DeepCopy
{
template<typename E> struct result { typedef typename boost::proto::result_of::deep_copy<E>::type type; };
template<typename E>
typename result<E>::type
operator()(E const& expr) const {
return boost::proto::deep_copy(expr);
}
};
static const ph::function<DeepCopy> deepcopy_;
With this code, lo and behold, the output becomes:
Syntax error:abc 8.81
Remaining unparsed: 'abc 8.81'
Parsed:-a atoken
Parsed:-b btoken
Parsed:-c ctoken
Parsed:-d dtoken
Bye
As a bonus, the code now allows you to use Spirit's builtin debug() capabilities (uncomment that line):
<-d>
<try>abc 8.81</try>
<fail/>
</-d>
<-c>
<try>abc 8.81</try>
<fail/>
</-c>
<-b>
<try>abc 8.81</try>
<fail/>
</-b>
<-a>
<try>abc 8.81</try>
<fail/>
</-a>
Syntax error:abc 8.81
Remaining unparsed: 'abc 8.81'
Tested with
- Boost 1_54_0
- GCC 4.7.2, 4.8.x, Clang 3.2
- Note the
#define
s which are significant.
FULL CODE
#define BOOST_RESULT_OF_USE_DECLTYPE
#define BOOST_SPIRIT_USE_PHOENIX_V3
#include <boost/fusion/adapted/boost_tuple.hpp>
#include <boost/fusion/include/fold.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
namespace qi = boost::spirit::qi;
namespace ph = boost::phoenix;
namespace ascii = boost::spirit::ascii;
using namespace ph::arg_names;
typedef qi::rule<std::string::const_iterator,ascii::space_type> mrule_t;
typedef qi::rule<std::string::const_iterator,std::string() > wrule_t;
struct TStruct
{
mrule_t rule_;
template<typename T,typename R>
TStruct( T& rVar,const std::string&name, R& rule ) :
rule_( qi::lit(name) >> rule[ ph::ref(rVar) = qi::_1 ] )
{
rule_.name(name);
// debug(rule_);
}
};
struct DeepCopy
{
template<typename E> struct result { typedef typename boost::proto::result_of::deep_copy<E>::type type; };
template<typename E>
typename result<E>::type
operator()(E const& expr) const {
return boost::proto::deep_copy(expr);
}
};
static const ph::function<DeepCopy> deepcopy_;
template<typename ...Tail>
void mparse(const std::string& line,Tail& ...tail)
{
auto parser = boost::fusion::fold(
boost::tie(ph::bind(&TStruct::rule_, arg1)(tail)...),
qi::eps(false),
deepcopy_(arg2 | arg1)
);
auto f=begin(line), l=end(line);
if( qi::phrase_parse(f, l, parser, ascii::space ) )
std::cout << "Parsed:" << line << std::endl;
else
std::cout << "Syntax error:" << line << std::endl;
if (f!=l)
std::cout << "Remaining unparsed: '" << std::string(f,l) << "'
";
}
int main()
{
wrule_t rword=+~ascii::space;
std::string par1,par2,par3,par4;
TStruct r1( par1, "-a", rword );
TStruct r2( par2, "-b", rword );
TStruct r3( par3, "-c", rword );
TStruct r4( par4, "-d", rword );
mparse("abc 8.81" ,r1,r2,r3,r4);
mparse("-a atoken" ,r1,r2,r3,r4);
mparse("-b btoken" ,r1,r2,r3,r4);
mparse("-c ctoken" ,r1,r2,r3,r4);
mparse("-d dtoken" ,r1,r2,r3,r4);
std::cout << "Bye
";
}
这篇关于从可选解析器表达式的可变参数列表生成 Spirit 解析器表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!