【问题标题】:Parse quoted strings with boost::spirit使用 boost::spirit 解析引用的字符串
【发布时间】:2012-05-04 14:55:31
【问题描述】:

我想解析一个句子,其中某些字符串可能未引用、“引用”或“引用”。下面的代码几乎可以工作 - 但它无法匹配右引号。我猜这是因为qq参考。在代码中注释了修改,修改导致“引用”或“引用”也解析并有助于显示原始问题与结束引号有关。该代码还描述了确切的语法。

完全清楚:不带引号的字符串解析。像'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)的解决方案将是首选,但从实际的角度来看,我会使用任何可行的方法!

【问题讨论】:

  • 为了记录,使char qq 成为struct test_parser 的成员变量以完全相同的方式失败。
  • 以什么“同样的方式”失败?您还没有告诉我们这个失败的原因(尽管我可以想象这是由于 qq 参考)。
  • @NicolBolas 这是代码中的注释 - 我已经澄清了这个问题,感谢您指出。我也怀疑 ref(qq),但是 boost lambda&co 的缺点是它们很难调试,因为你无法按照传统意义上的单步执行!

标签: c++ parsing boost boost-spirit boost-spirit-qi


【解决方案1】:

离开构造函数后,qq 的引用变得悬空,确实是个问题。

qi::locals 是在解析器表达式中保持本地状态的规范方法。您的另一个选择是延长 qq 的生命周期(例如,通过使其成为语法类的成员)。最后,您可能也对 inherited attributes 感兴趣。这种机制为您提供了一种使用“参数”(传递本地状态)调用规则/语法的方法。

注意使用 kleene 运算符 + 有一些注意事项:它是贪婪的,如果字符串没有以预期的引号终止,则解析失败。

查看我写的另一个答案,以获取更完整的处理(可选/部分)引用字符串中的任意内容的示例,它允许在引用字符串中转义引号以及更多类似的内容:

我已将语法简化为相关部分,并包含了一些测试用例:

#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/fusion/adapted.hpp>

namespace qi = boost::spirit::qi;

template <typename Iterator>
struct test_parser : qi::grammar<Iterator, std::string(), qi::space_type, qi::locals<char> >
{
    test_parser() : test_parser::base_type(any_string, "test")
    {
        using namespace qi;

        quoted_string = 
               omit    [ char_("'\"") [_a =_1] ]             
            >> no_skip [ *(char_ - char_(_a))  ]
            >> lit(_a)
        ; 

        any_string = quoted_string | +qi::alnum;
    }

    qi::rule<Iterator, std::string(), qi::space_type, qi::locals<char> > quoted_string, any_string;
};

int main()
{
    test_parser<std::string::const_iterator> grammar;
    const char* strs[] = { "\"str1\"", 
                           "'str2'",
                           "'str3' trailing ok",
                           "'st\"r4' embedded also ok",
                           "str5",
                           "str6'",
                           NULL };

    for (const char** it = strs; *it; ++it)
    {
        const std::string str(*it);
        std::string::const_iterator iter = str.begin();
        std::string::const_iterator end  = str.end();

        std::string data;
        bool r = phrase_parse(iter, end, grammar, qi::space, data);

        if (r)
            std::cout << "Parsed:    " << str << " --> " << data << "\n";
        if (iter!=end)
            std::cout << "Remaining: " << std::string(iter,end) << "\n";
    }
}

输出:

Parsed:    "str1" --> str1
Parsed:    'str2' --> str2
Parsed:    'str3' trailing ok --> str3
Remaining: trailing ok
Parsed:    'st"r4' embedded also ok --> st"r4
Remaining: embedded also ok
Parsed:    str5 --> str5
Parsed:    str6' --> str6
Remaining: '

【讨论】:

  • 谢谢,这正是我所追求的。您能否发布指向有关当地人的任何文档/示例的链接,我花了一段时间才注意到规则签名中的qi::local&lt;char&gt;,这对我和其他任何查看这个问题的人来说都是一个很好的参考。
  • @Zero 谢谢!而且,erm qi::locals 是我的回答中的一个超链接 :) - 点击查看文档
  • @Zero 对于一个好的示例,我会参考您在问题中链接到的页面,特别是这里:One More Take
  • 啊哈,明白了——在One More Take 的底部,他们谈到了“locals”模板参数。再次感谢。
  • 略微改进了字符串文字的解析(接受引号内的任何文本)。现在也有固定测试
猜你喜欢
  • 1970-01-01
  • 2017-08-10
  • 1970-01-01
  • 2023-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多