Coder Social home page Coder Social logo

luke-holt / mathparser-v3 Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 13 KB

3rd implementation of a math expression parser and evaluator. This time, using recursive the descent algorithm and defining a formal Backus-Naur grammar.

License: The Unlicense

Shell 0.76% C 99.24%

mathparser-v3's Introduction

Overview

This time, I use recursive descent with a context-free grammar.

Incorrect Implementation

The Backus-Naur grammar that I define below is incorrect. It parses the math expression assuming every operation is right-associative. This parser respects the grammer defined below, but is not useful for parsing and evaluating math expressions. See v4 for the (hopefully) correct implementation.

Example Usage

$ ./parsemath
usage: ./parsemath <expr>
supported operations: [ +, -, *, /, ** ]

$ ./parsemath "1 + 1 / 5"
tree node list:
0 -> { type: NUM, value: 1.00 }
1 -> { type: NUM, value: 1.00 }
2 -> { type: NUM, value: 5.00 }
3 -> { type: DIV, left: 1, right: 2 }
4 -> { type: ADD, left: 0, right: 3 }
tree: (1.0+(1.0/5.0))

$ ./parsemath "5 - 4 - 3 - 2 - 1"
tree node list:
0 -> { type: NUM, value: 5.00 }
1 -> { type: NUM, value: 4.00 }
2 -> { type: NUM, value: 3.00 }
3 -> { type: NUM, value: 2.00 }
4 -> { type: NUM, value: 1.00 }
5 -> { type: SUB, left: 3, right: 4 }
6 -> { type: SUB, left: 2, right: 5 }
7 -> { type: SUB, left: 1, right: 6 }
8 -> { type: SUB, left: 0, right: 7 }
tree: (5.0-(4.0-(3.0-(2.0-1.0))))

Backus-Naur Grammar

ADDSUB   ::= <MULDIV> ("+" | "-") <ADDSUB> | <MULDIV>;
MULDIV   ::= <POWER> ("*" | "/") <MULDIV> | <POWER>;
POWER    ::= <PARENS> "**" <POWER> | <PARENS>;
PARENS   ::= "(" <ADDSUB> ")" | <NUMBER>;
NUMBER   ::= <DECIMAL> | <INTEGER>;
DECIMAL  ::= <INTEGER> "." <INTEGER>;
INTEGER  ::= <DIGIT> <INTEGER> | <DIGIT>;
DIGIT    ::= "0"|"1"|"2"|"3"|"4"|"5"|"6"|"7"|"8"|"9";

mathparser-v3's People

Watchers

Luke Holt 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.