目录
2. Parsing with Shunting Yard Algorithm (Convert to RPN)
3. Evaluating the RPN Expression
4. Putting It All Together
5. Usage Example
Key Features & Benefits
Possible Enhancements
首页 后端开发 php教程 在PHP中实现自定义数学表达式解析器和评估器

在PHP中实现自定义数学表达式解析器和评估器

Jul 31, 2025 pm 12:43 PM
PHP Math

答案是:通过分步实现词法分析、Shunting Yard算法解析和RPN求值,可构建安全可控的PHP数学表达式求值器。1. tokenize函数将输入拆分为数字、变量、操作符等标记;2. parseToRPN使用Shunting Yard算法按优先级和结合性转换为逆波兰表示;3. evaluateRPN利用栈结构结合变量上下文计算结果;4. evaluateExpression整合流程并处理异常;5. 示例显示支持变量和标准运算,具备安全性、可扩展性与错误处理能力,适用于需避免eval()风险的场景。

Implementing a Custom Mathematical Expression Parser and Evaluator in PHP

Creating a custom mathematical expression parser and evaluator in PHP can be useful when you need more control than eval() provides—especially for security, extensibility, or educational purposes. While eval() can execute expressions, it's dangerous with untrusted input and lacks flexibility. Building your own parser allows you to safely evaluate expressions like 2 + 3 * (4 - 1) while supporting variables, functions, and error handling.

Implementing a Custom Mathematical Expression Parser and Evaluator in PHP

Here’s how to implement a simple yet effective mathematical expression parser and evaluator in PHP.


1. Tokenization: Breaking the Input into Meaningful Parts

The first step is to convert the input string into tokens—numbers, operators, parentheses, variables, and functions.

Implementing a Custom Mathematical Expression Parser and Evaluator in PHP
function tokenize($expression) {
    $tokens = [];
    $length = strlen($expression);
    $i = 0;

    while ($i < $length) {
        $char = $expression[$i];

        if (ctype_digit($char) || $char === '.') {
            $num = '';
            while ($i < $length && (ctype_digit($expression[$i]) || $expression[$i] === '.')) {
                $num .= $expression[$i++];
            }
            $tokens[] = ['type' => 'number', 'value' => (float)$num];
            $i--;
        } elseif (ctype_alpha($char)) {
            $var = '';
            while ($i < $length && ctype_alnum($expression[$i])) {
                $var .= $expression[$i++];
            }
            $tokens[] = ['type' => 'variable', 'value' => $var];
            $i--;
        } elseif (in_array($char, ['+', '-', '*', '/', '^', '(', ')'])) {
            $tokens[] = ['type' => 'operator', 'value' => $char];
        } elseif ($char === ' ') {
            // Skip whitespace
        } else {
            throw new Exception("Unknown character: $char");
        }
        $i++;
    }

    return $tokens;
}

This tokenizer handles numbers (including decimals), variables (like x, y1), operators, and parentheses.


2. Parsing with Shunting Yard Algorithm (Convert to RPN)

We’ll use the Shunting Yard algorithm by Edsger Dijkstra to convert infix notation (standard math) to Reverse Polish Notation (RPN), which is easier to evaluate.

Implementing a Custom Mathematical Expression Parser and Evaluator in PHP
function parseToRPN($tokens) {
    $output = [];
    $operators = [];
    $precedence = ['+' => 1, '-' => 1, '*' => 2, '/' => 2, '^' => 3];
    $associativity = ['+' => 'left', '-' => 'left', '*' => 'left', '/' => 'left', '^' => 'right'];

    foreach ($tokens as $token) {
        if ($token['type'] === 'number' || $token['type'] === 'variable') {
            $output[] = $token;
        } elseif ($token['value'] === '(') {
            $operators[] = $token;
        } elseif ($token['value'] === ')') {
            while (!empty($operators) && end($operators)['value'] !== '(') {
                $output[] = array_pop($operators);
            }
            array_pop($operators); // Remove '('
        } elseif ($token['type'] === 'operator') {
            $op = $token['value'];
            while (
                !empty($operators) &&
                ($top = end($operators))['value'] !== '(' &&
                isset($precedence[$top['value']]) &&
                (
                    ($associativity[$op] === 'left' && $precedence[$op] <= $precedence[$top['value']]) ||
                    ($associativity[$op] === 'right' && $precedence[$op] < $precedence[$top['value']])
                )
            ) {
                $output[] = array_pop($operators);
            }
            $operators[] = $token;
        }
    }

    while (!empty($operators)) {
        $output[] = array_pop($operators);
    }

    return $output;
}

This algorithm respects operator precedence and associativity (e.g., ^ is right-associative).


3. Evaluating the RPN Expression

Now evaluate the RPN using a stack. Variables are resolved from a provided context (e.g., ['x' => 5]).

function evaluateRPN($rpn, $variables = []) {
    $stack = [];

    foreach ($rpn as $token) {
        if ($token['type'] === 'number') {
            $stack[] = $token['value'];
        } elseif ($token['type'] === 'variable') {
            if (!isset($variables[$token['value']])) {
                throw new Exception("Undefined variable: " . $token['value']);
            }
            $stack[] = $variables[$token['value']];
        } elseif ($token['type'] === 'operator') {
            if (count($stack) < 2) {
                throw new Exception("Invalid expression");
            }
            $b = array_pop($stack);
            $a = array_pop($stack);
            switch ($token['value']) {
                case '+': $stack[] = $a + $b; break;
                case '-': $stack[] = $a - $b; break;
                case '*': $stack[] = $a * $b; break;
                case '/': 
                    if ($b == 0) throw new Exception("Division by zero");
                    $stack[] = $a / $b; 
                    break;
                case '^': $stack[] = pow($a, $b); break;
                default: throw new Exception("Unknown operator: " . $token['value']);
            }
        }
    }

    if (count($stack) !== 1) {
        throw new Exception("Invalid expression");
    }

    return $stack[0];
}

