You need to write a function that takes the stream of tokens and
returns a new stream of tokens which are re-parenthesized. For
example, ['p', '>>', 'q', '>>', 'r'] would become ['p', '>>', '(',
'q', '>>', 'r', ')']. But you'd also need to handle the case where one
of the operands is itself parenthesized or involves operators with a
higher precedence than >>.
Unfortunately, this isn't the simplest thing in the world to do. It
would probably be useful to add something to SymPy that lets you take
a general transformer that can adjust operator precedence and
associative rules. Or we should write our own custom parser for
parse_expr that makes it easier to make these sorts of changes (both
of these are things we would like to see, but not easy tasks).
Here are examples of existing token transformers for parse_expr
https://github.com/sympy/sympy/blob/master/sympy/parsing/sympy_parser.py.
My guide to the Python tokenizer module can also be useful
https://www.asmeurer.com/brown-water-python/
Aaron Meurer
Aaron Meurer
> To view this discussion on the web visit
https://groups.google.com/d/msgid/sympy/ba92bd20-1a6c-4eff-9c8a-b3402125365cn%40googlegroups.com.