【问题标题】:Boost Spirit syntax for recognising series of numbers from a line into vector?提升 Spirit 语法以将一系列数字从一条线识别为向量?
【发布时间】:2014-07-08 07:29:31
【问题描述】:

我有一个这样的字符串:

  SCALE FACTORS      16.      0.0     0.0     0.0     0.0      .

或

  SCALE FACTORS      30.       .       .       .       .       .

或

  SCALE FACTORS     256.      10.0     20.0     30.0    .

因此,几个数字末尾带有点,并且只有点和空格。这是一些历史数据格式,我有太多文件无法手动将它们调整为更易读的形状。

行首始终是“SCALE FACTORS”,可以作为固定模板“读取”。

我需要一个增强精神表达,帮助我识别向量中的这个字符串。 必须丢弃单独的点(或至少读取为零)。数字必须存储在向量中。一行中的有效位数介于一到几之间(不固定)。

我的主要问题是,如何将一堆数字存储在向量中。原则上,我可以在没有 Boost Spirit 帮助的情况下去除杂散点。

【问题讨论】:

  • Askar,我想这个链接可以帮助你解决你想要的问题:boost.org/doc/libs/1_39_0/libs/spirit/classic/example/…
  • @Arkady 你链接到一个精神经典的例子。现在已经过时了。
  • @sehe 谢谢你,我离 boost:spirit 还差得很远,只是想了解一些东西并找到了这篇文章。我想知道它已经过时了,但仍然包含在 boost 网站上。
  • @Arkady 向后兼容是件好事。顺便说一句,您链接到 Boost 1.39,它是从 2009 年 5 月 2 日 开始的。此外,如果您查看 here,您会发现“经典”版本早于所有其他版本

标签: c++ boost vector boost-spirit


【解决方案1】:

有了blank_type 船长,你可以

    start = line % eol;
    line  = lit("SCALE") >> "FACTORS" >> *(double_|'.')

这会导致 '.' 项目为零:

Parsed 3 lines
[SCALE FACTORS] 16 0 0 0 0 0 
[SCALE FACTORS] 30 0 0 0 0 0 
[SCALE FACTORS] 256 10 20 30 0 
Remaining input: '
'

看Live On Coliru

#define BOOST_SPIRIT_USE_PHOENIX_V3
#include <boost/format.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <cstdint>

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

typedef std::vector<double> line_t;
typedef std::vector<line_t> lines_t;

template <typename Iterator>
struct sfparser : public qi::grammar<Iterator, lines_t(), qi::blank_type>
{
    sfparser () : sfparser::base_type(start)
    {
        using namespace qi;

        start = line % eol;
        line  = lit("SCALE") >> "FACTORS" >> *(double_|'.');
    }

  private:
    qi::rule<Iterator, lines_t(), qi::blank_type> start;
    qi::rule<Iterator, line_t(), qi::blank_type > line;
};

int main()
{
    std::string const input = " SCALE FACTORS      16.      0.0     0.0     0.0     0.0      .\n"
        "SCALE FACTORS      30.       .       .       .       .       .\n"
        "  SCALE FACTORS     256.      10.0     20.0     30.0    .\n";

    std::string::const_iterator f = input.begin();
    std::string::const_iterator l = input.end();
    sfparser<std::string::const_iterator> grammar;

    lines_t data;
    if (qi::phrase_parse (f, l, grammar, qi::blank, data))
    {
        std::cout << "Parsed " << data.size() << " lines\n";
        for (auto& line : data)
        {
            std::cout << "[SCALE FACTORS] ";
            for (auto d : line)
                std::cout << d << " ";

            std::cout << "\n";
        }
    }
    else
    {
        std::cout << "Failed\n";
    }

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

【讨论】:

    猜你喜欢
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多