问题描述
在CSS问题上阅读后,我想知道:
After reading this answer on a CSS question, I wonder:
在计算机科学中,单个常量值是否被视为表达式?
换句话说, 7px
一个表达式?只是 7
怎么办?
In other words, is 7px
an expression? What about just 7
?
引用,重点是我的:
引用,重点是我的:
这两个似乎都表明值是表达式。但是,有人可能会争辩说不会对值进行评估,因为它已经只是一个值,因此不符合条件。
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.
引用,重点是我的:
这表明即使 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.
您可以查看各种语言的语法,例如
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.
这篇关于是否将单个常量值视为表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!