更新
这个问题涉及两个问题(如接受的答案所示),这两个问题都存在于 Boost 1.64 附带的 Boost Spirit X3 版本中,但现在都已修复(或至少在编译时检测到)在撰写本文时(2017-04-30)开发分支。
我已经更新了mcve 项目以反映我使用开发分支而不是最新的 boost 版本所做的更改,希望它可以帮助其他面临类似问题的人。
原来的问题
我正在尝试学习如何将 Spirit X3 解析器分解为单独的可重用语法,正如示例代码(尤其是 rexpr_full 和 calc)以及CppCon 2015和BoostCon的演示文稿所鼓励的那样。
我有一个符号表(基本上将不同类型映射到我支持的类型的枚举类),我想在多个解析器中重用它。我能找到的符号表的唯一示例是罗马数字示例,它位于单个源文件中。
当我尝试以更结构化示例的样式将符号表移动到其自己的 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;
}
}