## Re: [sdcc-devel] Two questions about the yacc script sdcc.y

 Re: [sdcc-devel] Two questions about the yacc script sdcc.y From: Erik Petrich - 2014-01-04 06:45:06 ``` On Fri, 3 Jan 2014, Maarten Brock wrote: > Sebastien, > >> I think I found a convincing reason for not semantically accepting this >> construct: >> >> Using the unusual comma operator [1]"," ++a could be (sort-of) >> rewritten as : a=a+1,a >> so ++a = b; would be a=a+1,a = b; >> >> [1] http://en.wikipedia.org/wiki/Comma_operator Both C99 and C11 (and probably the earlier specs as well, but I don't have them handy to check) specifically say the "expression ++E is equivalent to (E+=1)". Later when discussing the semantics of assignment operators, it says an "assignment expression has the value of the left operand after the assignment, but is not an lvalue". So I think SDCC should really be rejecting "++a = b" rather than accepting it as it currently does. > > This is a convincing reason NOT to accept this? I would think it is a > reason to DO accept it. And it is exactly how I think about the meaning of > "++a". > >> On the contrary a++ = b would be rewritten a,a=a+1=b which would >> probably fail because in 'a+1=b", a+1 is not an lvalue for sure. >> >> but since in C, IIRC, the order of evaluation is not guaranteed, then >> the two versions may be considered equivalent, and the decision seems >> not to consider ++a as an lvalue. >> >> At least gcc thinks that neither a++ nor ++a are lvalues. >> >> Sebastien Lorquet > > The order is not guaranteed? Are you saying C can interpret ++a=b as > a=b;++a ? Or even c=a+1;a=b;a=c ? > > Maarten The ordering has some flexibility, but there still some constraints. The C standard defines certain points in time in the execution of the program called "sequence points". Between consecutive sequence points, when there are multiple side effects (such as modifying a variable's value), these side effects are allowed to occur in any order. Without getting into the all of details of how sequence points are defined, a basic expression statement that does not call a function or use the logical AND, logical OR, conditional ( ? : ), or comma operators will be bounded by the same pair of sequence points and so the order in which any variables are modified is left to the compiler implementation. Some example expressions with ambiguous results: x = (x = 1) + 1; /* final value for x may be either 1 or 2 */ x = x++; /* final value for x may or may not reflect the increment */ The comma operator (as well as the other operators I noted above) does introduce a new sequence point, so any side effects in the left expression will complete (in some order) before any side effects in the right expression. a = a + 1, a = b; /* final value for a is always b */ but as I mentioned, ++a is expanded as (a+=1) not (a=a+1,a) Erik ```