Welcome to
aleprojects.com

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.

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.