How does the compiler know that the comma in a function call is not a comma operator?

后端 未结 6 782
Happy的楠姐
Happy的楠姐 2020-12-06 04:11

Consider the function call (calling int sum(int, int))

printf(\"%d\", sum(a,b));

How does the compiler decide that the

6条回答
  •  庸人自扰
    2020-12-06 04:23

    Existing answers say "because the C language spec says it's a list separator, and not an operator".

    However, your question is asking "how does the compiler know...", and that's altogether different: It's really no different from how the compiler knows that the comma in printf("Hello, world\n"); isn't a comma operator: The compiler 'knows' because of the context where the comma appears - basically, what's gone before.

    The C 'language' can be described in Backus-Naur Form (BNF) - essentially, a set of rules that the compiler's parser uses to scan your input file. The BNF for C will distinguish between these different possible occurences of commas in the language.

    There are lots of good resources on how compilers work, and how to write one.

提交回复
热议问题