Parse Math Expression in PHP

2019-04-17 14:37发布

I'm currently trying to parse math expression into expression tree. But I'm stuck on the stage where I need to implement functions and negates. I don't understand logic to do it using Shunting-Yard algorithm.

What I currently want to do is to support

  • Negates, like -(x+5)
  • Function calls, like min(x,y)
  • Power just after function name, like cos^2(x)
  • Implicit multiplication, like 2x is same as 2*x
  • Scientific notation
  • Constants e and pi

Can somebody tell me hints how to implement this?

1条回答
\"骚年 ilove
2楼-- · 2019-04-17 15:02

An working, PSR-0 compatible implementation of the shunting yard algorithm can be found here: https://github.com/andig/php-shunting-yard/tree/dev. It supports constants, custom functions etc.

查看更多
登录 后发表回答