PHPackages                             j84reginato/my-eval - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. j84reginato/my-eval

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

j84reginato/my-eval
===================

PHP parser for mathematical expressions.

1.0.0(4y ago)2652LGPL-3.0PHPPHP &gt;=8.1

Since Feb 17Pushed 4y ago1 watchersCompare

[ Source](https://github.com/j84reginato/my-eval)[ Packagist](https://packagist.org/packages/j84reginato/my-eval)[ Docs](https://github.com/j84reginato/my-eval)[ RSS](/packages/j84reginato-my-eval/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (4)Versions (4)Used By (0)

My-eval
=======

[](#my-eval)

[![Latest Stable Version](https://camo.githubusercontent.com/39a31665851581cd833e8a859ba85f4a668ef25f89ba3751ffcd4ef0cdc55d41/68747470733a2f2f706f7365722e707567782e6f72672f6a3834726567696e61746f2f6d792d6576616c2f762f737461626c65)](https://packagist.org/packages/j84reginato/my-eval) [![Total Downloads](https://camo.githubusercontent.com/d3286ce147016d7d32dad1976a03041d258a40db92d977a746af9abab580c28b/68747470733a2f2f706f7365722e707567782e6f72672f6a3834726567696e61746f2f6d792d6576616c2f646f776e6c6f616473)](https://packagist.org/packages/j84reginato/my-eval) [![License](https://camo.githubusercontent.com/359a34d29cbfceb8d2e12c310dfd38d36e63a084ed34f8e06c63b606a97cd678/68747470733a2f2f706f7365722e707567782e6f72672f6a3834726567696e61746f2f6d792d6576616c2f6c6963656e7365)](https://packagist.org/packages/j84reginato/my-eval)[![Code Climate](https://camo.githubusercontent.com/2872be752cc3cbb0f110ed75c17ca3be03662fa9fb5079795fc905e0d3a4b84a/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f6a3834726567696e61746f2f6d792d6576616c2f6261646765732f6770612e737667)](https://codeclimate.com/github/j84reginato/my-eval)

DESCRIPTION
-----------

[](#description)

PHP parser and evaluator library for mathematical and logical expressions.

Intended use: safe and reasonably efficient evaluation of user submitted formulas and/or logical expressions. The library supports basic arithmetic and elementary functions, as well as variables and extra functions, ternary ( if/then/else) expressions and conditional, logical (conjunction and disjunction) and relational operations.

The lexer and parser produces an abstract syntax tree (AST) that can be traversed using a tree interpreter. The math-parser library ships with three interpreters:

- an evaluator computing the value of the given expression.
- a differentiator transforming the AST into a (somewhat) simplied AST representing the derivative of the supplied expression.
- a rudimentary LaTeX output generator, useful for pretty printing expressions using MathJax

EXAMPLES
--------

[](#examples)

It is possible to fine-tune the lexer and parser, but the library ships with a StdMathParser class, capable of tokenizing and parsing standard mathematical expressions, including arithmetical operations as well as elementary functions.

```
Use MyEval\Lexing\StdMathLexer;
use MyEval\Parsing\Parser;
use MyEval\Solving\StdMathEvaluator;

// Tokenize
$lexer = new StdMathLexer();
$tokens = $lexer->tokenize('1+2');

// Parse
// Generate an abstract syntax tree
$parser = new Parser();
$ast = $parser->parse($tokens);

// Do something with the AST, e.g. evaluate the expression:
$evaluator = new StdMathEvaluator();
$value = $ast->accept($evaluator);
echo $value;

```

More interesting example, containing variables:

```
Use MyEval\Lexing\StdMathLexer;
use MyEval\Parsing\Parser;
use MyEval\Solving\StdMathEvaluator;

// Tokenize
$lexer = new StdMathLexer();
$tokens = $lexer->tokenize('x+sqrt(y)');

// Parse
// Generate an abstract syntax tree
$parser = new Parser();
$ast = $parser->parse($tokens);

// Evaluate
$evaluator = new StdMathEvaluator([ 'x' => 2, 'y' => 3 ]);
$value = $ast->accept($evaluator);

```

We can do other things with the AST. The library ships with a differentiator, computing the (symbolic) derivative with respect to a given variable.

```
use MyEval\Lexing\StdMathLexer;
use MyEval\Parsing\Parser;
use MyEval\Solving\Differentiator;
use MyEval\Solving\StdMathEvaluator;

// Tokenize
$lexer = new StdMathLexer();
$tokens = $lexer->tokenize('exp(2*x)-x*y');

// Parse
// Generate an abstract syntax tree
$parser = new Parser();
$ast = $parser->parse($tokens);

// Differentiate
$differentiator = new Differentiator('x');
$derivative = $ast->accept($differentiator);
$df = $derivative->accept($differentiator);

// Evaluate
// $df now contains the AST of '2*exp(x)-y' and can be evaluated further
$evaluator = new StdMathEvaluator([ 'x' => 1, 'y' => 2 ]);
$value = $df->accept($evaluator);

```

### Implicit multiplication

[](#implicit-multiplication)

Another helpful feature is that the parser understands implicit multiplication. An expression as `2x` is parsed the same as `2*x` and `xsin(x)cos(x)^2` is parsed as `x*sin(x)*cos(x)^2`.

Note that implicit multiplication has the same precedence as explicit multiplication. In particular, `xy^2z` is parsed as `x*y^2*z` and **not** as `x*y^(2*z)`.

To make full use of implicit multiplication, the standard lexer only allows one-letter variables. (Otherwise, we wouldn't know if `xy` should be parsed as `x*y` or as the single variable `xy`).

DOCUMENTATION
-------------

[](#documentation)

For complete documentation, see the TODO

THANKS
------

[](#thanks)

The Lexer is based on the lexer described by Marc-Oliver Fiset in his [blog](http://marcofiset.com/programming-language-implementation-part-1-lexer/).

The parser is a version of the "Shunting yard" algorithm, described for example by [Theodore Norvell](http://www.engr.mun.ca/~theo/Misc/exp_parsing.htm#shunting_yard).

###  Health Score

29

—

LowBetter than 60% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

1542d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/2695ab6fe0d978ab5d24b4fa63a98cfe1545fc81afeafe23a1a5f043f3fd6b2c?d=identicon)[jnreginato](/maintainers/jnreginato)

---

Top Contributors

[![jnreginato](https://avatars.githubusercontent.com/u/22461202?v=4)](https://github.com/jnreginato "jnreginato (2 commits)")

---

Tags

parsermathematics

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/j84reginato-my-eval/health.svg)

```
[![Health](https://phpackages.com/badges/j84reginato-my-eval/health.svg)](https://phpackages.com/packages/j84reginato-my-eval)
```

###  Alternatives

[nikic/php-parser

A PHP parser written in PHP

17.4k902.6M1.8k](/packages/nikic-php-parser)[doctrine/lexer

PHP Doctrine Lexer parser library that can be used in Top-Down, Recursive Descent Parsers.

11.2k910.8M118](/packages/doctrine-lexer)[erusev/parsedown

Parser for Markdown.

15.0k151.8M729](/packages/erusev-parsedown)[league/commonmark

Highly-extensible PHP Markdown parser which fully supports the CommonMark spec and GitHub-Flavored Markdown (GFM)

2.9k404.0M702](/packages/league-commonmark)[masterminds/html5

An HTML5 parser and serializer.

1.8k242.8M228](/packages/masterminds-html5)[sabberworm/php-css-parser

Parser for CSS Files written in PHP

1.8k191.2M64](/packages/sabberworm-php-css-parser)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
