Re: Building a parse tree that reflects C Semantics

"David Thompson" <david.thompson1@worldnet.att.net>
25 Oct 2002 00:16:00 -0400

          From comp.compilers

Related articles
Building a parse tree that reflects C Semantics vbdis@aol.com (VBDis) (2002-10-13)
Re: Building a parse tree that reflects C Semantics grosch@cocolab.de (Josef Grosch) (2002-10-18)
Re: Building a parse tree that reflects C Semantics idbaxter@semdesigns.com (Ira Baxter) (2002-10-20)
Re: Building a parse tree that reflects C Semantics whopkins@alpha2.csd.uwm.edu (Mark) (2002-10-20)
Re: Building a parse tree that reflects C Semantics rbates@southwind.net (Rodney M. Bates) (2002-10-20)
Re: Building a parse tree that reflects C Semantics david.thompson1@worldnet.att.net (David Thompson) (2002-10-25)
Re: Building a parse tree that reflects C Semantics rbates@southwind.net (Rodney M. Bates) (2002-11-06)
Re: Building a parse tree that reflects C Semantics idbaxter@semdesigns.com (Ira Baxter) (2002-11-07)
| List of all articles for this month |
From: "David Thompson" <david.thompson1@worldnet.att.net>
Newsgroups: comp.compilers
Date: 25 Oct 2002 00:16:00 -0400
Organization: AT&T Worldnet
References: 02-10-058
Keywords: parse
Posted-Date: 25 Oct 2002 00:15:59 EDT

(Basically agree but a few minor quibbles)
Josef Grosch <grosch@cocolab.de> wrote :
....
> > How to interpret the type declaration syntax of C, in order to obtain
> > general type descriptions for every declaration?
>
> The concrete syntax of declarations in C and C++ is weird in my humble
> opinion. A declaration for a variable should specify a name, a type,
> and maybe an initial value as well as additional attributes. These
> constituents are organized in a strange order in the concrete syntax
> of C. Consider the following example:
>
> static unsigned int * a [5], b = 1;
>
> A declaration in C begins with a specifier list (e. g. static unsigned
> int). The specifier list describes the attributes (static) and the
> "base type" (unsigned int) for a list of variables


The specifier list can include at most one storage-class-specifier
(including typedef, which is really not a storage class at all),
type-specifiers (sometimes more than one, even non-adjacent,
although that's generally considered bad style), and type-qualifiers
(const, volatile, and in C99 restrict, although restrict on a base type
makes no sense). In C90, but not C++ or C99, the type-specifiers
can be omitted, defaulting the base type to (signed) int.


The type-specifiers can also refer to, or be/contain the definition
of, a struct (or in C++ class) or union or enum type; in most such
cases, the init-declarators can be omitted entirely to declare or define
only the type, but no objects (or typedefs) of that type.


> ... in general followed by a list of so-called declarators. A
> declarator describes the name of a variable (a and b), its type (* [5]
> meaning array of pointer), and optionally an initial value (1). In
> general the name of the variable is found within the type
> specification (* a [5]). However, the type specification at the
> declarator is incomplete because the base type is missing. The base
> type is given in the specifier list. In our example the complete type
> of the variable a is "array of pointer to unsigned int".


Actually, array-size-5 of pointer to unsigned int. The array bound
(if specified; sometimes it may be omitted, or in C99 variable) is
part of the type and does count in compatibility, though since
you can't assign arrays in C, and can't pass arrays as such to
or return them from a function, this is often overlooked.


The declarator does not include (the specification of) the initial
value; that's the 'initializer'. Normal declarations consist of
initial specifiers followed by a comma-separated list of
init-declarators, each of which is a declarator optionally followed by
'=' and an initializer. Some other declaration-like constructs
(struct/union members, parameter-declarations in prototype format) use
a restricted specifier list (disallowing storage-class-specifiers) and
a declarator without the option of "= initializer" in C; in C++ the
initializer syntax *is* allowed, for default values of function
arguments, and on some struct/class members. (For oldstyle=K&R1
function definitions, only in C, the normal declaration syntax is used
by constrained to no storage-class except 'register' and no
initializer.)


--
- David.Thompson 1 now at worldnet.att.net


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.