possible operator precedence bug?

kenny at m.cs.uiuc.edu kenny at m.cs.uiuc.edu
Fri Oct 7 03:08:00 AEST 1988


/* Written  6:17 pm  Oct  4, 1988 by jejones at mcrware.UUCP in m.cs.uiuc.edu:comp.lang.c */
/* ---------- "possible operator precedence bug?" ---------- */
[... expression that looks like]
|
|	M[Z]=Z[<hairy expression> ? <expression>, <expression>, "_." : " |"];
|
|The question is this: why does this make it through the compiler at all?
|K&R says that the comma operator has the lowest priority, so shouldn't
|the following be required for the code to correctly parse?
|
|	M[Z]=Z[<hairy expression> ? (<expression>, <expression>, "_.") : " |"];
|	                            ^                                ^
/* End of text from m.cs.uiuc.edu:comp.lang.c */

The precedence rules are used only when needed to resolve ambiguity
among two syntactically correct parses of the same sentence.  The use
of the comma between the query and the colon is unambiguous, and the
precedence rules do not come into play.  The relative precedence of ?:
and , is used is in ambiguous contexts like:

	x = ( a , b ? c : d );
and	x = ( a ? b : c , d);

where the higher precedence of ? : causes the statements to be
interpreted as

	x = ( a , ( b ? c : d ) );
and	x = ( ( a ? b : c ) , d ); respectively.

Kevin

Kevin Kenny			UUCP: {uunet,pur-ee,convex}!uiucdcs!kenny
Department of Computer Science	ARPA Internet or CSNet: kenny at CS.UIUC.EDU
University of Illinois
1304 W. Springfield Ave.
Urbana, Illinois, 61801		Voice: (217) 333-6680



More information about the Comp.lang.c mailing list