简体   繁体   中英

ANTLR C grammar, optional init_declarator_list?

In the ANSI C grammar for ANTLR v3 ( http://antlr.org/grammar/1153358328744/Cg ), how can 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?

一个大胆的猜测:为了简化编写产生机器生成的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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM