Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!ames!haven!adm!smoke!gwyn From: gwyn@smoke.BRL.MIL (Doug Gwyn) Newsgroups: comp.lang.c Subject: Re: precedence of ?: Message-ID: <11039@smoke.BRL.MIL> Date: 12 Sep 89 15:54:05 GMT References: <1265@gmdzi.UUCP> <11030@smoke.BRL.MIL> Organization: Ballistic Research Lab (BRL), APG, MD. Lines: 19 In article <11030@smoke.BRL.MIL>, gwyn@smoke.BRL.MIL (Doug Gwyn) writes: > In article <1265@gmdzi.UUCP> wittig@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.