In The C Programming Language, 2nd Edition, by authors Brian W. Kernighan and Dennis M. Ritchie, they talk about declarators and direct-declarators. The discussion starts in the book on p. 122 with dcl and direct-dcl's Will you please explain the difference between a declarator and a direct-declarator in and easy to understand way? What makes it direct?
Also, on p. 225
where the direct-declarator is an identifier or a parenthesized identifier. In particular, it must not achieve function type by means of a typedef.
It seems to me that declarators are the declaration of a variable or function. In "T D", the T part specifies its specifiers and types, and the D part specifies the identity, meaning the unique identification name of the variable or function. It has something to do with the language's grammar.
Are declarators indirect since they are not specified as direct, as in direct-declarator?
The grammar needs a direct-declarator token to specify precedence. As it is,
* foo [ 3 ]must be parsed as a declarator that is*followed by a direct-declarator that isfoo [ 3 ]. If the grammar did not separate a declarator and a direct-declarator, it could be unclear whether this was*followed byfoo [ 3 ]grouped together or was* foogrouped together followed by[ 3 ].The grammar says a declarator is:
where pointer is
*followed by optional qualifiers (likeconst) and direct-declarator is one of:(declarator)[type-qualifier-listopt assignment-expressionopt][statictype-qualifier-listopt assignment-expression][type-qualifier-liststaticassignment-expression][type-qualifier-listopt *](parameter-type-list)(identifier-listopt)So, given
* foo [ 3 ], we must take this as a declarator with*for pointer andfoo [ 3 ]for direct-declarator. There is no way to have*at the start of a direct-declarator. Thus,* foo [ 3 ]must declare an array of 3 pointers, not a pointer to an array of 3 elements.If those options for declarator and direct-declarator were merged into a single grammar token, then the parsing would be ambiguous. You could parse it as
* foo [ 3 ]being that* foois a declarator followed by[ 3 ], which is not what we desire.The name is of no great significance; we merely need another name for the additional token. There are other examples of this in the C grammar. Notably, the expressions grammar starts with expression and then goes through a chain of assignment-expression, conditional-expression, logical-OR-expression, and so on. These have names associated with the operators they involve, until you get to primary-expression. That has some semantic similarity to direct-declarator, suggesting they are both named in the spirit of “okay, we got to the bottom of this grammar chain, here is the primary/direct token.”