C declaration grammar
Cedric Ramsey
ramsey at NCoast.ORG
Tue Aug 7 11:26:57 AEST 1990
I am curious about the K&R2 grammar for a declaration. The grammar
for a declaration is as follows (section A8 pg. 210):
declaration: declaration-specifiers init-declarator-list_opt
declaration-specifiers: storage-class-specifier declaration-specifers_opt
type-specifier declaration-specifier_opt
type-qualifier declaration-specifier_opt
I will not write the grammer for the other productions because they can be
obtained from the book. Anyway, my concern is this. The production doesn't
specify any precedence or order for what should be seen first. For instance,
according to this grammar, the following declarations:
const char static i; or
static const char i; or
static char const i;
All would be valid. I don't recall reading anywhere in the manual a mention
of how the precedence is to be taken. I guess a programmer would have to use
his or her common sense and write: static const char i = 0xHH;.
You guys with ansi compilers can you get away with those ,strang ?, kind of
declarations.
However, the ansi standard is clear on other precedence rules. Take
expressions, for example just by looking at the grammar you can see
that && has higher precedence than || and * higher than +.
The semantic rules talk about how type specifiers may specified with a
type qualifier and the semantics for the const/volatile qualifiers but
does not say which should come first.
I came to the conclusion that their grammer is not exactly Backus-Naur form.
What do you guys think.
Please no flames, just thrill me!
More information about the Comp.lang.c
mailing list