是否存在诸如左关联前缀运算符或右关联后缀运算符之类的东西? [英] Is there such thing as a left-associative prefix operator or right-associative postfix operator?

查看:125
本文介绍了是否存在诸如左关联前缀运算符或右关联后缀运算符之类的东西?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此页面说,前缀运算符是通常是右关联的,而后缀运算符是左关联的"(强调我的).

This page says "Prefix operators are usually right-associative, and postfix operators left-associative" (emphasis mine).

是否存在左关联前缀运算符或右关联后缀运算符的真实示例?如果不是,那么一个假想的人会是什么样子,以及如何对其进行解析?

Are there real examples of left-associative prefix operators, or right-associative postfix operators? If not, what would a hypothetical one look like, and how would it be parsed?

推荐答案

原则上可以.例如,考虑前缀一元加号和减号运算符:假定+是标识运算,而-取一个数字值.

There could be in principle. Consider for example the prefix unary plus and minus operators: suppose + is the identity operation and - negates a numeric value.

它们通常"是右关联的,这意味着+-1等同于+(-1),结果为负一.

They are "usually" right-associative, meaning that +-1 is equivalent to +(-1), the result is minus one.

假设它们是左关联的,则表达式+-1将等同于(+-)1.

Suppose they were left-associative, then the expression +-1 would be equivalent to (+-)1.

因此,该语言必须为子表达式+-赋予含义.语言通常"不需要它具有某种含义并且不给它一个含义,但是您可以想象一种功能语言,其中将身份运算符应用于否定运算符的结果是一个具有完全相同含义的运算符/函数.与否定运算符的效果相同.那么对于此示例,完整表达式的结果将再次为-1.

The language would therefore have to give a meaning to the sub-expression +-. Languages "usually" don't need this to have a meaning and don't give it one, but you can probably imagine a functional language in which the result of applying the identity operator to the negation operator is an operator/function that has exactly the same effect as the negation operator. Then the result of the full expression would again be -1 for this example.

实际上,如果将功能/运算符并置的结果定义为功能/运算符,其效果与按从右到左的顺序应用这两种功能相同,则始终对表达式的结果,即您将它们关联的方式.这些只是定义(f g)(x) == f(g(x))的两种不同方式.但是,如果您的语言将+-定义为除-之外的其他含义,那么关联性的方向将很重要(我怀疑对于习惯于常用"语言的人来说,该语言将非常难以阅读...)

Indeed, if the result of juxtaposing functions/operators is defined to be a function/operator with the same effect as applying both in right-to-left order, then it always makes no difference to the result of the expression which way you associate them. Those are just two different ways of defining that (f g)(x) == f(g(x)). If your language defines +- to mean something other than -, though, then the direction of associativity would matter (and I suspect the language would be very difficult to read for someone used to the "usual" languages...)

另一方面,如果该语言不允许并置运算符/函数,则前缀运算符必须是右关联的,以允许表达式+-1.不允许并置是(+-)没有意义的另一种说法.

On the other hand, if the language doesn't allow juxtaposing operators/functions then prefix operators must be right-associative to allow the expression +-1. Disallowing juxtaposition is another way of saying that (+-) has no meaning.

这篇关于是否存在诸如左关联前缀运算符或右关联后缀运算符之类的东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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