2

所以我想做的是解析一个字符串列表:

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

std::string TEST = "aa\nbbbb\nccc\n";

std::istringstream INPUT (TEST);
std::noskipws(INPUT);

typedef std::istreambuf_iterator<char> base_iterator;
typedef boost::spirit::multi_pass<base_iterator>  multi_pass_iter;
typedef boost::spirit::classic::position_iterator2<multi_pass_iter> pos_iterator;

base_iterator base_begin(INPUT);

multi_pass_iter first =  boost::spirit::make_default_multi_pass(base_begin);
multi_pass_iter last;

pos_iterator pfirst(first,last,std::string("DD"));
pos_iterator plast;

using qi::lexeme;
using ascii::alpha;

std::vector<std::string> DDD;
bool res = qi::phrase_parse(pfirst,plast,* lexeme[+alpha],ascii::space,DDD);

for (const auto & d : DDD) std::cout << d << " (" << d.size() << ")" << std::endl;

我得到的DDD是正确大小的 3 个字符串,但都是空格。

相反,如果我使用

bool res = qi::phrase_parse(first,last,* lexeme[+alpha],ascii::space,DDD);

一切都按预期工作。我position_iterator2过去使用没有任何问题,所以我不相信这是一个错误。我错过了什么吗?

4

2 回答 2

2

我认为这是一个精神错误如果您查看票证的底部,您会发现我认为可以修复它的补丁。

于 2013-06-10T06:40:04.263 回答
1

这里有一个例子也不起作用。使用 Visual Studio 2012 都会发出警告:

boost/iterator/iterator_adaptor.hpp(306): warning C4172: returning address of local variable or temporary
boost_1_52_0\boost/iterator/iterator_adaptor.hpp(306) : while compiling class template member function 'const char &boost::iterator_adaptor<Derived,Base,Value,Traversal>::dereference(void) const'
         with
         [
            Derived=boost::spirit::classic::position_iterator2<forward_iterator_type>,
            Base=forward_iterator_type,
            Value=const char,
            Traversal=boost::forward_traversal_tag
         ]

快速谷歌搜索“iterator_adaptor dereference temporary”会导致建议Reference参数iterator_adaptor为非引用类型。

为了实现这一点,您需要更改文件“boost/spirit/home/classic/iterator/impl/position_iterator.ipp”。具体来说,您需要更改:

typedef boost::iterator_adaptor<
    main_iter_t,
    ForwardIterT,
    const_value_type,
    boost::forward_traversal_tag
> type;

到:

typedef boost::iterator_adaptor<
    main_iter_t,
    ForwardIterT,
    const_value_type,
    boost::forward_traversal_tag,
    const_value_type
> type;

这会导致 g++ 和 vc11 中出现新错误:

boost_1_52_0\boost/concept_check.hpp(212): error C2440: 'initializing' : cannot convert from 'boost::detail::iterator_category_with_traversal<Category,Traversal>' to 'std::forward_iterator_tag'
          with
          [
              Category=std::input_iterator_tag,
              Traversal=boost::forward_traversal_tag
          ]
          No constructor could take the source type, or constructor overload resolution was ambiguous

如果将iterator_adaptortypedef 更改为:

typedef boost::iterator_adaptor<
    main_iter_t,
    ForwardIterT,
    const_value_type,
    std::forward_iterator_tag,
    const_value_type
> type;

这使得下面的程序(基于您的代码)和 boost-spirit.com 中的示例都可以工作,但我不确定它在其他情况下不会中断,因此请自行决定使用它。

#include <vector>
#include <istream>
#include <sstream>
#include <iostream>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/support_multi_pass.hpp>
#include <boost/spirit/include/classic_position_iterator.hpp>

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

int main()
{

    std::string TEST = "aa\nbbbb\nccc\n";

    std::istringstream INPUT (TEST);
    std::noskipws(INPUT);

    typedef std::istreambuf_iterator<char> base_iterator;
    typedef boost::spirit::multi_pass<base_iterator>  multi_pass_iter;
    typedef boost::spirit::classic::position_iterator2<multi_pass_iter> pos_iterator;

    base_iterator base_begin(INPUT);

    multi_pass_iter first =  boost::spirit::make_default_multi_pass(base_begin);
    multi_pass_iter last;

    pos_iterator pfirst(first,last,std::string("DD"));
    pos_iterator plast;

    using qi::lexeme;
    using ascii::alpha;

    std::vector<std::string> DDD;
    bool res = qi::phrase_parse(pfirst,plast,* lexeme[+alpha],ascii::space,DDD);

    if(res && pfirst==plast)
    {
        for (const auto & d : DDD) 
            std::cout << d << " (" << d.size() << ")" << std::endl;
    }
    else
    {
        std::cout << "Parsing error." << std::endl;
    }

    return 0;
}
于 2012-12-23T11:18:14.777 回答