How to implement multiple assignment in an interpreter written in python?
python, python-2.7, python-3.x
Solution
Most languages get away with it by declaring assignment to be an expression.
In your example, assignment becomes:
def p_expression_assign(t):
'expression : NAME EQUALS expression'
t[0] = names[t[1]] = t[3]
I just changed "statement" to "expression", both in the function name and in the docstring syntax, and "returned" (assigned to `t[0]`) the value that is being assigned.
I say "get away with" because other languages (such as Python) go the extra mile, as they allow multiple assignments, but forbid using the result of the assignment in any other kind of expression.
But your second example `a=(b=1)*1` tells me you want the former, more lax (or C-like) form of multiple assignment.
Problem
I am writing an interpreter in python and I am following this example http://www.dabeaz.com/ply/example.html I would like to know how I can implement multiple assignment such as: `a=b=c=1` and `a=(b=1)*1` I have tried a few rules but all in vain. I know the parsing should be something like this. ``` a b c 1 \ \ \/ \ \ / \ \ / \ / ``` I am just not sure how to write this using PLY.