简体   繁体   English

BNF C语法中的函数定义

[英]function definition in BNF C grammar

I'm reading this C BNF grammar. 我读 ÇBNF语法。 I have the following questions: 我有以下问题:

  1. Is correct which it's <declarator> job to parse this syntax: id(int a, int b) (in <direct-declarator> ) and so on to arrays in parameters of a function prototype/definition, etc; 解析该语法的<declarator>作业是正确的: id(int a, int b) (在<direct-declarator> ),依此类推,用于函数原型/定义的参数中的数组,等等;
  2. In <function-definition> , why is <declarator> followed by a {<declaration>}* ? <function-definition> ,为什么在<declarator>后跟{<declaration>}* from what I understood, it could make valid a type name or storage class followed by a function header like id(int a, int b) int . 据我了解,它可以使类型名称或存储类有效,后跟一个函数标头,例如id(int a, int b) int But I'm sure it isn't valid in C. What am I missing? 但是我确定它在C语言中无效。我想念什么?
  1. Yes, <declarator> in that grammar is the name of the object being declared, plus its arguments or array size (and also the pointer qualifiers of its type). 是的,该语法中的<declarator>是要声明的对象的名称,加上其参数或数组大小(以及其类型的指针限定符)。 <declarator> does not include the base type (return type for a function; element type for an array). <declarator>不包括基本类型(函数的返回类型;数组的元素类型)。

  2. Note that there are two alternatives in <direct-declarator> , both of which seem relevant to functions: <direct-declarator> ( <parameter-type-list> ) <direct-declarator> ( {<identifier>}* ) The first of these is what we normally think of as a function declaration, where the parameters are types or types-with-parameter-name. 请注意,在<direct-declarator>中有两种选择,它们似乎都与函数有关: <direct-declarator> ( <parameter-type-list> ) <direct-declarator> ( {<identifier>}* )第一个这些就是我们通常认为的函数声明,其中参数是类型或具有参数名称的类型。 The second one is just a list of identifiers. 第二个只是标识符列表。 (It should, I think, be a comma-separated list of identifiers.) The second case is the old-style "K&R" function definition syntax, which you may never have seen before, and should immediately forget about after reading this answer because -- while C compilers still accept it -- it has been deprecated for not just years, but decades. (我认为它应该是逗号分隔的标识符列表。)第二种情况是老式的“ K&R”函数定义语法,您以前可能从未见过,阅读完此答案后应立即忘记,因为-尽管C编译器仍然接受它-它已被弃用了不仅是几年,甚至数十年。 So don't use it . 所以不要使用它 For historical completeness, here's how it looked: int foo(n, p) int n; char p; { / Body of the function */ } 为了历史上的完整性,它是这样的: int foo(n, p) int n; char p; { / Body of the function */ } int foo(n, p) int n; char p; { / Body of the function */ }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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