是否将单个常量值视为表达式? [英] Is a single constant value considered an expression?

查看:172
本文介绍了是否将单个常量值视为表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在CSS问题上阅读此答案后,我想知道:

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

在计算机科学中,单个常量值是否被视为表达式?

换句话说, 7px 一个表达式?只是 7 怎么办?

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

引用 Wikipedia ,重点是我的:


一种编程语言中的表达式是一个或多个显式值,常量,变量,运算符和函数的组合,编程语言将其解释并计算以产生另一个值<... / strong>。对于数学表达式,此过程称为求值。

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.

引用 MS Docs ,重点是我的:


表达式是由一个或多个操作数零或多个运算符组成的序列,可以对其进行求值到单个值,对象,方法或名称空间。表达式可以由文字值[...]组成。

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.

引用技术百科全书,重点是我的:


[...]就结构而言,专家指出,表达式固有地至少需要一个操作数或所作用的值,并且必须具有一个或多个运算符强>。 [...]

[...] 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. [...]

这表明即使 x 也没有资格因为缺少一个或多个运算符

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

推荐答案

这取决于课程的确切定义,但是在大多数定义下,表达式都是使用常量作为基本情况之一递归定义的。因此,是的,文字值是表达式的特殊情况。

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.

您可以查看各种语言的语法,例如一个适用于Python的

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

如果您通过语法进行跟踪,则会发现 expr 可以是包含数字文字的 atom 。当您考虑以下生成时,数字文字就是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)*

这是捕获诸如 x< 7 ,如果 7 不是有效的表达式,则不会被捕获。

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

这篇关于是否将单个常量值视为表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