I've been trying to set up a rather simple math parser, and it's been working fine. Only I can't figure out how to put in implied multiplication.
I've been using lark as an lalr(1) parser.
Here's most of the grammar:
?start: expression
?expression: sub
?sub: plus ("-" plus)*
?plus: times ("+" times)*
?times: divide ("*" divide)*
?divide: power ("/" power)?
?power: unary ("^" unary)*
?unary: positive
| negative
| atom
?atom: "(" expression ")"
| numeric
| symbol
positive: "+" unary
negative: "-" unary
?numeric: FLOAT
| INT
?symbol: WORD
%import common.WORD
%import common.INT
%import common.FLOAT
%import common.WS_INLINE
%ignore WS_INLINE
How would I go about adding in and implicit times operator?
Making the times "*" operator optional works fine, except that addition/subtraction doesn't work because it's interpreted as unary positive/negative instead. x - y is interpreted as x times -y, but it should be x sub y.
(exp1)(exp2), right ?2 2or2 x yto count as multiplication.2 2isn't clear to be a multiplication imho.+and-should have equal precedence, as should*and/.