programming puzzle (silly)

steve emmerson steve at umigw.MIAMI.EDU
Tue Mar 14 00:40:31 AEST 1989


In article <24820 at amdcad.AMD.COM> tim at amd.com (Tim Olson) opines that the
expression "n&&m*=n--" is incorrect C:

>Interesting -- the program *is* incorrect, because && has higher
>precedence than *= so it is parsed as:
>
>   (n&&m) *= n--
>
>which is illegal because (n&&m) is not an lvalue.

I belive the correctness of a expression is determined by whether or not
the expression can be *generated from* the grammer's production rules; not
whether or not a given implementation of a compiler can parse it.

The given expression is correct C as it can be generated by the following
rules:

    expresion -> expression1 binop expression2
        expression1 -> identifier -> n
        binop       -> &&
        expression2 -> lvalue asgnop expression
            lvalue      -> identifier -> m
            asgnop      -> *=
            expression  -> lvalue-- -> identifier-- -> n--

Thus,
    expression -> n && m *= n--

Precedence rules are needed only when there is more than one way to 
generate a given expression and (consequently) more than one way to parse 
it.  They appear to be unnecessary for the above expression.
-- 
Steve Emmerson                     Inet: steve at umigw.miami.edu [128.116.10.1]
SPAN: miami::emmerson (host 3074::)      emmerson%miami.span at star.stanford.edu
UUCP: ...!ncar!umigw!steve               emmerson%miami.span at vlsi.jpl.nasa.gov
"Computers are like God in the Old Testament: lots of rules and no mercy"



More information about the Comp.lang.c mailing list