02-04-2009 09:10 AM
02-05-2009 03:43 AM
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 "-".
02-05-2009 08:10 AM - edited 02-05-2009 08:16 AM
02-12-2009 09:40 AM