Context-Free Grammars? What about arrays?

Jonathan M Davis jmdavisProg at gmx.com
Fri Feb 11 04:01:55 PST 2011


On Friday 11 February 2011 03:22:54 spir wrote:
> On 02/11/2011 10:03 AM, %u wrote:
> > Hi,
> > 
> > I think I'm having a little trouble understanding what's meant by
> > context-free grammar. I've read that D is context-free, but is it
> > really? What about an expression like:
> > 
> > int[U] s;
> > 
> > ? You can't tell -- without looking at the context -- whether U is a data
> > type or a number, and so because associative arrays and regular arrays
> > are syntactically different elements of the language, the syntax of D is
> > tied in with its semantics, just like in C++.
> > 
> > So is D really context-free? Or am I misunderstanding the meaning of the
> > term?
> 
> Have you tried it? int[U] cannot be a plain array because the size must be
> constant. But I agree there is some context in play for the compiler (or
> rather the linker?) to determine /that/:
> --> some error if U undefined
> --> some other error if defined, but neither uint nore type
> --> some other error if uint but not constant
> --> ass array if type
> (I guess)

Actually, it _can_ be a plain, static array. U could be a template argument 
which is a number.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list