简体   繁体   English

如果后跟空格和左括号,C11关键字'_Atomic'是否计为类型限定符或说明符?

[英]Does the C11 keyword '_Atomic' count as type qualifier or specifier if followed by a whitespace and a left parenthesis?

Reading the N1570 draft of the C11 standard, it says on p. 阅读N1170的C11标准草案,它在p。 121 about the _Atomic keyword: 121关于_Atomic关键字:

If the _Atomic keyword is immediately followed by a left parenthesis, it is interpreted as a type specifier (with a type name), not as a type qualifier. 如果_Atomic关键字后面紧跟左括号,则将其解释为类型说明符(具有类型名称),而不是类型限定符。

Now I am wondering, what constitutes as 'immediate' in this context? 现在我想知道,在这种背景下什么构成“直接”?

I find the wording quite ambiguous: Are the two following lines of code guaranteed by the standard to always be identical? 我发现措辞很模糊:标准保证的以下两行代码是否始终相同?

Unambiguous: 明确:

static _Atomic(type) var;

Ambiguous: 暧昧:

static _Atomic (type) var;

Does the insertion of a whitespace destroy the immediacy of the left parenthesis? 插入空格是否会破坏左括号的即时性?

While in the first case, the keyword is always a type specifier, in the second case I am not sure whether it is a type specifier or a type qualifier and whether that is a question of interpretation or firmly defined by the standard. 在第一种情况下,关键字始终是类型说明符,在第二种情况下,我不确定它是类型说明符还是类型限定符,以及这是解释问题还是标准明确定义的。 I am also referring to cases where 'var' is a pointer. 我还指的是'var'是指针的情况。

_Atomic as a type specifier or type qualifier is shown in the grammar in clauses 6.7.2.4 and 6.7.3, respectively. _Atomic作为类型说明符或类型限定符分别在语法6.7.2.4和6.7.3中显示。 The grammar is expressed in tokens (the terminal symbols of the grammar are the tokens defined by the C specification), and the grammar is analyzed in translation phase 7 (clause 5.1.1.2): 语法用标记表示(语法的终端符号是C规范定义的标记),语法在翻译阶段7(第5.1.1.2节)中分析:

White-space characters separating tokens are no longer significant. 分隔标记的空白字符不再重要。 Each preprocessing token is converted into a token. 每个预处理令牌都转换为令牌。 The resulting tokens are syntactically and semantically analyzed and translated as a translation unit. 由此产生的标记在语法和语义上进行分析并翻译为翻译单元。

Thus, white space is irrelevant. 因此,空白区域无关紧要。

Your two lines of code are identical; 你的两行代码完全相同; "immediately followed by" means the next phase 7 token, not the next character in the source file. “紧跟其后”表示下一阶段7令牌,而不是源文件中的下一个字符。

I don't believe this is ever stated explicitly anywhere, but it's instructive to compare the specification of the one place in C where the presence or absence of whitespace between an identifier and a left parenthesis does control which of two grammar rules applies: 我不相信这在任何地方都有过明确的说明,但是比较C中一个地方的规范是有益的,其中标识符和左括号之间存在或不存在空格确实控制了两个语法规则中的哪一个适用:

#define foo(bar) ...  // defines function-like macro 'foo(bar)' with replacement '...'
#define foo (bar) ... // defines object-like macro 'foo' with replacement '(bar) ...'

That's 6.10.3 , easiest understood by reading paragraphs 9, 10, and 3 in that order : 这是6.10.3 ,通过按顺序阅读第9,10和3段最容易理解:

[9] A preprocessing directive of the form [9]表单的预处理指令

 # define identifier replacement-list new-line 

defines an object-like macro ... 定义一个类似对象的宏...

[10] A preprocessing directive of the form[s] [10]形式[s]的预处理指令

 # define identifier lparen identifier-listopt ) replacement-list new-line [...two other forms...] 

defines a function-like macro ... 定义一个类似函数的宏...

[3] There shall be white-space between the identifier and the replacement list in the definition of an object-like macro. [3]在类似对象的宏的定义中,标识符和替换列表之间应该有空格。

The inference you can draw from this is that when the C standard means to give whitespace significance in the grammar, it says so explicitly. 你可以从中得出的推论是,当C标准意味着在语法中给出空白意义时,它就是如此明确地说的。 When there is no such explicit statement, you can assume that the presence or absence of whitespace is only significant when it affects how the source text is divided into tokens. 当没有这样的显式语句时,您可以假设空格的存在与否仅在影响源文本被分成标记的方式时才有意义。

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

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