规则定义中的AST和运算符优先级 [英] AST and operator precedence in rule definition

查看:189
本文介绍了规则定义中的AST和运算符优先级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好 [¹]



我有一个简单的解析器



它打算解析条件表达式(关系算术运算及其逻辑组合)。



那么它会成功解析A> 5,但是会停止并忽略输入的其余部分,这与我的内容一致。



如何更改 expr _ 规则,使其解析整个输入?

  #include< cstdint> 
#include< boost / spirit / include / qi.hpp>
#include< boost / spirit / include / phoenix.hpp>
#include< boost / spirit / include / phoenix_operator.hpp>
#include< boost / variant / recursive_wrapper.hpp>

命名空间qi = boost :: spirit :: qi;
namespace phx = boost :: phoenix;

///终端
枚举metric_t:std :: uint8_t {A = 0u,B};
const std :: string metric_names [] = {A,B};
struct metrics_parser:boost :: spirit :: qi :: symbols< char,metric_t>
{
metrics_parser()
{
this-> add
(metric_names [A],A)
(metric_names [B],B)
;
}
};


///运算符
struct op_or {};
struct op_and {};
struct op_xor {};
struct op_not {};

struct op_eq {};
struct op_lt {};
struct op_let {};
struct op_gt {};
struct op_get {};

template< typename tag> struct unop;
template< typename tag> struct binop;

///表达式
typedef boost :: variant<
int,
double,
metric_t,
boost :: recursive_wrapper< unop< op_not> >,
boost :: recursive_wrapper< binop< op_and> >,
boost :: recursive_wrapper< binop< op_or> >,
boost :: recursive_wrapper< binop< op_xor> >,
boost :: recursive_wrapper< binop< op_eq> >,
boost :: recursive_wrapper< binop< op_lt> >,
boost :: recursive_wrapper< binop< op_gt> >
> expr;

template< typename tag>
struct binop
{
explicit binop(const expr& l,const expr& r):oper1(l),oper2(r){}
expr oper1,oper2;
};

template< typename tag>
struct unop
{
显式unop(const expr& o):oper1(o){}
expr oper1;
};

