Is a single constant value considered an expression?
Asked Answered
P

2

1

After reading this answer on a CSS question, I wonder:

In Computer Science, is a single, constant value considered an expression?

In other words, is 7px an expression? What about just 7?

Quoting Wikipedia, emphasis mine:

An expression in a programming language is a combination of one or more explicit values, constants, variables, operators, and functions that the programming language interprets [...] and computes to produce [...] another value. This process, as for mathematical expressions, is called evaluation.

Quoting MS Docs, emphasis mine:

An expression is a sequence of one or more operands and zero or more operators that can be evaluated to a single value, object, method, or namespace. Expressions can consist of a literal value [...].

These both seems to indicate that values are expressions. However, one could argue that a value will not be evaluated, as it is already only a value, and therefore doesn't qualify.

Quoting Techopedia, emphasis mine:

[...] In terms of structure, experts point out that an expression inherently needs at least one 'operand’ or value that is acted on, and must have one or more operators. [...]

This suggests that even x does not qualify as expression as it is lacking one or more operators.

Pifer answered 19/12, 2017 at 14:12 Comment(5)
Note that I'm aware of this related question. However, the answers there do not answer this more specific question.Pifer
@Racil You might be right, but then again, "zero or more operators" is clearly different from "one or more operators", no?Pifer
Yes, I agree that the wording can be enhanced, but I don't see a real value in this question. Either way, what difference that makes? In most (if not all) cases where an expression can be used, a single value can be used too. I guess my question is: what will we gain from answering this question?Bicorn
Curiosity? Search of an unambiguous definition? Creating material for future interview questions? Not sure. I guess I just want to know. These things intrigue me, and I can only assume that I'm not the only one.Pifer
The first two are valid reasons, but having such a question in an interview only shows the low quality of such an interview. It is like asking "what is the meaning of JavaScript". Yeah, it's nice to know, but having it as an interview question? It definitely pushes me away from working at such company. Interview questions should be focused on required skills, not irrelevant knowledge.Bicorn
N
2

It depends on the exact definition of course, but under most definitions expressions are defined recursively with constants being one of the basis cases. So, yes, literal values are special cases of expressions.

You can look at grammars for various languages such as the one for Python

If you trace through the grammar you see that an expr can be an atom which includes number literals. The fact that number literals are Python expressions is also obvious when you consider productions like:

comparison: expr (comp_op expr)*

This is the production which captures expressions like x < 7, which wouldn't be captured if 7 isn't a valid expression.

Nanna answered 19/12, 2017 at 14:16 Comment(6)
This answer (especially the text after the first paragraph) has nothing to do with the question. The fact that you can use a value where an expression is expected does not in any way constitute that a value is considered to be an expression. Programming languages are full of examples of using different things in some places. For example, some languages allow you to assign an integer to a string variable, and implicitly convert it to string for you. Does this mean according to your logic that an integer is considered to be a string?Bicorn
The question is about programming concepts, not the behavior of compilers and programming languages.Bicorn
@RacilHilan The fact that in Python I can use 7 where an expression is required means that in Python 7 is an expression. I don't think that Python is idiosyncratic here. A quick check of the official C grammar will show that it also considers 7 to be an expression. If most programming languages have a nonterminal called "expression" (or something similar) and the grammar that defines these nonterminals allows for literal expressions like 7, it would be arbitrary to say that there is a Platonic ideal definition of "expression" which doesn't include that.Nanna
I didn't say your description was wrong. You're right, all languages I'm aware of accept a value where an expression is expected. The issue that I raised is about the word "definition". You are describing the definition in programming languages, which can be different from the definition in other fields (e.g. math, physics, etc). For example, in math we consider integers to be real numbers, but in typed programming languages, integers and floats are two different things.Bicorn
@RacilHilan In any event, the first paragraph is already somewhat decisive (with the rest of the answer more of a confirmation. Expressions have a tree-like structure. The most natural way to define an expression is in such a way that subtrees of an expression tree are themselves expression trees. This leads to leaves being 1-node expression trees, with constants being one possible type of leaf. Still, I can appreciate your point that precision in definitions is important, and that the practice of languages like Python doesn't settle the question.Nanna
Agreed. In the MSDN page that the OP linked to, to them, an expression is almost anything that evaluates to a single value. That includes single values because they can evaluate to, duh, single values. But if you scroll down to the title "iterals and simple names", it's funny how they described why: When those variables are used in an expression, the variable name evaluates to the value that is currently stored in the variable's location in memory. Silly.Bicorn
S
1

In Computer Science, is a single, constant value considered an expression?

It depends entirely on the context. For example, FORTRAN, BASIC, and COBOL all have line numbers. Those are numeric constant values that are not expressions.

In other contexts (even within those languages) a numeric constant may be an expression.

Struve answered 21/12, 2017 at 4:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.