n-arms / math-parser

A library to take in a string and output a mathematical result, as well as acting as a Computer Algebra System.
0 stars 0 forks source link

Add standardization to parse trees #8

Open n-arms opened 3 years ago

n-arms commented 3 years ago

Description

Too much time on this project has been wasted covering edge cases (I have put in over 3 hours and still don't have a working distributive property rule). Applying mathematical concepts to reduce trees would be significantly easier to do if the trees had some rhyme and reason to them. Thus, standardization.

BNF

Although trees can be entered (as strings) in pure math form, the parse tree must conform to the following:

<expr> ::= <expr> <op> <expr>
<expr> ::= <unary> <expr>
<expr> ::= <value>
<op> ::= + | * | ^ | & | | 
<unary> ::= - | 1/ | ~
<value> ::= true | false | <number> | <var>
<number> ::= <digit>* . <digit>* | <digit>*
<var> ::= <letter> (<digit> | <letter>)*

The most notable differences are that division will be replaced by reciprocal multiplication and subtraction by negative addition. This would allow for far less cases to be considered when manipulating the parse tree.

Implementation

The following parsing methods would be needed:

Potential Issues

Possible Solutions

Enhancement

The majority of numerical operations can now be described with entirely commutative operations (with the exception of exponents). This would allow for more than 2 nodes on a previously binary operation like addition. This would further standardize parse trees:

        *
     /     \                       *
    *       2    ->            /   |  \
 /     \                      2    3   4
3      4    

When I get around to implementing like terms #2, it will be a lot easier to do if all of the terms are on the same node of a tree.