This is my situation: the input is a string that contains a normal mathematical operation like 5+3*4
. Functions are also possible, i.e. min(5,A*2)
. This string is already tokenized, and now I want to parse it using stacks (so no AST). I first used the Shunting Yard Algorithm, but here my main problem arise:
Suppose you have this (tokenized) string: min(1,2,3,+)
which is obviously invalid syntax. However, SYA turns this into the output stack 1 2 3 + min(
, and hopefully you see the problem coming. When parsing from left to right, it sees the +
first, calculating 2+3=5
, and then calculating min(1,5)
, which results in 1. Thus, my algorithm says this expression is completely fine, while it should throw a syntax error (or something similar).
What is the best way to prevent things like this? Add a special delimiter (such as the comma), use a different algorithm, or what?