benjamin-hodgson/Pidgin

What could I add to the ExprParser from Examples such that it throws an exceptions for invalid parenthesis?

Closed this issue · 1 comments

I am using the ExprParser from Examples to parse mathematical formulae. It works really well with minor modifications. I also had to add a way to evaluate Expressions, given values for the Identifiers, (e.g. a + b given values for a and b returns the sum).

The only bit that isn't working is identifying when an expression is invalid because the parenthesis are put in incorrectly.

E.g a + b)*c will not throw an exception, will simply return a + b.

I can easily add a pre-processing step in my own code to check for invalid expressions, but I was wondering if the parser can do that for me?

If you’re definitely only parsing a single expression you can use End which looks for the end of the input stream. (Bung .Before(End) on your outermost parser.)

In general parsers don’t expect a total parse by themselves in order to enable composition. (The ExpressionParser doesn’t know whether it’s going to be .Thened with a parser that can recognise whatever comes next, even if it’s not a valid expression.)

Hope this helps!