如何将中缀和堆栈优先级扩展到其他运算符?

发布于 2024-07-09 19:23:47 字数 479 浏览 6 评论 0原文

如何扩展中缀和堆栈优先级以包含运算符 <><=>===!=!&&||?

解析中缀表达式时,例如: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

许久 2024-07-16 19:23:47

这取决于您想要优先考虑什么,对吗? 除非您询问特定语言的优先级(如果是这样,请详细说明)。

无论如何,<、>、<= 和 >= 不适用于布尔值,== 和 != 适用于任何内容,而 !、&& 则适用于任何值。 和 || 仅适用于布尔值。 但它们都返回布尔值,因此您需要首先应用那些不适用于布尔值的值,然后应用那些可能适用于布尔值的值,最后应用那些仅适用于布尔值的值。 至于最后一个,! 优先于 && 和||。 虽然没有必要,但我会制作 && 优先于 ||,因为某些逻辑符号是这样工作的。

所以优先级最终会是:

(
* /
+ -
< > <= >=
== !=
!
&&
||
) #

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:

(
* /
+ -
< > <= >=
== !=
!
&&
||
) #
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文