- 论坛徽章:
- 20
|
回复 6# nswcfd
找到一句话,不知道是否可以作为依据?姑且记录一下。
6.7.6 Declarators
Syntax
declarator:
pointer/opt direct-declarator
direct-declarator:
identifier
( declarator )
direct-declarator [ type-qualifier-list/opt assignment-expression/opt ]
direct-declarator ( parameter-type-list )
...
pointer:
* type-qualifier-list/opt
* type-qualifier-list/opt pointer
Semantics
2 Each declarator declares one identifier, and asserts that when an operand of the same
form as the declarator appears in an expression, it designates a function or object with the
scope, storage duration, and type indicated by the declaration specifiers.
不知道是不是可以理解为,尽管声明不是(?)表达式,但是可以按照“同样形式”的表达式来解析,
也就是说,可以利用表达式的操作符优先级,来判断标识符具体代表什么(普通object,指针,数组,函数等)。
例如,对于如下的声明,static int (*a)[10];
其中declartion specifier是static int,declarator是(*a)[10],
按照expression的规则去解析(*a)[10],要求a是一个指针,并且(*a)是个数组,那么a就是一个指向数组的指针。
言外之意,解析declator和解析表达式的逻辑/代码是重用的?
---------------------------------------------------------------------
注,这里主要用到的expression规则为:
primary-expression:
identifier
constant
( expression )
...
postfix-expression:
primary-expression
postfix-expression [ expression ]
postfix-expression ( argument-expression-list/opt )
...
unary-expression:
postfix-expression
unary-operator cast-expression
...
unary-operator:
*
...
cast-express到assignment-expression,略
expression:
expression , assignment-expression
...
(*a)[10]的解析结果为
expression
-> assignment-expression -> ...
-> postfix-expression [ expression ]
-> ( expresion ) [ expression ] -> ...
-> ( unary-operator cast-expression) [ expression ]
-> ( * cast-expresion ) [ expression ] -> ...
-> ( * identifier ) [ expression ]
-> ( * a ) [ expression ] -> ...
-> ( * a ) [ primary-expression ]
-> ( * a ) [ constant ]
-> ( * a ) [ 10 ]
|
评分
-
查看全部评分
|