Skip to content

chenx/ExpressionValidatorLib

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 

Repository files navigation

ExpressionValidatorLib

About

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.

Usage

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());
}

Implementation detail

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_]+.

License

BSD/MIT/Apache

Dev environment

VS.NET 2010

Author

Author: X. C.
Created on: 3/12/2015
Last modified: 6/2/2015

About

Recursive-descent math expression validator library in C#

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages