我想解析一个句子,其中一些字符串可能未引用、“引用”或“引用”。下面的代码几乎可以工作 - 但它无法匹配右引号。我猜这是因为qq参考。在代码中注释了修改,修改导致“引用”或“引用”也解析并有助于显示原始问题与结束引号有关。该代码还描述了确切的语法。
要完全清楚:不带引号的字符串解析。带引号的字符串 like'hello'
将解析打开的引号'
,所有字符 hello
,但无法解析最终的引号'
。
我又做了一次尝试,类似于boost tutorials中的开始/结束标签匹配,但没有成功。
template <typename Iterator>
struct test_parser : qi::grammar<Iterator, dectest::Test(), ascii::space_type>
{
test_parser()
:
test_parser::base_type(test, "test")
{
using qi::fail;
using qi::on_error;
using qi::lit;
using qi::lexeme;
using ascii::char_;
using qi::repeat;
using namespace qi::labels;
using boost::phoenix::construct;
using boost::phoenix::at_c;
using boost::phoenix::push_back;
using boost::phoenix::val;
using boost::phoenix::ref;
using qi::space;
char qq;
arrow = lit("->");
open_quote = (char_('\'') | char_('"')) [ref(qq) = _1]; // Remember what the opening quote was
close_quote = lit(val(qq)); // Close must match the open
// close_quote = (char_('\'') | char_('"')); // Enable this line to get code 'almost' working
quoted_string =
open_quote
>> +ascii::alnum
>> close_quote;
unquoted_string %= +ascii::alnum;
any_string %= (quoted_string | unquoted_string);
test =
unquoted_string [at_c<0>(_val) = _1]
> unquoted_string [at_c<1>(_val) = _1]
> repeat(1,3)[any_string] [at_c<2>(_val) = _1]
> arrow
> any_string [at_c<3>(_val) = _1]
;
// .. <snip>set rule names
on_error<fail>(/* <snip> */);
// debug rules
}
qi::rule<Iterator> arrow;
qi::rule<Iterator> open_quote;
qi::rule<Iterator> close_quote;
qi::rule<Iterator, std::string()> quoted_string;
qi::rule<Iterator, std::string()> unquoted_string;
qi::rule<Iterator, std::string()> any_string; // A quoted or unquoted string
qi::rule<Iterator, dectest::Test(), ascii::space_type> test;
};
// main()
// This example should fail at the very end
// (ie not parse "str3' because of the mismatched quote
// However, it fails to parse the closing quote of str1
typedef boost::tuple<string, string, vector<string>, string> DataT;
DataT data;
std::string str("addx001 add 'str1' \"str2\" -> \"str3'");
std::string::const_iterator iter = str.begin();
const std::string::const_iterator end = str.end();
bool r = phrase_parse(iter, end, grammar, boost::spirit::ascii::space, data);
对于奖励积分:避免使用本地数据成员(例如char qq
上面的示例)的解决方案将是首选,但从实际的角度来看,我将使用任何可行的方法!