c++ - 使用 Iterator 解析 Boost::Spirit Grammars

标签 c++ boost boost-spirit

当我尝试使用迭代器形式解析 Spirit 语法时,我得到一个参数传递从迭代器类型到 const char* 的转换错误。我该如何解决这个问题?

有一些限制。我在大型输入上使用迭代器适配器,因此我无法转换为 C 风格的字符串。

这是演示问题的示例代码:

#include <boost/spirit/core.hpp>
#include <boost/spirit/iterator/file_iterator.hpp>
#include <vector>
#include <string>
using std;
using boost::spirit;
struct ex : public grammar<route_grammar> {
  template <typename ScannerT> struct defintion {
    definition(ex const& self) {
      expression = real_p; 
    }
    rule<ScannerT> expression;
    rule<ScannerT> const& start() const { return expression; }
};

int main() {
  file_iterator<char> first;
  file_iterator<char> last = first.make_end();
  ex ex_p;
  parse_info<file_iterator<char> > info = parse(first, last, ex_p, space_p);
  return 0;
}

此代码中断:错误:无法转换 const boost::spirit::file_iterator<char_t, boost::spirit::fileiter_impl::mmap_file_iterator<char_t> >const char*在参数传递中

最佳答案

很难从发布的代码中分辨出来,因为它包含一些基本错误。 更正这些后,它在我的机器上编译得很好(使用 MSVC++7.1):

#include <boost/spirit/core.hpp>
#include <vector>
#include <string>
using namespace std;
using namespace boost::spirit;
struct ex : public grammar<ex> {
template <typename ScannerT> 
struct definition {
    definition(ex const& self)
    {
    expression = real_p; 
    }
    rule<ScannerT> expression;
    rule<ScannerT> const& start() const { return expression; }
};
};

int main() {
vector<char> v;
v.push_back('3'); v.push_back('.'); v.push_back('2');
ex ex_p;
parse_info<vector<char>::iterator> info = parse(v.begin(), v.end(), ex_p, space_p);
return 0;
}

关于c++ - 使用 Iterator 解析 Boost::Spirit Grammars,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/529706/

相关文章:

c++ - &vec[0] 是否为 std::vector vec 定义了行为?

python - 使用 SWIG 将 C++ 对象指针传递给 Python,而不是再次传回 C++

c++ - 并行化 std::for_each 中的数据竞争

c++ - 我可以使用 Boost.Spirit 进行多功能数学 (AST) 模式匹配和操作吗?

c++ - 在 Clang 中判断访问的 CXXRecordDecl 是类、结构还是 union

c++ - 如何在不删除元素并将其重新插入到 boost::multi_index_container 的情况下移动元素?

c++ - 测试是否存在左移运算符

C++ boost 理性类,floor 函数

c++ - 使用精神以替代方式解析结构时混淆输出

c++ - 如何从 boost::spirit::multi_pass 获取基础迭代器?