在 boost::spirit 和 std::vectors 中使用 auto_ 表达式

Using the auto_ expression in boost::spirit with std::vectors

我是 boost::spirit 的新手。我想将一串逗号分隔的对象解析为 std::vector(与教程中的类似)。字符串可以是不同类型(在编译时已知):整数,如 "1,2,3"、字符串 "Apple, Orange, Banana" 等。 我希望所有类型都有一个统一的界面。

如果我解析单个元素,我可以使用 auto_ 表达式。 是否有可能与向量有类似的接口? 我可以定义一个规则,给定一个模板参数,实际上可以解析这个向量吗?

这是一个简单的示例代码(由于上次调用 phrase_parse 而无法编译):

#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_stl.hpp>

#include <iostream>
#include <vector>
#include <boost/spirit/include/qi_auto.hpp>


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

using qi::auto_;
using qi::phrase_parse;
using ascii::space;
using phoenix::push_back;


int main()
{
    std::string line1 = "3";
    std::string line2 = "1, 2, 3";

    int v;
    std::vector<int> vector;

    typedef std::string::iterator stringIterator;

    stringIterator first = line1.begin();
    stringIterator last  = line1.end();

    namespace qi    = boost::spirit::qi;
    namespace ascii = boost::spirit::ascii;

    bool r1 = qi::phrase_parse( first,
                                last,
                                qi::auto_,
                                ascii::space,
                                v );

    first = line2.begin();
    last  = line2.end();

    //The following call is wrong!
    bool r2 = qi::phrase_parse( first,
                                last,
                              //  Begin grammar
                               (
                                qi::auto_[push_back(phoenix::ref(vector), qi::_1)]
                                >> *(',' >> qi::auto_[push_back(phoenix::ref(vector),qi::_1)])
                               ),
                              //  End grammar
                                ascii::space, 
                                vector);
    return 0;
}

更新

我找到了一个解决方案,前提是向量的大小在解析之前是已知的。另一方面,我不能使用语法 *( ',' >> qi::auto_ ).

#include <boost/spirit/include/qi.hpp>

namespace qi     = boost::spirit::qi;

int main()
{
    std::string s = "1, 2, 3";

    std::vector<int> vector;
    //This works
    qi::phrase_parse(s.begin(), s.end(), qi::auto_ >> ',' >> qi::auto_  >> ',' >> qi::auto_  , qi::blank, vector);
    //This does not compile
    qi::phrase_parse(s.begin(), s.end(), qi::auto_ >> *( ',' >> qi::auto_ ) , qi::blank, vector);

    for(int i = 0; i < vector.size() ; i++)
        std::cout << i << ": " << vector[i] << std::endl;

    return 0;
}

此外,使用 auto_,我无法解析字符串。是否可以定义e模板函数,其中语法可以通过模板参数推导?

template< typename T >
void MyParse(std::string& line, std::vector<T> vec)
{
     qi::phrase_parse( line.begin(), 
                       line.end(), 
                       /* 
                       How do I define a grammar based on T
                       such as: 
                          double_ >> *( ',' >> double_ )    for T = double
                       +qi::alnum >> *( ',' >> +qi::alnum ) for T = std::string
                       */, 
                       qi::blank, 
                       vec);
}

auto_ 支持开箱即用的容器属性:

Live On Coliru

std::istringstream iss("1 2 3 4 5; 6 7 8 9;");
iss.unsetf(std::ios::skipws);

std::vector<int> i;
std::vector<double> d;

if (iss >> qi::phrase_match(qi::auto_ >> ";" >> qi::auto_, qi::space, i, d))
{
    for (auto e:i) std::cout << "int: " << e << "\n";
    for (auto e:d) std::cout << "double: " << e << "\n";
}

版画

int: 1
int: 2
int: 3
int: 4
int: 5
double: 6
double: 7
double: 8
double: 9

所以你基本上可以通过使用 ',' 作为船长来编写你的模板函数。不过,我更喜欢 operator% 变体。

简单拿

template<typename Container>
void MyParse(std::string const& line, Container& container)
{
    auto f(line.begin()), l(line.end());
    bool ok = qi::phrase_parse(
            f, l,
            qi::auto_ % ',', qi::blank, container);

    if (!ok || (f!=l))
        throw "parser error: '" + std::string(f,l) + "'"; // FIXME
} 

变体 2

template<typename Container>
void MyParse(std::string const& line, Container& container)
{
    auto f(line.begin()), l(line.end());
    bool ok = qi::phrase_parse(
            f, l,
            qi::auto_, qi::blank | ',', container);

    if (!ok || (f!=l))
        throw "parser error: '" + std::string(f,l) + "'"; // FIXME
} 

解决 string 案例(及其他案例):

如果元素类型不是Spirit 'deducible'(任何东西都可以解析成字符串),只需要一个知道如何解析元素类型的可选parser/grammar?

template<typename Container, typename ElementParser = qi::auto_type>
void MyParse(std::string const& line, Container& container, ElementParser const& elementParser = ElementParser())
{
    auto f(line.begin()), l(line.end());
    bool ok = qi::phrase_parse(
            f, l,
            elementParser % ",", qi::blank, container);

    if (!ok || (f!=l))
        throw "parser error: '" + std::string(f,l) + "'"; // FIXME
} 

现在,它可以很好地解析字符串:

std::vector<int> i;
std::set<std::string> s;

MyParse("1,22,33,44,15", i);
MyParse("1,22,33,44,15", s, *~qi::char_(","));

for(auto e:i) std::cout << "i: " << e << "\n";
for(auto e:s) std::cout << "s: " << e << "\n";

版画

i: 1
i: 22
i: 33
i: 44
i: 15
s: 1
s: 15
s: 22
s: 33
s: 44

完整列表

Live On Coliru

#include <boost/spirit/include/qi.hpp>
#include <iostream>

namespace qi = boost::spirit::qi;

    template<typename Container, typename ElementParser = qi::auto_type>
    void MyParse(std::string const& line, Container& container, ElementParser const& elementParser = ElementParser())
    {
        auto f(line.begin()), l(line.end());
        bool ok = qi::phrase_parse(
                f, l,
                elementParser % ",", qi::blank, container);

        if (!ok || (f!=l))
            throw "parser error: '" + std::string(f,l) + "'"; // FIXME
    } 

#include <set>

int main()
{
    std::vector<int> i;
    std::set<std::string> s;

    MyParse("1,22,33,44,15", i);
    MyParse("1,22,33,44,15", s, *~qi::char_(","));

    for(auto e:i) std::cout << "i: " << e << "\n";
    for(auto e:s) std::cout << "s: " << e << "\n";
}