ken@turtleva.UUCP (Ken Turkowski) (01/11/84)
I never trust the precedence or associativity of any operators (how's that for superstition?). My rule is, use parentheses, unless the only operators involved are {-, +, *}. Ken Turkowski CADLINC, Palo Alto {decwrl,amd70}!turtlevax!ken
keesan@bbncca.ARPA (Morris Keesan) (01/12/84)
----------------------------- Steve Summit asks, >>>> How should ?: group? The C Reference Manual says it groups left >>>> to right, just like all the binary operators. In the case of a >>>> ternary operator it's not as obvious what "left to right" means, >>>> but I would think that a?b:c?d:e should be evaluated as >>>> (a?b:c)?d:e (do the leftmost one first). I agree that this is the correct interpretation of "left to right" in this case. However, what we have here is a genuine bug in the C Reference Manual, which says in section 7.13 (page 191 of K&R), "Conditional expressions group right-to-left," and in section 18.1 (page 215 of K&R), "Binary operators and the conditional operator all group left-to-right. . ." I believe that the right-to-left grouping is correct because section 18 (page 214, K&R) says, "This summary of C syntax is intended more for aiding comprehension than as an exact statement of the language." This seems to indicate that section 7.13 takes precedence. FLASH!! I just took a look at the UN*X System V "Programming Guide" published by Western Electric, and the error has been fixed. In that manual, the syntax summary agrees that the conditional operator groups right-to-left. Personally, I feel that if practical compilers should issue a "Warning: cryptic code" message when encountering unparenthesized expressions like the above. -- Morris M. Keesan {decvax,linus,wjh12}!bbncca!keesan keesan @ BBN-UNIX.ARPA
bobw@onyx.UUCP (Bob White) (01/12/84)
You had better recheck your C Reference Manual -- The K & R book (page 49) says that ?: groups RIGHT to LEFT.
stevesu@azure.UUCP (Steve Summit) (01/15/84)
I'm usually pretty bored with these nitty-gritty compiler
questions, because I don't want to think that hard, but now I'm
working on a compiler myself, and I came up with one.
How should ?: group? The C Reference Manual says it groups left
to right, just like all the binary operators. In the case of a
ternary operator it's not as obvious what "left to right" means,
but I would think that a?b:c?d:e should be evaluated as
(a?b:c)?d:e (do the leftmost one first). The first time I
implemented it, I ended up with something that would interpret it
as a?b:(c?d:e), for no other reason than because it was easier to
implement that way. Lo and behold, that's what the 4.1 compiler
does, too! As a quick example, the program
main()
{
printf("%d\n", 1?1:0?3:4);
}
prints 1. (1?1:0)?3:4 would be 3.
I'm not too worried about this, because the "compiler" I'm working
on is actually just a stripped-down version of cpp, and I'm
reasonably sure that nobody is ever going to use a ?: on an #if
line in this application. Still, it is a curious question.
Steve Summit
tektronix!tekmdp!stevesu