简体   繁体   English

如何理解C99标准语法

[英]how to understand C99 standard syntax

I can't quite understand what the syntax mean in C99. 我不太明白C99中的语法含义。 Here by saying C99 I mean ISO/IEC 9899:1999. 这里说C99我的意思是ISO / IEC 9899:1999。 Well, I think the grammar syntax part doesn't change much since ANSI C, C89. 好吧,我认为语法语法部分自ANSI C,C89以来没有太大变化。

Take for an example from this question : 这个问题为例:

6.5.5 Multiplicative operators
  Syntax
    multiplicative-expression:
      cast-expression
      multiplicative-expression * cast-expression
      multiplicative-expression / cast-expression
      multiplicative-expression % cast-expression

  Constraints

Each of the operands shall have arithmetic type. The operands of the % operator
shall have integer type.

  Semantics

The usual arithmetic conversions are performed on the operands.
The result of the binary * operator is the product of the operands.
The result of the / operator is the quotient 

I wonder why in the syntax of multiplicative operators we have a "cast-expression" ? 我想知道为什么在乘法运算符的语法中我们有一个“cast-expression”? And what grammar can this syntax imply? 这句语法意味着什么语法? In that question @Avi said that in 那个问题中, @ Avi说

a*b*c

"c must be parsed as a cast-expression", I can't quite understand this. “c必须被解析为一个演员表达”,我不太明白这一点。

Take another example from c99 6.6.1,the syntax of constant expressions 从c99 6.6.1中获取另一个例子,即常量表达式的语法

Syntax
   constant-expression:
       conditional-expression

why this conditional-expression come up here? 为什么这个条件表达式出现在这里? Can someone show me how to explain those syntax? 有人可以告诉我如何解释这些语法吗? Thank you all in advance. 谢谢大家。

I wonder why in the syntax of multiplicative operators we have a "cast-expression" ? 我想知道为什么在乘法运算符的语法中我们有一个“cast-expression”?

This is a typical device used in context free grammars to specify the grouping of operators of the same precedence and to specify the precedence in the first place (OK, this isn't precise; the grammar can express more than just assigning precedence to operators, but if we only wanted to express simple precedence based expression evaluation in CFG, we would do it just this way). 这是在上下文无关语法中使用的典型设备,用于指定相同优先级的运算符的分组,并首先指定优先级(好的,这不精确;语法可以表达的不仅仅是为运算符分配优先级,但如果我们只想在CFG中表达简单的基于优先级的表达式评估,我们就会这样做。) The cast-expression is not related anyhow to the multiplicative-expression ; 无论如何, cast-expressionmultiplicative-expression无关; it's just "any expression not containing multiplicative operations or operations of lower precedence". 它只是“任何不包含乘法运算或低优先级运算的表达式”。

So, how would this work: 那么,这将如何工作:

  1. The grouping of same-precedence operators: 同一优先级运算符的分组:

     1*2*3*4 

    in this case, there are following possibilities for grouping (top level only): 在这种情况下,有以下分组的可能性(仅限顶级):

    • 1*(2*3*4)
    • (1*2)*(3*4)
    • (1*2*3)*4

    by looking at the grammar, we know that whatever stands left of * must be a cast-expression , that is, must not contain (unparenthesized) * . 通过查看语法,我们知道*左边的任何内容必须是一个强制转换cast-expression ,即不得包含(未表示的) * Therefore, only the third alternative is viable 因此,只有第三种选择是可行的

  2. The precedence. 优先权。 Imagine the expression 想象一下这个表达方式

     a*b+c 

    It could be parsed as either 它可以被解析为

    • (a*b)+c
    • a*(b+c)

    However, we know that the cast-expression on the right hand side can't contain (unparenthesized) + (or we can work that out from the grammar if we inspect it). 但是,我们知道右侧的cast-expression不能包含(notarenthesized) + (或者如果我们检查它,我们可以从语法中解决)。 That leaves the first option as the only possible alternative. 这使得第一种选择成为唯一可行的选择。

    Another expression 另一个表达

     a+b*c 

    could be, in turn, parsed as 反过来,可以解析为

    • (a+b)*c
    • a+(b*c)

    However, the multiplicative-expression on the left of * can't contain (unparenthesized) + either (left as an exercise for the reader). 但是, *左边的multiplicative-expression不能包含(未表示的) + (左侧作为读者的练习)。 Therefore, only the second alternative is viable. 因此,只有第二种选择是可行的。

To your question about conditional-expression: The grammar rule just disallows anything equivalent or below assignment operators to be parsed as constant-expression s. 关于条件表达式的问题:语法规则只是不允许将任何等价或低于赋值运算符解析为constant-expression s。 Therefore, a=b , a,b , a+=b can't possibly be constant expressions, while a+b , a[b] and a(b) possibly can. 因此, a=ba,ba+=b不可能是常数表达式,而a+ba[b]a(b)可能是。 Note that it doesn't disallow eg. 请注意,它不允许例如。 (a=b) ; (a=b) ; you have to look to the actual clause specifying what are constant expressions, which restricts the realm of constant expressions more than the grammar. 你必须查看实际的子句,指定什么是常量表达式,这限制了常量表达式的领域而不是语法。

I wonder why in the syntax of multiplicative operators we have a "cast-expression" ? 我想知道为什么在乘法运算符的语法中我们有一个“cast-expression”?

Let's start with a much simpler problem, and a correspondingly simpler grammar. 让我们从一个更简单的问题开始,以及相应简单的语法。 We'll define a grammar to represent a simple arithmetic expression ( 1 + 2 , 3 * (4 - 2) , 42 , etc.). 我们将定义一个语法来表示一个简单的算术表达式( 1 + 2 3 * (4 - 2)42等)。 Since we want a plain numeric constant to be a legal expression, we must define our grammar such that there is a path from the root non-terminal expression to a simple number : 因为我们想要一个普通的数字常量作为一个合法的表达式,我们必须定义我们的语法,以便有一个从根非终端表达式到一个简单数字的路径:

expression:
    term  |
    term add-op expression

term:
    factor |
    factor mul-op term

factor:
    number |
    ( expression )

Non-terminals like number , add-op , and mul-op should be obvious. numberadd-opmul-op这样的非终端应该是显而易见的。

42 is a number , which is a production of factor , which is a production of term , which is a production of expression . 42数字 ,它是因子的产生 ,它是术语的产生 ,是表达的产生 Thus 42 is a legal arithmetic expression according to our grammar; 因此, 42是根据我们的语法的合法算术表达式; term and factor are intermediate productions in this path. 术语因子是这条路径中的中间产物。

A similar thing is happening in the C grammar. 类似的事情正在C语法中发生。 A cast-expression is an intermediate production between an expression and some terminal such as an identifier or numeric constant. cast-expression表达式和某些终端(如标识符或数字常量)之间的中间生成。 Our simple grammar defines 2 levels of precedence; 我们的简单语法定义了2个优先级; the C grammar defines 16 distinct levels of precedence with a boatload more operators, so there are a lot more intermediate productions along that path. C语法定义了16个不同级别的优先级,其中包含更多运算符,因此沿着该路径有更多的中间产品。 And we want a cast-expression to have a higher level of precedence than a multiplicative-expression so we can handle cases like 并且我们希望一个强制转换表达式具有比乘法表达式更高的优先级,因此我们可以处理类似的情况

x = a * (int) b;

properly. 正常。

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

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