struct printer:boost :: static_visitor< void>
{
printer(std :: ostream& os):_os(os){}
std :: ostream& _os;

void operator()(const binop< op_and>& b)const {print(and,b.oper1,b.oper2); }
void operator()(const binop< op_or>& b)const {print(or,b.oper1,b.oper2); }
void operator()(const binop< op_xor>& b)const {print(xor,b.oper1,b.oper2); }
void operator()(const binop< op_eq&& b)const {print(=,b.oper1,b.oper2); }
void operator()(const binop< op_lt>& b)const {print(<,b.oper1,b.oper2); }
void operator()(const binop< op_gt>& b)const {print(>,b.oper1,b.oper2); }

void print(const std :: string& op,const expr& l,const expr& r)const
{
_os< (;
boost :: apply_visitor(* this,l);
_os<< op;
boost :: apply_visitor(* this,r);
_os <<);
}

void operator()(const unop< op_not>& u)const
{
_os& (;
_os<<!;
boost :: apply_visitor(* this,u.oper1);
_os<
}
void operator()(metric_t m)const
{
_os< metric_names [m];
}

template< typename other_t>
void operator()(other_t i)const
{
_os<<一世;
}
};

std :: ostream& operator<<<(std :: ostream& os,const expr& e)
{boost :: apply_visitor(printer(os),e); return os; }

std :: ostream& operator<<(std :: ostream& os,metric_t m)
{os< metric_names [m]; return os; }

template< typename It,typename Skipper = qi :: space_type>
struct parser:qi :: grammar< It,expr(),Skipper>
{
parser():parser :: base_type(expr_)
{
使用命名空间qi;
using namespace phx;
使用local_names :: _ a;

number_r_%= int_ |双_;

metric_r_%= metric_p_;

eq_r_ =
(metric_r_>>=>> number_r_)
[_val = phx :: construct< binop< op_eq> (_1,_2)] |
(metric_r_>>!=>> number_r_)
[_val = phx :: construct& unop< op_not> >(phx :: construct< binop< op_eq>(_ 1,_2))]
;

ineq_r_ =
(metric_r_>>>>>>< number_r_)
[_val = phx :: construct< binop< op_gt> (_1,_2)] |
(metric_r_>><>> number_r_)
[_val = phx :: construct< binop< op_lt> (_1,_2)] |
(metric_r_>>> =>> number_r_)
[_val = phx :: construct< binop< op_or> >(
phx :: construct< binop< op_gt>>(_ 1,_2),
phx :: construct< binop< op_eq>(_ 1,_2))
] |
(metric_r_>>< =>> number_r_)
[_val = phx :: construct< binop< op_or> >(
phx :: construct< binop< op_lt>>(_ 1,_2),
phx :: construct< binop< op_eq>(_ 1,_2))
]
;

ineq_2_r_ =
(number_r_>><>> metric_r_>><>> phx :: construct& binop< op_and> >(
phx :: construct< binop< op_gt>>(_ 2,_1),
phx :: construct< binop< op_lt>(_ 2,_3))
] |
(number_r_>>< =>>> metric_r_>><>> number_r_)
[_val = phx :: construct< binop< op_and> >(
phx :: construct< binop< op_or>>(
phx :: construct< binop< op_gt>>(_ 2,_1),
phx :: construct< binop< ; op_eq>(_ 2,_1)
),
phx :: construct< binop< op_lt>(_ 2,_3))
]
(number_r_>><>>" metric_r_>>< =>>>>" number_r_)
[_val = phx :: construct< binop< op_and> >(
phx :: construct< binop< op_gt>>(_ 2,_1),
phx :: construct< binop< op_or>>(
phx :: construct< ; op_eq>(_ 2,_3),
phx :: construct< binop< op_lt>(_ 2,_3))

]
(number_r_>>< =>> metric_r_>>< =>> number_r_)
[_val = phx :: construct& binop< op_and> >(
phx :: construct< binop< op_or>>(
phx :: construct< binop< op_eq>>(_ 2,_1),
phx :: construct< ; op_gt>>(_ 2,_1)
),
phx :: construct< binop< op_or>(
phx :: construct< binop< op_eq> _3),
phx :: construct< binop< op_lt>>(_ 2,_3)


]

expr_ =
eq_r_ [_val = _1] |
ineq_r_ [_val = _1] |
ineq_2_r_ [_val = _1] |
(not>> expr_)[_val = phx :: construct< unop< op_not> >(_ 1)] |
(expr_>>和>> expr_)[_val = phx :: construct< binop< op_and> (_1,_2)] |
(expr_>>或>> expr_)[_val = phx :: construct< binop< op_or> (_1,_2)] |
(expr_>>xor>> expr_)[_val = phx :: construct< binop< op_xor> >(_ 1,_2)];

metric_r_.name(metric r);
eq_r_.name(eq_r_);
ineq_r_.name(ineq_r_);
ineq_2_r_.name(ineq_2_r_);
expr_.name(expr_);
debug(metric_r_);
debug(eq_r_);
debug(ineq_r_);
debug(ineq_2_r_);
debug(expr_);
}

private:
metrics_parser metric_p_;
qi :: rule< It,expr(),Skipper> number_r_;
qi :: rule< It,expr(),Skipper> metric_r_;
qi :: rule< It,expr(),Skipper> eq_r_;
qi :: rule< It,expr(),Skipper> ineq_r_;
qi :: rule< It,expr(),Skipper> ineq_2_r_;
qi :: rule< It,expr(),Skipper> expr_;
};



int main()
{
std :: list< std :: string> lstr;
lstr.emplace_back(A> 5且B <4 xor A> 3.4或2
for(auto i = std :: begin(lstr); i!= std :: end(lstr); ++ i)
{
auto& input = * i;

auto f(std :: begin(input)),l(std :: end(input));
parser< decltype(f)> p;

try
{
expr result;
bool ok = qi :: phrase_parse(f,l,p,qi :: space,result);

if(!ok)
std :: cerr<< invalid input\\\
;
else
std :: cout<< result:<结果< \\\
;

} catch(const qi :: expectation_failure< decltype(f)>& e)
{
std :: cerr< expectation_failure at'< std :: string(e.first,e.last)<< '\\\
;
}

if(f!= l)std :: cerr<< unparsed:'< std :: string(f,l)<< '\\\
;
}

return 0;
}







MM



[¹] 问题从 [精神]用户列表

$

解决方案


  relop_expr = eq_r_ | ineq_r_ | ineq_2_r_; 

expr_ =
(not>> expr_)[_val = phx :: construct< unop< op_not> >(_ 1)] |
(relop_expr>>和>> expr_)[_val = phx :: construct< binop< op_and> (_1,_2)] |
(relop_expr>>或>> expr_)[_val = phx :: construct< binop< op_or> (_1,_2)] |
(relop_expr>>xor>> expr_)[_val = phx :: construct< binop< op_xor> (_1,_2)] |
(relop_expr)[_val = _1]
;

BOOST_SPIRIT_DEBUG_NODES((metric_r _)(eq_r _)(ineq_r _)(ineq_2_r _)(relop_expr)(expr_))

注意:




  • 分支的顺序

  • 一个额外的级别( relop_expr )以诱导优先



3.4 尚未解析, 2 也没有解析)。此外,它是令人难以置信的低效率(可以做左因子分解)。修正这些:

  number_r_ = real_parser< double,strict_real_policies< double>> int_; 

relop_expr = eq_r_ | ineq_2_r_ | ineq_r_;

expr_ =
(not>> expr_)[_val = construct< unop< op_not> (_1)] |
relop_expr [_a = _1]>> (
(和>> expr_ [_val = bin_< op_and>()])|
(or>> expr_ [_val = bin_< op_or> )|
(xor>> expr_ [_val = bin_< op_xor>()])|
(eps [_val = _a])


正如你所看到的,我真的不能满足那些复杂的语义动作。这个的主要原因是BUGS。使代码可读,丢失一半的错误。因此,只需两个简单的帮助,我们可以减少冗长:

 模板< typename标签>使用bin_ = decltype(phx :: construct< binop< Tag>>(qi :: _a,qi :: _ 1)); 
template< typename T1,typename T2>使用tern_ = decltype(phx :: construct< binop< op_and>>(phx :: construct< binop< T1>>(qi :: _ a,qi :: _1),phx :: construct< binop& ;(qi :: 1,qi :: 2)));

正如你所看到的,我不会努力写出特质等等。对



4非常干净的行



  ineq_2_r_ = number_r_ [_a = _1]> (
(<>> metric_r_>><>> number_r_)[_val = tern_< op_lt,op_lt>()] |
>>" metric_r_>>< =>> number_r_)[_val = tern_< op_lt,op_lte>()] |
(< => metric_r_> ;>>>>>>>>>< = >>>< number_r_)[_val = tern_< op_lte,op_lte>()] |

//看,这很容易,我们甚至可以在奖金中 - 与书写锅炉板:)

(>>> metric_r_>>>>>>>>< number_r_)[_val = tern_< op_gt,op_gt> |
(>>> metric_r_>>> =>> number_r_)[_val = tern_< op_gt,op_gte>()] |
(> =>> metric_r_>>>>> number_r_)[_val = tern_< op_gte,op_gt>()] |
(> =>> metric_r_>>> =>> number_r_)[_val = tern_< op_gte,op_gte>()]
);

哦,我刚刚记住:我已经定义了 op_gte op_lte 运算符,因为没有它们导致语义动作的二次增长。我的经验法则是:



  • 规则1 ,避免语义操作

  • 推论#1 :使您的AST直接反映语法。


在这种情况下,你使用解析来混合AST转换。如果你想将AST转换为'expand'lte(a,b)< - (lt(a,b)|| eq(a,b)),你可以在解析。 更新 查看演示的其他答案


$ b $总而言之,我在一个工作计划中附上了建议。它实现更多的功能,并在73行更短(28%)。即使有更多的测试用例:

 'A> 5':结果:(A> 5)
'A< 5':结果:(A <5)
'A> = 5':结果:(A> = 5)
'A <= 5':result: = 5)
'A = 5':结果:(A = 5)
'A!= 5':结果: (A> 3.4)或2 5)和((B <4) ; 3)))))
'A> 5和B <4 xor A!= 3.4或7.9e10> = B> = -42':结果:((A> 5) B <4)xor(A = 3.4)或((7.9e + 10 = B)和(B> = -42))))
/ pre>

