5

According to Wikipedia the shunting-yard algorithm is used to parse mathematical expressions. But is there any reason it could not be used with a mix of logical and arithmetic expressions, as well as comparisons?

As an example, could one use it to parse this:

a+b<17 && a+b>3 || a==b

As far as I can see, you could just define logical operators to have the lowest precedence, followed by comparison operators, and then have the usual arithmetic operators with increasing precedence. Or am I missing something?

Alex
  • 1,157
  • 3
  • 11
  • 25
  • Nothing, I'm just askin whether this works in theory. – Alex Apr 27 '16 at 16:27
  • 2
    Sure it works. Syntactically, the logical ops are binary infix operators, as are the arithmetical operators. Parsing is syntax so that makes no difference. The difference is purely in the semantics, meaning the annotations to the parse tree will have to change or the code to process the RPN stack,respectively. – collapsar Apr 27 '16 at 16:54
  • You could probably use it to parse multiple statements too, and even simple if-then-else blocks, unless you have loops, function calls, gotos etc. – o9000 Apr 27 '16 at 16:57
  • You are correct. All you have to do is add to the operators and precedence tables. The algorithm itself doesn't need to change at all. However @o9000 is mistaken in asserting that it can be used to parse anything other than expressions composed of infix operators and parentheses. – user207421 Oct 23 '20 at 00:59
  • Wikipedia wrong again. I fixed it. – user207421 Mar 01 '22 at 00:54

0 Answers0