A calculator that parses expressions using pratt's top down operator precedence algorithm
This is an implementation of Pratt algorithm for parsing mathematical expressions, providing a flexible and efficient way to handle different operators and precedence levels.
This implementation uses Pratt's Algorithm to ensure the correct parse trees are constructed for expressions, taking into consideration the different precedence levels of operators:
Consider the expression "1 + 2 * 3". The parse tree for this expression can be represented as follows:
+
/ \
1 *
/ \
2 3
This illustrates how the Pratt algorithm builds a parse tree, giving higher precedence to the multiplication operation.
Now, let's explore the expression "2^3 + 4". The parse tree for this expression looks like:
+
/ \
^ 4
/ \
2 3
This shows how the Pratt algorithm handles the exponentiation operator, which has higher precedence than addition.
You can execute the test files by running:
go test -v
After pulling the code down to your machine, you can run:
go run .
To start the application.
This is what the app looks like:
- Validating expressions before attempting to parse them
- Allow mathematical functions like sin(x), log(x) in expressions
- Expose a consummable public API to be able to use as a libary/package