symbol with double colon

66 views
Skip to first unread message

Giorgino R

unread,
Nov 11, 2021, 4:42:59 AM11/11/21
to sy...@googlegroups.com
Hi all,

I am using `parse_exr` to parse some strings as sympy symbols and then generate cxx code with `cxxcode`. When I use an expression with double colon, parse_expr is trying to split it. In contrast, if I define this expression with Sympy Symbol, I can create cxx code (see below). 

image.png

Any idea how I could overwrite this behavior in `parse_expr`?
Many thanks in advance,
Giorgino

Oscar Gustafsson

unread,
Nov 11, 2021, 5:22:25 AM11/11/21
to sy...@googlegroups.com
This behavior is from the Python tokenizer.


In [13]: input_code = StringIO('l_::p+3')

In [14]: list(generate_tokens(input_code.readline))

Out[14]:

[TokenInfo(type=1 (NAME), string='l_', start=(1, 0), end=(1, 2), line='l_::p+3'),

TokenInfo(type=54 (OP), string=':', start=(1, 2), end=(1, 3), line='l_::p+3'),

TokenInfo(type=54 (OP), string=':', start=(1, 3), end=(1, 4), line='l_::p+3'),

TokenInfo(type=1 (NAME), string='p', start=(1, 4), end=(1, 5), line='l_::p+3'),

TokenInfo(type=54 (OP), string='+', start=(1, 5), end=(1, 6), line='l_::p+3'),

TokenInfo(type=2 (NUMBER), string='3', start=(1, 6), end=(1, 7), line='l_::p+3'),

TokenInfo(type=4 (NEWLINE), string='', start=(1, 7), end=(1, 8), line=''),

TokenInfo(type=0 (ENDMARKER), string='', start=(2, 0), end=(2, 0), line='')]


I guess you can add a transformation that merges NAME, OP=':', OP=':', NAME sequences into a single name token.


BR Oscar



--
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/CA%2Bc1-%2B-iMn8BiYWU3T23yb2_Yj3ASTW3LDiy6DWr149TJRGJkg%40mail.gmail.com.

Giorgino R

unread,
Nov 11, 2021, 5:32:14 PM11/11/21
to sy...@googlegroups.com
Hi Oscar,

Many thanks for this, I see. I just went through the documentation and I found the transformations. I guess I will have to add this to the standard transformations (e.g. (merge_double_colon, ) + standard_transformations) so as to work.

BW
Giorgino



Aaron Meurer

unread,
Nov 12, 2021, 7:53:46 PM11/12/21
to sympy
Yes, you will need to write a custom parser transformer that merges colon tokens into name tokens. This should be straightforward because colon tokens wouldn't appear in any other context in an expression (only in a larger block of code, which parse_expr() cannot handle). So you can unconditionally merge colons in your transformer.

This is actually something that would be nice to document better. For now, Oscar's suggestion gives the basic idea of what you need to do. If you need help with the Python tokenizer, I have written a guide here that goes over it https://www.asmeurer.com/brown-water-python/

Aaron Meurer

Giorgino R

unread,
Nov 15, 2021, 10:50:58 AM11/15/21
to sy...@googlegroups.com
Hi Aaron, 

Many thanks for your response. I think it is clear how to create the parser transformer. I just followed the transformations already written inside sympy's parser module.
Thanks for sharing the documentation for the python tokenizer, as well :).

BW
Giorgino 

Reply all
Reply to author
Forward
0 new messages