3

I have to create a simple formula interpreter with PHP. It has to support 4 operators: exp, ln, addition, subtraction and brackets.

Where should I start? I've heard that the formula entered by the user must be transformed into a tree, is that true? Maybe interpreters already exist?

hidarikani
  • 1,121
  • 1
  • 11
  • 25
  • Do you need to create this as homework? If so, you must have been taught the relevant background materials to start. – Shamim Hafiz - MSFT Aug 08 '11 at 06:34
  • *(tip)* [Clean Code - Inheritance, Polymorphism & Testing](http://www.youtube.com/watch?v=4F72VULWFvc) – Gordon Aug 08 '11 at 06:50
  • (reference) Have a look at: [Reverse Polish notation](http://en.wikipedia.org/wiki/Reverse_Polish_notation) and following articles. And this one probably too: [Shunting-yard algorithm](http://en.wikipedia.org/wiki/Shunting-yard_algorithm) – Yoshi Aug 08 '11 at 07:02
  • Shamim, no it's not homework, so I haven't been taught anything – hidarikani Aug 08 '11 at 07:43

3 Answers3

4

Yes, correct the formula entered by the user must be converted into an abstract syntax tree.

Mathematical formulas are normally written using the infix notation. You need to convert this to postfix or prefix notation. The postfix notation is also known as the reverse polish notation.

You can use the shunting yard algorithm to accomplish this. See a detailed example.

This stackoverflow question links to a implementation in PHP.

Community
  • 1
  • 1
Ocaj Nires
  • 3,295
  • 1
  • 18
  • 10
  • How can I validate the user input and display errors like 'No closing bracket'? – hidarikani Aug 08 '11 at 08:39
  • Read [the algorithm in detail](http://en.wikipedia.org/wiki/Shunting_yard_algorithm#The_algorithm_in_detail) part of the shunting yard algorithm. It handles invalid input like mismatched parenthesis. – Ocaj Nires Aug 08 '11 at 08:55
1

You could probably use basic string manipulation to rewrite the formula into a PHP expression and eval it. Depending on the syntax, you could even leave the expression alone and just define PHP functions for exp and ln, so when you eval the input it can be evaluated directly.

That'd be a lot more straightforward than writing your own parser and interpreter for such a simple language.

If this is classwork, your teacher will likely fail you for doing it that way.

Dan Grossman
  • 51,866
  • 10
  • 112
  • 101
  • Fortunately it's not classwork :) Eval sounds like a good solution but it's dangerous to allow users to execute their own PHP code. I should probably come up with a huge regular expression to validate the input... – hidarikani Aug 08 '11 at 06:43
  • Should be a very simple regex, no? Allow numbers, `+`, `-`, `exp`, `ln`, brackets and whitespace. You can't write any dangerous PHP code with just that. – Dan Grossman Aug 08 '11 at 06:47
0

I created this, check it out: Formula Interpreter

How does it work ?

First, create an instance of FormulaInterpreter with the formula and its parameters

$formulaInterpreter = new FormulaInterpreter("x + y", ["x" => 10, "y" => 20]);

Use the execute() method to interpret the formula. It will return the result:

echo $formulaInterpreter->execute();

in a single line

echo (new FormulaInterpreter("x + y", ["x" => 10, "y" => 20]))->execute();

Examples

# Formula: speed = distance / time
$speed = (new FormulaInterpreter("distance/time", ["distance" => 338, "time" => 5]))->execute() ;
echo $speed;


#Venezuela night overtime (ordinary_work_day in hours): (normal_salary * days_in_a_work_month)/ordinary_work_day
$parameters = ["normal_salary" => 21000, "days_in_a_work_month" => 30, "ordinary_work_day" => 8];
$venezuelaLOTTTArt118NightOvertime = (new FormulaInterpreter("(normal_salary/days_in_a_work_month)/ordinary_work_day", $parameters))->execute();
echo $venezuelaLOTTTArt118NightOvertime;


#cicle area
$cicleArea = (new FormulaInterpreter("3.1416*(radio*radio)", ["radio" => 10]))->execute();
echo $cicleArea;

About the formulas

  1. It must contain at least two operands and an operator.
  2. Operands' name could be in upper or lower case.
  3. By now, math functions as sin, cos, pow… are not included. I'm working to include them.
  4. If your formula is not valid, you will get an error message like: Error, your formula (single_variable) is not valid.
  5. Parameters' values must be numeric.

You can improve it if you want to!

Community
  • 1
  • 1
Carlos Espinoza
  • 1,115
  • 11
  • 13