Disallowing ?:?: syntax
BCS
ao at pathlink.com
Mon Jan 5 10:08:02 PST 2009
Reply to Miles,
> 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).
>
You have just proven my exact point. To find how it parses you need to dig
up the syntax, as you just did (Note I said that you can't tell how it's
parsed *without* doing that).
Also note that most (or all) of the other operators in C/C++/C#/D/etc go
the other way:
OrOrExpression:
AndAndExpression
OrOrExpression || AndAndExpression
> 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.
>
Yes, I think you've spotted the point. I'm referring to ambiguities that
will cause problems with <joke>I-BAL</joke> type parsers.
More information about the Digitalmars-d
mailing list