好吧,我会显示它在Coliru上,但它似乎下来了。希望你喜欢这个。



完整示例



  //#define BOOST_SPIRIT_DEBUG 
#include< boost / spirit / include / qi.hpp>
#include< boost / spirit / include / phoenix.hpp>
#include< boost / spirit / include / phoenix_operator.hpp>
#include< boost / variant / recursive_wrapper.hpp>
#include< cstdint>

命名空间qi = boost :: spirit :: qi;
namespace phx = boost :: phoenix;

///终端
枚举metric_t:std :: uint8_t {A = 0u,B};
const std :: string metric_names [] = {A,B};

struct metrics_parser:boost :: spirit :: qi :: symbols< char,metric_t> {
metrics_parser(){
this-> add(metric_names [A],A)
(metric_names [B],B);
}
};

/// Operators
template< typename tag> struct unop;
template< typename tag> struct binop;

///表达式
typedef boost :: variant<
int,
double,
metric_t,
boost :: recursive_wrapper< unop< struct op_not> >,
boost :: recursive_wrapper< binop< struct op_and> >,
boost :: recursive_wrapper< binop< struct op_or> >,
boost :: recursive_wrapper< binop< struct op_xor> >,
boost :: recursive_wrapper< binop< struct op_eq> >,
boost :: recursive_wrapper< binop< struct op_lt> >,
boost :: recursive_wrapper< Binop< struct op_gt> >,
boost :: recursive_wrapper< binop< struct op_lte> >,
boost :: recursive_wrapper< binop< struct op_gte> >
> expr;

template< typename tag>
struct binop {
explicit binop(const expr& l,const expr& r):oper1(1),oper2(r){}
expr oper1,oper2;
};

template< typename tag>
struct unop {
explicit unop(const expr& o):oper1(o){}
expr oper1;
};

std :: ostream& operator<<<(std :: ostream& os,metric_t m)
{return os< metric_names [m]; }

