使用迭代器解析Boost :: Spirit Grammars(Using Iterator parsing with Boost::Spirit Grammars)

编程入门 行业动态 更新时间:2024-10-18 10:15:17
使用迭代器解析Boost :: Spirit Grammars(Using Iterator parsing with Boost::Spirit Grammars)

当我尝试使用迭代器形式解析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*

When I attempt to use the iterator form of parsing for a Spirit grammar I get a argument passing conversion error from the iterator type to const char*. How do I fix this?

There are some restrictions. I'm using an iterator adapter on large inputs, so it is not feasible for me to convert to a C style string.

Here is sample code demonstrating the issue:

#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; }

This code breaks with: error: cannot convert const boost::spirit::file_iterator<char_t, boost::spirit::fileiter_impl::mmap_file_iterator<char_t> > to const char* in argument passing

最满意答案

很难从代码中看出已发布,因为它包含一些基本错误。 纠正这些后,它在我的机器上编译很好(使用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; }

Hard to tell from the code as posted, since it contains a few basic errors. After correction of these, it compiles fine on my machine (with 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; }

更多推荐

本文发布于:2023-07-22 09:53:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1219134.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:迭代   Boost   Spirit   parsing   Iterator

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!