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

Doug Gwyn gwyn at smoke.BRL.MIL
Sun Sep 17 09:41:14 AEST 1989


In article <3269 at solo5.cs.vu.nl> maart at cs.vu.nl (Maarten Litmaath) writes:
>\	conditional-expression:
>\		logical-OR-expression
>\		logical-OR-expression ? expression : conditional-expression
>\So the interesting question becomes, why is that "expression" for the
>\second operand of ?: and not "logical-OR-expression".  (I think that
>\"conditional-expression" might result in ambiguity.)  [...]
>I don't think so: a ? b ? c : d : e has only one parse.

I had thought perhaps if you got enough interlaced ?: pairs that they
might be parsed ambiguously.  However (see below), if that were the
case then the Standard would have the same problem.  After doing some
"yacc" experiments with ?: grammars for a while, I found that it's not
possible to parse them in the wrong order even under
logical-OR-expression ? conditional-expression : conditional-expression
However,
conditional-expression ? logical-OR-expression : conditional-expression
or
conditional-expression ? conditional-expression : conditional-expression
would lead to an ambiguous grammar.

>(If "expression" is OK, "conditional-expression" surely is too!)

Yes, "expression" can be expanded to produce "conditional-expression".
The more general form in the Standard appears to not introduce ambiguity.



More information about the Comp.lang.c mailing list