Skip to content

Operator Precedence problem in Shunting Yard Algo #84

Description

@Hallett-K

In the Shunting Yard Algorithm video, we give + a higher precedence than -. Whilst this is usually fine, there's an issue here.

Consider the expression 1 - 2 + 3. Mathematically, if we were to add then subtract, what we want to be doing is -2 + 3. What the code does in this case instead is 2 + 3. This means that we end up doing 1 - 5, rather than 1 + 1, which is incorrect.

The easiest solution would be to give + and - the same precedence, and for the sake of consistency also give * and / the same precedence. Alternatively we'd need to check whether the first parameter is part of a subtraction, which can be a little finnicky.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions