LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Source for simple math parsing ?

Hello all,
I need to do some simple math parsing from a CVI program and although that's
the kind of thing we all did in computer science courses, I have better
things to do now than to reinvent the wheel.

So I have a list of variables in memory, and their types (double, integer,
bool) and I want the user to be able to enter a formula in a text box and
get the result or a syntax/math error.

I don't need anything fancy: +-*/ operators, logical operators, numerical
constants, double/int/bool variables, parenthesis or reverse polish,
possibly the usual math functions (sin, log...). And I need stdC source...

Any suggestions ? Google finds tons of irrelevant links.
I'm not familiar with Yacc/Bison/Lex, but I know they can generate C from a
grammar. But can they interface with a list of variables ?
--
Guillaume Dargaud
http://www.gdargaud.net/


0 Kudos
Message 1 of 4
(3,319 Views)

i cannot recommend a particular library to perform this task, because i always loved writing parsers so i wrote my own to do this. anyway, writing a parser for simple math expression is really not complicated, it is a matter of  2 or 3 hours. the most difficult part is getting the type system right: having a single type (double for simplicity) is easier. start by writing a BNF grammar of what you want the user to enter, then write the parser. a recursive descent parser for a mathematical expression is straightforward if the grammar is well-constructed.

 

regarding yacc/bison/lex: they are powerful toys, but it will take you some time to manage them. they do generate C from a grammar and they can absolutely interface with your own variables. these tools are only an aid in writing a parser: you tell them the grammar, then you have to write C functions to actually perform something interesting on the parsed text. these functions are standard C and can include anything you like.

 

by the way: a correct grammar for mathematical expressions can be found easily in language manuals for Pascal or Ada for example, where they are generally expressed in BNF form. if you don't use them and try to roll your own grammar: be careful with operator precedence, especially with "+" and "-" which do have the same precedence (a common error in self-made grammar), and with the unary operator "-".

0 Kudos
Message 2 of 4
(3,300 Views)
Attached is some code you could use as a starting point. It evaluates functions in X, Y and T.
Message Edited by msaxon on 02-05-2009 02:16 PM
--
Martin
Certified CVI Developer
0 Kudos
Message 3 of 4
(3,291 Views)
> Attached is some code you could use as a starting point. It evaluates
> functions in X,
> Y and T. Message Edited by msaxon on 02-05-2009 02:16 PM

Thanks. I saw your post only after I rolled my own while at home with the
flu...
To keep things simple (like the first lazy HP programmers !) I used postfix
notation. It also allowed for very simple type checking.


BTW, I need to get back here with a very unexplainable bug about !!B not
being what you expect if B is a boolean part of an enum. I couldn't believe
my eyes and need to untangle it from the rest of my code.
--
Guillaume Dargaud
http://www.gdargaud.net/


0 Kudos
Message 4 of 4
(3,245 Views)