Start by noting that comma is an overloaded token whose meaning is
context sensitive.
> > An expression-list is a comma separated list of assignment-expressions
> > which is not an expression.
> > An expression may include assignment-expressions as arguements to the
> > comma operator.
> Um...re-read the definition of expression:
> expression:
> assignment-expression
> expression, assignment-expression
And the definition of expression list:
assignment-expression
expression-list , assignment-expression
In the former, the comma is the comma operator which joins subexpressions
to form a larger expression. In the latter, the comma is a separator which
isolates the individual expressions and does not produce an expression. An
expression is a scalar (one thing) and an expression-list is an aggregate
(a list of things).
You are parsing bottom up and claiming they are the same thing. We are
parsing top down and know what we have. There is no place in the grammar
where there is a choice.
> I'm not sure what you're trying to imply by changing your phrasing to
> "may include." An expression *is defined* as a comma-separated list
> of assignment-expressions. Period.
It is defined as a comma operator joined sequence of assignment-expressions
which is left associative. There might not be a comma in the expression,
but if there is, it introduces a sequence point and produces a result.
> > Which interpretation is context sensitive.
> Whether it is valid for an expression-list to appear at a particular
> position is context dependent. Whether a particular production is or
> is not a valid expression-list is not context dependent.
It has nothing to do with validity. There are places in the grammar
where only an expression-list may appear. There are other places
where only an expression may appear. The context determines whether
it is a expression or an expression-list and that determines the
meaning of the commas.
> I think we're just disagreeing about what "is" means. :-)
> When I say "X is an expression-list" or "Y is an expression" I mean X
> is a gramatically valid expression-list or Y is a gramatically valid
> expression. In other words, X may be used where context requires an
> expression-list or Y may be used where the context requires an
> expression.
> I believe OP was using "is" in my sense, based on the following:
I don't think so. You are saying bottom up, this looks like an expression
and it looks like an expression-list. He and I are saying top down, only
an expression-list may appear here or only an expression may appear here.
The context determines what it is.
> >> No it isn't. There are expressions which are not
> >> assignment-expressions -- for instance, "a = b, c = d".
> >> According to the grammar, I can't put that
> >> expression in an expression-list.
> I take the phrase "I can't put that expression in an expression-list"
> to mean "the production 'a = b, c = d' is a gramatically valid
> expression, but not a gramatically valid portion of an
> expression-list." Which is, I think, plainly false.
You are wrong and he is right. The expression-list is a comma
separated list of assignment-expressions. All outer commas are
separators. There can be no exposed commas in any of the
expressions.
> I also think it's correct to say that any comma separated list of
> productions which are gramatically valid expressions is a gramatically
> valid expression-list. Which seemed to be OP's original question.
True and meaningless. Nothing to do with his question.
> There is an interesting question floating around behind all of this,
> namely, that since expression and expression-list are interchangeable
That is your problem. If the context demands an expression-list, the
thing there is an expression-list. If the context demands an expression,
the thing there is an expression. The syntax is then the same, but the
semantics is very different.
> (in the sense that any production that is a valid expression is a
> valid expression-list and vice versa), why in the world does the
> grammar distinguish the two? Is there some rule or restriction on one
> or the other that doesn't show up in the formal grammar? Could one
> rewrite the grammar without expression-list, replacing it throughout
> with expression, with no change to the meaning or validity of any
> program?
No. Expressions can have exposed commas and can be used as
subexpressions. Expression-lists are not expressions and may not
be used as subexpressions. They may only be used where they are
required in the grammar.
Please read the examples here and note the difference in meaning for
expression and expression-list.
Expression 2, 3 has value 3.
Expression-list 2, 3 has no value, it is a list of two expressions.
complex<double> c1(2, 3);
Expression-list with two items by context, result (2, 3).
complex<double> c2((2, 3));
Expression-list with one item by context, result (3, 0).
The (2, 3) is not an expression-list because they are not
allowed to have parens around them.
complex<double> c3 = static_cast<complex<double> >(2, 3);
Expression by context, result (3, 0).
int i1(2, 3);
Expression-list with two items by context, result syntax error.
int i2((2, 3));
Expression-list with one item by context, result 3.
int i3 = static_cast<int>(2, 3);
Expression by context, result 3.
int i(40);
complex<double> c4((++ i, ++ i));
Expression-list with one item, comma operator introduces a
sequence point, result (42, 0).
complex<double> c5(++ i, ++ i);
Expression-list with two items by context, no sequence point,
result undefined behavior.
Note the original point that the expression a = b, c = d may not
be part of an expression-list because it becomes two expressions
separated by rather than joined by the comma. The
primary-expression (a = b, c = d) may be part of an expression-list
because the comma operator is protected by the parens.
Getting back to the original question. An expression-list (sloppy)
is a comma separated list of expressions because an assignment-expression
is an expression. His point was that expression-list is a sloppy term
because it really is a comma separated list of assignment-expressions and
should have been called assignment-expression-list. His question was all
about names. He knows the difference between an expression-list and an
expression.
John
[ about comp.lang.c++.moderated. First time posters: do this! ]