Workflows
These examples show the native EXS tasks that most applications combine: evaluate an expression, inspect its structure, and replace the operator configuration when the default language is not the right fit.
Evaluate and print a result
Use the default solver when the expression language is numeric or Boolean:
#include <snt/exs/solver.h>
snt::exs::Solver<snt::exs::Atom> solver;
auto result = solver.eval("1 + 2 * 3");
result.print();
The built-in steps give multiplication precedence over addition. See Operators and evaluation order for the complete default pass list.
Inspect expression structure
Pass a CompositionGraph when the caller also needs the syntax tree:
snt::exs::CompositionGraph graph;
auto result = solver.eval("1 + 2 * 3", &graph);
const auto& root = graph.nodes.at(graph.root);
const auto& right = graph.nodes.at(root.children.at(1));
The example expression reduces to an add node whose right child is a
mul node. The graph is syntactic: it does not record DIP references,
branch decisions, or intermediate values. See Solver usage and
composition graphs for graph invariants and the standalone
build_composition_graph pass.
Replace symbols and evaluation order
Supply an OperatorList and StepList when a domain language uses
different symbols or precedence:
#include <memory>
#include <snt/exs/solver.h>
snt::exs::OperatorList operators;
operators.append(snt::exs::NOT_OPERATOR,
std::make_shared<snt::exs::OperatorNot>("N"));
operators.append(snt::exs::AND_OPERATOR,
std::make_shared<snt::exs::OperatorAnd>("A"));
operators.append(snt::exs::OR_OPERATOR,
std::make_shared<snt::exs::OperatorOr>("O"));
snt::exs::StepList steps;
steps.append(snt::exs::BINARY_OPERATION, {snt::exs::OR_OPERATOR});
steps.append(snt::exs::BINARY_OPERATION, {snt::exs::AND_OPERATOR});
steps.append(snt::exs::UNARY_OPERATION, {snt::exs::NOT_OPERATOR});
snt::exs::Solver<snt::exs::Atom> custom(operators, steps);
auto result = custom.eval("N false A false O true");
Passing both lists replaces both defaults, so every operator used by the expression must be registered and scheduled. See Custom atoms and operations when a new operation type is required.
Pass application settings
Derive a settings type and use it as the solver’s second template argument when atom parsing or operators need application data:
struct Settings : snt::exs::BaseSettings {
std::string symbol;
int value;
};
Settings settings{{}, "?", 5};
snt::exs::Solver<CustomAtom, Settings> solver(operators, steps, settings);
auto result = solver.eval("2 + ?");
The solver stores a copy of the settings object. See Solver settings for the from_string and operator callback signatures.