首先,我通常在RFCs中看到的语法规范是(总是?)RFCs。在99%的情况下没有问题,例如:
myrule = skip(space) [ uint_ >> uint_ ];
Boost.Spirit qi value sequence vector
).
这样一来,根据定义,skipper应用了零次或多次,因此没有一种方法可以通过现有的有状态指令(如
skip()
http://stackoverflow.com/questions/17072987/boost-spirit-skipper-issues/17073965#17073965
或者
docs
-低于
lexeme
[no_]skip
和
skip_flag::dont_postskip
).
看看你具体的语法,我会这么做:
bool r = qi::phrase_parse(iter, end, token >> token, qi::blank);
在这里,您可以在lexeme中添加一个否定的lookahead断言,以断言“到达了令牌的末尾”——在解析器中,这将被强制为
!qi::graph
auto token = qi::copy(qi::lexeme [ qi::char_ >> !qi::graph ]);
查看演示:
Live On Coliru
#include <iostream>
#include <iomanip>
#include <boost/spirit/include/qi.hpp>
namespace qi = boost::spirit::qi;
int main() {
for (std::string const str : { "ab", " ab ", " a b ", "a b" }) {
auto iter = str.begin(), end = str.end();
auto token = qi::copy(qi::lexeme [ qi::char_ >> !qi::graph ]);
bool r = qi::phrase_parse(iter, end, token >> token, qi::blank);
std::cout << " --- " << std::quoted(str) << " --- ";
if (r) {
std::cout << "parse succeeded.";
} else {
std::cout << "parse failed.";
}
if (iter != end) {
std::cout << " Remaining unparsed: " << std::string(iter, str.end());
}
std::cout << std::endl;
}
}
印刷品
--- "ab" --- parse failed. Remaining unparsed: ab
--- " ab " --- parse failed. Remaining unparsed: ab
--- " a b " --- parse succeeded.
--- "a b" --- parse succeeded.
奖金审查说明
-
你的船长应该是语法的责任。令人难过的是,所有的Qi样本都让人们相信你需要让打电话的人来决定
-
结束迭代器检查
相等错误检查。很有可能
parse things correctly without consuming all input
. 这就是为什么在解析失败的情况下不应该只报告“剩余输入”的原因。
-
如果尾随未分析的输入
spell it out
:
Live On Coliru
#include <iostream>
#include <iomanip>
#include <boost/spirit/include/qi.hpp>
namespace qi = boost::spirit::qi;
int main() {
for (std::string const str : { "ab", " ab ", " a b ", "a b happy trees are trailing" }) {
auto iter = str.begin(), end = str.end();
auto token = qi::copy(qi::lexeme [ qi::char_ >> !qi::graph ]);
bool r = qi::parse(iter, end, qi::skip(qi::space) [ token >> token >> qi::eoi ]);
std::cout << " --- " << std::quoted(str) << " --- ";
if (r) {
std::cout << "parse succeeded.";
} else {
std::cout << "parse failed.";
}
if (iter != end) {
std::cout << " Remaining unparsed: " << std::quoted(std::string(iter, str.end()));
}
std::cout << std::endl;
}
}
印刷品
--- "ab" --- parse failed. Remaining unparsed: "ab"
--- " ab " --- parse failed. Remaining unparsed: " ab "
--- " a b " --- parse succeeded.
--- "a b happy trees are trailing" --- parse failed. Remaining unparsed: "a b happy trees are trailing"