/go_pratt_calculator

A calculator that parses expressions using pratt's top down operator precedence algorithm

Primary LanguageGo

Calculator using Pratt's Top Down Operator Precedence Algorithm

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.

Parse Tree Diagrams

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:

Addition and Multiplication

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.

Exponentiation and Addition

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.

Test

You can execute the test files by running:

go test -v

Local Testing

After pulling the code down to your machine, you can run:

go run .

To start the application.

This is what the app looks like:

image

Improvements

  • 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