Need a way to parse algebraic expressions in C

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.

+7
source share
4 answers

I was very lucky with ANTLR . It has battery life for various languages, including C, and has very good syntax for specifying grammars and building trees. I recently wrote a similar grammar (algebraic expressions) in 131 lines, which is certainly controllable.

+2
source

The standard Linux flex and bison tools are likely to be the most appropriate here. The IIRC analysts and lexers used in these tools do something close to what you want, so you can simply modify this code to get what you need.

These tools seem to fit your requirements. You can customize the grammar, compile to C, and use whatever operator you want.

+4
source

you can create a simple parser yourself or use any of the popular " compiler-compiler " (some of them were indicated by other messages). just decide if your parser will be complex enough to use (and learn) an external tool. In any case, you will need to determine the grammar, as a rule, this is the most difficult task if you do not have previous experience. formal way of defining BNF or EBNF syntax grammars

0
source

I used the code (found on the net) from the following:

Translation Fundamentals of Peter Calingaert

I improved it to handle functions that allow things like “if (a, b, c)” to be implemented (such as how Excel does something).

0
source

All Articles