Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What precisely is an expression?

Consider whether x in the declaration int x; is an expression.

I used to think that it's certainly not, but the grammar calls the variable name an id-expression here.

One could then argue that only expression is an expression, not ??-expression. But then in 1 + 2, neither 1 nor 2 match, because those are additive-expression and multiplicative-expression respectively, not expressions. But common sense says those should be called expressions too.

We could decide that any ??-expression (including expression) is an expression, but then the variable name in a declaration matches as well.

We could define an expression to be any ??-expression except id-expression, but this feels rather arbitrary.

What's the right grammatical definition of an expression, and is the variable name in its declaration an expression or not?

like image 448
HolyBlackCat Avatar asked Apr 30 '26 13:04

HolyBlackCat


1 Answers

After looking at the links provided by @LanguageLawyer (1, 2), I'm convinced the consensus is that id-expression is a misnomer, and not always an expression (e.g. it's not an expression in a declaration).

Then, a source substring is an expression if at least one of its parents in the parse tree is called:

  • expression, or
  • *-expression1 but not id-expression

, and that parent expands exactly to this substring and nothing more.

This is the same definition @n.m. proposed, except I allow "*-expression and not id-expression" nodes as well.


1 * is a wildcard for any string.

like image 172
HolyBlackCat Avatar answered May 02 '26 02:05

HolyBlackCat