3

parser.hpp在将语法拆分为推荐的, parser_def.hpp,parser.cpp文件后,我遇到了 boost spirit x3 的一种奇怪行为。我的示例语法解析了一些简单的枚举:

enum = "enum" > identifier > "{" > identifier % "," > "}

这是我的枚举语法。当我不将枚举和标识符解析器拆分为推荐的文件时,一切正常,尤其是字符串"enum {foo, bar}" 按预期抛出预期失败。这个例子可以在这里找到:未拆分的工作示例

但是当我将完全相同的语法拆分为不同的文件时,解析器会抛出

terminate called after throwing an instance of 'std::logic_error'
  what():  basic_string::_M_construct null not valid

试图解析相同的字符串"enum {foo, bar}"

这个例子可以在这里找到:分裂奇怪的例子

  1. ast.hpp

    #pragma once
    
    #include <vector>
    #include <string>
    #include <boost/fusion/include/adapt_struct.hpp>
    
    
    
    namespace ast{
    
    namespace x3 = boost::spirit::x3;
    
    struct Enum {
        std::string _name;
        std::vector<std::string> _elements;
    };
    
    
    }
    
    BOOST_FUSION_ADAPT_STRUCT(ast::Enum, _name, _elements)
    
  2. 配置文件

    #pragma once 
    
    #include <boost/spirit/home/x3.hpp>
    
    namespace parser{
    
        namespace x3 = boost::spirit::x3;
    
        typedef std::string::const_iterator iterator_type;
        typedef x3::phrase_parse_context<x3::ascii::space_type>::type context_type;
    
    }
    
  3. 枚举.cpp

    #include "enum_def.hpp"
    #include "config.hpp"
    
    namespace parser { namespace impl {
         BOOST_SPIRIT_INSTANTIATE(enum_type, iterator_type, context_type)
    }}
    
    namespace parser {
    
    const impl::enum_type& enum_parser()
    {
        return impl::enum_parser;
    }
    
    }
    
  4. 枚举定义.hpp

    #pragma once
    
    #include "identifier.hpp"
    #include "enum.hpp"
    #include "ast.hpp"
    
    namespace parser{ namespace impl{
    
        namespace x3=boost::spirit::x3;
    
        const enum_type enum_parser = "enum";
    
        namespace{
            const auto& identifier = parser::identifier();
        }
        auto const enum_parser_def =
            "enum"
            > identifier
            > "{"
            > identifier % ","
            >"}";
    
        BOOST_SPIRIT_DEFINE(enum_parser)
    }}
    
  5. 枚举.hpp

    #pragma once
    
    #include <boost/spirit/home/x3.hpp>
    #include "ast.hpp"
    
    namespace parser{ namespace impl{
        namespace x3=boost::spirit::x3;
    
        typedef x3::rule<class enum_class, ast::Enum> enum_type;
    
        BOOST_SPIRIT_DECLARE(enum_type)
    
    }}
    
    namespace parser{
        const impl::enum_type& enum_parser();
    }
    
  6. 标识符.cpp

    #include "identifier_def.hpp"
    #include "config.hpp"
    
    namespace parser { namespace impl {
         BOOST_SPIRIT_INSTANTIATE(identifier_type, iterator_type, context_type)
    }}
    
    namespace parser {
    
    const impl::identifier_type& identifier()
    {
        return impl::identifier;
    }
    
    }
    
  7. 标识符_def.hpp

    #pragma once
    #include <boost/spirit/home/x3.hpp>
    #include "identifier.hpp"
    
    namespace parser{ namespace impl{
    
        namespace x3=boost::spirit::x3;
    
        const identifier_type identifier = "identifier";    
    
        auto const identifier_def = x3::lexeme[
            ((x3::alpha | '_') >> *(x3::alnum | '_'))
        ];
    
        BOOST_SPIRIT_DEFINE(identifier)
    }}
    
  8. 标识符.hpp

    #pragma once
    #include <boost/spirit/home/x3.hpp>
    
    namespace parser{ namespace impl{
        namespace x3=boost::spirit::x3;
    
        typedef x3::rule<class identifier_class, std::string> identifier_type;
    
        BOOST_SPIRIT_DECLARE(identifier_type)
    }}
    
    
    namespace parser{
        const impl::identifier_type& identifier();
    }
    
  9. 主文件

    #include <boost/spirit/home/x3.hpp>
    #include "ast.hpp"
    #include "enum.hpp"
    
    namespace x3 = boost::spirit::x3;
    
    template<typename Parser, typename Attribute>
    bool test(const std::string& str, Parser&& p, Attribute&& attr)
    {
        using iterator_type = std::string::const_iterator;
        iterator_type in = str.begin();
        iterator_type end = str.end();
    
        bool ret = x3::phrase_parse(in, end, p, x3::ascii::space, attr);
        ret &= (in == end);
        return ret;
    
    }
    
    int main(){
        ast::Enum attr;
        test("enum foo{foo,bar}", parser::enum_parser(), attr);
        test("enum {foo,bar}", parser::enum_parser(), attr);    
    }
    

这是一个错误,我错过了什么,还是这是一个预期的行为?

编辑:是我的回购与一个抛出一个std::logic_error而不是一个例子expectation_failure

4

2 回答 2

4

我已经找到了错误的原因。

错误在于,expect 指令按值接受它的主题解析器,这是在parser::impl::identifier初始化程序运行之前。

为了形象化,想象一下parser::impl::enum_parser运行 before的静态初始化程序parser::impl::identifier。这对编译器来说是有效的。

因此,副本有一个未初始化的name字段,一旦期望点尝试x3::expectation_failurewhich_成员构造 a 就会失败,因为std::string从 a 构造 anullptr是非法的。

总而言之,我担心这里的根本原因是Static Initialization Order Fiasco。我会看看我是否可以修复它并提交 PR。

解决方法:

一个直接的解决方法是反向列出源文件的顺序,以便在定义之后使用:

set(SOURCE_FILES 
    identifier.cpp
    enum.cpp 
    main.cpp 
)

请注意,如果这可以在您的编译器上修复它(它在我的编译器上),那么它是实现定义的。该标准没有指定跨编译单元的静态初始化顺序。

于 2017-01-21T22:33:38.677 回答
2

这是一个对我有用的解决方案,当上述解决方法不起作用时。

假设您有文件a.cpp, a.h, a_def.hpp, b.cpp, b.h, b_def.hpp, ... 按照 Boost.Spirit X3 文档的建议。

基本思想是将*.cpp*_def.hpp文件合并为每个组的 1 个文件。这些*.h文件可以而且应该保留。

  1. ls *_def.hpp > parser_def.hpp(假设parser_def.hpp不存在)并parser_def.hpp#include正确的顺序编辑文件。删除多余的行(添加标题保护等)目标是parser_def.hpp按正确的顺序包含其他文件。
  2. cat *.cpp > parser.cpp并编辑parser.cpp为语法正确,并将所有#include <*_def.hpp>行替换#include <parser_def.hpp>为顶部的单行。在您的构建文件(例如 make 或 cmake)中,将*.cpp文件的编译替换为单个parser.cpp.
  3. 您可以摆脱旧*.cpp文件。

你失去了单独编译文件的便利,但它会避免静态初始化顺序惨败。

于 2019-06-06T21:03:38.273 回答