使用带有 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
到 const char*
参数传递
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
编译错误不在提供的示例中。 在上面的语法中,我没有语义操作,而在我简化的代码中,我做了。
这些操作的回调使用 char* 而不是我使用的迭代器类型。
The compilation error was not in the sample provided. In the grammar above I have no semantic actions, while in the code I was simplifying I did.
The callbacks for those actions used char* instead of the iterator type I was using.
您可以尝试确保您的语义操作在参数类型上是多态的。 准确地说,在代码中,您需要这样的操作:
您将使用如下所示的一元语义操作:
或者,如果您需要二元语义操作,您将执行以下操作:
You can try making sure that your semantic actions were polymorphic on the type of the argument. Precisely in code you'd want something like this:
You'd use a unary semantic action like this as shown below:
Or if you needed a binary semantic action, you'd do something like:
很难从发布的代码中看出,因为它包含一些基本错误。
修正这些之后,它在我的机器上编译良好(使用 MSVC++7.1):
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):
这是让 char * 指向与迭代器相同的元素的一种方法:
我不确定如何编译它:
Here is one way of getting the char *'s pointing at the same elements as the iterators:
I'm not sure how you got this to compile though: