Expression Parser

Expression Parser is an open-source library written in PHP that can be used for parsing arithmetic expressions and functions in different programming languages.

When to use it

Installation

composer install anastasd/expression-parser

Examples

<?php
$settings = new Settings(Settings::PHP);
$parser = new Parser($settings);

$input = '-pow($a, 2) * sin(log($b)) + cos(sqrt($c)) * exp(tan($d)) / log(abs($e)) - sqrt(pow($f, 3))';

$outputSource = $parser->parse($input, ['$a', '$b', '$c', '$d', '$e', '$f'])
     ->prepare()
     ->compile();

The output is

function expressionResult( $a,$b,$c,$d,$e,$f ){ $param_24=3;$param_2=$a;$param_3=2;$param_23=$f;$param_7=$b;$param_19=$e;$param_11=$c;$param_15=$d;$param_22=pow($param_23, $param_24);$param_21=$param_22;$param_20=sqrt($param_21);$param_18=abs($param_19);$param_17=$param_18;$param_16=log($param_17, M_E);$param_14=tan($param_15);$param_13=$param_14;$param_12=exp($param_13);$param_10=sqrt($param_11);$param_9=$param_10;$param_8=cos($param_9);$param_6=log($param_7, M_E);$param_5=$param_6;$param_4=sin($param_5);$param_1=pow($param_2, $param_3);$param_0_0=$param_1*$param_4;$param_0_1=$param_8*$param_12;$param_0_2=$param_0_1/$param_16;$param_0_3=0-$param_0_0;$param_0_4=$param_0_2-$param_20;$param_0_5=$param_0_3+$param_0_4; return $param_0_5 ;}

How it works?

The input to be parsed is divided into nodes. Each node contains a single function, number, function argument, or a group of other nodes. Nodes are then sorted in the order of execution, primarily defined by the operator’s order property.

The prepared nodes can then be compiled into executable code or evaluated with the provided values.

Structure

Settings

A class that holds all configurable information about the parser. Has the following properties:
$functionRegex – used if the names of functions can be matched with a regular expression, otherwise null. Useful for parsing spreadsheet functions
$variableRegex – used if the variables can be matched with a regular expression, otherwise null. Useful when the variables are not known prior to parsing.
$paramPrefix – prefix for the intermediate variables that will be used by the compiler
$longTemplate – the template for the output of the compiler with all chunks in it. Takes these parameters: {{FUNCTION_PARAMS}}, {{FUNCTION_BODY}} and {{FUNCTION_RESULT}}
$compressedTemplate – the template of the compressed output of the compiler in one line. Takes these parameters: {{FUNCTION_PARAMS}}, {{FUNCTION_BODY}} and {{FUNCTION_RESULT}}

The class supports these methods:
setOperator – Adds a new operator to the mapper or replaces an existing one
removeOperator – Removes an operator from the mapper
setFunction – Adds a new function to the mapper or replaces an existing one
removeFunction – Removes a function from the mapper

For argument definitions of these methods please check the examples.

Mapper

A class that holds relations between the operators, functions in the Compiler helper, brackets used to enclose function arguments, separators to split the function arguments and quote marks that enclose strings. Has the following properties:
$operators – array of mappings that links operator symbols to evaluating/compiling functions. Example:

... 
        '+' => [
            'arity' => 'binary', // Only 'binary' is accepted
            'order' => 0, // The higher the order the earlier is execution
            'evaluate' => 'add', // Function that evaluates the operator
            'compile' => 'add' // Function that compiles the operator
        ],
...
$functions – array of mappings that links function used in the input to evaluating/compiling functions. Example:
...
        'abs' => [
            'evaluate' => 'abs', // Function that evaluates 
            'compile' => 'abs', // Function that compiles the operator
            'min_args' => 1, // Minimum arguments that the function takes
            'max_args' => 1 // Maximum arguments that the function takes
        ],
...
$brackets – array of pairs of brackets that are used in the input. Must be grouped opening-closing. Example:
[['(', ')']]
$argsSeparators – array of symbols that are used to separate function arguments in the input. Example:
[',', ';']
$quotemarks – array of symbols that are used to enclose the strings in the input. Example:
['"', '\'']

Compiler

A class that performs the compilation of the parsed input. The abstract parent has a default method for compilation that can be replaced in custom compilators.

CompilerHelper

A helper class that has the methods for compilation of individual operators and functions.

Evaluator

A class that performs the evaluation of the parsed input. The abstract parent has a default method for evaluation that can be replaced in custom evaluators.

EvaluatorHelper

A helper class that has the methods for evaluation of individual operators and functions.