C++ Logo

std-discussion

Advanced search

Re: When is an expression not an expression?

From: Krystian Stasiowski <sdkrystian_at_[hidden]>
Date: Sun, 22 Sep 2019 13:41:53 -0400
> 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 "+".`1` is certainly an *expression* (the grammatical production) and normal English word "expression" as used in the standard.literal > primary-expression > postfix-expression > unary-expression > cast-expression > pm-expression > multiplicative-expression > additive-expression > shift-expression > compare-expression > relational-expression > equality-expression > and-expression > exclusive-or-expression > inclusive-or-expression > logical-and-expression > logical-or-expression > conditional-expression > assignment-expression > expression> Even some full-expressions are not expressions: the thing following an "=" in an "initializer" can be an "assignment-expression", but is not an "expression".This has been something that bothered me in the past, I have largely gotten over it.As for the other things you address, yeah, it exists, but the effort required to fix those is not worth it.
-------- Original message --------From: Andrew Schepler via Std-Discussion <std-discussion_at_[hidden]> Date: 9/22/19 13:04 (GMT-05:00) To: std-discussion_at_[hidden] Cc: Andrew Schepler <aschepler_at_[hidden]> Subject: [std-discussion] When is an expression not an expression? 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 "expression".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:44:06