This might be useless, but there's a great, fully featured, calculator for Linux called qalculate and it's built using the libqalculate library which (AFAIK) specializes in parsing math expressions. There might be better solutions, but I don't know of them.
The API reference for the library is
here.