结构打印机:boost :: static_visitor< void>
{
printer(std :: ostream& os):_os(os){}
std :: ostream& _os;

void operator()(const binop< op_and>& b)const {print(and,b.oper1,b.oper2); }
void operator()(const binop< op_or>& b)const {print(or,b.oper1,b.oper2); }
void operator()(const binop< op_xor>& b)const {print(xor,b.oper1,b.oper2); }
void operator()(const binop< op_eq&& b)const {print(=,b.oper1,b.oper2); }
void operator()(const binop< op_lt>& b)const {print(<,b.oper1,b.oper2); }
void operator()(const binop< op_gt>& b)const {print(>,b.oper1,b.oper2); }
void operator()(const binop< op_lte>& b)const {print(< =,b.oper1,b.oper2); }
void operator()(const binop< op_gte>& b)const {print(> =,b.oper1,b.oper2); }

void print(const std :: string& op,const expr& l,const expr& r)const {
_os& (;
boost :: apply_visitor(* this,l); _os<< op; boost :: apply_visitor(* this,r);
_os<
}

void operator()(const unop< op_not>& u)const {
_os< !; boost :: apply_visitor(* this,u.oper1);
}

template< typename other_t> void operator()(other_t i)const {
_os<<一世;
}
};

std :: ostream& operator<<<(std :: ostream& os,const expr& e)
{boost :: apply_visitor(printer(os),e); return os; }

template< typename It,typename Skipper = qi :: space_type>
struct parser:qi :: grammar< It,expr(),Skipper,qi :: locals< expr> >
{
template< typename Tag>使用bin_ = decltype(phx :: construct< binop< Tag>>(qi :: _a,qi :: _ 1));
template< typename T1,typename T2>使用tern_ = decltype(phx :: construct< binop< op_and>>(phx :: construct< binop< T1>>(qi :: _ a,qi :: _1),phx :: construct< binop& ;(qi :: 1,qi :: 2)));

parser():parser :: base_type(expr_)
{
使用命名空间qi;
using namespace phx;

number_r_ = real_parser< double,strict_real_policies< double>>()| int_;

metric_r_ = metric_p_;

eq_r_ = metric_r_ [_a = _1]>> (
(=>> number_r_)[_val = bin_< op_eq>()] |
(!=>> number_r_)[_val = construct< unop< op_not> >(bin_< op_eq>())]
);
ineq_2_r_ = number_r_ [_a = _1]>> (
(<>> metric_r_>><>> number_r_)[_val = tern_< op_lt,op_lt>()] |
>>" metric_r_>>< =>> number_r_)[_val = tern_< op_lt,op_lte>()] |
(< => metric_r_> ;>>>>>>>>>< = >>>>< number_r_)[_val = tern_< op_lte,op_lte>()] |
(>>> metric_r_>>& tern_< op_gt,op_gt>()] |
(>>>>>>>>>>< number_r_)[_val = tern_< op_gt,op_gte> ] |
(> =>> metric_r_>>>>>< number_r_)[_val = tern_< op_gte,op_gt>()] |
> =>> metric_r_>> =>>>< number_r_)[_val = tern_< op_gte,op_gte>()]
);
ineq_r_ = metric_r_ [_a = _1]>> (
(>>> number_r_)[_val = bin_< op_gt>()] |
(<> number_r_)[_val = bin_< op_lt> ;()] |
(> =>> number_r_)[_val = bin_< op_gte>()] |
(< => number_r_) = bin_< op_lte>()]
);

relop_expr = eq_r_ | ineq_2_r_ | ineq_r_;

expr_ =
(not>> expr_)[_val = construct< unop< op_not> (_1)] |
relop_expr [_a = _1]>>> (
(和>> expr_ [_val = bin_< op_and>()])|
(or>> expr_ [_val = bin_< op_or> )|
(xor> expr_ [_val = bin_< op_xor>()])|
(eps [_val = _a])
);

BOOST_SPIRIT_DEBUG_NODES((metric_r _)(eq_r _)(ineq_r _)(ineq_2_r _)(relop_expr)(expr_))
}
private:
qi :: rule< It ,expr(),Skipper,qi :: locals< expr> > eq_r_,ineq_r_,ineq_2_r_,relop_expr,expr_;
qi :: rule< It,expr(),Skipper> number_r_,metric_r_;
metrics_parser metric_p_;
};

int main()
{
for(std :: string const& input:{
A> 5,
A< ; 5,
A> = 5,
A <= 5,
A = 5,
A!= 5 b $ bA> 5且B <4或A> 3.4或2
A> 5且B <4或A 0 = 3.4或7.9e 10> = B> -42
})
{
auto f(std :: begin(input)),l(std :: end(input));
parser< decltype(f)> p;

try
{
std :: cout< '<输入<< ':\t;
expr result;
bool ok = qi :: phrase_parse(f,l,p,qi :: space,result);

if(!ok)std :: cout<< invalid input\\\
;
else std :: cout<< result:<结果< \\\
;

} catch(const qi :: expectation_failure< decltype(f)>& e)
{
std :: cout< expectation_failure at'< std :: string(e.first,e.last)<< '\\\
;
}

if(f!= l)std :: cout<< unparsed:'< std :: string(f,l)<< '\\\
;
}
}


Hello [¹]

I have a simple parser (see below).

It intends to parse conditional expressions (relational arithmetic operations and logic combinations thereof).

In the example given there, it parses successfully A>5 but then stops and ignores the rest of the input, and this is consistent with my impl.

How do I change the expr_ rule to make it parse the entire input?

#include <cstdint>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/variant/recursive_wrapper.hpp>

namespace qi    = boost::spirit::qi;
namespace phx   = boost::phoenix;

/// Terminals
enum metric_t : std::uint8_t { A=0u, B };
const std::string metric_names[] = { "A", "B" };
struct metrics_parser : boost::spirit::qi::symbols<char, metric_t>
{
metrics_parser()
{
    this->add
    ( metric_names[A], A )
    ( metric_names[B], B )
    ;
}
};


