Topic: i = i++ (almost), syntax, semantics, etc.


Author: dick@silicon.csci.csusb.edu (Dr. Richard Botting)
Date: 9 Oct 1994 16:37:12 GMT
Raw View
Dan Pop (danpop@cernapo.cern.ch) wrote:
: In <372cbh$7m3@panix.com> berke@panix.com (Wayne Berke) writes:

: >Both ++i and i++ are rvalue expressions, neither is an lvalue.  Since
: >++, --, =, *=, or any of the other "assignment class" operators require
: >an lvalue operand, neither (++i)++ nor ++(i++) nor ++i++ is legal C.
: >It's worse(*) than undefined, it's a syntax error!

: It's certainly an error, but not a _syntax_ error. A syntax error is a
: piece of code that cannot be parsed according to the C rules. There is
: nothing syntactically wrong with these expressions. They're errors
: because they're constraint violations.

Some people argued earlier that the correct parsing for ++i++ is determined
as the one that makes sense( (++i)++ not ++(i++) ).  Sadly this is unlikely.
My  notes on C/C++ syntax in this area shows:
 post_fix_expression::=primary_expression #(post_fix),
 unary_expression::=#(pre-fix) post_fix_expression | unary_operator
  cast_expression | "sizeof" "("type_name")",
(I used '#' to many "any number of, including none").
This implies that
 ++i++
should be parsed as
 ++(i++)
My notes could be wrong of course (if so I need to know).

It is quite typical of progrmming languages that the syntax that is
(1) standardized, (2) embedded in the compilers, (3) taught, ...
and so set in concrete
forces a meaningless structure on a statement.  Typically
this comes from a context dependency related to the types of the commponents.

In C++ for example you can overload operators + and * so that
 (a + b) * c
is valid, and
 a + (b * c)
is not, but the compiler will (I expect) force
 a + b * c
to be parsed in the invalid form.  This may also become part of
the standard - if the standard uses the kind of context free grammar
I've seen in K&R, the ARM, and other reference manuals back to Algol 60.


--
dick@silicon.csci.csusb.edu=rbotting@wiley.csusb.edu.
dick::="Dr. Richard John Botting".  csci::="Computer Science Department".
csusb::="California State University, San Bernardino".
Disclaimer::=`CSUSB may or may not agree with this message`.
Copyright(1994)::=`Copy and use as you wish as long as you include this
        copyright and signature`.
Ask me about our new Masters Degree in Computer Science!