我尝试使用按照以下规则构造的语法来解析字符串“1-2”:
spirit::qi::rule<Iterator, spirit::utf8_symbol_type()> op1 = "-";
spirit::qi::rule<Iterator, spirit::utf8_symbol_type()> op2 = "+";
spirit::qi::rule<Iterator, spirit::utree(), spirit::qi::space_type> numberParser = boost::spirit::qi::double_;
spirit::qi::rule<Iterator, spirit::utree(), spirit::qi::space_type> expressionParser;
expressionParser = numberParser >> -( (op1 >> expressionParser) | (op2 >> expressionParser));
start = expressionParser.alias();
其中 start 是我的语法类的成员:
spirit::qi::rule<Iterator, spirit::utree(), spirit::qi::space_type> start;
根据这里的规则,我想为每个二元运算 + 和 - 从左到右创建一个节点。
当我现在使用以下方法解析字符串“1-2”时:
void Parse(const std::string& testString, const CDynamicExpressionSyntaxParser<const char*>& parser)
{
char const* first = testString.c_str();
char const* last = &first[testString.size()];
boost::spirit::utree tree;
bool success = boost::spirit::qi::phrase_parse(first,last,parser, boost::spirit::qi::space,tree);
std::cout << "tree: " << tree << '\n';
}
我在 rule.hpp 中遇到访问冲突。我做错了什么?
最佳答案
我想原因可以在这里找到:Copy or reference semantics of boost::spirit's rule<>? - 特别是:
When a rule is referenced anywhere in the right hand side of an EBNF expression, the rule is held by the expression by reference. It is the responsibility of the client to ensure that the referenced rule stays in scope and does not get destructed while it is being referenced.
事实并非如此。
关于c++ - 使用 `alias()` 解析精神规则时发生访问冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13721346/