inducer / pymbolic

A simple package to do symbolic math (focus on code gen and DSLs)
http://mathema.tician.de/software/pymbolic
Other
108 stars 25 forks source link

ParseError when parsing assignment expression #15

Closed lmwang9527 closed 4 years ago

lmwang9527 commented 4 years ago

When I parse an assignment expression, I am getting a "ParseError: leftover input after completed parse at index 1: ...=x+1...", while it works fine if I parse only the rhs of the assignment. Is this the expected behavior, or am I missing something? Thanks!

In [1]: from pymbolic import parse
   ...: parse("x+1")
Out[1]: Sum((Variable('x'), 1))

In [2]: parse("y=x+1")
---------------------------------------------------------------------------
ParseError                                Traceback (most recent call last)
<ipython-input-2-3431e375dd42> in <module>
----> 1 parse("y=x+1")

~/py3env/lib/python3.8/site-packages/pymbolic-2019.2-py3.8.egg/pymbolic/parser.py in __call__(se
lf, expr_str, min_precedence)
    536         result = self. parse_expression(pstate, min_precedence)
    537         if not pstate.is_at_end():
--> 538             pstate.raise_parse_error("leftover input after completed parse")
    539         return result
    540

~/py3env/lib/python3.8/site-packages/pytools-2019.1.1-py3.8.egg/pytools/lex.py in raise_parse_er
ror(self, msg)
    150             raise ParseError(msg, self.raw_string, None)
    151
--> 152         raise ParseError(msg, self.raw_string, self.lexed[self.index])
    153
    154     def expected(self, what_expected):

ParseError: leftover input after completed parse at index 1: ...=x+1...
inducer commented 4 years ago

Assignments are statements, not expressions (in Python, and also in general, with C being a prominent exception).

lmwang9527 commented 4 years ago

Ahh, I hope you can forgive the stupid questions from a novice.

inducer commented 4 years ago

So pymbolic cannot parse statement? Thanks!

Nope. What you can do is use Python's parser library and process the expressions occurring there with pymbolic, as in this demo.

lmwang9527 commented 4 years ago

Yes, I found the code snippet that does statement in the ast interop section of the pymbolic document. Thanks again!