# Mathematical String Parsing!

Hi all.

I’ve written an application which creates graphs where the values of Y and X are functions of a third variable T. For example, if X = t^2 + 2t + 4, and Y = log2t, a graph like the following is produced:

I have gotten that far.

However, to create such a graph, the following code is used:

``````<?php
include 'Graph.php';
\$Graph = new Graph('t^2 + 2t + 4 against log(2t)', 250, 500);
\$Graph->Axes->X = new Axis("t^2 + 2t + 4", -6, 5);
\$Graph->Axes->Y = new Axis("log2t", -6, 50);
\$Graph->LineThickness = 3;
\$Graph->Inputs = range(-100, 10, 0.01);
\$Graph->MajorGrid = new Grid( new Box(6, 6), Color::Black() );
\$Graph->MinorGrid = new Grid( new Box(0.5, 0.5), Color::Grey() );
\$Graph->addFormula( new Formula( 'x', 'y', Color::Red() ) );
function x(\$t){
return pow(\$t, 2) + 2 * \$t + 4;
}
function y(\$t){
return log(2 * \$t);
}
imagejpeg(\$Graph->Run()->Resource, null, 100);
``````

What I want to do is make this graph application accessible to a few teachers, who would want to enter the X and Y formulae via a form, as a mathematical input.

``````
x = pow(\$t, 2) + 2 * \$t + 4
y = log(2 * t)

``````

They’d want to enter:

``````
x = t^2 + 2t + 4
y = log2t

``````

So I’m interested in looking at the various ways that I can parse the mathematical input into commands that PHP can understand. I am going to be trying a few ideas in the mean time, however I thought I’d ask here to see what you guys could come up with! The theory behind it is what I’m interested in, so code itself isn’t required - I’m just after ideas.

Example Ins/Outs:

``````t^2 + log(t) => pow(\$t, 2) + log(\$t)
(t + sint)^3 => pow( (\$t + sin(\$t) ), 3)
2^t + tan^2(t) => pow( 2, \$t ) + pow( tan( \$t ), 2 )
``````

Take a look at the eval() page…

Eval parses PHP code. I’m looking for a way to parse mathematical language into instructions.

http://www.phpclasses.org/browse/file/11680.html