Pcc bites it (was Re: programming puzzle (silly))

steve emmerson steve at umigw.MIAMI.EDU
Tue Mar 21 00:47:42 AEST 1989


(This discussion reminds me of the skit on Saturday Night Live where one
side argues that a certain product is a dessert topping and the other argues 
that it's a floor wax :-).

I believe the string "n&&m*=n--" is legal under the grammar rules for
*K&R-1st edition C*.  Under pANS (or K&R-2nd edition C) however, I
believe it's illegal.  (Since in his original posting Tim Olson didn't 
say which grammar he was assuming, it turns out we're both right!).

I believe a given string is legal if it can be *generated* by the production
rules for the grammar.  Arguments as to whether or not a particular type 
of parser can correctly interpret a given string are, in my opinion, 
irrelevant.  The string "n&&m*=n--" can be generated from the production
rules for K&R-I C found in Appendix A.  Here's the parse tree:

                            expression
                                 |
            ---------------------------------------
            |             |                       |
        expression       binop                expression
            |             |                       |
            |             |         -------------------------------
            |             |         |             |               |
        primary           &&    expression      asgnop        expression
            |                       |             |               |
            |                       |             |         -------------
            |                       |             |         |           |
            n                   identifier        *=    expression     unop
                                    |                       |           |
                                    m                   identifier      --
                                                            |
                                                            n

It's up to the parser to (re)discover this parse tree upon encountering
the string "n&&m*=n--".  Some can; some can't.  Since it can be
*generated* from the K&R-I grammar, however, it's legal.

Admittedly, the Appendix A production rules are not very formal, still,
a very good argument can be made for their being the ultimate arbiter of
K&R-I C.  (They are certainly at least as valid as anything else ;-).
Arguments that the string is invalid Appendix A K&R-I C must show that

    1) the above parse tree is invalid; or
    2) the above parse tree is non-unique.  This would require the use
       of disambiguating precedence rules in order to parse it and a
       consequent conclusion that it's illegal.

In pANS C (and K&R-II C), on the other hand, the string "n&&m*=n--" cannot 
be generated---much less parsed.

Interesting, no?
-- 
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