From Newsgroup: comp.lang.c
Tim Rentsch <
tr.17687@z991.linuxsc.com> writes:
Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:
Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:
[...]
The actual text of the standard implies that 42 is not an
expression. I rely on the obvious intent to conclude that it is.
I made the above statement to demonstrate that just following the
exact wording of the standard, without thinking about the (sometimes
unclear) intent behind it, can lead to absurd results.
I've discussed this particular glitch before, but it's been a while.
N3220 6.5.1 says:
An *expression* is a sequence of operators and operands that
specifies computation of a value, or that designates an object
or a function, or that generates side effects, or that performs
a combination thereof.
I believe the wording is unchanged from C90 up to the latest C202y
draft. Since the word "expression" is in italics, this is the
standard's definition of the word.
This is a flawed definition. The terms "operator" and "operand"
are defined in 6.4.6:
*punctuator: one of
[ ] ( )
[snip]
A punctuator is a symbol that has independent syntactic and
semantic significance. Depending on context, it may specify an
operation to be performed (which in turn may yield a value or a
function designator, produce a side effect, or some combination
thereof) in which case it is known as an *operator* (other forms
of operator also exist in some contexts). An *operand* is an
entity on which an operator acts.
Consider this expression statement:
42;
Is `42` an expression? Clearly it's intended to be, but there is no
operator, and therefore there is no operand, so it doesn't meet the
standard's definition of the word "expression".
I think this conclusion can be explained as a misreading of the text
in the C standard. In reading the text "An *expression* is a
sequence of operators and operands", I think you are interpreting it
as meaning "at least one of each of operators and operands". But
this text could also be read as "at least one of either of operators
and operands", or in other words a sequence of elements of the set
containing both operands and operators, in which case 42 would
qualify as an expression.
No. The term "operand" is defined in N3220 6.4.6p2:
An *operand* is an entity on which an operator acts.
As I already explained, in the expression statement `42;`, 42 it is
neither an operator nor an operand. It can't be an operand unless
there's an operator to act on it. So your reading of the text as
"at least one of either of operators and operands" doesn't help.
An argument might be made about whether the text in the C standard
_should_ mean that, but certainly it _could_ mean that.
It might be reasonable to define "operand" in a way that includes
a constant that is a full expression, but the standard clearly
doesn't say that.
To restate the obvious, my argument is not that `42` is not an
expression. It's that the standard's definition of "expression"
is flawed. The only sensible workaround is to pretend that `42`
is an expression -- basically to ignore the italicized definition of "expression" and instead rely on the grammar production.
--
Keith Thompson (The_Other_Keith)
Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */
--- Synchronet 3.22a-Linux NewsLink 1.2