11  How it Works

te_parser::evaluate() uses a simple recursive descent parser to compile your expression into a syntax tree. For example, the expression "sin x + 1/4" parses as:

te_parser::evaluate() also automatically prunes constant branches. In this example, the compiled expression returned by compile() would become:

Grammar

TinyExpr++ parses the following grammar (from lowest-to-highest operator precedence):

<list>      =    <expr> {(",", ";" [dependent on locale]) <expr>}
<expr>      =    <term> {("&" | "|") <term>}
<expr>      =    <term> {("<>" | "!=" | "=" | "<" | "<=" | ">" | ">=") <term>}
<expr>      =    <term> {("<<" | ">>") <term>}
<expr>      =    <term> {("+" | "-") <term>}
<term>      =    <factor> {("*" | "/" | "%") <factor>}
<factor>    =    <power> {("^" | "**") <power>}
<power>     =    {("-" | "+" | "~")} <base>
<base>      =    <constant>
               | <variable>
               | <function-0> {"(" ")"}
               | <function-1> <power>
               | <function-X> "(" <expr> {"," <expr>} ")"
               | <function-arg> "(" [<argument> {"," <argument>}] ")"
               | "(" <list> ")"
<argument>  =    <expr> | <string>
<string>    =    '"' {any character except '"'} '"'

<function-arg> is a function bound to te_arg_fun or te_arg_confun, which accepts any number of string or numeric arguments. String literals are only valid there; using one anywhere else is a syntax error. (Refer to Binding to Functions Accepting Strings.)

Because the parser is recursive, <base> nesting (parentheses and function application) is limited to 128 levels. Deeper nesting is a syntax error, rather than a possible stack overflow (refer to TE_MAX_DEPTH).