1

I try to parse the string "1-2" using a grammar that is constructed with the following rules:

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();

where start is a member of my grammar class:

spirit::qi::rule<Iterator, spirit::utree(), spirit::qi::space_type> start;

with the rules here, I want to create a node for each of the binary operations + and - from left to right.

When I now parse the string "1-2" using the following method:

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'; 
}

I get an access violation in rule.hpp. What am I doing wrong?

sehe
  • 374,641
  • 47
  • 450
  • 633
Tobias Langner
  • 10,634
  • 6
  • 46
  • 76

2 Answers2

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.

Community
  • 1
  • 1
Tobias Langner
  • 10,634
  • 6
  • 46
  • 76
  • +1 foor good information. This might help someone else. I'm going to sneak in a mention of `rule::alias()` in the title or tags for SEO – sehe Dec 05 '12 at 16:28
0

Instead of this....

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

Try this....

char const* last = &first[testString.size()-1];
phonetagger
  • 7,701
  • 3
  • 31
  • 55