Disallowing ?:?: syntax

Miles _______ at _______.____
Mon Jan 5 09:45:56 PST 2009


BCS wrote:
> I think not.
> 
> x ? y : a ? b : c => (x ? y : a) ? b : c
> 
> or
> 
> x ? y : a ? b : c => x ? y : (a ? b : c)
> 
> without checking the actual syntax you can't tell which of the above
> will be used and (according to bearophile) if ?: followed after +/-/etc
> the first would be.

It simply can't be the first, due to the kind of parser used for C and
most of its derived languages (D included). When a '?' is found, the
parser recurses until it finds a ':' (it gets stuck in a branch of the
syntax tree until a colon token is found).

This is in how the language is defined
(http://www.digitalmars.com/d/2.0/expression.html):

	ConditionalExpression:
		OrOrExpression
		OrOrExpression ? Expression : ConditionalExpression

So, the third operand to the ternary operator is a ConditionalExpression
itself, the parser have no reason to finish this evaluation branch if it
finds another '?', it naturally recurses.

So, x ? y : a ? b : c => x ? y : (a ? b : c).

But I think that I know what kind of ambiguity you are talking about
now... For me, ambiguity is something like the <...> C++ template
definition/instantiation operator, or the function declaration/object
variable definition ambiguity.



More information about the Digitalmars-d mailing list