Parsing Implicit Multiplication

388 Views Asked by At

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.

0

There are 0 best solutions below