Evaluate expression using Shunting Yard algorithm


I needed this recently so I implemented Shunting Yard algorithm in one of my projects. Please find attached v16 database with set of methods to accomplish expression evaluation within 4D:

http://forums.4d.fr/4DBB_Main/x_User/3996/files/19433575.zip (link updated 2017-05-17)

[]19376678;“Your comment here…”[/]
I was a great fan of my HP calculator when I was a student, you make me feel younger :wink:


Nothing’s new in smartphones after all :mrgreen:

I think there is an error somewhere in your code, 8 + 12 returns 3.

Are there spaces in expression? You should filter them out.

You are right, expect new version soon.

no spaces


Numbers are considered as single digits:
[]19377001;“Your comment here…”[/]
This regex may help to capture numbers:

i know this notation but i really don’t get it.
is there a real situation where you needed it ?

See “advantages” at the beginning of http://www.calculatrices-hp.com/index.php?page=rpnthis page>. I’m interested in “what for” too.


I updated the link in first message, and here it is again:


I fixed the issue with numbers and added support for decimal and thousand separators.


I’m interested in “what for” too.

I am consulting a friend working on application for golf association here (not as a golfer that I am not, but as 4D consultant B) ) and there are team tournaments in golf. Now golfers will know that every player has something called “playing handicap” that gets calculated at the beginning of the tournament for each player/team. For teams they usually have formulas like two third of sum of handicaps of all players, sometimes they want to use playing handicap of each player, sometimes they want to use handicap. So, we tell them to enter something like 1/3P1+2/3P2 and later we replace P1, P2, … to handicaps of players and use these methods to evaluate the formula.

Hi Milan,
thanks a lot!

You already said spaces must be removed, but I’ve seen that more generally any “unexpected” char in the expression results in non ending loop. Better to do something like this before:

<code 4D>
For ($i;1;Length($rawExpression))
If (Position($rawExpression[$i];"+*/-^\()0123456789.")>0)
$cleanExpression:=$cleanExpression +$rawExpression[$i]
End if
End for
</code 4D>

Thanks, I will do it over weekend.