4. Putting It All Together

Now create a simple wrapper function:

function evaluateExpression($expression, $variables = []) {
    try {
        $tokens = tokenize($expression);
        $rpn = parseToRPN($tokens);
        return evaluateRPN($rpn, $variables);
    } catch (Exception $e) {
        return "Error: " . $e->getMessage();
    }
}

5. Usage Example

echo evaluateExpression("2 + 3 * (4 - 1)"); // Output: 11
echo evaluateExpression("x + y * 2", ['x' => 5, 'y' => 3]); // Output: 11
echo evaluateExpression("z ^ 2", ['z' => 4]); // Output: 16

Key Features & Benefits

  • Safe: No use of eval(), so no code injection.
  • Extensible: You can add functions (e.g., sin, sqrt) by extending the tokenizer and evaluator.
  • Error Handling: Clear messages for undefined variables, syntax issues, or division by zero.
  • Supports Variables: Useful for dynamic expressions in forms, calculators, or rule engines.

Possible Enhancements

  • Support for built-in math functions (e.g., sin(x)).
  • Unary operators (e.g., -5, +3).
  • Better error recovery and syntax validation.
  • Custom operator definitions.

Building your own parser gives deep insight into how programming languages and calculators work. While tools like eval() or external libraries exist, a custom parser offers control, safety, and learning value.

Basically, it's not complex once you break it down—tokenize, parse, evaluate.

以上是在PHP中实现自定义数学表达式解析器和评估器的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

在PHP中导航浮点不准确的陷阱 在PHP中导航浮点不准确的陷阱 Jul 29, 2025 am 05:01 AM

浮点数不精确是PHP中常见问题,答案在于其使用IEEE754双精度格式导致十进制小数无法精确表示;1.0.1或0.2等数在二进制中为无限循环小数,计算机需截断造成误差;2.比较浮点数时应使用容差而非==,如abs($a-$b)

数值精度的细微差别:`round()`,`ceil() 数值精度的细微差别:`round()`,`ceil() Jul 29, 2025 am 04:55 AM

round()uses"roundhalftoeven",not"roundhalfup",soround(2.5)returns2andround(3.5)returns4tominimizestatisticalbias,whichmaysurprisethoseexpectingtraditionalrounding.2.Floating-pointrepresentationerrorscausenumberslike2.675tobestored

处理加密货币计算:为什么BCMATH在PHP中至关重要 处理加密货币计算:为什么BCMATH在PHP中至关重要 Aug 01, 2025 am 07:48 AM

bcmathisesene forAccratecryptoCurrencyCalcalsionSinphpBecausefloing-pointarithmeticIntroducesunAcceptablebablerOundingErrors.1.floation-pointnumberslike0.1 0.2yieldimimpreciseresults(e.g.,e.g.,0.30000000000000000000004)

PHP中2D/3D图形的矢量数学基础知识 PHP中2D/3D图形的矢量数学基础知识 Jul 29, 2025 am 04:25 AM

AvectorinPHPgraphicsrepresentsposition,direction,orvelocityusingaclasslikeVector3Dwithx,y,zcomponents.2.Basicoperationsincludeaddition,subtraction,scalarmultiplication,anddivisionformovementandscaling.3.MagnitudeiscalculatedviathePythagoreantheorem,a

掌握数字系统:PHP中的高级基础转换技术 掌握数字系统:PHP中的高级基础转换技术 Jul 30, 2025 am 02:33 AM

要提升PHP中的进制转换能力,首先需实现自定义进制转换函数以支持超过36的进制和自定义字符集,1.使用toBase和fromBase函数结合自定义digits数组可实现任意进制转换;2.处理大数时应使用BCMath扩展的bccomp、bcmod和bcdiv函数确保精度;3.构建BaseEncoder类实现双向安全映射,确保编码解码可逆;4.始终验证输入并统一字符顺序;5.避免使用base_convert处理大数,优先选择GMP提升性能,最终实现健壮、可扩展的进制转换系统。

构建统计分析工具包:PHP中的均值,中位和标准偏差 构建统计分析工具包:PHP中的均值,中位和标准偏差 Jul 30, 2025 am 05:17 AM

计算平均值:使用array_sum()除以元素个数得到均值;2.计算中位数:排序后取中间值,偶数个元素时取中间两个数的平均值;3.计算标准差:先求均值,再计算每个值与均值差的平方的平均数(样本用n-1),最后取平方根;通过封装这三个函数可构建基础统计工具类,适用于中小规模数据的分析,且需注意处理空数组和非数值输入,最终实现无需依赖外部库即可获得数据的核心统计特征。

模块化算术在PHP中的作用 模块化算术在PHP中的作用 Jul 30, 2025 am 12:17 AM

ModularArithMeticisessentialInphPcryptographlicationsdeSpitePhpnotBeingAhigh-Performancelanguage; 2. ItunderPinspublic-keysystemsslikersaanddiffie-hellmanthranthroughoperationssuchasmodularexpormentiationAndirestiationAndIrverses; 3.php'snative; 3.php'snative; 3.php'snative;

加速大量算术:深入研究PHP的GMP扩展 加速大量算术:深入研究PHP的GMP扩展 Jul 29, 2025 am 04:53 AM

GMPisessentialforhandlinglargeintegersinPHPbeyondnativelimits.1.GMPenablesarbitrary-precisionintegerarithmeticusingoptimizedClibraries,unlikenativeintegersthatoverfloworBCMaththatisslowerandstring-based.2.UseGMPforheavyintegeroperationslikefactorials

See all articles