/// Operators
struct op_or  {};
struct op_and {};
struct op_xor {};
struct op_not {};

struct op_eq {};
struct op_lt {};
struct op_let {};
struct op_gt {};
struct op_get {};

template <typename tag> struct unop;
template <typename tag> struct binop;

/// Expression
typedef boost::variant<
int,
double,
metric_t,
boost::recursive_wrapper< unop<op_not> >,
boost::recursive_wrapper< binop<op_and> >,
boost::recursive_wrapper< binop<op_or> >,
boost::recursive_wrapper< binop<op_xor> >,
boost::recursive_wrapper< binop<op_eq> >,
boost::recursive_wrapper< binop<op_lt> >,
boost::recursive_wrapper< binop<op_gt> >
> expr;

template <typename tag>
struct binop 
{ 
    explicit binop(const expr& l, const expr& r) : oper1(l), oper2(r) { }
    expr oper1, oper2; 
};

template <typename tag>
struct unop  
{ 
    explicit unop(const expr& o) : oper1(o) { }
    expr oper1; 
};

struct printer : boost::static_visitor<void>
{
    printer(std::ostream& os) : _os(os) {}
    std::ostream& _os;

    void operator()(const binop<op_and>& b) const { print(" and ", b.oper1, b.oper2); }
    void operator()(const binop<op_or >& b) const { print(" or ",  b.oper1, b.oper2);  }
    void operator()(const binop<op_xor>& b) const { print(" xor ", b.oper1, b.oper2); }
    void operator()(const binop<op_eq>& b) const  { print(" = ",   b.oper1, b.oper2);   }
    void operator()(const binop<op_lt>& b) const  { print(" < ",   b.oper1, b.oper2);   }
    void operator()(const binop<op_gt>& b) const  { print(" > ",   b.oper1, b.oper2);   }

    void print(const std::string& op, const expr& l, const expr& r) const
    {
        _os << "(";
            boost::apply_visitor(*this, l);
            _os << op;
            boost::apply_visitor(*this, r);
        _os << ")";
    }

    void operator()(const unop<op_not>& u) const
    {
        _os << "(";
            _os << "!";
            boost::apply_visitor(*this, u.oper1);
        _os << ")";
    }
    void operator()(metric_t m) const
    {
    _os << metric_names[m];
    }

    template <typename other_t>
    void operator()(other_t i) const
    {
    _os << i;
    }
};

std::ostream& operator<<(std::ostream& os, const expr& e)
{ boost::apply_visitor(printer(os), e); return os; }

std::ostream& operator<<(std::ostream& os, metric_t m)
{ os<< metric_names[m]; return os; }

template <typename It, typename Skipper = qi::space_type>
struct parser : qi::grammar<It, expr(), Skipper>
{
    parser() : parser::base_type(expr_)
    {
        using namespace qi;
        using namespace phx;
        using local_names::_a;

        number_r_ %= int_ | double_;

        metric_r_ %= metric_p_;

        eq_r_ =
        (metric_r_ >> "=" >> number_r_)  
            [ _val = phx::construct< binop<op_eq> >(_1,_2) ] |
        (metric_r_ >> "!=" >> number_r_) 
            [ _val = phx::construct< unop<op_not> >( phx::construct< binop<op_eq> >(_1,_2) ) ]
        ;

        ineq_r_ =
        (metric_r_ >> ">" >> number_r_)  
            [ _val = phx::construct< binop<op_gt> >(_1,_2) ] |
        (metric_r_ >> "<" >> number_r_)  
            [ _val = phx::construct< binop<op_lt> >(_1,_2) ] |
        (metric_r_ >> ">=" >> number_r_) 
            [ _val = phx::construct< binop<op_or> >( 
            phx::construct< binop<op_gt> >(_1,_2),
            phx::construct< binop<op_eq> >(_1,_2) ) 
            ] |
        (metric_r_ >> "<=" >> number_r_) 
            [ _val = phx::construct< binop<op_or> >( 
            phx::construct< binop<op_lt> >(_1,_2), 
            phx::construct< binop<op_eq> >(_1,_2) )
            ]
        ;

        ineq_2_r_ = 
        (number_r_ >> "<" >> metric_r_ >> "<" >> number_r_)
            [ _val = phx::construct< binop<op_and> >(
            phx::construct< binop<op_gt> >(_2,_1), 
            phx::construct< binop<op_lt> >(_2,_3) ) 
            ] |
        (number_r_ >> "<=" >> metric_r_ >> "<" >> number_r_)
            [ _val = phx::construct< binop<op_and> >(
            phx::construct< binop<op_or> >(
                phx::construct< binop<op_gt> >(_2,_1),
                phx::construct< binop<op_eq> >(_2,_1)
            ),
            phx::construct< binop<op_lt> >(_2,_3) )
            ] |
        (number_r_ >> "<" >> metric_r_ >> "<=" >> number_r_)
            [ _val = phx::construct< binop<op_and> >(
            phx::construct< binop<op_gt> >(_2,_1),
            phx::construct< binop<op_or> >(                
                phx::construct< binop<op_eq> >(_2,_3),
                phx::construct< binop<op_lt> >(_2,_3) )
            )
            ] |
        (number_r_ >> "<=" >> metric_r_ >> "<=" >> number_r_)
            [ _val = phx::construct< binop<op_and> >(
            phx::construct< binop<op_or> >(
                phx::construct< binop<op_eq> >(_2,_1),
                phx::construct< binop<op_gt> >(_2,_1) 
            ),
            phx::construct< binop<op_or> >(                
                phx::construct< binop<op_eq> >(_2,_3),
                phx::construct< binop<op_lt> >(_2,_3)
            )
            )
            ]
        ;

        expr_  = 
        eq_r_                     [ _val = _1 ]                                     |
        ineq_r_                   [ _val = _1 ]                                     |
        ineq_2_r_                 [ _val = _1 ]                                     |
        ("not" >> expr_)          [ _val = phx::construct< unop<op_not> >(_1) ]     |
        (expr_ >> "and" >> expr_) [ _val = phx::construct< binop<op_and> >(_1,_2) ] |
        (expr_ >> "or" >> expr_)  [ _val = phx::construct< binop<op_or>  >(_1,_2) ] |
        (expr_ >> "xor" >> expr_) [ _val = phx::construct< binop<op_xor> >(_1,_2) ];

        metric_r_.name("metric r");
        eq_r_.name("eq_r_");
        ineq_r_.name("ineq_r_");
        ineq_2_r_.name("ineq_2_r_");
        expr_.name("expr_");
        debug(metric_r_);
        debug(eq_r_);
        debug(ineq_r_);
        debug(ineq_2_r_);
        debug(expr_);
    }

private:
    metrics_parser                metric_p_;
    qi::rule<It, expr(), Skipper> number_r_;
    qi::rule<It, expr(), Skipper> metric_r_;
    qi::rule<It, expr(), Skipper> eq_r_;
    qi::rule<It, expr(), Skipper> ineq_r_;
    qi::rule<It, expr(), Skipper> ineq_2_r_;
    qi::rule<It, expr(), Skipper> expr_;
};



