boost :: spirit :: qi Expectation Parser和parser对意外行为进行分组

Zer*_*ero 4 c++ parsing boost boost-spirit boost-spirit-qi

我希望有人可以通过我在精神解析中使用>>>操作符的无知来发光.

我有一个工作语法,顶级规则看起来像

test = identifier >> operationRule >> repeat(1,3)[any_string] >> arrow >> any_string >> conditionRule;
Run Code Online (Sandbox Code Playgroud)

它依赖于属性来自动将解析后的值分配给融合自适应结构(即boost元组).

但是,我知道一旦我们匹配operationRule,我们必须继续或失败(即我们不希望允许回溯尝试以其他规则开头identifier).

test = identifier >> 
           operationRule > repeat(1,3)[any_string] > arrow > any_string > conditionRule;
Run Code Online (Sandbox Code Playgroud)

这会导致一个神秘的编译器错误('boost::Container' : use of class template requires template argument list).Futz一点点以下编译:

test = identifier >> 
           (operationRule > repeat(1,3)[any_string] > arrow > any_string > conditionRule);
Run Code Online (Sandbox Code Playgroud)

但属性设置不再有效 - 我的数据结构在解析后包含垃圾.这可以通过添加类似的操作来修复[at_c<0>(_val) = _1],但这似乎有点笨拙 - 以及根据boost文档使事情变慢.

所以,我的问题是

  1. 是否值得阻止反向追踪?
  2. 为什么我需要分组运算符 ()
  3. 我的上一个例子在operationRule匹配之后是否真的停止了回溯(我怀疑不是,似乎如果(...)允许整个解析器内部失败回溯)?
  4. 如果上一个问题的答案是/ no /,如何构建允许回溯的规则,如果operation是/不匹配,但是一旦操作/匹配就不允许回溯?
  5. 为什么分组操作符会破坏属性语法 - 需要操作?

我意识到这是一个相当广泛的问题 - 任何指向正确方向的提示都将受到高度赞赏!

seh*_*ehe 5

  1. 是否值得阻止反向追踪?

    绝对.一般来说,防止反向跟踪是提高解析器性能的可靠方法.

    • 减少(负)前瞻(操作员!,操作员 - 以及某些操作员&)的使用
    • 命令分支(运算符|,运算符||,运算符^和某些运算符*/ - /+),以便首先排序最频繁/可能的分支,或者最后尝试最昂贵的分支

    使用期望点(>)基本上不会减少回溯:它只是不允许它.这将启用有针对性的错误消息,防止无用的"解析到未知".

  2. 为什么我需要分组 operator ()

    我不确定.我从这里使用我的助手检查了一下what_is_the_attr

    我没有发现需要使用括号(事物编译)对子表达式进行分组,但显然DataT需要修改以匹配更改的布局.

    typedef boost::tuple<
        boost::tuple<std::string, std::string>, 
        std::vector<std::string>, 
        std::string
    > DataT;
    
    Run Code Online (Sandbox Code Playgroud)

下面的完整代码显示了我喜欢如何使用改编的结构.

  1. 我的上述示例是否真的在operationRule匹配后停止回溯(我怀疑没有,似乎如果(...)内的整个解析器失败回溯将被允许)?

    绝对.如果未满足期望,qi::expectation_failure<>则抛出异常.这默认情况下会中止解析.你可以使用气:: ON_ERROR到retry,fail,acceptrethrow.该MiniXML例如对使用的期望点具有非常好的例子qi::on_error

  2. 如果上一个问题的答案是/ no /, 如何构建一个规则,如果操作是/不匹配,则允许回溯,但是一旦操作/匹配,就不允许回溯?

  3. 为什么分组操作符会破坏属性语法 - 需要操作?

    它不会破坏属性语法,只是更改了暴露的类型.因此,如果将适当的属性引用绑定到规则/语法,则不需要语义操作.现在,我觉得应该有没有分组的方法,所以让我尝试一下(最好是你的短自我样本). 事实上,我没有找到这样的需要.我添加了一个完整的示例来帮助您了解我的测试中发生了什么,而不是使用语义操作.

完整代码

完整代码显示了5个场景:

  • 选项1:原始没有期望

    (无相关变化)

  • 方案2:满怀期望

    使用修改后的typedef进行DataT(如上所示)

  • 选项3:适应结构,没有期望

    将用户定义的结构与BOOST_FUSION_ADAPT_STRUCT一起使用

  • 选项4:适应结构,有期望

    从OPTION 3修改改编的结构

  • 选项5:前瞻黑客

    这一个利用了一个"聪明"(?)黑客攻击,通过使所有人都>>达到预期,并operationRule预先检测到匹配的存在.这当然不是最理想的,但允许您保持不变DataT,而不使用语义操作.

