ANTLR C grammar, optional init_declarator_list?
In the ANSI C grammar for ANTLR v3 ( http://antlr.org/grammar/1153358328744/C.g ), how c开发者_开发百科an init_declarator_list be optional in rule declaration ?
Instead of:
| declaration_specifiers init_declarator_list? ';'
-I would say:
| declaration_specifiers init_declarator_list ';'
What part of the C standard allows statements like:
int;
EDIT:
I just tried, it is allowed! Okay then, why is it allowed?
A wild guess: To make it simpler to write programs that produce machine-generated C.
Probably the ANTLR grammar is directly following that of the C standard grammar. I haven't read the C standard but for C++, the standard says separately that the init_declarator_list
can be omitted only when declaring a class or enum type. So the grammar alone only encompasses all the possible forms of declaration while each particular case is further defined using plain language.
As to the case you indicated, int;
is disallowed by the rules outside the grammar.
Note that the C/C++ language cannot be completely defined by the grammar alone. Many extra rules must be specified in plain human language.
精彩评论