Skip to content

VinceValence/shunting-yard-algorithm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Shunting Yard Algorithm

In this repository, you can find an implementation of Dijkstra's Shunting Yard Algorithm. It can convert an infix math expression into a postfix one, and it can evaluate the latter. It is not intended to be a maximally efficient implementation.

Usage

from shunting_yard import ShuntingYard

yard = ShuntingYard()  # Create a shunting yard. This class has knowledge of some operators and functions
postfix = yard.parse("(1+2)*4^3.14")  # Returns a queue with the postfix tokens
yard.evaluate(postfix)  # Returns the value of the postfix expression

The following operators and functions are known to the ShuntingYard class by default, if you don't want them, set load_basic to False when instantiating the ShuntingYard:

  • Operators: +, -, /, *, ^. With respective precedences 2, 2, 3, 3, 4 and all with arity 2
  • Functions: min, max. Both with two arguments and precedence 5

You can define your own operators or functions with the Operator class, with certain constraints:

  • Their symbol or identification should be unique
  • If you don't use letters for their symbols, you must use one character
  • If you use letters for their symbols, you must only use letters, but can use more characters

Like this:

import operator
op1 = Operator(symbol="%", arity=2, precedence=3, actual_function=operator.mod) 

About

Implementation of Dijkstra's Shunting Yard Algorithm

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages