z3python: converting string to expression

2019-02-13 05:46发布

问题:

Given that x,y,z = Ints('x y z') and a string like s='x + y + 2*z = 5', is there a quick way to convert s into a z3 expression ? If it's not possible then it seems I have to do lots of string operations to do the conversion.

回答1:

You can use the Python eval function. Here is an example:

from z3 import *
x,y,z = Ints('x y z') 
s = 'x + y + 2*z == 5'
F = eval(s)
solve(F)

This script displays [y = 0, z = 0, x = 5] on my machine.

Unfortunately, we can't execute this script at http://rise4fun.com/z3py. The rise4fun website rejects Python scripts containing eval (for security reasons).



标签: python z3