|
Boost : |
From: Michael Burbidge (mburbidg_at_[hidden])
Date: 2003-09-08 09:56:58
Great, thanks. I was puzzled for a while as to why the example
ast_calc.cpp worked since it didn't declare an indirection rule as the
root rule of the parser. The reason is because the start() method is
declared to return a rule<ScannerT, parser_context,
parser_tag<expressionID> > const&, instead of a rule<ScannerT> const&
like I was declaring.
Bottom like is that I can fix my problem either as you describe or
declare the start() method to return the right type so that a temporary
object does not have to be created.
Thanks for the help!
Michael-
On Sunday, September 7, 2003, at 12:35 AM, Hartmut Kaiser wrote:
> Michael Burbidge wrote:
>
>> The following is a simple program that uses spirit to parse a trivial
>> grammar. Of course my real grammar is not so simple, but I've
>> narrowed
>> it down to a very small example. When I run the program it crashes
>> during the call to parse. I've tried this program on Windows and
>> Macintosh and using the spirit in boost 1.30.2 and the latest spirit
>> 1.7.0. They all crash. This code was derived from the ast
>> calc example.
>>
>> If I change the line:
>>
>> rule<ScannerT, parser_context, parser_tag<escaped_expr_id> >
>> escaped_expr;
>>
>> to:
>>
>> rule<ScannerT > escaped_expr;
>>
>> it doesn't crash. But since I'm trying to build a abstract
>> syntax tree
>> I really need the rule id. Can anyone tell me how I might change my
>> simple program so that it doesn't crash?
>
> My compiler spits out a warning about returning a reference to a
> temporary object here:
>
> rule<ScannerT, parser_context, parser_tag<escaped_expr_id> >
> escaped_expr;
> rule<ScannerT> const& start() const
> { return escaped_expr; } // <<--- HERE
>
> which is the root of your problem. The compiler has to implicitely
> construct a temporary of the type rule<ScannerT> to fit your type
> conversion needs. To solve this problem you'd need to insert another
> indirection rule at the top of your parser hierarchy:
>
> template <typename ScannerT>
> struct definition
> {
> definition(expr_grammar const& self)
> {
> start_rule = escaped_expr;
> escaped_expr
> = ch_p('{') >> ch_p('}')
> ;
> }
>
> rule<ScannerT, parser_context, parser_tag<escaped_expr_id> >
> escaped_expr;
> rule<ScannerT> start_rule;
> rule<ScannerT> const& start() const { return start_rule; }
> };
>
>
> HTH
> Regards Hartmut
>
>
>
>
>
> -------------------------------------------------------
> This sf.net email is sponsored by:ThinkGeek
> Welcome to geek heaven.
> http://thinkgeek.com/sf
> _______________________________________________
> Spirit-general mailing list
> Spirit-general_at_[hidden]
> https://lists.sourceforge.net/lists/listinfo/spirit-general
>
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk