C++ Logo


Advanced search

When is an expression not an expression?

From: Andrew Schepler <aschepler_at_[hidden]>
Date: Sun, 22 Sep 2019 13:04:42 -0400
I'm somewhat bothered by the way the Standard sometimes uses terms related
to the syntax grammar. For an example, in the statement "f(n+1);" the
expressions are "f", "n", "1", "n+1", and "f(n+1)", right? Maybe not: just
looking at the grammar production, the token "1" is a "literal", a
"primary-expression", a "postfix-expression", a "unary-expression", a
"cast-expression", a "pm-expression", and a "multiplicative-expression".
But it is not an instance of the non-terminal grammar symbol "expression",
which can't appear to the right of binary "+".

Even some full-expressions are not expressions: the thing following an "="
in an "initializer" can be an "assignment-expression", but is not an

Of course, we know what's really meant by sentences like "The
postfix-expression [of a function call syntax] is sequenced before each
expression in the expression-list and any default argument". There are two
concepts: the grammar non-terminal symbol "expression", and the more
general thing "expression" which has a value category and type, might be
used as a subexpression or a full-expression, and might be evaluated. But
the Standard never actually makes this clear in any way. (In the quoted
sentence, C++17-ish draft N4659 [expr.call]/5 even has the word
"expression" italicized.)

There's a similar issue with "declaration". The non-terminal symbol
"declaration" only appears at the translation-unit top level, in a
namespace-body, in a linkage-specification (e.g. following 'extern "C"'),
following a "template < template-parameter-list >" introduction, in an
explicit instantiation, or in an explicit specialization. Which means that
grammatically, a member-declaration in a class definition, a
declaration-statement, or a parameter-declaration is never a declaration,
although we normally consider them declarations.

Is this an actual issue and worth fixing? Or am I missing some usual
convention for discussing grammar productions that does make this more
technically clear?

For a possible fix, maybe rename the non-terminal grammar symbols to
"general-expression" and "general-declaration". Then define "expression" to
mean an instance of any grammar symbol with symbol name ending in
"-expression", or actually list all of them (about 25?). And define
"declaration" to be a general-declaration, member-declaration,
declaration-statement, parameter-declaration, or exception-declaration.
Except for these two definitions, and in actual grammar rules, almost all
instances of the words "expression" and "declaration" should no longer be
in italics.

-- Andrew Schepler

Received on 2019-09-22 12:07:04