Coder Social home page Coder Social logo

arithmetic-parser's Introduction

arithmetic-parser

Parser/interpreter/evaluator for arithmetic expressions, written in once in Java and twice in Haskell (2019 update). Written to learn. Not suitable for situations that require fast and reliable parsing/evaluation.

Haskell

TODO: Write description

Java

Some expressions that can be (correctly) evaluated (output is always a double):

  • "2-1"
    • result: 1
  • "2^3-1"
    • result: 7
  • "4!%2"
    • result: 0
  • "1.5(18/(10-1))"
    • result: 3

Class responsibilities by steps

1. Lexer

The lexer (or tokenizer) splits up the given arithmetic expression into valid tokens. A valid token is a symbol or number, that can be used as a building block for a valid arithmetic expression. These can be operators (+, -, * etc) or numbers (2, 4.1 etc). The tokens are stored in a list. The only check done in this step is looking for disallowed symbols.

2. Parser

The parser transforms the order of tokens into the postfix (or Reverse Polish Notation) notation. This is done using Dijkstra's shunting-yard algorithm. The only check done in this step is looking for unbalanced parantheses. Since the lexer already implicitly creates a parse-tree, the shunting-yard algorithm generates the abstract syntax tree for the evaluator.

3. Evaluator

The evaluator takes the expression in AST form and runs it through a stack-based evaluation algorithm. This last step implicitly checks the validity of the expression. Each binary operator (+, -, * etc) needs a mapping to two numbers/sub-expressions, each unary operator (only ! for now) needs a mapping to one number/sub-expression. If this condition isn't fulfilled, the evaluator fails and exits.

Other classes (used in all steps)

The other classes, like the Token class, build the base. Tokens enforce the correct type congruency so no invalid tokens can be created by the lexer. An enum of operators and an operator util class helps the parser to identify operator precedence and associativity. Each operator in the enum is mapped to a function (e.g. ADD operator is mapped to (a,b) -> a+b). A math util class implements the factorial function and a way to round double numbers to a given accuracy.

More info

  • The modulo operator (%) works with float numbers.
  • The expression "5--2" is valid. Tokens generated: 5, - and -2. Equivalent to: 5+2
  • The expression "3(4(5))" is valid. This is parsed as implicit multiplication and is equivalent to: 3*4*5
  • The expression "4:(2)" is valid. Equivalent to 4/2
  • : and / can be used interchangably.
  • While the factorial operator has a precedence, it is never used to parse the order of operations.
  • The factorial operator can only be applied to whole numbers.

arithmetic-parser's People

Contributors

aronerben avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 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.