Math expression validator library in C#.
A console program for testing purpose is also included (Tester).
This is written in C#, but can be easily converted to Java. The syntax involved are almost identical.
You can use it either as a library (ExpressionValidatorLib.dll), or as a class file (ExprValidator.cs).
To use the validator, create an object and call method Validate(). It will return either True or False.
Call method Message() to show validation message (error or trace information) in detail.
Set Trace on to show entrance of functions.
Use varType to set variable type (type1 or type2, see below).
Code example:
using System; using ExpressionValidatorLib; ... string test = "(1 + 2) * 3"; ExpressionValidatorLib.ExprValidator psr = new ExprValidator(); bool isValid = psr.Validate(test); if (! isValid) { System.Console.WriteLine("validation error: " + psr.Message()); }
Parse a math expression to see if it's correct.
Algorithm: recursive descent.
Grammar for mathematical expressions:
E = T + E | T - E | T
T = F * T | F / T | F
F = (E) | num | var
num is of format a or a.b, cannot be a. or .b;
var type1 is of format id;
var type2 is of format id.id, i.e., a prefix and a suffix separated by a dot (.);
id is of format [a-zA-Z][a-zA-Z0-9_]+.
BSD/MIT/Apache
VS.NET 2010
Author: X. C.
Created on: 3/12/2015
Last modified: 6/2/2015