I'm writing a C compiler which follows this standard, and if I parse statements like this:
int i;
(i) = 1;
my compiler will report an error which point out that (i)
is a rvalue and should not be assignable.
I checked the code and the rules, and found this: in assignment expression semantics:
An assignment operator shall have a modifiable lvalue as its left operand.
An assignment expression has the value of the left operand after the assignment, but is not an lvalue.
In my case, the there are two assignment expressions:
(i) = 1
and i
in parentheses. So the (i)
should be a rvalue.
So my question is:
Is
(i) = 1
illegal in this C standard?
This answer is inspired by @Eric Postpischil.
the production of a assignment-expression
is:
<assignment-expression> ::= <conditional-expression>
| <unary-expression> <assignment-operator> <assignment-expression>
in the standard, the assignment expression
specific means expressions with assignment operators. So:
<conditional-expression> is not an assignment expression
<unary-expression> <assignment-operator> <assignment-expression> is an assignment expresssion
so the rule:
An assignment expression has the value of the left operand after the assignment, but is not an lvalue.
only fits for production<unary-expression> <assignment-operator> <assignment-expression>
, not for <conditional-expression>
in the example (i) =1
, i
is an <assignment-expression>
but not an assignment expression
, it is a <conditional-expression>
so it is a lvaule so (i)
is a lvalue.