显然,OPTION在编译之前定义到所需的值.

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

namespace qi    = boost::spirit::qi; 
namespace karma = boost::spirit::karma; 

#ifndef OPTION
#define OPTION 5
#endif

#if OPTION == 1 || OPTION == 5 // original without expectations (OR lookahead hack)
    typedef boost::tuple<std::string, std::string, std::vector<std::string>, std::string> DataT;
#elif OPTION == 2 // with expectations
    typedef boost::tuple<boost::tuple<std::string, std::string>, std::vector<std::string>, std::string> DataT;
#elif OPTION == 3 // adapted struct, without expectations
    struct DataT
    {
        std::string identifier, operation;
        std::vector<std::string> values;
        std::string destination;
    };

    BOOST_FUSION_ADAPT_STRUCT(DataT, (std::string, identifier)(std::string, operation)(std::vector<std::string>, values)(std::string, destination));
#elif OPTION == 4 // adapted struct, with expectations
    struct IdOpT
    {
        std::string identifier, operation;
    };
    struct DataT
    {
        IdOpT idop;
        std::vector<std::string> values;
        std::string destination;
    };

    BOOST_FUSION_ADAPT_STRUCT(IdOpT, (std::string, identifier)(std::string, operation));
    BOOST_FUSION_ADAPT_STRUCT(DataT, (IdOpT, idop)(std::vector<std::string>, values)(std::string, destination));
#endif

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

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

        any_string = quoted_string | +qi::alnum;

        identifier = lexeme [ alnum >> *graph ];

        operationRule = string("add") | "sub";
        arrow = "->";

#if OPTION == 1 || OPTION == 3   // without expectations
        test = identifier >> operationRule >> repeat(1,3)[any_string] >> arrow >> any_string;
#elif OPTION == 2 || OPTION == 4 // with expectations
        test = identifier >> operationRule  > repeat(1,3)[any_string]  > arrow  > any_string;
#elif OPTION == 5                // lookahead hack
        test = &(identifier >> operationRule) > identifier > operationRule > repeat(1,3)[any_string] > arrow > any_string;
#endif
    }

    qi::rule<Iterator, qi::space_type/*, qi::locals<char> */> arrow;
    qi::rule<Iterator, std::string(), qi::space_type/*, qi::locals<char> */> operationRule;
    qi::rule<Iterator, std::string(), qi::space_type/*, qi::locals<char> */> identifier;
    qi::rule<Iterator, std::string(), qi::space_type, qi::locals<char> > quoted_string, any_string;
    qi::rule<Iterator, DataT(),       qi::space_type, qi::locals<char> > test;
};

int main()
{
    std::string str("addx001 add 'str1'   \"str2\"       ->  \"str3\"");
    test_parser<std::string::const_iterator> grammar;
    std::string::const_iterator iter = str.begin();
    std::string::const_iterator end  = str.end();

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

    if (r)
    {
        using namespace karma;
        std::cout << "OPTION " << OPTION << ": " << str << " --> ";
#if OPTION == 1 || OPTION == 3 || OPTION == 5 // without expectations (OR lookahead hack)
        std::cout << format(delimit[auto_ << auto_ << '[' << auto_ << ']' << " --> " << auto_], data) << "\n";
#elif OPTION == 2 || OPTION == 4 // with expectations
        std::cout << format(delimit[auto_ << '[' << auto_ << ']' << " --> " << auto_], data) << "\n";
#endif
    }
    if (iter!=end)
        std::cout << "Remaining: " << std::string(iter,end) << "\n";
}
Run Code Online (Sandbox Code Playgroud)

所有OPTIONS的输出:

for a in 1 2 3 4 5; do g++ -DOPTION=$a -I ~/custom/boost/ test.cpp -o test$a && ./test$a; done
OPTION 1: addx001 add 'str1'   "str2"       ->  "str3" --> addx001 add [ str1 str2 ]  -->  str3 
OPTION 2: addx001 add 'str1'   "str2"       ->  "str3" --> addx001 add [ str1 str2 ]  -->  str3 
OPTION 3: addx001 add 'str1'   "str2"       ->  "str3" --> addx001 add [ str1 str2 ]  -->  str3 
OPTION 4: addx001 add 'str1'   "str2"       ->  "str3" --> addx001 add [ str1 str2 ]  -->  str3 
OPTION 5: addx001 add 'str1'   "str2"       ->  "str3" --> addx001 add [ str1 str2 ]  -->  str3 
Run Code Online (Sandbox Code Playgroud)