c++ - 在 boost spirit x3 解析结果中包含前导零

标签 c++ boost boost-spirit

我想知道在使用 boost Spirit X3 解析数字时是否可以保留前导零。我目前拥有的是一个将整数解析为我的数据结构的程序(基于员工示例)。但是,在解析过程中我丢失了前导零。这是我的应用领域的一个问题,其中任何整数前面的前导零给出不同的解释。

  #include <boost/spirit/home/x3.hpp>
  #include <boost/spirit/include/support_istream_iterator.hpp>
  #include <boost/fusion/adapted/struct.hpp>
  #include <iostream>

  namespace client {
    namespace ast {
        struct number
        {
            int number;
        };
    }
  }
  BOOST_FUSION_ADAPT_STRUCT(client::ast::number, number)

  namespace client
  {
    namespace parser
    {
        namespace x3 = boost::spirit::x3;
        using x3::int_;
        x3::rule<class number, ast::number> const number = "number";
        auto const number_def = int_;
        BOOST_SPIRIT_DEFINE(number)
    }
  }

  int main()
  {
    using boost::spirit::x3::ascii::space;
    using client::parser::number;
    std::istringstream iss("1 02 030 00400 0005");
    std::vector<client::ast::number> nums;
    boost::spirit::istream_iterator iter(iss >> std::noskipws), eof;
    bool ok = phrase_parse(iter, eof, *number, space, nums);
    if (ok)
    {
        std::cout << "parsed: " << std::endl;
        for (size_t i = 0; i < nums.size(); ++i)
        {
            std::cout << nums[i].number << "\n";
        }
    }
  }

程序的结果是:

  parsed:
  1
  2
  30
  400
  5

而我需要

  parsed:
  1
  02
  030
  00400
  00005

编辑

我在这方面取得了一些进展:

http://coliru.stacked-crooked.com/a/9f06f02613956230

#include <boost/spirit/home/x3.hpp>
#include <boost/fusion/adapted/struct.hpp>

namespace x3 = boost::spirit::x3;
namespace ast {
    struct fullnumber
    {
        std::string leadingZeros = "";
        int number = -1;
    };
}

BOOST_FUSION_ADAPT_STRUCT(ast::fullnumber, leadingZeros, number)
x3::rule<class fullnumber, ast::fullnumber> const fullnumber = "fullnumber";
auto const fullnumber_def = x3::lexeme[-(+x3::char_("0") >> &x3::int_) >> +x3::int_];
BOOST_SPIRIT_DEFINE(fullnumber);

int main() {
    std::vector<ast::fullnumber> fullnumbers;
    std::string parse_numbers_input("0 1 00 01 20 003000 00004 500000");
    auto begin = parse_numbers_input.begin();
    auto end = parse_numbers_input.end();
    bool ok = phrase_parse(begin, end, *fullnumber, x3::space, fullnumbers);
    if (ok) {
        std::cout << "parsed: " << std::endl;
        for (auto n : fullnumbers)
            std::cout << "leading: '" << n.leadingZeros << "', num: " << n.number << "\n";
    }
}
parsed: 
leading: '0', num: 0
leading: '', num: 1
leading: '00', num: 0
leading: '0', num: 1
leading: '', num: 20
leading: '00', num: 3000
leading: '0000', num: 4
leading: '', num: 500000

如您所见,我已经接近我想要的了。诀窍是理解 x3::lexeme 是必需的,因为如果我们不使用它,解析器将始终使用每个元素之间的分隔符。所以 x3::lexeme[-(+x3::char_("0") >> &x3::int_) >> +x3::int_]; 说:[可选]所有零后跟一个整数(非消耗),后跟一个整数。

我还有一个关于解析器正在做什么的问题:

|----|----------|----------|-----------|----------|
| id | input    | leading  | number    | expected |
|----|----------|----------|-----------|----------|
| 1  |   0      |     0    |    0      |    no    |
|----|----------|----------|-----------|----------|
| 2  |   1      |          |    1      |    yes   |
|----|----------|----------|-----------|----------|
| 3  |   00     |     00   |    0      |    no    |
|----|----------|----------|-----------|----------|
| 4  |   01     |     0    |    1      |    yes   |
|----|----------|----------|-----------|----------|
| 5  |   20     |          |    20     |    yes   |
|----|----------|----------|-----------|----------|
| 6  |   003000 |     00   |    3000   |    yes   |
|----|----------|----------|-----------|----------|
| 7  |   00004  |     0000 |    4      |    yes   |
|----|----------|----------|-----------|----------|
| 8  |   500000 |          |    500000 |    yes   |
|----|----------|----------|-----------|----------|
  • 对于 1,我期望 leading="", num="0"
  • 对于 3,我期望 leading="0", num="0"

为什么在这些情况下会出现两次 0?

最佳答案

就像其他评论者说的,只是解析为字符串。

如果您仍然希望 Spirit 的 int_ 解析器方便地解析(带符号的)整数,请将其包装在 raw[] 中以传播到迭代器范围(这是与字符串兼容):

Live On Coliru

#include <boost/spirit/home/x3.hpp>
#include <boost/spirit/include/support_istream_iterator.hpp>
#include <iostream>
#include <iomanip>

namespace x3 = boost::spirit::x3;

int main() {
    std::istringstream iss("1 02 030 00400 0005");
    std::vector<std::string> nums;
    boost::spirit::istream_iterator iter(iss >> std::noskipws), eof;

    bool ok = phrase_parse(iter, eof, *x3::raw[x3::int_], x3::space, nums);
    if (ok) {
        std::cout << "parsed: " << std::endl;
        for (auto num : nums)
            std::cout << std::quoted(num) << "\n";
    }
}

打印

parsed: 
"1"
"02"
"-030"
"00400"
"0005"

关于c++ - 在 boost spirit x3 解析结果中包含前导零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55553148/

相关文章:

c++ - 为什么包含字符串文字的初始化程序对初始化 `char` 数组有效?

c++ - 使用 C++ 解析 robots.txt 文件

c++ - 使用 Boost::Spirit 解析时出现段错误

c++ - 如果进程崩溃,映射文件会怎样?

c++ - QGraphicsView EnsureVisible() 和 centerOn()

c++ - CMake + boost 测试 : ignore tests that fail to build

c++ - 虚拟类作为具有 Spirit 的 AST 节点

c++ - Boost spirit 2.5.2 独立版

c++ - 如何在阅读时获取套接字流的位置

c++ - 带参数的函数 vector