2012-02-24 4 views
2

다음은 코드 샘플입니다.boost :: spirit을 사용하여 복식 목록 구문 분석

// file temp.cpp 

#include <iostream> 

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

namespace qi = boost::spirit::qi; 

struct parser : qi::grammar<std::string::const_iterator, std::vector<double> > 
{ 
    parser() : parser::base_type(vector) 
    { 
     vector = +qi::double_; 
    } 

    qi::rule<std::string::const_iterator, std::vector<double> > vector; 
}; 

int main() 
{ 
    std::string const x("1 2 3 4"); 
    std::string::const_iterator b = x.begin(); 
    std::string::const_iterator e = x.end(); 
    parser p; 
    bool const r = qi::phrase_parse(b, e, p, qi::space); 
    // bool const r = qi::phrase_parse(b, e, +qi::double_, qi::space); // this this it PASSES 
    std::cerr << ((b == e && r) ? "PASSED" : "FAILED") << std::endl; 
} 

나는 parserpstd::stringx을 구문 분석합니다.

로서 struct parser의 정의로부터 다음, 라인

qi::phrase_parse(b, e, p, qi::space); // PASSES 

qi::phrase_parse(b, e, +qi::double_, qi::space); // FAILS 

은 동일해야한다. 그러나 첫 번째 구문 분석은 실패하고 두 번째 구문 분석은 통과합니다.

내가 struct parser의 정의에서 잘못하고있는 것은 무엇입니까?

답변

2

공백을 건너 뛰는 것에 대한 문법을 ​​알려야합니다 - 템플릿에서 하나 이상의 인수.

#include <iostream> 

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

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

struct parser 
    : qi::grammar<std::string::const_iterator, std::vector<double>(), ascii::space_type> 
{ 
    parser() : parser::base_type(vector) 
    { 
    vector %= +(qi::double_); 
    } 

    qi::rule<std::string::const_iterator, std::vector<double>(), ascii::space_type> vector; 
}; 

int main() 
{ 
    std::string const x("1 2 3 4"); 
    std::string::const_iterator b = x.begin(); 
    std::string::const_iterator e = x.end(); 
    parser p; 
    bool const r = qi::phrase_parse(b, e, p, ascii::space); 
    //bool const r = qi::phrase_parse(b, e, +qi::double_, qi::space); 
    std::cout << ((b == e && r) ? "PASSED" : "FAILED") << std::endl; 
} 

나는 또한 몇 가지 작은 수정 사항을 수행했습니다. 인수에 대괄호를 추가해야합니다.이 대괄호는 속성 유형에 대해 알려줍니다. std::vector<double>().

관련 문제