precedence of && (was: precedence of ?:)

Doug Gwyn gwyn at smoke.BRL.MIL
Thu Sep 14 22:23:03 AEST 1989


In article <8034 at goofy.megatest.UUCP> djones at megatest.UUCP (Dave Jones) writes:
>But let's be generous and give him partial credit, assuming that
>the grammar is the Law and the precedence table is for amusement
>purposes only.

Don't give me any credit, because it turned out that there was no
legal parse for the expression at all.  The grammar IS the Law.
I don't know exactly WHAT the precedence table is for, but it may
be intended as a convenient quick way to check whether one needs
to use grouping parentheses when writing code, without having to
painstakingly work through the production rules.

>I would like to hear the rationale behind creating such a convoluted
>grammar, when the precedence rules are so much easier to understand.

Having recently made several mistakes with this, I'm perhaps not
the best source for that rationale.  Note, however, that the grammar
allowed me to eventually figure out the correct answer, whereas the
precedence rules initially led me to jump to a false conclusion.
(I made an error in applying the grammar, too, but that could be
manually verified and corrected.  It wasn't the fault of the grammar.)

I don't find the grammar especially "convoluted"; in fact it's
relatively straightforward.  The reason for the several types of
expression is precisely to reflect the correct precedence without
requiring precedence to be provided by means outside the grammar.

>Besides, the precedence rules are also much better for compiler-
>writers, too!

I disagree with this.  In implementing several language translators,
I've always used the formal grammar production rules directly in the
implementation.  I've never had occasion to try to impose precedence
rules any other way.



More information about the Comp.lang.c mailing list