This content originally appeared on DEV Community and was authored by Sarun Tapee
In this series, I will walk through the implementation of a mathematical parser in C/C++ that I learned from Udemy course with an instructor, Abhishek. My completed code is in my GitHub. I do not get sponsorship from him, but I think his courses are very good for learning C/C++. I recommend checking out his courses, there are a lot of interesting projects.
Let’s begin with the expectation of this project. By the end of this series, you will have your mathematical expression parser that parses an input string and evaluates the result, including inequality and logical expressions as demonstrated in the following picture.
How does it work
- Try parsing the input string expression in the following order: logical, inequality, and math expression. If the parsing fails, try the next expression.
- During parsing, create a lexical stack using a backtracking algorithm.
- Convert a mathematical expression from infix form to postfix form, which does not need parentheses to reduce ambiguity of operator precedence.
- Create a lexical tree from the postfix form.
- Evaluate the result by traversing the tree in postorder.
This content originally appeared on DEV Community and was authored by Sarun Tapee