context-free grammar

Jonathan M Davis jmdavisProg at gmx.com
Sun Mar 6 22:47:08 PST 2011


On Sunday 06 March 2011 22:38:50 %u wrote:
> I didn't see this example being mentioned in this thread (although I
> might have missed this), but would someone explain why (1) the code
> below doesn't compile, and (2) why it's considered context-free?
> 
> struct MyStruct { ref MyStruct opMul(MyStruct x) { return this; } }
> ...
> OpOverloadAbuse a, b;
> a * b = b;

That's essentially the example that's been under discussion - though in this 
case it's a ref instead of a temporary for the lvalue. Regardless, it's context 
free because a * b is by definition a variable declaration, not a call to the 
multiplication operator. If you want it to use the multiplication operator, then 
use parens: (a * b) = b. It's context free, because it just assumes one of the 
two and it's _always_ that one, so there's no ambiguity. It is, _by definition_, 
a variable declaration.

Also, opMul is on its way to deprecation. binaryOp should be used for 
overloading the multiplication operator.

- Jonathan M Davis


More information about the Digitalmars-d mailing list