I need to parse algebraic expressions for the application I'm working on, and hope to decorate a bit of collective wisdom before hacking it and possibly turning the wrong way.
What I need to do is pretty straightforward: taking into account the algebraic text expression (3 * x - 4 (y - sin (pi))) creates an object representation of the equation. Custom objects already exist, so I need a parser that creates a tree that I can walk around to create the objects I need.
Primary requirements:
The ability to express algebra as a grammar, so I can manage it and it can be customized / expanded as needed.
The original syntax will include integers, real numbers, constants, variables, arithmetic operators (+, -, *, /), degrees (^), equations (=), brackets, priority and simple functions (sin (pi)). I hope to expand the application fairly quickly to support the corresponding functions (f (x) = 3x +2).
Must compile in C since it needs to be integrated into my code.
I DO NOT need to evaluate the expression mathematically, so software that solves a variable or does arithmetic is noise.
I did Google homework, and it seems the best approach is to use grammar and BNF software to generate the compiler in C. Therefore my questions are:
Is there an existing BNF grammar with an appropriate parser generator for algebraic expressions (or, better yet, LaTex)? Someone should have done this already. I REALLY want to not be folded, mainly because I do not want to check it. I would be willing to pay a reasonable amount for the library (less than $ 50)
If not, which parser generator for C, in your opinion, is the easiest to learn / use here? Lex? YACC? Flex, Bison, Python / SymPy, others? I am not familiar with any of them.
David
source share