Boost logo

Boost :

From: Hartmut Kaiser (hartmutkaiser_at_[hidden])
Date: 2003-09-07 02:35:44


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


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk