Skip to main content

Module parse

Module parse 

Source
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):

FunctionResultExtra syntax
parse / Context::parseExjuxtaposition of numbers, symbols and parentheses is multiplication (2x, 2 x, x y, 2(x+1), (x+1)(x-1), 2pi)
parse_bool / Context::parse_boolBoolExrelations < <= > >= == !=, 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_implicitExfunction 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§

ParseError
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)).