int main()
{
    std::list<std::string> lstr;
    lstr.emplace_back("A>5 and B<4 xor A>3.4 or 2<A<3");

    for (auto i=std::begin(lstr); i!=std::end(lstr); ++i)
    {
        auto& input = *i;

        auto f(std::begin(input)), l(std::end(input));
        parser<decltype(f)> p;

        try
        {
            expr result;
            bool ok = qi::phrase_parse(f,l,p,qi::space,result);

            if (!ok)
                std::cerr << "invalid input\n";
            else
                std::cout << "result: " << result << "\n";

        } catch (const qi::expectation_failure<decltype(f)>& e)
        {
            std::cerr << "expectation_failure at '" << std::string(e.first, e.last) << "'\n";
        }

        if (f!=l) std::cerr << "unparsed: '" << std::string(f,l) << "'\n";
    }

    return 0;
}


Thanks, MM

[¹] question teleported from the [spirit-general] user list

解决方案

Sticking with simple:

relop_expr = eq_r_ | ineq_r_ | ineq_2_r_;

expr_  =
  ("not" >> expr_)               [ _val = phx::construct< unop<op_not> >(_1) ]     |
  (relop_expr >> "and" >> expr_) [ _val = phx::construct< binop<op_and> >(_1,_2) ] |
  (relop_expr >> "or" >> expr_)  [ _val = phx::construct< binop<op_or>  >(_1,_2) ] |
  (relop_expr >> "xor" >> expr_) [ _val = phx::construct< binop<op_xor> >(_1,_2) ] |
  (relop_expr                  ) [ _val = _1 ]
  ;

 BOOST_SPIRIT_DEBUG_NODES((metric_r_)(eq_r_)(ineq_r_)(ineq_2_r_)(relop_expr)(expr_))

Note:

  • the ordering of branches
  • the use of an extra "level" (relop_expr) to induce precedence

There's still work to do (3.4 did not parse yet, and neither did 2<A<3). Also, it's excruciatingly inefficient (could do with left factorization). Fixing those:

number_r_ = real_parser<double,strict_real_policies<double>>() | int_;

relop_expr = eq_r_ | ineq_2_r_ | ineq_r_;

expr_  =
  ("not" >> expr_)       [ _val = construct<unop<op_not>> (_1) ] |
  relop_expr [_a = _1] >> (
         ("and" >> expr_ [ _val = bin_<op_and>() ]) |
         ("or"  >> expr_ [ _val = bin_<op_or >() ]) |
         ("xor" >> expr_ [ _val = bin_<op_xor>() ]) |
         (eps            [ _val = _a ])
    )
  ;

As you can see, I can't really stand those complicated semantic actions. The chief reason for this is BUGS. Make the code readable, lose half the bugs. So, with just a two simple helpers we can reduce the verbosity:

template <typename Tag>             using bin_  = decltype(phx::construct<binop<Tag>>(qi::_a, qi::_1));
template <typename T1, typename T2> using tern_ = decltype(phx::construct<binop<op_and>>(phx::construct<binop<T1>>(qi::_a, qi::_1), phx::construct<binop<T2>>(qi::_1, qi::_2)));

As you can see, I don't make a great effort to write traits etc. Just a quick decltype on whatever you'd write anyways, and, bam

