将解析结果自动并置到向量中 [英] auto concatenation of parse results into vectors

查看:113
本文介绍了将解析结果自动并置到向量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一些规则将浮点解析为两个std :: vector的浮点数,然后存储在结构中:

I've written some rules to parse floats into two std::vector's of floats, which in turn are stored in a struct:

数据输入:

#
# object name01
#

v  -1.5701 33.8087 0.3592
v  -24.0119 0.0050 21.7439
# a comment

vn 0.0000 0.5346 0.8451
vn 0.8331 0.5531 -0.0000
# another comment

结构:

struct ObjParseData
{
    ObjParseData() : verts(), norms() {}

    std::vector<float> verts;
    std::vector<float> norms;
};

以及相关的解析代码:

struct objGram : qi::grammar<std::string::const_iterator, ObjParseData(), iso8859::space_type>
    {
        objGram() : objGram::base_type(start)
        {
            vertex  = 'v' >> qi::double_ >> qi::double_ >> qi::double_;
            normal  = "vn" >> qi::double_ >> qi::double_ >> qi::double_;
            comment = '#' >> qi::skip(qi::blank)[ *(qi::print) ];
            vertexList = *(vertex | comment);
            normalList = *(normal | comment);
            start = vertexList >> normalList;
        }

        qi::rule<std::string::const_iterator, ObjParseData(), iso8859::space_type> start;
        qi::rule<std::string::const_iterator, std::vector<float>(), iso8859::space_type> vertexList;
        qi::rule<std::string::const_iterator, std::vector<float>(), iso8859::space_type> normalList;
        qi::rule<std::string::const_iterator, std::vector<float>(), iso8859::space_type> vertex;
        qi::rule<std::string::const_iterator, std::vector<float>(), iso8859::space_type> normal;
        qi::rule<std::string::const_iterator, iso8859::space_type> comment;
    } objGrammar;


    ObjParseData resultData;

    std::string::const_iterator f = data.cbegin();
    bool res = qi::phrase_parse( f, data.cend(), objGrammar, iso8859::space, resultData );

它将所有前缀为'v'的浮点解析为结构的verts向量,将所有浮点数前面的vn解析为norm。

And this works. It parses all the floats preceded by 'v' into the verts vector of the struct and all floats preceded by "vn" into norms. Which is great, but I don't really know why this works.

现在,如果我正确理解了这一点,下面定义的规则将所有的结果放入std: :浮动矢量。

Now if I understand this correctly, a rule defined like below puts all its results into a std::vector of floats.

qi::rule<std::string::const_iterator, std::vector<float>(), iso8859::space_type> vertex;

因此,查看上面显示的解析代码,知道顶点规则解析为std: :向量的浮点数,显然一个规则像vertexList(上面显示的一个)将顶点的结果连接到一个std :: vector的浮点数?所以看到这种行为,你会认为你可以只是写这两个规则(顶点和vertexList)作为一个,但不幸的是这不工作:

So, looking at the parsing code shown above and knowing that a rule like vertex parses into a std::vector of floats, apparently a rule like vertexList (the one shown above) concatenates the results from vertex into one std::vector of floats? So seeing this behaviour, you would think that you could just write those two rules (vertex and vertexList) as one, but unfortunately this doesn't work:

vertex  = *('v' >> qi::double_ >> qi::double_ >> qi::double_) | comment;
normal  = *("vn" >> qi::double_ >> qi::double_ >> qi::double_) | comment;
comment = '#' >> qi::skip(qi::blank)[ *(qi::print) ];
start = vertex >> normal;

代码会编译,qi :: phrase_parse会返回一个成功的解析,但std :: vector

The code does compile and qi::phrase_parse does return a succesfull parse, but the std::vector's in the struct aren't filled anymore.. What am I missing here?

推荐答案

您错过了分组括号:expand

You missplaced the grouping parentheses: expanding

    vertexList = *(vertex | comment);
    normalList = *(normal | comment);

通过删除子项导致

    vertex     = *(('v'  >> qi::double_ >> qi::double_ >> qi::double_) | comment);
    normal     = *(("vn" >> qi::double_ >> qi::double_ >> qi::double_) | comment);

或者根据我的喜好:

完整工作示例( 下次使用SSCCE代码示例 http://meta.stackexchange.com/questions/22754/sscce-how-to-provide-examples-for-programming-questions ):

Full working sample (please make your code samples SSCCE next time? http://meta.stackexchange.com/questions/22754/sscce-how-to-provide-examples-for-programming-questions):

#include <iterator>
#include <fstream>
#include <boost/fusion/adapted.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/karma.hpp>
#include <boost/spirit/include/phoenix.hpp>

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

struct ObjParseData
{
    ObjParseData() : verts(), norms() {}

    std::vector<float> verts;
    std::vector<float> norms;
};

BOOST_FUSION_ADAPT_STRUCT(ObjParseData, (std::vector<float>, verts)(std::vector<float>, norms))



template <typename It, typename Skipper = qi::space_type>
    struct parser : qi::grammar<It, ObjParseData(), Skipper>
{
    parser() : parser::base_type(start)
    {
        using namespace qi;


        vertex     = 'v'  >> qi::double_ >> qi::double_ >> qi::double_;
        normal     = "vn" >> qi::double_ >> qi::double_ >> qi::double_;
        comment    = '#' >> qi::skip(qi::blank)[ *(qi::print) ];
#if 0
        vertexList = *(vertex | comment);
        normalList = *(normal | comment);
        start      = vertexList >> normalList;
#else
        vertex     = *(comment | ('v'  >> qi::double_ >> qi::double_ >> qi::double_));
        normal     = *(comment | ("vn" >> qi::double_ >> qi::double_ >> qi::double_));
        start      = vertex >> normal;                                              
#endif

        BOOST_SPIRIT_DEBUG_NODE(start);
    }

  private:
    qi::rule<std::string::const_iterator, ObjParseData(), qi::space_type> start;
    qi::rule<std::string::const_iterator, std::vector<float>(), qi::space_type> vertexList;
    qi::rule<std::string::const_iterator, std::vector<float>(), qi::space_type> normalList;
    qi::rule<std::string::const_iterator, std::vector<float>(), qi::space_type> vertex;
    qi::rule<std::string::const_iterator, std::vector<float>(), qi::space_type> normal;
    qi::rule<std::string::const_iterator, qi::space_type> comment;
};

bool doParse(const std::string& input)
{
    typedef std::string::const_iterator It;
    auto f(begin(input)), l(end(input));

    parser<It, qi::space_type> p;
    ObjParseData data;

    try
    {
        bool ok = qi::phrase_parse(f,l,p,qi::space,data);
        if (ok)   
        {
            std::cout << "parse success\n";
            std::cout << "data: " << karma::format_delimited(
                    "v: " << karma::auto_ << karma::eol <<
                    "n: " << karma::auto_ << karma::eol, ' ', data);
        }
        else      std::cerr << "parse failed: '" << std::string(f,l) << "'\n";

        if (f!=l) std::cerr << "trailing unparsed: '" << std::string(f,l) << "'\n";
        return ok;
    } catch(const qi::expectation_failure<It>& e)
    {
        std::string frag(e.first, e.last);
        std::cerr << e.what() << "'" << frag << "'\n";
    }

    return false;
}

int main()
{
    std::ifstream ifs("input.txt", std::ios::binary);
    ifs.unsetf(std::ios::skipws);
    std::istreambuf_iterator<char> f(ifs), l;

    bool ok = doParse({ f, l });
}

输出:

parse success
data: v:  -1.57 33.809 0.359 -24.012 0.005 21.744 
 n:  0.0 0.535 0.845 0.833 0.553 0.0 

这篇关于将解析结果自动并置到向量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