从另一种解析器前pressions一个可变参数列表产生精神解析器前pressions [英] Generating Spirit parser expressions from a variadic list of alternative parser expressions

查看:126
本文介绍了从另一种解析器前pressions一个可变参数列表产生精神解析器前pressions的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找实现可变参数函数,该函数列表提振精神:: ::气规则,并扩展到列表格式前pression最简单的方法:规则|规则2 |规则3 | ....假设规则合成无属性。你的帮助是非常AP preciated。

 的#include<升压/配置/ warning_disable.hpp>
#包括LT&;升压/精神/有/ qi.hpp>
#包括LT&;串GT;
#包括LT&;&iostream的GT;
#包括LT&;升压/精神/有/ phoenix_operator.hpp>命名空间补气=的boost ::精神::补气;
命名空间的pH =提振::凤;
命名空间ASCII =的boost ::精神:: ASCII;
使用boost ::精神::补气:: phrase_parse;
使用boost ::精神::齐:: ASCII ::空间;
使用boost ::精神:: ISO8859_1 :: char_;补气的typedef ::规则<的std ::字符串::为const_iterator,ASCII ::空间类型> mrule_t;
补气的typedef ::规则<标准::字符串::为const_iterator,标准::字符串()> wrule_t;


  //如何演绎expandBitwise()的返回类型?模板< typename的T>
