YACC grammer for c

Gregory Smith greg at utcsri.UUCP
Sun Nov 30 05:49:38 AEST 1986


In article <1338 at nicmad.UUCP> karsh at nicmad.UUCP (Bruce Karsh) writes:
>  I tried to write a yacc grammer for the C language by using the
>grammer in K&R appendix A.  I typed in the grammer from the book almost
>verbatim.  I've found the following anomaly:
>
>..."type-decl-list" is not defined anywhere.
>
>  Is there any difference between "type-decl-list" and "struct-decl-list"?
>I.e. can I get away with:
>
>         type-decl-list : struct-decl-list ;
No.
type-decl-list:
	<nothing> /* since type-decl-list is not optional in function-body */
	type-decl-list type-declaration
type-declaration:
	register(opt) type-specifier(opt) declarator-list ;
declarator-list:
	declarator
	declarator-list , declarator

"declarator" is defined on page 216. "struct-decl-list" does not allow
'register', demands the type-specifier, and allows bitfields, so you
can't use that.

As a side issue, K&R uses a right-recursive definition, e.g.
parameter-list can be "identifer, parameter-list". Since you are using
yacc, I hope you are turning these around to a left-recursive form,
i.e. "parameter-list, identifier".

>  By the way, does anybody know if there are any known bugs in the K&R
>C grammar?

On page 218, 'function-declarator' is given as

	declarator ( parameter-list(opt) )

which is bogus - the way to declare a function
which returns a pointer to an array of 10 ints is

int	(*f())[10];

Therefore if this function has parameters a,b it is defined using

int (*f(a,b))[10]
int a,b;
{ etc...

...which is legal but cannot be produced by the K&R grammar. I think
this function can be declared in some other way which fits into the K&R
grammar, on UNIX compilers.  I would stay away from this, since such
declarations are confusing enough without using 'slang', and because
Ansi C apparently will not allow the 'slang' form.

Anyhow...
function-declarator:
	identifier ( parameter-list(opt) )
	function-declarator ( )
	( function-declarator )
	* function-declarator
	function-declarator [ constant-expression ]

The first two lines enforce the restriction that the parameter list
may only be inserted in the 'correct' set of parentheses. Again, I think
existing UNIX compilers do not enforce this restriction, but Ansi will.
The grammar above also demands that 'identifier' be declared as
'function-returning-something' while the K&R version does not.

Furthermore, "function-definition" should be

	static(opt) type-specifier(opt) function-declarator function-body
	^^^^^^^^^^^
At the top of page 214 there is a statement which indicates that the
grammar given is intended to aid comprehension, which would explain why
they would want to simplify some of the weirder parts at the expense of
absolute accuracy.

While I'm on the subject, I might as well add the only other corrections
that I have scribbled into Appendix A:

Page 194, section 8.4, third line: delete "and storage class".

Page 215, third line, delete "and the conditional operator".
	Add "The conditional operator ?: groups right-to-left."
-- 
----------------------------------------------------------------------
Greg Smith     University of Toronto      UUCP: ..utzoo!utcsri!greg
Have vAX, will hack...



More information about the Comp.lang.c mailing list