如何将中缀和堆栈优先级扩展到其他运算符?
如何扩展中缀和堆栈优先级以包含运算符 <
、>
、<=
、>=
、==
、!=
、!
、&&
和 ||?
解析中缀表达式时,例如:P + (Q – F) / Y#
,每个符号都有一个与其操作顺序相关的优先级。 /
和 *
的优先级高于 +
和 -
。
以下是我所拥有/理解的优先事项:
Priority * / + - ( ) #
Infix 2 2 1 1 3 0 0
Stack 2 2 1 1 0 n/a 0
How would the infix and stack priorities be extended to include the operators <
, >
, <=
, >=
, ==
, !=
, !
, &&
, and ||
?
When parsing an infix expression, for example: P + (Q – F) / Y#
, each symbol has a priority which is relevant to their order of operation. /
and *
have a higher priority than +
and -
.
Here are the priorities I have/understand:
Priority * / + - ( ) #
Infix 2 2 1 1 3 0 0
Stack 2 2 1 1 0 n/a 0
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这取决于您想要优先考虑什么,对吗? 除非您询问特定语言的优先级(如果是这样,请详细说明)。
无论如何,<、>、<= 和 >= 不适用于布尔值,== 和 != 适用于任何内容,而 !、&& 则适用于任何值。 和 || 仅适用于布尔值。 但它们都返回布尔值,因此您需要首先应用那些不适用于布尔值的值,然后应用那些可能适用于布尔值的值,最后应用那些仅适用于布尔值的值。 至于最后一个,! 优先于 && 和||。 虽然没有必要,但我会制作 && 优先于 ||,因为某些逻辑符号是这样工作的。
所以优先级最终会是:
That depends on what priorities you want, right? Unless you are asking about the priorities in a specific language (if so, elaborate).
Anyway, <, >, <= and >= do not apply to booleans, == and != apply to anything, and !, && and || apply solely to booleans. But they ALL return booleans, so you want to apply those which do not apply to booleans first, those which might apply to booleans next, and finally those which only apply to booleans. As for the last, ! has precedence over && and ||. Though not necessary, I'd make && have precedence over ||, because some logic notations work that way.
So the precedence would wind up being: