Welcome to
aleprojects.com

Expression parser and evaluator (.NET, C#)

This project is a set of classes for expression tokenizing and parsing, for representation of parsing results, for description of operations (operators and functions) and for evaluation. Expressions can have variables of any type, user-defined functions and operators, array or collection elements accessed by indexes, properties and methods.

       

Download

Overview

There are two namespaces in this project: AleProjects.AleLexer and AleProjects.AleLexer.AleParser.

The AleProjects.AleLexer namespace contains two classes: AleToken and AleSimpleLexer. The AleToken class represents a token extracted from expression. Token is a text unit like number, text constant, variable name, parentheses, function, operator, list, etc. The AleSimpleLexer class is a base class for expression parser. This class is able to tokenize numbers, string constants, lists, parentheses, key-value pairs, to strip comments and can be used for simple tokenization tasks.

The AleProjects.AleLexer.AleParser namespace contains three main classes: AleExpressionParser, AleTerm, AleOperation and structure AleTermResult. The AleExpressionParser class tokenizes and parses expression and is the main class to use. The result of tokenization is a linear list of tokens (AleToken class). After tokenization, AleExpressionParser processes list of tokens and produces tree-like structure of expression. Each tree node is an expression unit like constant, variable, property (terminals) or operator, function, method, index (non-terminals). All terminals represent or store value itself. All non-terminals have child nodes like left and right parts of operator or parameters of function. These expression units (tree nodes) are represented by the AleTerm class. The AleTerm class has method Evaluate that evaluates expression unit and stores result (or error information) in the structure of AleTermResult type. Method Evaluate of the tree root evaluates the whole expression. The AleOperation class defines operations (all non-terminal expression units) and how they are evaluated. It allows to define custom functions and operators.

Using parser