这是为什么合法的C 2 [英] Why is this legal in C?
问题描述
我在我的大学写一个编译器/语言课程的玩具C编译器。
I am writing a toy C compiler for a compiler/language course at my university.
我想充实在C符号解析语义,并与我试图对普通编译器这个测试用例上来铛&安培;海湾合作委员会。
I'm trying to flesh out the semantics for symbol resolution in C, and came up with this test case which I tried against regular compilers clang & gcc.
void foo() { }
int main() { foo(5); } // foo has extraneous arguments
大多数编译器似乎只是警告无关的参数。
Most compilers only seem to warn about extraneous arguments.
问:什么是基本的推理背后
有关我的符号表的生成/消退期,我正在考虑一个函数与返回类型的符号,和几个参数化的参数(基于语法),每一个相应的类型。
For my symbol table generation/resolution phase, I was considering a function to be a symbol with a return type, and several parametrized arguments (based on the grammar) each with a respective type.
感谢。
推荐答案
与函数的没有的原型列出参数被视为具有的不确定的数量,而不是零
A function with no listed arguments in the prototype is deemed to have an indeterminate number, not zero.
如果你真的想零参数,它应该是:
If you really want zero arguments, it should be:
void foo (void);
空列表变体是从古代℃的保持,甚至在ANSI有它自己的手中,在那里你有这样的话:
The empty-list variant is a holdover from ancient C, even before ANSI got their hands on it, where you had things like:
add_one(val)
int val;
{
return val + 1;
}
(用 INT
作为声明符外指定的默认返回类型和参数类型)。
(with int
being the default return type and parameter types specified outside the declarator).
如果你正在做一个玩具的编译器和你不担心遵守的每个的小小的一块C99的,我只是折腾该选项并需要某种形式的参数列表
If you're doing a toy compiler and you're not worried about compliance with every tiny little piece of C99, I'd just toss that option out and require a parameter list of some sort.
这会让你的生活大大简化了,我质疑是否有必要让人们使用的功能反正。
It'll make your life substantially easier, and I question the need for people to use that "feature" anyway.
这篇关于这是为什么合法的C 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!