Giter Club home page Giter Club logo

shuntingyard's Introduction

ShuntingYard Build Status

Java implementation of Dijkstra's Shunting-Yard algorithm for parsing and evaluating mathematical expressions.

Contains support for:

  • Operator precedence
  • Brackets
  • Functions (e.g. sine)
  • Left-associative operators (e.g. ^)
  • The plus-minus notation (e.g. ±5)
  • Variables (e.g. x)
  • Constants (e.g. π)

Usage

The algorithm has been divided into 3 main units:

  • Lexer: Tokenises a raw expression
  • Parser: Converts a tokenised expression into Reverse-Polish Notation (RPN) form
  • Evaluator: Evaluates an expression in RPN form to produce a list of output values

Basic Demo

// Initial expression
String expression = "±(2+1*3)+√(2^2)";

// Step 1: Lexer
Lexer lexer = new DefaultLexer();
List<Token> tokens = lexer.readTokens(expression);

// Step 2: Parser
Parser parser = new DefaultParser();
List<Token> rpn = parser.parse(tokens);

// Step 3: Evaluator
Evaluator evaluator = new DefaultEvaluator();
List<Double> outputs = evaluator.evaluate(rpn);

// Output values
System.out.println(expression + "=");
for (Double value : outputs) {
    System.out.println(value);
}

// Produces:
// ±(2+1*3)+√(2^2)=
// 7.0
// -3.0

Variables Demo

When using variables, we need to define variables both in the lexer and the evaluator.

The lexer definitions are required to prevent any invalid tokens being interpreted as variable names.

The evaluator definitions are kept separate as a map of names to values so that, when the value of a variable is udpated, the expression can be evaluated again without the need to tokenise and parse the equation again (which is a relatively computationally expensive operation).

Note: if a variable is not defined in either the lexer or evaluator, an exception will be thrown.

// Initial expression
String expression = "x*y";

// Step 1: Lexer
DefaultLexer lexer = new DefaultLexer();
lexer.getVariables().add("x"); // Define variable x
lexer.getVariables().add("y"); // Define variable y
List<Token> tokens = lexer.readTokens(expression);

// Step 2: Parser
DefaultParser parser = new DefaultParser();
List<Token> rpn = parser.parse(tokens);

// Step 3: Evaluator
DefaultEvaluator evaluator = new DefaultEvaluator();
evaluator.getVariables().put("x", 3d); // Define x with value 3
evaluator.getVariables().put("y", 4d); // Define y with value 4
List<Double> outputs = evaluator.evaluate(rpn);

// Output values
System.out.println(expression + "=");
for (Double value : outputs) {
    System.out.println(value);
}

// Re-run with different values
evaluator.getVariables().put("x", 5d); // Re-define x with value 5
evaluator.getVariables().put("y", 6d); // Re-define y with value 6
outputs = evaluator.evaluate(rpn);

// Output values
System.out.println(expression + "=");
for (Double value : outputs) {
    System.out.println(value);
}

// Produces:
// x*y=
// 12.0
// x*y=
// 30.0

Class Diagram

Class diagram

shuntingyard's People

Contributors

ed-cooper avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.