problems with the C parser

Gregory Smith greg at utcsri.UUCP
Sun Jun 1 00:45:16 AEST 1986


In article <5072 at topaz.RUTGERS.EDU> brisco at topaz.RUTGERS.EDU (T.p.) writes:
>	Which I had translated into C as:
>
>	typedef union {
>		int intfield;   /* change the record name - keyword */
>		struct bool{unsigned x[32]:1};
>		} newtype;
>
>
>	The C parser barfed all over this. I tried every combination

'illegal field type', which is what I get, is not an example of
parser-vomit by any stretch of the imagination. You did forget the ';'
after the 1 in the above, though - which might cause the first '}' to be
lost - instant upchuck.
And you didn't name the second member of the union - that's not
a syntax error, though.

>of unsigned x:1[32], ..... that I could think of.  Out of frustration
>I looked up the syntax of the declarations in K&R and found that the
>initial guess had been correct!
>	Am I going crazy or is our C parser brain-damaged?  Can anyone
>else compile the above structure declaration?

Well, K&R says that the kinds of things that can put in a bitfield is
implementation-defined - thus `declarator : constant_expression'.
I think it would be rare, though, to find anything but unsigned ints and
ints to be implemented. If an array was allowed, it would be declared as
`unsigned x[32]:32', since the width applies to the whole declarator.
As it stands, I think you are out of luck. Write 'unsigned bits32'
and then use shifts and masks to get at them:

#define boolread( rval, bitnum ) ( ((rval)>>(bitnum)) &1)
#define boolset( lval,bitnum )	( (lval) |= 1<<(bitnum))
#define boolclr( lval,bitnum )  ( (lval) &= ~(1<<(bitnum))
#define boolasg( lval,bitnum, new )( new? boolset( lval,bitnum):boolclr(lval\
,bitnum)

-- 
"We demand rigidly defined areas of doubt and uncertainty!" - Vroomfondel
----------------------------------------------------------------------
Greg Smith     University of Toronto      UUCP: ..utzoo!utcsri!greg



More information about the Comp.lang.c mailing list