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 On Mon, Aug 12, 2024 at 1:46 PM Paul Royik <distantjob...@gmail.com> wrote: > > Thank you. > > Can you point me how it could look like? > > Thank you. > > On Monday, August 12, 2024 at 10:20:25 PM UTC+3 asme...@gmail.com wrote: >> >> There's no way to change the Python operator precedence, but you can >> modify the string parsing in parse_expr() by passing in a transformer >> for the tokenizer. >> >> Aaron Meurer >> >> On Mon, Aug 12, 2024 at 12:38 PM Paul Royik <distan...@gmail.com> wrote: >> > >> > Hello. >> > >> > I'm trying to parse the ">>" symbol as implies operator. >> > >> > I've managed to parse p>>q>>r as Implies(Implies(p,q), r) using sympy >> > functions as a base (possibly, there is a built-in solution that I'm not >> > aware of), but it appeares that Implies is a right-associative operator. >> > >> > Are there some functions in Sympy that allow me to parse p>>q>>r as >> > Implies(p, Implies(q,r))? >> > >> > Thank you. >> > >> > -- >> > You received this message because you are subscribed to the Google Groups >> > "sympy" group. >> > To unsubscribe from this group and stop receiving emails from it, send an >> > email to sympy+un...@googlegroups.com. >> > To view this discussion on the web visit >> > https://groups.google.com/d/msgid/sympy/38551028-8785-4ecb-a407-e58b7ea4b037n%40googlegroups.com. > > -- > You received this message because you are subscribed to the Google Groups > "sympy" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to sympy+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/sympy/ba92bd20-1a6c-4eff-9c8a-b3402125365cn%40googlegroups.com. -- You received this message because you are subscribed to the Google Groups "sympy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sympy+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CAKgW%3D6L7Zq%2BJ8DjX5o4sJm_ALnPOQ1nEEdEotcyOKk4L-zDOjA%40mail.gmail.com.