如何使用parse/phrase_parse函数
How to use the parse/phrase_parse function
具体来说,使用语法g,我如何解析字符串s ?我应该给出什么论据呢?我试了很多次电话,总是出错。
另外,由于我还不确定以后要使用哪一个,使用phrase_parse
会有什么不同吗?
namespace qi = boost::spirit::qi;
int main() {
My_grammar<std::string::const_iterator> g;
std::string s = "a"; // string to parse
if (qi::parse( /*...*/ )) {
std::cout << "String parsed !";
} else {
std::cout << "String doesn't parse !";
}
return EXIT_SUCCESS;
}
基本上,您应该查看教程,但问题的一部分是,您或多或少需要创建一个变量来保存开始迭代器。因为,它是通过对qi::parse
的引用传递的,并且它停止的确切位置可以被认为是qi::parse
函数的输出。如果您试图通过s.begin()
传递它,它将不起作用,因为您试图将引用绑定到临时对象。
namespace qi = boost::spirit::qi;
int main() {
My_grammar<std::string::const_iterator> g;
std::string s = "a"; // string to parse
std::string::const_iterator it = s.begin(); // The type declaration here
std::string::const_iterator end = s.end(); // and here needs to match template parameter
// which you used to instantiate g
if (qi::parse( it, end, g )) {
std::cout << "String parsed !";
} else {
std::cout << "String doesn't parse !";
}
return EXIT_SUCCESS;
}
仅当您想显式指定跳过语法时才使用phrase_parse
。
是有区别的。
phrase_parse
使用一个skipper,可能不会消耗所有的输入,仍然返回true。
在所有其他方面,两者是相同的。你应该直接点击文档:
- http://www.boost.org/doc/libs/1_59_0/libs/spirit/doc/html/spirit/qi/reference/parse_api/iterator_api.html
std::string::const_iterator f(s.begin(), l(s.end());
if (qi::parse(f, l, g/*, optional, bound, attribute, references*/))
相关文章:
- "error: no matching function for call to"构造函数错误
- 什么时候调用组成单元对象的析构函数
- 继承函数的重载解析
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- C++模板来检查友元函数的存在
- 递归函数计算序列中的平方和(并输出过程)
- 对RValue对象调用的LValue ref限定成员函数
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 在C++STL中是否有Polyval(Matlab函数)等价物?
- 为什么使用 "this" 指针调用派生成员函数?
- 将对象数组的引用传递给函数
- 函数调用中参数的顺序重要吗
- 函数向量_指针有不同的原型,我可以构建一个吗
- 使用不带参数的函数访问结构元素
- 调用 Parse int 函数时编译 parse 整数程序时出现问题
- 在一个不带参数的函数的声明中指定void是否解决了Most Vexing Parse
- 当const在另一行处的const时,doxygen parse const成员函数不正确
- 匿名函数,"Parse Issue: Expected Expression"
- 如何使用parse/phrase_parse函数