質問

I want to have a precise comprehension about the constant expression of C. So, I read the related provisions of C99. The first provision in chapter "6.6 Constant expressions" describes the syntax of constant expression, but I can't understand it.

The first provision in chapter "6.6 Constant expressions" of C99:

constant-expression:
conditional-expression

Why does C99 use conditional-expression to explain constant-expression? No doubt, expression 2 + 3 is a constant expression. Is it a conditional expression?

役に立ちましたか?

解決

The notation “constant-expression: conditional-expression” is only a specification of the syntax of a constant expression. The syntax describes the strings that may be a constant expression. In addition to satisfying the syntax, a constant expression must also satisfy the constraints listed in clause 6.6.

The fact that the C standard uses “constant-expression: conditional-expression” may be confusing. However, it would be exactly the same if the standard used “constant-expression: expression”, as I will show below. That is, any expression is a constant expression provided it follows not just the syntax for expressions but also the constraints listed in clause 6.6.

One example of a conditional expression is 3 < 4 ? 5 : 6. This expression is also a constant expression, because it satisfies the constraints in clause 6.6. On the other hand, x < 4 ? 5 : 6 is also a conditional expression but is not a constant expression (assuming x is the name of an object and is not a macro that gets replaced by a constant, et cetera).

The clause 6.6 tells you that a conditional expression may be a constant expression if it satisfies the constraints. Clause 6.5.15 shows that a conditional expression may be either a logical-OR-expression or a logical-OR-expression ? expression : conditional-expression. Continuing to work through the chain, a logical-OR-expression may be the a logical-AND-expression or may be an expression built with the logical OR operator, ||. Although the name at each level is something like foo-expression, each level is actually either the type of expression that came before or is an expression built with foo. It would just be cumbersome to name them foo-expression-or-prior-type-of-expression instead of just foo-expression.

Thus, the grammar starts with the primaries, which are identifiers, constants, string literals, and one special thing I will discuss in a moment, and the grammar builds those primaries into more complicated expressions, adding the possibility of using each operator that C permits. Layering the grammar in this way defines the operator precedence, because the earlier expressions must be parsed before the later expressions, where possible.

The special thing in the primary is a parenthesized expression, ( expression ). The expression token is the end of the chain of definitions, defined in 6.5.17. This loops the grammar back; any expression can be placed in parentheses, which makes it a new primary, and then operators can be applied to it.

Clause 6.6 could have stated that a constant-expression is an expression, and it would have the same effect as saying it is a conditional-expression. This is because the constraints in 6.6 forbid assignment operators and comma operators. So, if we consider what an expression could be, clause 6.5.17 says it can be either an assignment-expression or expression , assignment-expression. But the constraint prohibiting the comma operator prohibits the latter. Therefore, an expression in a constant expression must be an assignment-expression. Similarly, an assignment-expression must be a conditional-expression and not unary-expression assignment-operator assignment-expression.

他のヒント

To understand §6.6 Constant Expressions, you need to have understood something of the way the grammar works, and you need to read §6.5 Expressions.

Within §6.5 Expressions, section §6.5.15 Conditional expressions says:

conditional-expression:
        logical-OR-expression
        logical-OR-expression ? expression : conditional-expression

This is the way that the standard specifies precedence, etc. The idea is to exclude assignment operators and comma expressions from being 'constant expressions', but allows most of the rest, subject to the constraints defined in §6.6 (which, amongst other things, explicitly excludes assignment and comma operators, probably because the generic expression in the rule does include them).

First of all, the C standard does not explain anything. It tells how the C language is defined, but it does not tell the why.

The syntax sections are written in a special language (a variant of EBNF) that is used to describe how a compiler should interpret C source code. The specification

 constant-expression:
     conditional-expression

essentially means that a constant expression uses the same syntax as a conditional-expression. In the section about the conditional operator (6.5.15), you will find a similar syntax section defining how a conditional-expression is built up.

If you trace all the syntax rules, you will find that a constant-expression can use any operator except the assignment operators (=, *=, /=, +=, -=, <<=, >>= &=, ^=, |=) and the comma operator (,) (unless they are enclosed in parentheses).

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top