简体   繁体   中英

function definition in BNF C grammar

I'm reading this C BNF grammar. 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;
  2. In <function-definition> , why is <declarator> followed by a {<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 . But I'm sure it isn't valid in C. What am I missing?
  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> does not include the base type (return type for a function; element type for an array).

  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. 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. 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 */ }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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