precedence of && (was: precedence of ?:)
Doug Gwyn
gwyn at smoke.BRL.MIL
Thu Sep 14 04:38:19 AEST 1989
In article <3242 at solo12.cs.vu.nl> maart at cs.vu.nl (Maarten Litmaath) writes:
>gwyn at smoke.BRL.MIL (Doug Gwyn) writes:
>\... there is no legal way to parse
>\ 0 && i = 0
>\as
>\ (0 && i) = 0
>\but there is a legal parse as
>\ 0 && (i = 0)
Actually, now that I've gotten back to my desk where I keep a copy of
the proposed Standard, I find that you must supply the parentheses to
get a legal parse. Without them there is no derivation from the
grammar production rules. (I think. I keep getting this wrong.)
>gcc (ANSI or what?) does accept
> 0 ? 0 : i = 0
>but it does NOT accept
> 0 && i = 0
>In fact, I've never used a C compiler that accepted the latter construct.
>Of course I fully agree it should be accepted.
Maybe the intention is to avoid confusion. For example,
i = 0 && i = 0
would either have to have an ambiguous parse, or else its interesting
subexpression would be parsed differently depending on context, which
is confusing.
I seem to recall Dave Prosser telling me that the Standard's grammar
for C (apart from the preprocessor) constituted a "phrase structure"
grammar. Perhaps if I knew what that meant I'd understand the
rationale behind these particular expression parsing rules.
It does appear that the current rules avoid parsing ambiguity. That's
probably a worthwhile constraint.
More information about the Comp.lang.c
mailing list