lur*_*her 5 c++ parsing boost-spirit-qi
我试图理解以下结果.测试用例代码是
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_fusion.hpp>
#include <boost/spirit/include/phoenix_stl.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/variant/recursive_variant.hpp>
#include <boost/spirit/home/support/context.hpp>
#include <boost/spirit/home/phoenix.hpp>
#include <boost/foreach.hpp>
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <vector>
namespace sp = boost::spirit;
namespace qi = boost::spirit::qi;
using namespace boost::spirit::ascii;
namespace fusion = boost::fusion;
namespace phoenix = boost::phoenix;
using phoenix::at_c;
using phoenix::push_back;
using phoenix::bind;
template <typename P>
void test_parser(
char const* input, P const& p, bool full_match = true)
{
using boost::spirit::qi::parse;
char const* f(input);
char const* l(f + strlen(f));
if (parse(f, l, p) && (!full_match || (f == l)))
std::cout << "ok" << std::endl;
else
std::cout << "fail" << std::endl;
}
int main() {
test_parser("+12345", qi::int_ ); //Ok
test_parser("+12345", qi::double_ - qi::int_ ); //failed, as expected
test_parser("+12345.34", qi::int_ ); // failed, as expected
test_parser("+12345.34", qi::double_ - qi::int_ ); //failed but it should be Ok!
};
Run Code Online (Sandbox Code Playgroud)
这里的动机是我希望将数字'12345'匹配为整数而不是匹配浮点数.'12345.34'将匹配double_并且永远不会是int_但是倒数的情况不是真的; '12345'匹配整数(int_)和浮点(double_).我尝试了double_ - int_并且它成功地无法匹配'12345'.但是我的希望是最后一个测试用例'12345.34'肯定会匹配double_ - int_,但是我得到的结果是无法匹配的.
为什么会这样,我如何得到一个只匹配整数的解析器和另一个只匹配浮点的解析器(比如c,5.0将被解释为浮点)
Jac*_*cob 14
对于您的具体示例,我认为它实际上是在Specialization 下的Boost Spirit文档中描述的RealPolicies.为了让事情变得更简单了你,我掀起了一个快速的"真正"的解析器,只解析实数,而不是整数(或至少将其与简化的例子曾):
template <typename T>
struct strict_real_policies : qi::real_policies<T>
{
static bool const expect_dot = true;
};
qi::real_parser< double, strict_real_policies<double> > real;
Run Code Online (Sandbox Code Playgroud)
你可以像任何其他解析器一样使用它(比如int_和double_).您可能需要添加:
#include <boost/spirit/include/qi_numeric.hpp>
Run Code Online (Sandbox Code Playgroud)
要让它编译.
| 归档时间: |
|
| 查看次数: |
3013 次 |
| 最近记录: |