kyy*_*kyy 5 c++ boost boost-spirit-qi
我有一个如下的词位,用于表示字母数字的单词。
属性 = lexeme[+(boost::spirit::qi::alpha|boost::spirit::qi::digit)];
我想要一个语法规则,它会跳过不适合该规则的任何其他字符,并将这些字符放入向量中。
例如:输入:STR1 + STR2 % STR3() STR4 = STR5+ STR6
output: (STR1, STR2, STR3, STR4, STR6)
Run Code Online (Sandbox Code Playgroud)
我尝试过下面的语法,但在解析字符串中的第一个单词后它会跳过所有内容。我怎样才能改变它来解析我所描述的?
typedef std::vector<std::wstring> Attributes;
template <typename It, typename Skipper=boost::spirit::qi::space_type>
struct AttributeParser : boost::spirit::qi::grammar<It, Attributes(), Skipper>
{
AttributeParser() : AttributeParser::base_type(expression)
{
expression =
*( attributes [phx::push_back(qi::_val, qi::_1)])
>> qi::omit[*qi:char_]
;
attributes = qi::lexeme[+(boost::spirit::qi::alpha|qi::boost::spirit::qi::digit)];
BOOST_SPIRIT_DEBUG_NODE(expression);
BOOST_SPIRIT_DEBUG_NODE(attributes);
}
private:
boost::spirit::qi::rule<It, std::wstring() , Skipper> attributes;
boost::spirit::qi::rule<It, Attributes() , Skipper> expression;
};
Run Code Online (Sandbox Code Playgroud)
我真的会写下你所描述的:
std::vector<std::wstring> parsed;
bool ok = qi::phrase_parse(
begin(input), end(input),
*qi::lexeme [ +qi::alnum ],
~qi::alnum,
parsed);
Run Code Online (Sandbox Code Playgroud)
即:
parsed向量中这是完整的程序
#include <boost/spirit/include/qi.hpp>
namespace qi = boost::spirit::qi;
int main()
{
std::wstring input = L"STR1 + STR2 % STR3 () STR4 = STR5+ STR6";
std::vector<std::wstring> parsed;
bool ok = qi::phrase_parse(begin(input), end(input),
*qi::lexeme [ +qi::alnum ],
~qi::alnum,
parsed);
for(auto& v : parsed)
std::wcout << v << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
那打印
STR1
STR2
STR3
STR4
STR5
STR6
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1647 次 |
| 最近记录: |