Recursive Descent Parsing for MQL Trading Expressions
Summary
This article explains how to evaluate arithmetic and logical expressions supplied as text, a feature that can let users change formulas in compiled trading programs without editing their source. It defines supported values, operators, precedence, associativity, functions, and variable handling, then describes a shared parser base class for scanning input, reading numbers, tracking errors, and accessing variable and function tables.
The focus is recursive descent parsing, one of several parser approaches the article introduces. It also discusses compiling expressions into reusable syntax trees so that changing variable values can update calculations without reparsing the formula. Examples show arithmetic, grouping, variables, functions, comparisons, and conditional expressions. The article notes limits including no arrays, assignments, bitwise operators, or scientific notation, and explains that syntactically valid expressions can still produce invalid numeric results such as division by zero. It presents implementation concepts rather than comparative performance results; parser comparisons and trading applications are deferred to a later installment.
Key ideas
- Recursive descent parsers use mutually recursive routines to analyze expression grammar.
- Operator precedence and associativity determine how an expression is grouped and evaluated.
- Variables and built-in functions can be managed through tables shared by parser implementations.
- A syntax tree can be reused with changing variable values without reparsing the expression.
- Valid syntax does not guarantee a valid numeric result, so runtime values also need checking.
Tags
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.