down from 35 crufty lines to 4 very clean lines:

ineq_2_r_ = number_r_ [ _a = _1 ] >> (
     ("<"  >> metric_r_ >> "<"  >> number_r_) [_val = tern_<op_lt , op_lt>()  ] |
     ("<"  >> metric_r_ >> "<=" >> number_r_) [_val = tern_<op_lt , op_lte>() ] |
     ("<=" >> metric_r_ >> "<"  >> number_r_) [_val = tern_<op_lte, op_lt>()  ] |
     ("<=" >> metric_r_ >> "<=" >> number_r_) [_val = tern_<op_lte, op_lte>() ] |

// see, that's so easy, we can even trow in the bonus - I bet you were just fed up with writing boiler plate :)

     (">"  >> metric_r_ >> ">"  >> number_r_) [_val = tern_<op_gt , op_gt>()  ] |
     (">"  >> metric_r_ >> ">=" >> number_r_) [_val = tern_<op_gt , op_gte>() ] |
     (">=" >> metric_r_ >> ">"  >> number_r_) [_val = tern_<op_gte, op_gt>()  ] |
     (">=" >> metric_r_ >> ">=" >> number_r_) [_val = tern_<op_gte, op_gte>() ]
 );

Oh, I just remembered: I have defined the op_gte and op_lte operators, since not having them was causing quadratic growth of your semantic actions. My fast rule of thumb is:

  • Rule #1: keep rules simple, avoid semantic actions
  • Corollary #1: make your AST directly reflect the grammar.

In this case, you were conflating AST transformation with parsing. If you want to transform the AST to 'expand' lte (a,b) <- (lt(a,b) || eq(a,b)), you can trivially do that after parsing. Update see the other answer for a demo

All in all, I have attached the suggestions in a working program. It implements many more features, and comes in 73 lines shorter (28%). That's even with more test cases:

'A  >  5':    result: (A > 5)
'A  <  5':    result: (A < 5)
'A  >= 5':    result: (A >= 5)
'A  <= 5':    result: (A <= 5)
'A   = 5':    result: (A = 5)
'A  != 5':    result: !(A = 5)
'A>5 and B<4 xor A>3.4 or 2<A<3':    result: ((A > 5) and ((B < 4) xor ((A > 3.4) or ((2 < A) and (A < 3)))))
'A>5 and B<4 xor A!=3.4 or 7.9e10 >= B >= -42':    result: ((A > 5) and ((B < 4) xor (!(A = 3.4) or ((7.9e+10 >= B) and (B >= -42)))))

Well, I'd have shown it live on Coliru, but it seems down at the moment. Hope you like this.

Full sample

//#define BOOST_SPIRIT_DEBUG
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/variant/recursive_wrapper.hpp>
#include <cstdint>

namespace qi    = boost::spirit::qi;
namespace phx   = boost::phoenix;

/// Terminals
enum metric_t : std::uint8_t { A=0u, B };
const std::string metric_names[] = { "A", "B" };

struct metrics_parser : boost::spirit::qi::symbols<char, metric_t> {
    metrics_parser() {
        this->add(metric_names[A], A)
                 (metric_names[B], B);
    }
};

/// Operators
template <typename tag> struct unop;
template <typename tag> struct binop;

/// Expression
typedef boost::variant<
  int,
  double,
  metric_t,
  boost::recursive_wrapper< unop< struct op_not> >,
  boost::recursive_wrapper< binop<struct op_and> >,
  boost::recursive_wrapper< binop<struct op_or> >,
  boost::recursive_wrapper< binop<struct op_xor> >,
  boost::recursive_wrapper< binop<struct op_eq> >,
  boost::recursive_wrapper< binop<struct op_lt> >,
  boost::recursive_wrapper< binop<struct op_gt> >,
  boost::recursive_wrapper< binop<struct op_lte> >,
  boost::recursive_wrapper< binop<struct op_gte> >
> expr;

template <typename tag>
struct binop { 
    explicit binop(const expr& l, const expr& r) : oper1(l), oper2(r) { }
    expr oper1, oper2; 
};

template <typename tag>
struct unop  { 
    explicit unop(const expr& o) : oper1(o) { }
    expr oper1; 
};

std::ostream& operator<<(std::ostream& os, metric_t m)
{ return os << metric_names[m]; }

struct printer : boost::static_visitor<void>
{
    printer(std::ostream& os) : _os(os) {}
    std::ostream& _os;

    void operator()(const binop<op_and>& b) const { print(" and ", b.oper1, b.oper2); }
    void operator()(const binop<op_or >& b) const { print(" or ",  b.oper1, b.oper2); }
    void operator()(const binop<op_xor>& b) const { print(" xor ", b.oper1, b.oper2); }
    void operator()(const binop<op_eq >& b) const { print(" = ",   b.oper1, b.oper2); }
    void operator()(const binop<op_lt >& b) const { print(" < ",   b.oper1, b.oper2); }
    void operator()(const binop<op_gt >& b) const { print(" > ",   b.oper1, b.oper2); }
    void operator()(const binop<op_lte>& b) const { print(" <= ",  b.oper1, b.oper2); }
    void operator()(const binop<op_gte>& b) const { print(" >= ",  b.oper1, b.oper2); }

    void print(const std::string& op, const expr& l, const expr& r) const {
        _os << "(";
        boost::apply_visitor(*this, l); _os << op; boost::apply_visitor(*this, r);
        _os << ")";
    }

    void operator()(const unop<op_not>& u) const {
        _os << "!"; boost::apply_visitor(*this, u.oper1);
    }

    template <typename other_t> void operator()(other_t i) const { 
        _os << i; 
    }
};

std::ostream& operator<<(std::ostream& os, const expr& e)
{ boost::apply_visitor(printer(os), e); return os; }

template <typename It, typename Skipper = qi::space_type >
struct parser : qi::grammar<It, expr(), Skipper, qi::locals<expr> >
{
    template <typename Tag>             using bin_  = decltype(phx::construct<binop<Tag>>(qi::_a, qi::_1));
    template <typename T1, typename T2> using tern_ = decltype(phx::construct<binop<op_and>>(phx::construct<binop<T1>>(qi::_a, qi::_1), phx::construct<binop<T2>>(qi::_1, qi::_2)));

    parser() : parser::base_type(expr_)
    {
        using namespace qi;
        using namespace phx;

        number_r_ = real_parser<double,strict_real_policies<double>>() | int_;

        metric_r_ = metric_p_;

        eq_r_ = metric_r_ [ _a = _1 ] >> (
                ("="  >> number_r_) [ _val = bin_<op_eq>() ] |
                ("!=" >> number_r_) [ _val = construct<unop<op_not>>(bin_<op_eq>()) ]
            );
        ineq_2_r_ = number_r_ [ _a = _1 ] >> (
                ("<"  >> metric_r_ >> "<"  >> number_r_) [_val = tern_<op_lt , op_lt>()  ] |
                ("<"  >> metric_r_ >> "<=" >> number_r_) [_val = tern_<op_lt , op_lte>() ] |
                ("<=" >> metric_r_ >> "<"  >> number_r_) [_val = tern_<op_lte, op_lt>()  ] |
                ("<=" >> metric_r_ >> "<=" >> number_r_) [_val = tern_<op_lte, op_lte>() ] |
                (">"  >> metric_r_ >> ">"  >> number_r_) [_val = tern_<op_gt , op_gt>()  ] |
                (">"  >> metric_r_ >> ">=" >> number_r_) [_val = tern_<op_gt , op_gte>() ] |
                (">=" >> metric_r_ >> ">"  >> number_r_) [_val = tern_<op_gte, op_gt>()  ] |
                (">=" >> metric_r_ >> ">=" >> number_r_) [_val = tern_<op_gte, op_gte>() ]
            );
        ineq_r_ = metric_r_ [ _a = _1 ] >> (
                (">" >> number_r_)  [ _val = bin_<op_gt >() ] |
                ("<" >> number_r_)  [ _val = bin_<op_lt >() ] |
                (">=" >> number_r_) [ _val = bin_<op_gte>() ] |
                ("<=" >> number_r_) [ _val = bin_<op_lte>() ]
            );

        relop_expr = eq_r_ | ineq_2_r_ | ineq_r_;

        expr_  = 
            ("not" >> expr_)       [ _val = construct<unop<op_not>> (_1) ] |
            relop_expr [_a = _1] >> (
                 ("and" >> expr_ [ _val = bin_<op_and>() ]) |
                 ("or"  >> expr_ [ _val = bin_<op_or >() ]) |
                 ("xor" >> expr_ [ _val = bin_<op_xor>() ]) |
                 (eps            [ _val = _a ])
            );

        BOOST_SPIRIT_DEBUG_NODES((metric_r_)(eq_r_)(ineq_r_)(ineq_2_r_)(relop_expr)(expr_))
    }
  private:
    qi::rule<It, expr(), Skipper, qi::locals<expr> > eq_r_, ineq_r_, ineq_2_r_, relop_expr, expr_;
    qi::rule<It, expr(), Skipper>                    number_r_, metric_r_;
    metrics_parser                                   metric_p_;
};

int main()
{
    for (std::string const& input : { 
        "A  >  5",
        "A  <  5",
        "A  >= 5",
        "A  <= 5",
        "A   = 5",
        "A  != 5",
        "A>5 and B<4 xor A>3.4 or 2<A<3",
        "A>5 and B<4 xor A!=3.4 or 7.9e10 >= B >= -42"
    })
    {
        auto f(std::begin(input)), l(std::end(input));
        parser<decltype(f)> p;

        try
        {
            std::cout << "'" << input << "':\t";
            expr result;
            bool ok = qi::phrase_parse(f,l,p,qi::space,result);

            if (!ok) std::cout << "invalid input\n";
            else     std::cout << "result: " << result << "\n";

        } catch (const qi::expectation_failure<decltype(f)>& e)
        {
            std::cout << "expectation_failure at '" << std::string(e.first, e.last) << "'\n";
        }

        if (f!=l) std::cout << "unparsed: '" << std::string(f,l) << "'\n";
    }
}

这篇关于规则定义中的AST和运算符优先级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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