precedence of ?:

Doug Gwyn gwyn at smoke.BRL.MIL
Wed Sep 13 01:54:05 AEST 1989


In article <11030 at smoke.BRL.MIL>, gwyn at smoke.BRL.MIL (Doug Gwyn) writes:
> In article <1265 at gmdzi.UUCP> wittig at gmdzi.UUCP (Georg Wittig) writes:
> -How should
> -	0 ? 0 : i = 0
> -be interpreted?
> -1)	as	(0) ? (0) : (i=0)
> -	resulting in a (strange but) legal expression
> -or 2)	as	(0 ? 0 : i) = 0
> -	resulting in a syntax error
> The latter is the correct parse.  See the table on p. 49 of K&R I.

OOPS!  As Steve Emmerson was the first to point out to me, the correct
way to parse expressions in C is to follow the formal grammar reduction
rules, not rely on the precedence/associativity tables.  Because the
left operand in an assignment expression cannot be a conditional
expression (it is constrained to be a unary expression with lvalueness),
there is no legal way to parse the example into the second form.  Thus
I was mistaken, as were the compiler implementors who parsed it that
way.  Perhaps they made the same mistake I just did.



More information about the Comp.lang.c mailing list