2
我想解析包含連字符的逗號分隔的標記。但lexeme
忽略所有的連字符。部分程序如下。boost :: spirit :: qi :: lexeme沒有捕獲完整的令牌
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/support_utree.hpp>
namespace qi = boost::spirit::qi;
namespace bs = boost::spirit;
template<typename Iterator>
struct my_grammar : public qi::grammar<Iterator,bs::utree(),bs::ascii::space_type>
{
my_grammar() : my_grammar::base_type(start,"MY")
{
start = token % ',';
token = qi::lexeme[ +qi::alnum % qi::char_('-') ];
}
qi::rule<Iterator,bs::utree(),bs::ascii::space_type> start;
qi::rule<Iterator,std::string()> token;
};
template<typename Iterator>
bool parse(Iterator & begin,Iterator end,my_grammar<Iterator> const & grammar)
{
bs::utree a;
auto r = qi::phrase_parse(begin,end,grammar,bs::ascii::space,a);
std::cout<<a<<'\n';
return r;
}
int main()
{
std::string input = "i-j-k,l-m-n,p3-14 ,5jhjj-kkk";
auto it = input.begin();
my_grammar<decltype(it)> g;
if(::parse(it,input.end(),g))
{
std::cout<<"parse success\n";
}
else
{
std::cout<<"parse failed\n";
}
std::cout<<"Unparsed input => "<< std::string{it,input.end()}<<'\n';
}
給很多吸脂與精神X3,如果你可以使用C++ 14:** [住在Coliru(http://coliru.stacked-crooked.com/a/b1539818a9062a78)** – sehe
我只有C++ 11和GCC 4.7.3 – gjha
以上建議的問題是我不想接受像'abc - xyz'這樣的字符串,即不允許使用'double hyphen' 。 – gjha