- SPEEDTRAP ALERT for Android
- Simple geo calculations
- Expression parser and evaluator (.NET, C#)
- Start - simple example
- Variables, properties, arrays and collections
- Functions and methods
- Operators
- Reference
- Something about iterating through UTF-16 Unicode strings (.NET, C#)
- Few words about toolkit:ExpanderView (XAML, .NET, C#, Windows Phone)
Using parser - simple example
This is example without variables or user-defined operations.
using AleProjects.AleLexer.AleParser; using AleProjects.AleLexer; ... AleExpressionParser AP = new AleExpressionParser(); AP.Options = AleExpressionParser.OPTION_IGNORECASE; AP.InitOperations(AleExpressionParser.OPERATIONS_STANDARDSET); AP.Text = "\"X = \" + 12.ToString()"; AleTerm term = AP.Parse(); AleTermResult result; if (AP.ErrorCode == AleExpressionParser.ERROR_OK) { if (term.Evaluate(out result)) { // success -> result of evaluation is in result.Value ("X = 12") } else { // evaluation error -> error information in result.ErrorCode and result.ErrorPos properties } } else { // syntax or semantic error -> error information in AP.ErrorCode, AP.ErrorPosition, AP.ErrorLine, AP.ErrorCol properties }
The AleExpressionParser.Options property is of type System.Int32 and defines how to tokenize and parse expression. Bit flag AleExpressionParser.OPTION_IGNORECASE means that there is no difference between names of variables, function names and operators written in lower/upper/mixed case.
Value | Description |
AleExpressionParser.OPTION_IGNORECASE (0x00000002) | No difference between names of variables, function names and operators written in lower/upper/mixed case |
AleExpressionParser.OPTION_STRICTSYNTAX (0x00000004) | Whitespace between number and operator that starts or ends with letter is required. With this flag set "2and5" is an error, while "2+5" is ok. |
AleExpressionParser.OPTION_ENDOFEXPRESSIONREQUIRED (0x00000040) | Expression must end with one of the endings listed in the AleExpressionParser.EndOfExpression property (see below). |
AleExpressionParser.OPTION_ALLOWMULTIDIMINDEXES (0x00010000) | Allows multidimensional indexes. "A[0,1,2]" is ok with this flag set. Without this flag only "A[0]" is ok. |
AleExpressionParser.OPTION_ALLOWEMPTYINDEX (0x00020000) | Allows expressions like "A[]=1". Empty brackets mean adding element to array with the next integer index. Allowed only in the left part of assignment operator. |
AleExpressionParser.OPTION_STRICTINDEXES (0x00040000) | No whitespace is allowed between index opening bracket and its left operand. "A [2]" - is an error with this flag set. |
AleExpressionParser.OPTION_ALLOWEMPTYPARAMS (0x00100000) | Functions can have optional parameters. |
The AleExpressionParser.InitOperations method initializes parser with standard operations. With AleExpressionParser.OPERATIONS_STANDARDSET option parser is initialized with pascal-like operators (+, -, *, /, and, or, xor, not, etc.). With AleExpressionParser.OPERATIONS_CLIKESET option parser is initialized with c-like operators (+, -, *, /, &, &&, |, ||, ^, !, ++, --, etc.).
There is no explicit tokenization in example. Tokenization is done by the Parse method implicitly. The AleExpressionParser.Parse method has the following syntax:
public AleTerm Parse(List<AleToken> tokens = null)
When tokens is null, the Parse method performs tokenization by itself. When to tokenize expression explicitly? When there are multiple expressions in one string. For example:
using AleProjects.AleLexer.AleParser; using AleProjects.AleLexer; ... AleExpressionParser AP = new AleExpressionParser(); AP.Options = AleExpressionParser.OPTION_IGNORECASE; AP.InitOperations(AleExpressionParser.OPERATIONS_STANDARDSET); AP.EndOfExpression = "; 'end of expression'"; AP.Text = "2*(3-10); \"value=\" + 10 end of expression 0xFF and 0x11"; AleTerm term; AleTermResult result; List<AleToken> tokens; int start = 0; while (start >= 0 && start < AP.Text.Length) { start = AP.Tokenize(out tokens, start); if (AP.ErrorCode == AleExpressionParser.ERROR_OK) term = AP.Parse(tokens); if (AP.ErrorCode == AleExpressionParser.ERROR_OK) { if (term.Evaluate(out result)) { // success // result of evaluation is in result.Value (-14 in first iteration, "value=10" in second, 0x11 in third) } else { // evaluation error // error information in result.ErrorCode and result.ErrorPos properties } } else { // syntax or semantic error // error information in AP.ErrorCode, AP.ErrorPosition, AP.ErrorLine, AP.ErrorCol properties // start == -1 } }
The AleExpressionParser.Tokenize method accepts two parameters. First out parameter is a list that contains tokens and second parameter is a starting position within a string. Method performs tokenization until it reaches the ending of expression given in AleExpressionParser.EndOfExpression property or until the end of the string. Method returns position of the next character after the ending of expression or it returns the length of the string.
The AleExpressionParser.EndOfExpression property defines endings that separate expressions from each other within string. There can be multiple endings. In this case, they must be separated by space. Those endings which contains spaces itself (in expressions they may have any whitespaces in any count, but in description only one '\u0020' character separates parts of such endings) must be enclosed in a single quotation marks. If expression requires ending in any case set bit flag AleExpressionParser.OPTION_ENDOFEXPRESSIONREQUIRED in AleExpressionParser.Options property. AleExpressionParser.Tokenize and AleExpressionParser.Parse methods will fail with error code AleExpressionParser.ERROR_NOEXPRESSIONENDING if they can't reach expression ending.