help-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: x + (y) + z


From: Kelly Leahy
Subject: Re: x + (y) + z
Date: Thu, 3 Mar 2005 21:02:01 -0800 (PST)

By the way, you might want to have a look at 

http://www.lysator.liu.se/c/ANSI-C-grammar-y.html

I don't know if it's different from what you are
working from, and I'm not sure how it does in Bison,
but it might be worth a try.

Kelly

--- Derek M Jones <address@hidden> wrote:

> All,
> 
> The statement (y)+z can be parsed as casting
> +z to the type y, or as adding y to z.  A couple of
> %dprecs solve this problem (I think the cast is the
> common case for - and a binary expression for +).
> However, things are more complicated for x + (y) +
> z,
> whose parse tree can be either
> 
>             +
>           /    \
>          x     ( )
>                /   \
>               y     +
>                      |
>                      z
> 
> or
> 
>                +
>               /   \
>             +     z
>            /  \
>          x    (y)
> 
> As currently implemented the %dprec functionality
> does
> not appear to be any help here.  Effectively, it
> will only
> select between two productions that consume the same
> number of input tokens.
> 
> At the moment the only solution I can think of is to
> change
> the grammar for additive-expressions from left
> recursive to
> right recursive, ie from
> 
> add-expr:
>                 mul-expr                    |
>                 add-expr '+' mult-expr |
>                 add-expr '-' mult-expr ;
> 
> to
> 
> add-expr:
>                 mul-expr                    |
>                 mult-expr '+' add-expr |
>                 mult-expr '-' add-expr ;
> 
> I don't like rewriting grammar like this and I'm
> sure
> more complicated cases will arise (I know there are
> other operators that need to be handled for this
> example).
> What is needed is a get-out-of-ambiguity option.
> The %gooa option would take a single argument which
> specified the weight of a production.  When two or
> more ambiguous parses are encountered the weights
> of the various productions involved would be added
> up and the one with the greatest weight selected.
> 
> derek
> 
> --
> Derek M Jones                                       
>    tel: +44 (0) 1252 520 667
> Knowledge Software Ltd                           
> mailto:address@hidden
> Applications Standards Conformance Testing  
> http://www.knosof.co.uk
> 
> 
> 
> 
> _______________________________________________
> address@hidden
> http://lists.gnu.org/mailman/listinfo/help-bison
> 





reply via email to

[Prev in Thread] Current Thread [Next in Thread]