C++ 规则定义中的AST和运算符优先级
你好[imk_] 我有一个简单的解析器(见下文) 它旨在解析条件表达式(关系算术运算及其逻辑组合) 在这里给出的示例中,它成功地解析了>5,但随后停止并忽略了其余的输入,这与我的impl是一致的 如何更改C++ 规则定义中的AST和运算符优先级,c++,parsing,grammar,boost-spirit,boost-spirit-qi,C++,Parsing,Grammar,Boost Spirit,Boost Spirit Qi,你好[imk_] 我有一个简单的解析器(见下文) 它旨在解析条件表达式(关系算术运算及其逻辑组合) 在这里给出的示例中,它成功地解析了>5,但随后停止并忽略了其余的输入,这与我的impl是一致的 如何更改expr\uu规则以使其解析整个输入 #include <cstdint> #include <boost/spirit/include/qi.hpp> #include <boost/spirit/include/phoenix.hpp> #include
expr\uu
规则以使其解析整个输入
#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;
}
#包括
#包括
#包括
#包括
#包括
名称空间qi=boost::spirit::qi;
名称空间phx=boost::phoenix;
///终端
枚举度量:std::uint8{A=0u,B};
const std::string metric_names[]={“A”,“B”};
结构度量\u解析器:boost::spirit::qi::symbols
{
度量_解析器()
{
此->添加
(度量单位名称[A],A)
(度量单位名称[B],B)
;
}
};
///操作员
结构op_或{};
结构op_和{};
结构op_xor{};
结构op_not{};
结构op_eq{};
结构op_lt{};
结构op_let{};
结构op_gt{};
结构op_get{};
模板结构unop;
模板结构binop;
///表情
typedef boost::variant<
int,
双重的
公制单位,
boost::递归_包装器,
boost::递归_包装器,
boost::递归_包装器,
boost::递归_包装器,
boost::递归_包装器,
boost::递归_包装器,
boost::递归_包装器
>expr;
模板
结构二进制
{
显式binop(const-expr&l,const-expr&r):oper1(l),oper2(r){}
expr oper1、oper2;
};
模板
结构unop
{
显式unop(const expr&o):oper1(o){}
expr-oper1;
};
结构打印机:boost::static\u访问者
{
打印机(std::ostream&os):\u os(os){}
std::ostream和os;
void操作符()(const binop&b)const{print(“and”,b.oper1,b.oper2);}
void操作符()(const binop&b)const{print(“or”,b.oper1,b.oper2);}
void操作符()(const binop&b)const{print(“xor”,b.oper1,b.oper2);}
void操作符()(const binop&b)const{print(“=”,b.oper1,b.oper2);}
void操作符()(const binop&b)const{print(“<”,b.oper1,b.oper2);}
void操作符()(const binop&b)const{print(“>”,b.oper1,b.oper2);}
无效打印(常量标准::字符串与运算、常量表达式与l、常量表达式与r)常量
{
_操作系统“>”>>编号(r)
[\u val=phx::construct(\u 1,\u 2)]|
(公制“=”>>数字)
[\u val=phx::construct(
phx::构造(_1,_2),
phx::构造(_1,_2))
] |
(公制)5和B3.4或2
正如您所见,我实在无法忍受这些复杂的语义操作。造成这种情况的主要原因是错误。让代码可读,减少一半的错误。因此,只需两个简单的助手,我们就可以减少冗长:
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)));
好吧,我本想在科里鲁现场直播的,但现在看起来好像不太好。希望你喜欢
全样本
/#定义BOOST_SPIRIT_调试
#包括
#包括
#包括
#包括
#包括
名称空间qi=boost::spirit::qi;
名称空间phx=boost::phoenix;
///终端
枚举度量:std::uint8{A=0u,B};
const std::string metric_names[]={“A”,“B”};
结构度量\u解析器:boost::spirit::qi::symbols{
度量_解析器(){
此->添加(度量单位名称[A],A)
(度量单位名称[B],B);
}
};
///操作员
模板结构unop;
模板结构binop;
///表情
typedef boost::variant<
int,
双重的
公制单位,
boost::recursive_wrapper>,
boost::递归_包装器,
boost::递归_包装器,
boost::递归_包装器,
boost::递归_包装器,
boost::递归_包装器,
boost::递归_包装器,
boost::递归_包装器,
boost::递归_包装器
>expr;
模板
结构binop{
显式binop(const-expr&l,const-expr&r):oper1(l),oper2(r){}
expr oper1、oper2;
};
模板
结构unop{
显式unop(const expr&o):oper1(o){}
expr-oper1;
};
std::ostream和操作员(
(“>>度量值”>“>”>>数字”[\u val=tern\u()|
(“>”>>度量值“>=”>>数字)[[u val=tern]|
(“>=”>>度量值“>”>“>>数字”[\u val=tern\uuu()]|
(“>=”>>度量值”>“>=”>>数字”[\u val=tern\uuu()]
);
ineq\u r\u=metric\u r\u[\u a=\u 1]>>(
(“>”>>数字)[[u val=bin]|
(“=”>>number\u r\uu)[\u val=bin\uu()]|
(" 5",
“A<5”,
“A>=5”,
“A 5和B3.4或2=-42”
})
{
自动f(标准::开始(输入)),l(标准::结束(输入));
语法分析器p;
尝试
{
std::cout来自另一个答案:
- 规则#1:保持规则简单,避免语义操作
- 推论1:让你的AST直接反映语法
在本例中,您将AST转换与解析混为一谈。如果您想将AST转换为“扩展”lte(a,b)模式。 .你用“简单”这个词…我不知道我该怎么想。让记录显示我引用的链接用户列表帖子是“MM”(@MikeM?)我明白了。你在试探性地投票赞成一个非常有思想性的答案,我们说,C++的特殊角落。@塞赫不是我,另一个MM……如果你想进入语法,操作员优先权(在我的脑海里),那么您应该使用这种方法。无论是使用括号还是不使用括号,无论哪种方式,您的语法都是正确的=)。
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 ])
)
;
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)));
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>() ]
);
'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)))))
//#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";
}
}
struct expander : boost::static_visitor<expr>
{
expr operator()(binop<op_lte> const& e) const {
expr oper1(recurse(e.oper1)), oper2(recurse(e.oper2));
return binop<op_or>(
binop<op_lt>(oper1, oper2),
binop<op_eq>(oper1, oper2));
}
expr operator()(binop<op_gte> const& e) const {
expr oper1(recurse(e.oper1)), oper2(recurse(e.oper2));
return binop<op_or>(
binop<op_gt>(oper1, oper2),
binop<op_eq>(oper1, oper2));
}
// recurse compound nodes
template <typename Tag> expr operator()(unop<Tag> const& e) const { return unop<Tag>(recurse(e.oper1)); }
template <typename Tag> expr operator()(binop<Tag> const& e) const { return binop<Tag>(recurse(e.oper1), recurse(e.oper2)); }
// copy leaf nodes
template <typename T> expr operator()(T const& e) const { return e; }
private:
expr recurse(expr const& e) const { return boost::apply_visitor(*this, e); };
};
expr expand(expr const& e) {
return boost::apply_visitor(expander(), e);
}
input: A > 5
result: (A > 5)
expanded: (A > 5)
input: A < 5
result: (A < 5)
expanded: (A < 5)
input: A >= 5
result: (A >= 5)
expanded: ((A > 5) or (A = 5))
input: A <= 5
result: (A <= 5)
expanded: ((A < 5) or (A = 5))
input: A = 5
result: (A = 5)
expanded: (A = 5)
input: A != 5
result: !(A = 5)
expanded: !(A = 5)
input: 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)))))
expanded: ((A > 5) and ((B < 4) xor ((A > 3.4) or ((2 < A) and (A < 3)))))
input: 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)))))
expanded: ((A > 5) and ((B < 4) xor (!(A = 3.4) or (((7.9e+10 > B) or (7.9e+10 = B)) and ((B > -42) or (B = -42))))))
//#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 unop {
explicit unop(const expr& o) : oper1(o) { }
expr oper1;
};
template <typename tag>
struct binop {
explicit binop(const expr& l, const expr& r) : oper1(l), oper2(r) { }
expr oper1, oper2;
};
std::ostream& operator<<(std::ostream& os, metric_t m)
{ return os << metric_names[m]; }
struct expander : boost::static_visitor<expr>
{
expr operator()(binop<op_lte> const& e) const {
expr oper1(recurse(e.oper1)), oper2(recurse(e.oper2));
return binop<op_or>(
binop<op_lt>(oper1, oper2),
binop<op_eq>(oper1, oper2));
}
expr operator()(binop<op_gte> const& e) const {
expr oper1(recurse(e.oper1)), oper2(recurse(e.oper2));
return binop<op_or>(
binop<op_gt>(oper1, oper2),
binop<op_eq>(oper1, oper2));
}
// recurse compound nodes
template <typename Tag> expr operator()(unop<Tag> const& e) const { return unop<Tag>(recurse(e.oper1)); }
template <typename Tag> expr operator()(binop<Tag> const& e) const { return binop<Tag>(recurse(e.oper1), recurse(e.oper2)); }
// copy leaf nodes
template <typename T> expr operator()(T const& e) const { return e; }
private:
expr recurse(expr const& e) const { return boost::apply_visitor(*this, e); };
};
expr expand(expr const& e) {
return boost::apply_visitor(expander(), e);
}
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: " << input << "\n";
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";
std::cout << "expanded: " << expand(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";
}
}