Ethereal-dev: Re: [Ethereal-dev] 0.9.4: Invalid trailing comma and semicolon

Note: This archive is from the project's previous web site, ethereal.com. This list is no longer active.

From: Guy Harris <guy@xxxxxxxxxx>
Date: Thu, 30 May 2002 18:37:32 -0700
On Wed, May 29, 2002 at 11:52:51PM -0400, Ashok Narayanan wrote:
> Just a brief question. Is there a specific compiler that complains
> about these commas and semicolons. Specifically why are they
> syntactically illegal (especially the semicolons)?

Note, BTW, that, trailing commas in initializers *ARE* legal.

For example

	int foo[] = {
		17,
		34,
	};

is *NOT* syntactically illegal according to the ANSI C standard, unlike

	enum foo {
		RED,
		GREEN,
	};

which *is* syntactically illegal, as the ANSI C standard says:

	3.5.7 Initialization

	Syntax

		<initializer>:
		    <assignment-expression>
		    { <initializer-list> }
		    { <initializer-list> , }

		<initializer-list>:
		    <initializer>
		    <initializer-list> , <initializer>

I.e., they explicitly added a rule to allow a trailing comma in
initializers.  (To quote the Rationale of the ANSI C standard:

	The Base Document allows a trailing comma in an initializer at
	the end of an initializer list.  The Standard has retained this
	syntax, since it provides flexibility in adding or deleting
	members from an initializer list, and simplifies machine
	generation of such lists.

The Base Document was Ritchie's "The C Reference Manual", one version of
which was Appendix A of the original (first edition) K&R.  I don't have
my K&R version 1 handy, so I don't know whether it allowed commas at the
end of an enum list; if it didn't, I don't know why it didn't, as the
arguments in favor of allowing trailing commas in initializers would
seem to me to apply to enum lists as well.

So one needn't avoid trailing commas in initializers, and one needn't
remove them - any compiler that rejects them isn't a C compiler. 
("C'est magnifique, mais c'est ne pas C.")