3

更新

这个问题涉及两个问题(如接受的答案所示),这两个问题都存在于 Boost 1.64 附带的 Boost Spirit X3 版本中,但现在都已修复(或至少在编译时检测到)在撰写本文时(2017-04-30)开发分支。

我已经更新了mcve 项目以反映我使用开发分支而不是最新的 boost 版本所做的更改,希望它可以帮助其他面临类似问题的人。


原来的问题

我正在尝试学习如何将 Spirit X3 解析器分解为单独的可重用语法,正如示例代码(尤其是 rexpr_full 和 calc)以及CppCon 2015BoostCon的演示文稿所鼓励的那样。

我有一个符号表(基本上将不同类型映射到我支持的类型的枚举类),我想在多个解析器中重用它。我能找到的符号表的唯一示例是罗马数字示例,它位于单个源文件中。

当我尝试以更结构化示例的样式将符号表移动到其自己的 cpp/h 文件中时,如果我尝试解析不在符号表中的任何字符串,我的解析器将出现段错误。如果符号表是在与使用它的解析器相同的编译单元中定义的,则会引发预期异常(这是我期望它做的)。

定义 BOOST_SPIRIT_X3_DEBUG 后,我得到以下输出:

<FruitType>
  <try>GrannySmith: Mammals</try>
  <Identifier>
    <try>GrannySmith: Mammals</try>
    <success>: Mammals</success>
    <attributes>[[
Process finished with exit code 11

我做了一个小项目,展示了我想要实现的目标,可以在这里找到:https ://github.com/sigbjornlo/spirit_fruit_mcve

我的问题:

  • 为什么在这种情况下将符号解析器移动到单独的编译单元会导致分段错误?
  • 使符号表可在多个解析器中重用的推荐方法是什么?(在 MCVE 中,我显然只fruit在另一个解析器中使用解析器,但在我的完整项目中,我想在其他几个解析器中使用它。)

以下是 MCVE 项目的代码:

主文件

#include <iostream>

#define BOOST_SPIRIT_X3_DEBUG
#include <boost/spirit/home/x3.hpp>
#include <boost/fusion/include/adapt_struct.hpp>

#include "common.h"
#include "fruit.h"

namespace ast {
    struct FruitType {
        std::string identifier;
        FRUIT fruit;
    };
}

BOOST_FUSION_ADAPT_STRUCT(ast::FruitType, identifier, fruit);

namespace parser {
    // Identifier
    using identifier_type = x3::rule<class identifier, std::string>;
    const auto identifier = identifier_type {"Identifier"};
    const auto identifier_def = x3::raw[x3::lexeme[(x3::alpha | '_') >> *(x3::alnum | '_')]];
    BOOST_SPIRIT_DEFINE(identifier);

    // FruitType
    struct fruit_type_class;
    const auto fruit_type = x3::rule<fruit_type_class, ast::FruitType> {"FruitType"};

    // Using the sequence operator creates a grammar which fails gracefully given invalid input.
    // const auto fruit_type_def = identifier >> ':' >> make_fruit_grammar();

    // Using the expectation operator causes EXC_BAD_ACCESS exception with invalid input.
    // Instead, I would have expected an expectation failure exception.
    // Indeed, an expectation failure exception is thrown when the fruit grammar is defined here in this compilation unit instead of in fruit.cpp.
    const auto fruit_type_def = identifier > ':' > make_fruit_grammar();

    BOOST_SPIRIT_DEFINE(fruit_type);
}

int main() {
    std::string input = "GrannySmith: Mammals";
    parser::iterator_type it = input.begin(), end = input.end();

    const auto& grammar = parser::fruit_type;
    auto result = ast::FruitType {};

    bool successful_parse = boost::spirit::x3::phrase_parse(it, end, grammar, boost::spirit::x3::ascii::space, result);
    if (successful_parse && it == end) {
        std::cout << "Parsing succeeded!\n";
        std::cout << result.identifier << " is a kind of " << to_string(result.fruit) << "!\n";
    } else {
        std::cout << "Parsing failed!\n";
    }

    return 0;
}

std::string to_string(FRUIT fruit) {
    switch (fruit) {
        case FRUIT::APPLES:
            return "apple";
        case FRUIT::ORANGES:
            return "orange";
    }
}

常见的.h

#ifndef SPIRIT_FRUIT_COMMON_H
#define SPIRIT_FRUIT_COMMON_H

namespace x3 = boost::spirit::x3;

enum class FRUIT {
    APPLES,
    ORANGES
};

std::string to_string(FRUIT fruit);

namespace parser {
    using iterator_type = std::string::const_iterator;
    using context_type = x3::phrase_parse_context<x3::ascii::space_type>::type;
}

#endif //SPIRIT_FRUIT_COMMON_H

水果.h

#ifndef SPIRIT_FRUIT_FRUIT_H
#define SPIRIT_FRUIT_FRUIT_H

#include <boost/spirit/home/x3.hpp>

#include "common.h"

namespace parser {
    struct fruit_class;
    using fruit_grammar = x3::rule<fruit_class, FRUIT>;

    BOOST_SPIRIT_DECLARE(fruit_grammar)

    fruit_grammar make_fruit_grammar();
}


#endif //SPIRIT_FRUIT_FRUIT_H

水果.cpp

#include "fruit.h"

namespace parser {
    struct fruit_symbol_table : x3::symbols<FRUIT> {
        fruit_symbol_table() {
            add
                    ("Apples", FRUIT::APPLES)
                    ("Oranges", FRUIT::ORANGES);
        }
    };

    struct fruit_class;
    const auto fruit = x3::rule<fruit_class, FRUIT> {"Fruit"};
    const auto fruit_def = fruit_symbol_table {};
    BOOST_SPIRIT_DEFINE(fruit);

    BOOST_SPIRIT_INSTANTIATE(fruit_grammar, iterator_type, context_type);

    fruit_grammar make_fruit_grammar() {
        return fruit;
    }
}
4

1 回答 1

4

非常好的复制器工作。这让我想起了我的 PR https://github.com/boostorg/spirit/pull/229(参见此处的分析Strange semantic behavior of boost spirit x3 after split)。

问题将是静态初始化命令 Fiasco 在初始化之前复制规则的调试名称。

事实上,确实禁用调试信息确实消除了崩溃,并正确地抛出了预期失败。

开发分支¹也是如此,所以要么有另一个类似的事情,要么我错过了一个地方。现在,知道您可以禁用调试输出。如果我找到位置,我会发布更新。

更新:

我没有错过任何地方。还有一个单独的问题call_rule_definition,它使用实际属性类型而不是转换后的属性类型参数化context_debug<>帮助程序类:

#if defined(BOOST_SPIRIT_X3_DEBUG)
                typedef typename make_attribute::type dbg_attribute_type;
                context_debug<Iterator, dbg_attribute_type>
                dbg(rule_name, first, last, dbg_attribute_type(attr_), ok_parse);
#endif

该评论似乎表明这种行为是所希望的:它尝试在转换之前打印属性。但是,除非合成的属性类型与实际的属性类型匹配,否则它完全不起作用。在这种情况下,它context_debug会对临时转换的属性进行悬空引用,从而导致Undefined Behavior

事实上,这也是工作案例中未定义的行为。我只能假设在内联定义的情况下事情发生得很好,使它看起来像预期的那样工作。

据我所知,这将是一个干净的解决方案,可以防止任何无根据的转换和随之而来的临时转换:

#if defined(BOOST_SPIRIT_X3_DEBUG)
                context_debug<Iterator, transform_attr>
                dbg(rule_name, first, last, attr_, ok_parse);
#endif

我为此创建了拉取请求:https ://github.com/boostorg/spirit/pull/232


¹开发分支似乎没有合并到1.64版本中

于 2017-04-28T20:21:02.257 回答