ANTLR C 语法,可选 init_declarator_list?
在 ANTLR v3 的 ANSI C 语法中 ( http://antlr.org/grammar/1153358328744/Cg ), init_declarator_list 在规则声明中如何是可选的?
而不是:
| declaration_specifiers init_declarator_list? ';'
-我会说:
| declaration_specifiers init_declarator_list ';'
C 标准的哪一部分允许这样的语句:
int;
编辑:
我刚刚尝试过,它是允许的!好吧,那为什么允许呢?
In the ANSI C grammar for ANTLR v3 ( http://antlr.org/grammar/1153358328744/C.g ), 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一个大胆的猜测:为了更简单地编写生成机器生成的 C 语言的程序。
A wild guess: To make it simpler to write programs that produce machine-generated C.
ANTLR 语法可能直接遵循 C 标准语法。我没有读过 C 标准,但对于 C++,标准单独规定只有在声明类或枚举类型时才能省略
init_declarator_list
。因此,语法本身仅包含所有可能的声明形式,而每个特定情况都使用简单语言进一步定义。至于您指出的情况,语法之外的规则不允许
int;
。请注意,C/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.