auto concatenation of parse results into vectors

与世无争的帅哥 提交于 2019-12-01 10:12:23
sehe

You missplaced the grouping parentheses: expanding

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

by eliminating subrules leads to

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

or, as I'd prefer:

Full working sample (please make your code samples SSCCE next time? https://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 });
}

Output:

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 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!