Expand description
Runtime expression parser — convert strings to symbolic expressions. Runtime expression parser.
Parses mathematical expressions from strings into the arena.
Uses a Pratt parser (precedence climbing) with the same grammar
as the expr! proc macro:
- Operators:
+,-,*,/,^(right-associative) - Unary:
-(prefix negation) - Functions:
sin,cos,tan,exp,ln,sqrt,abs - Parentheses:
(,) - Atoms: integer literals, symbol names
- Constants:
pi,e,I,inf,nan
Three entry points share this grammar (SymPy: sympify, parse_expr):
| Function | Result | Extra syntax |
|---|---|---|
parse / Context::parse | Ex | juxtaposition of numbers, symbols and parentheses is multiplication (2x, 2 x, x y, 2(x+1), (x+1)(x-1), 2pi) |
parse_bool / Context::parse_bool | BoolEx | relations < <= > >= == !=, connectives &/&&/and, |/||/or, prefix ~/!/not, True/False, and the function forms Eq(a, b), Ne, Lt, Le, Gt, Ge, And(…), Or(…), Not(a) |
parse_implicit / Context::parse_implicit | Ex | function application without parentheses (sin x, 2 sin x, sin 2x) and f(x) as a product for unknown f (x(x+1)) |
§Examples
use symplex::prelude::*;
let ctx = Context::new();
let expr = symplex::parse::parse(&ctx, "x^2 + 2*x + 1").unwrap();
assert_eq!(format!("{expr}"), "x^2 + 2*x + 1");Structs§
- Parse
Error - Error returned when parsing fails.
Functions§
- parse
- Parse a mathematical expression string into an
Ex. - parse_
bool - Parse a relation or Boolean combination of relations into a
BoolEx. - parse_
implicit - Parse with implicit multiplication and implicit function application
(SymPy:
parse_expr(s, transformations=implicit_multiplication_application)).