-->

Parse Math Expression in PHP

2019-04-17 14:50发布

问题:

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:

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.