Path: utzoo!attcan!uunet!mcvax!ukc!dcl-cs!aber-cs!pcg From: pcg@aber-cs.UUCP (Piercarlo Grandi) Newsgroups: comp.lang.c Subject: Re: Why 'struct foo *x' instead of 'foo *x' Summary: To aid a recursive descent parser Message-ID: <972@aber-cs.UUCP> Date: 28 May 89 11:58:24 GMT Reply-To: pcg@cs.aber.ac.uk (Piercarlo Grandi) Organization: Dept of CS, UCW Aberystwyth (Disclaimer: my statements are purely personal) Lines: 28 In article <9137@csli.Stanford.EDU> jkl@csli.stanford.edu (John Kallen) writes: Does anybody know the reason why structs are defined as they are in C? I.e. why do I have to say "struct foo" instead of just the tag "foo"? There are others I can think of, but most importantly is to make the grammar more palatable to a recursive descent parser. In this way, by looking at the first token in a "statement" you know whether it is a declaration (begins with a a storage class, "struct", a fundamental type name, ... all of which are reserved keywords). Typedef'ed names require In C++ it is difficult (without infinite lookahead, i.e. context sensitive parsing, just like Algol68) to distinguish declarations from imperative statements, especially as they now may appear at any point in a block, and especially as the '=' in an initializer can now be omitted again (even if in a different way from early C, but with much the same ambiguities). In certain contexts, some compilers require you to insert a storage class specifier to help making the context less ambiguous. Indeed, I am now of the opinion that it may help even the human reader to start any and every declaration in C++ with the storage class, even if it is redundant (like auto for locals and extern for globals). -- Piercarlo "Peter" Grandi | ARPA: pcg%cs.aber.ac.uk@nsfnet-relay.ac.uk Dept of CS, UCW Aberystwyth | UUCP: ...!mcvax!ukc!aber-cs!pcg Penglais, Aberystwyth SY23 3BZ, UK | INET: pcg@cs.aber.ac.uk