trailing comma inside enum...bug or feature?

Leendert van Doorn leendert at cs.vu.nl
Thu Dec 8 23:30:48 AEST 1988


In article <2174 at vedge.UUCP> lai at vedge.UUC writes:
>Why does something like:
>
>/* taken straight out of  X11/Intrinsic.h, from Xwindows from MIT */
>typedef enum  {
>    XtGeometryYes,        /* Request accepted. */
>    XtGeometryNo,         /* Request denied. */
>    XtGeometryAlmost,     /* Request denied, but willing to take replyBox. */
>    XtGeometryDone,       /* Request accepted and done. */
>} XtGeometryResult;
>	/* note trailing comma after XtGeometryDone */
>
>elicit no complaints from any of the C compilers I use?

You're probably using PCC which has the following grammar for enums:

	enum_dcl: enum_head '{' moe_list optcomma '}'
		| ENUM NAME
		;
	enum_head: ENUM
		|ENUM NAME
		;
	moe_list: moe
		| moe_list CM moe
		;
	moe	: NAME
		| NAME '=' con_expr
		;
	optcomma: /* epsilon */
		| ','
		;


This feature is only useful when automaticly generating enum tags (e.g. by
macros; in this case it's very hard to remove the last trailing comma).
Any further usage is unknown to me. I think that the designers (was this an
invention of Steve Johnson?) wanted to conform to the initialization syntax.

>Is this a bug or feature?
An undocumented feature.

>Should ANSI compilers allow this?
No, they shouldn't. However in the ANSI C compiler I wrote, the trailing
comma is accepted but flagged by a warning message. This makes it possible
to compile things like gcc and X11.

-- 
Leendert P. van Doorn 			   		 <leendert at cs.vu.nl>
Vrije Universiteit / Dept. of Maths. & Comp. Sc.
De Boelelaan 1081
1081 HV Amsterdam / The Netherlands			tel. +31 20 548 5302



More information about the Comp.std.c mailing list