Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am writing a toy C compiler for a compiler/language course at my university.

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.

Question: What is the fundamental reasoning behind this?

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.

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
728 views
Welcome To Ask or Share your Answers For Others

1 Answer

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);

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;
}

(with int being the default return type and parameter types specified outside the declarator).

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...