A simple maths parser that can parse and evaluate mathematical expressions. It uses a recursive descent parser to parse the expression and then evaluates the expression using a simple recursive algorithm. The parser can handle the following operators: +, -, *, /, ^.
It handles operator precedence and associativity. The precedence of the operators is as follows:
^*,/+,-
expression = term { ( "+" | "-" ) term }
term = factor { ( "*" | "/" ) factor }
factor = number | "(" expression ")"
number = [0-9]+NUMBER- A numberOPERATOR- An operator
type-NUMBERvalue- The number
type-OPERATORvalue- The operatorleft- The left operandright- The right operand
npm run build
npm run startYou can a cli where you can enter a mathematical expression and it will be parsed and evaluated.
> 3 + 3 ^ 2
{
"type": "operator",
"value": "+",
"left": {
"type": "number",
"value": 3
},
"right": {
"type": "operator",
"value": "^",
"left": {
"type": "number",
"value": 3
},
"right": {
"type": "number",
"value": 2
}
}
}
12