1

我尝试使用由以下规则构造的语法来解析字符串“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 中遇到访问冲突。我究竟做错了什么?

4

2 回答 2

2

I think the reason can be found here: Copy or reference semantics of boost::spirit's rule<>? - especially with:

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.

this was not the case.

于 2012-12-05T13:11:31.997 回答
0

而不是这个......

char const* last = &first[testString.size()];

尝试这个....

char const* last = &first[testString.size()-1];
于 2012-12-05T11:07:00.247 回答