When looking through C's BNF grammar, I thought it was weird that the production rule for a declaration looked like this (according to https://cs.wmich.edu/~gupta/teaching/cs4850/sumII06/The%20syntax%20of%20C%20in%20Backus-Naur%20form.htm):
<declaration> ::= {<declaration-specifier>}+ {<init-declarator>}* ;
Why use an *
quantifier (meaning zero or more occurrences) for the init-declarator
? This allows statements such as int;
or void;
to be syntactically valid, even though they're semantically invalid. Couldn't they have just used a +
quantifier (one or more occurrences) instead of *
in the production rule?
I tried compiling a simple program to see what the compiler outputs and all it does is issue a warning.
Input:
int main(void) {
int;
}
Output:
test.c: In function ‘main’:
test.c:2:5: warning: useless type name in empty declaration
int;
^~~
int
as a return type formain
and don't use()
as a parameters types list in functions but(void)
instead. – Foremanint;
on the line perhaps. – Decompound