简单的线条,不同类型的转义元素
Simple Line, escaped elements with different type
我想用 boost::spirit
解析以下行
0 "a" "b" "c"
我是这样创建规则的:
qi::rule<std::string::const_iterator, std::string()> escaped_ = qi::char_('"') >> *(qi::char_ - qi::char_('"')) >> qi::char_('"');
int id;
std::string v1,v2,v3;
qi::phrase_parse(bein, end, (qi::int_ >> escaped_ >> escaped_ >> escaped_ >> qi::eol), id, v1, v2, v3);
但是解析失败,我不知道为什么。希望有人能帮忙。
phrase_parse
需要(它,它,解析器,船长[,属性...])。您忘记传递船长:
bool ok = qi::phrase_parse(begin, end, (qi::int_ >> escaped_ >> escaped_ >> escaped_ >> qi::eol), qi::blank, id, v1, v2, v3);
我建议在那里 qi::blank
因为你的语法将 eol
视为重要的(跳过它永远不会匹配)。
Note the qi::rule
subtly omits the skipper, hence it's implicitly a lexeme[]
:
qi::rule<It, std::string(), qi::blank_type> escaped_ = qi::lexeme['"' >> *(qi::char_ - '"') >> '"'];
See also: Boost spirit skipper issues
最后请注意,您不想将 "
解析为结果值的一部分(将 qi::char_('"')
更改为 qi::lit('"')
或者,如果可能,等效地 '"'
).
演示
#include <boost/spirit/include/qi.hpp>
namespace qi = boost::spirit::qi;
int main() {
typedef std::string::const_iterator It;
qi::rule<It, std::string()> escaped_ = '"' >> *(qi::char_ - '"') >> '"';
std::string const input("0 \"a\" \"b\" \"c\"\n");
It begin(input.begin()), end(input.end());
int id;
std::string v1,v2,v3;
bool ok = qi::phrase_parse(begin, end, (qi::int_ >> escaped_ >> escaped_ >> escaped_ >> qi::eol), qi::blank, id, v1, v2, v3);
if (ok) {
std::cout << "Parse succeeded:"
<< " " << id
<< " '" << v1 << "'"
<< " '" << v2 << "'"
<< " '" << v3 << "'\n";
} else {
std::cout << "Parse failed\n";
}
if (begin != end)
std::cout << "Remaining unparsed '" << std::string(begin, end) << "'\n";
}
版画
Parse succeeded: 0 'a' 'b' 'c'
我想用 boost::spirit
解析以下行0 "a" "b" "c"
我是这样创建规则的:
qi::rule<std::string::const_iterator, std::string()> escaped_ = qi::char_('"') >> *(qi::char_ - qi::char_('"')) >> qi::char_('"');
int id;
std::string v1,v2,v3;
qi::phrase_parse(bein, end, (qi::int_ >> escaped_ >> escaped_ >> escaped_ >> qi::eol), id, v1, v2, v3);
但是解析失败,我不知道为什么。希望有人能帮忙。
phrase_parse
需要(它,它,解析器,船长[,属性...])。您忘记传递船长:
bool ok = qi::phrase_parse(begin, end, (qi::int_ >> escaped_ >> escaped_ >> escaped_ >> qi::eol), qi::blank, id, v1, v2, v3);
我建议在那里 qi::blank
因为你的语法将 eol
视为重要的(跳过它永远不会匹配)。
Note the
qi::rule
subtly omits the skipper, hence it's implicitly alexeme[]
:qi::rule<It, std::string(), qi::blank_type> escaped_ = qi::lexeme['"' >> *(qi::char_ - '"') >> '"'];
See also: Boost spirit skipper issues
最后请注意,您不想将 "
解析为结果值的一部分(将 qi::char_('"')
更改为 qi::lit('"')
或者,如果可能,等效地 '"'
).
演示
#include <boost/spirit/include/qi.hpp>
namespace qi = boost::spirit::qi;
int main() {
typedef std::string::const_iterator It;
qi::rule<It, std::string()> escaped_ = '"' >> *(qi::char_ - '"') >> '"';
std::string const input("0 \"a\" \"b\" \"c\"\n");
It begin(input.begin()), end(input.end());
int id;
std::string v1,v2,v3;
bool ok = qi::phrase_parse(begin, end, (qi::int_ >> escaped_ >> escaped_ >> escaped_ >> qi::eol), qi::blank, id, v1, v2, v3);
if (ok) {
std::cout << "Parse succeeded:"
<< " " << id
<< " '" << v1 << "'"
<< " '" << v2 << "'"
<< " '" << v3 << "'\n";
} else {
std::cout << "Parse failed\n";
}
if (begin != end)
std::cout << "Remaining unparsed '" << std::string(begin, end) << "'\n";
}
版画
Parse succeeded: 0 'a' 'b' 'c'