On Tue, Jun 23, 2009 at 1:00 AM, Ellery Newcomer<[email protected]> wrote: > Sorry for not posting this in learn, but I'd also like to hear the > Language Designer's input on this one. > > How does dmd resolve the declaration/expression ambiguity? > > My first instinct would be to try the declaration, and if it doesn't > work because the type doesn't exist or something like that then try the > expression, or vice versa. But that could easily lead to undefined and > unexpected behavior. what if both are valid?
You're right; if a statement begins with an identifier, the compiler requires arbitrary lookahead to determine whether it's looking at an expression or a declaration. There's a good bit of duplicated code in DMD dedicated to parsing declarations. IIRC there's one version of the parsing that just returns whether or not it's "probably" a declaration, and another version that does the exact same thing but which actually builds the AST. Kind of icky. But that being said, I don't think there are actually any ambiguities in the grammar when it comes to this. Neither of the "problem" lines in your example code could possibly be interpreted as declarations, and I don't think I can come up with any actually ambiguous code.
