-1

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
tshepang
  • 12,111
  • 21
  • 91
  • 136
twodayslate
  • 262
  • 1
  • 4
  • 13

1 Answers1

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:

(
* /
+ -
< > <= >=
== !=
!
&&
||
) #
Daniel C. Sobral
  • 295,120
  • 86
  • 501
  • 681