c++ - 尝试将整数解析为字符串 (qi::as_wstring)

标签 c++ visual-studio-2015 boost-spirit

(免责声明,我正在学习提升精神)

我正在尝试解析这样的表达式: F( 1 )

并希望将 1 作为字符串(“1”而不是数字(与 qi::int_ 一起使用))。

我试过这样的事情(这是错误的,但也许我的方向是正确的),但结果字符串是“\x1”而不是“1” >"

关于将数字解析为字符串需要什么,有什么见解吗?

template <typename Iterator>
struct range_parser : boost::spirit::qi::grammar<Iterator, RangeResult(), iso8859_1::space_type>
{
    range_parser() : range_parser::base_type(start)
    {
        using qi::lexeme;
        using qi::int_;
        using iso8859_1::digit;

        number_as_string %= lexeme[ +(int_) ];

        start %=
            qi::lit("F")
            >> '('
            >> number_as_string
            >> ")"
            ;
    }

    qi::rule<Iterator, std::wstring(), iso8859_1::space_type> number_as_string;
    qi::rule<Iterator, RangeResult(), iso8859_1::space_type> start;
};

最佳答案

我想你是想写 +(digit)(一个或多个数字)而不是 +(int_)(一个或多个整数)。请注意,+(digit) 不会做与 qi::int_ 相同的事情(没有范围检查,没有 +/- 符号,没有位数限制,接受像 "000" 等输入)。

如果你想捕获 qi::int_ 识别的输入,你可以尝试使用 raw 指令:raw[int_] http://www.boost.org/doc/libs/1_63_0/libs/spirit/doc/html/spirit/qi/reference/directive/raw.html

如有必要,可以同时保存qi::int_ 解析器的解析值和原始输入,参见此处:qi::rule with inherited attribute as inherited attribute

关于c++ - 尝试将整数解析为字符串 (qi::as_wstring),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42258463/

相关文章:

c++ - 错误 : no operand "<<" matches these operands when try to use Qstring with OStream object

visual-studio-2015 - Visual Studio 2015/TFS 2013 不忽略 ASP.NET 5 项目中的 bower_components

c++ - 使用 utf-32 解析器在 Boost.Spirit 中处理 utf-8

c++ - 使用 Boost.Qi 实现递归语法

C++Builder XE10.2 [ilink32 错误] 致命 : Exceeded memory limit for block Line number cache in module <somethingsomething. cpp>

c++ - 确定时间关键循环中 200 毫秒卡住的原因

c++ - WAV 压缩帮助

c++ - Visual Studio 2015 Natvis 如何显示函数静态变量?

c++ - 不能将 std::string 设置为等于另一个 std::string 的第零个索引

Xamarin 更新后 Xamarin.Android 调试构建在 Visual Studio 2015 中失败