Description
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:
- <Parser.parseLiteral()> for variables (now with names containing digits), numbers and booleans
- <Parser.parseParentheses()> the same as before
- <Parser.parsePower()> the same as before
- <Parser.parseNegation()> deals with the '-' unary operator
- <Parser.parseFactor()> deals with multiplication and the 1/ unary operator
- <Parser.parseTerm()> deals with addition
- <Parser.parseLogic()> deals with numerical and boolean comparisons, as well as the ~ operator
Potential Issues
- Parse power can't deal with negative exponents
- Parse negation and parse term are forced to be done in different passes due to unary '-' having precedence over addition
- Overall this has 7 passes over the token list, a far from ideal number
Possible Solutions
- Parse power
- Could continue searching for an exponent until a non '-' is fond
- Parse negation
- If parse parentheses, parse factor, and parse term all search for a '-' that could remove the need for it
- Parse power
- Not having run Parse negation earlier would lead to some issues with implicit multplication
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.