Download the PHP package php-rpn/rpn without Composer
On this page you can find all versions of the php package php-rpn/rpn. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package rpn
Short Description Reverse Polish Notation implementation in PHP
License MIT
Informations about the package rpn
RPN - Reverse Polish Notation Calculator
A flexible and extensible PHP library for parsing and evaluating mathematical expressions using Reverse Polish Notation (RPN). It uses a Shunting-yard algorithm implementation that is configurable and easy to use.
What is Reverse Polish Notation?
Reverse Polish Notation (also called postfix notation) is a mathematical notation in which operators follow their operands. For example:
- Infix:
3 + 4 * 2 - RPN/Postfix:
3 4 2 * +
RPN eliminates the need for parentheses and operator precedence rules during evaluation, making it simpler and faster to compute.
Installation
Requirements
- PHP 8.3 or higher
Features
- ✅ Fluent Builder Interface for creating customized parsers.
- ✅ Parse infix mathematical expressions to an RPN stream using the Shunting-yard algorithm.
- ✅ Extensible Operator Registry to add or override operators.
- ✅ Evaluate RPN expression streams.
- ✅ Support for standard operators:
- Binary operators:
+,-,*(×),/(÷) - Unary operators:
-(negation),!(factorial)
- Binary operators:
- ✅ Support for mathematical functions:
sqrt()|√- Square rootpow()|^- Power functionlog()- Natural logarithmexp()- Exponential functionmin()- Minimum of two valuesmax()- Maximum of two values∛(cube root)∜(fourth root)
- ✅ Variable support for dynamic expressions.
- ✅ Proper operator associativity handling.
- ✅ Type-safe PHP 8.3+ with strict types.
Usage
The library is designed with a fluent builder to make parsing and evaluation straightforward.
Basic Usage
Using Mathematical Functions
Using Variables
Customization
You can easily add your own custom operators if you need to. Just implement the OperatorInterface and register it with the parser builder:
Supported Syntax
The default parser supports both standard ASCII and Unicode mathematical symbols:
| Operation | ASCII | Unicode | Example |
|---|---|---|---|
| Addition | + |
- | 1 + 7 |
| Subtraction | - |
- | 5 - 49 |
| Multiply | * |
× |
3 × 4 |
| Divide | / |
÷ |
10 ÷ 2 |
| Power | ^, pow |
- | 2 ^ 3 |
| Factorial | ! |
- | 5! |
| Square Root | sqrt |
√ |
sqrt(16) or √16 |
| Cube Root | - | ∛ |
∛27 |
| Fourth Root | - | ∜ |
∜81 |
| Exponential function | exp |
- | exp(3) |
| Min | min |
- | min(3, :x) |
| Max | min |
- | max(7, :x) |
| Log | log |
- | log(10) |
| Percent | % |
- | 5% |
| Negation | - |
- | -3 |
Development
Feel free to contribute! Fork the repository and submit a pull request. Just make sure everything satisfies the coding standards and all tests pass.
How It Works
1. Parsing (Infix to RPN)
The ShuntingYardParser converts an infix string like 3 + 4 * 2 into an ExpressionPartsStream.
| Token | Action | Output Stream (Conceptual) | Operator Stack |
|---|---|---|---|
3 |
Add to stream | [Number(3)] |
|
+ |
Push to stack | [Number(3)] |
[+] |
4 |
Add to stream | [Number(3), Number(4)] |
[+] |
* |
Higher precedence, push | [Number(3), Number(4)] |
[+, *] |
2 |
Add to stream | [Number(3), Number(4), Number(2)] |
[+, *] |
| End | Pop all operators | [Number(3), Number(4), Number(2), *, +] |
2. Evaluation
The Expression::evaluate() method iterates the ExpressionPartsStream and uses a stack to compute the result.
Expression Stream: [Number(3), Number(4), Number(2), *, +]
| Token | Stack After | Explanation |
|---|---|---|
3 |
[3] |
Push 3 |
4 |
[3, 4] |
Push 4 |
2 |
[3, 4, 2] |
Push 2 |
* |
[3, 8] |
Pop 4, 2; compute 4*2=8; push 8 |
+ |
[11] |
Pop 3, 8; compute 3+8=11; push 11 |
License
This library is licensed under the MIT License - see the LICENSE file for details.
Author
Olexandr Mazur [email protected]
:star: Star this project on GitHub — it motivates me a lot!