why is `int test {}` a function definition in C language BNF
The grammar is necessary but not sufficient to describe a valid C program. For that you need constraints from the standard too. A simpler example of this would be 0++
, which follows the syntax of a C expression, but certainly isn't a valid program fragment...
C11 6.9.1p2:
- The identifier declared in a function definition (which is the name of the function) shall have a function type, as specified by the declarator portion of the function definition. [162]
The footnote 162 explains that the intent of the constraint is that a typedef
cannot be used, i.e. that
typedef int F(void);
F f { /* ... */ }
will not be valid, even though such a typedef
could be used for a function declaration, i.e.
F f;
would declare the function
int f(void);
But mere existence of this constraint also proves that the BNF grammar in itself is not sufficient in this case. Hence you are correct in that the grammar would consider such a fragment a function definition.