ŧexpandBitwise(T& T公司)
{
    返回t.rule_;
}模板< typename的T,typename的...尾>
ŧexpandBitwise(T& T公司,尾部和放大器; ...尾)
{
    返回t.rule_ | expandBitwise(尾...);
}结构TStruct
{
    mrule_t rule_;
    模板< typename的T,typename的R>
    TStruct(T&安培; RVAR,常量标准::字符串&放大器;名称,R&安培;规则):
        rule_(齐亮::(名称)GT;>规则[PH :: REF(RVAR)=气:: _ 1])
        {}
};模板< typename的T,typename的...尾>
无效mparse(常量标准::字符串&放大器;线,T& T公司,尾部和放大器; ...尾)
{
    标准::字符串::为const_iterator F,L;    F = line.begin();
    L = line.end();


  //我想在这里展开的规则...
    //if(phrase_parse(f,l,expandBitwise(t,tail...),space)及&放大器;˚F== L)    如果(phrase_parse(F,L,t.rule_,空间)及&放大器;˚F== L)
        性病::法院LT&;<经分析:<<线474;<的std :: ENDL;
    其他
        性病::法院LT&;<语法错误:<<线474;<的std :: ENDL;
}诠释的main()
{
    wrule_t rword = +〜空间;    标准::字符串PAR1,PAR2,PAR3,PAR4;    TStruct R1(PAR1, - 一个,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( - 一个atoken,R1,R2,R3,R4);
    mparse( - B btoken,R1,R2,R3,R4);
    mparse( - Çctoken,R1,R2,R3,R4);
    mparse( - Ðdtoken,R1,R2,R3,R4);    返回0;
}


解决方案

  

感谢您快速提示!我只是想你code,除非我做错了什么......我得到这样的输出:语法错误:ABC 8.81负担:-a atoken语法错误:-b btoken语法错误: - çctoken语法错误:-d dtoken 的 - <一个href=\"http://stackoverflow.com/questions/16177184/return-type-deduction-of-boost-spirit-ex$p$pssion-made-of-variadic-function-expan/16179565#comment23128636_16179565\">G. Civardi 1小时前


好了,所以,我不能离开它单独:/

原来有未定义行为参与的,因为在这种分析器前pressions被传递到 expandBitwise 的方式,并成为复制:提升原前pression模板没有被设计成被复制,因为它们可能包含对临时引用,他的一生中其包含完整-EX pression月底结束


  

有关详情,请背景,在的零到2秒60英里!


在与 rule_.alias()调整长(长)时间的boost ::原:: deepcopy的我已经达到了以下解决方案(顺便说一下,并不需要一个辅助函数所有,再):

 模板&LT; typename的...尾&GT;
无效mparse(常量标准::字符串&放大器;线,尾部和放大器; ...尾)
{
    自动分析器=提振::融合::倍(
                提高::领带(PH ::绑定(安培; TStruct :: rule_,ARG1)(尾)...)
                齐:: EPS(假),
                deepcopy_(ARG2 | ARG1)
            );    汽车F =开始(线),L =端(线);    如果(气:: phrase_parse(F,L,分析器,ASCII ::空间))
        性病::法院LT&;&LT; 经分析:&LT;&LT;线474;&LT;的std :: ENDL;
    其他
        性病::法院LT&;&LT; 语法错误:&LT;&LT;线474;&LT;的std :: ENDL;    如果(F!= 1)
        性病::法院LT&;&LT; 剩余未解析:'&LT;&LT;标准::字符串(F,L)LT;&LT; '\\ n;
}

针对UB的保护是 deepcopy的_()调用,这是琐碎多态性可调用适配器的boost ::原:: deepcopy的

 结构正是deepcopy
{
    模板&LT; typename的E&GT;结构的结果{的typedef typename的升压::原:: ::的result_of&DEEP_COPY LT; E&GT; ::类型类型; };    模板&LT; typename的E&GT;
        类型名造成&LT; E&GT; ::类型
        运算符()(E&const的放大器;表达式)常量{
            返回的boost ::原:: DEEP_COPY(表达式);
        }
};静态常量pH值::功能&LT; deepcopy的&GT; deepcopy_;

有了这个code,你瞧,输出变为:

 语法错误:ABC 8.81
剩余未解析:'ABC 8.81
解析:-a atoken
解析:-b btoken
解析:-c ctoken
解析:-d dtoken
再见

作为奖励,在code现在允许你使用圣灵的内置调试()功能(取消注释行):

 &LT;&-d GT;
  &LT;尽量&GT; ABC 8.81&LT; /&尝试GT;
  &LT;失败/&GT;
&LT; / - D&GT;
&LT;&-c GT;
  &LT;尽量&GT; ABC 8.81&LT; /&尝试GT;
  &LT;失败/&GT;
&LT; / - C&GT;
&LT;&-b GT;
  &LT;尽量&GT; ABC 8.81&LT; /&尝试GT;
  &LT;失败/&GT;
&LT; / - B&GT;
&LT;&-a GT;
  &LT;尽量&GT; ABC 8.81&LT; /&尝试GT;
  &LT;失败/&GT;
&LT; / - A&GT;
语法错误:ABC 8.81
剩余未解析:'ABC 8.81


  

与测试


  
  

      
  • 升压1_54_0

  •   
  • GCC 4.7.2,4.8.x,3.2锵

  •   
  • 注意的#define ■哪些是显著。

  •   

FULL code

 的#define BOOST_RESULT_OF_USE_DECLTYPE
#定义BOOST_SPIRIT_USE_PHOENIX_V3
#包括LT&;升压/融合/调整/ boost_tuple.hpp&GT;
#包括LT&;升压/融合/有/ fold.hpp&GT;
#包括LT&;升压/精神/有/ qi.hpp&GT;
#包括LT&;升压/精神/有/ phoenix.hpp&GT;命名空间补气=的boost ::精神::补气;
命名空间的pH =提振::凤;
命名空间ASCII =的boost ::精神:: ASCII;
使用命名空间的pH :: arg_names;补气的typedef ::规则&LT;的std ::字符串::为const_iterator,ASCII ::空间类型&GT; mrule_t;
补气的typedef ::规则&LT;的std ::字符串::为const_iterator,标准::字符串()&GT; wrule_t;结构TStruct
{
    mrule_t rule_;
    模板&LT; typename的T,typename的R&GT;
    TStruct(T&安培; RVAR,常量标准::字符串&放大器;名称,R&安培;规则):
        rule_(齐亮::(名称)GT;&GT;规则[PH :: REF(RVAR)=气:: _ 1])
    {
        rule_.name(名);
        //调试(rule_);
    }
};结构正是deepcopy
{
    模板&LT; typename的E&GT;结构的结果{的typedef typename的升压::原:: ::的result_of&DEEP_COPY LT; E&GT; ::类型类型; };    模板&LT; typename的E&GT;
        类型名造成&LT; E&GT; ::类型
        运算符()(E&const的放大器;表达式)常量{
            返回的boost ::原:: DEEP_COPY(表达式);
        }
};静态常量pH值::功能&LT; deepcopy的&GT; deepcopy_;模板&LT; typename的...尾&GT;
无效mparse(常量标准::字符串&放大器;线,尾部和放大器; ...尾)
{
    自动分析器=提振::融合::倍(
                提高::领带(PH ::绑定(安培; TStruct :: rule_,ARG1)(尾)...)
                齐:: EPS(假),
                deepcopy_(ARG2 | ARG1)
            );    汽车F =开始(线),L =端(线);    如果(气:: phrase_parse(F,L,分析器,ASCII ::空间))
        性病::法院LT&;&LT; 经分析:&LT;&LT;线474;&LT;的std :: ENDL;
    其他
        性病::法院LT&;&LT; 语法错误:&LT;&LT;线474;&LT;的std :: ENDL;    如果(F!= 1)
        性病::法院LT&;&LT; 剩余未解析:'&LT;&LT;标准::字符串(F,L)LT;&LT; '\\ n;
}诠释的main()
{
    wrule_t rword = +〜ASCII ::空间;    标准::字符串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( - 一个atoken,R1,R2,R3,R4);
    mparse( - B btoken,R1,R2,R3,R4);
    mparse( - Çctoken,R1,R2,R3,R4);
    mparse( - Ðdtoken,R1,R2,R3,R4);    性病::法院LT&;&LT; 再见\\ n;
}

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 dtokenG. 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) << "'\n";
}

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 #defines 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) << "'\n";
}

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\n";
}

这篇关于从另一种解析器前pressions一个可变参数列表产生精神解析器前pressions的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