<s> from __future__ import print_function , division <EOL> from re import match <EOL> from sympy import sympify <EOL> def mathematica ( s ) : <EOL> return sympify ( parse ( s ) ) <EOL> def parse ( s ) : <EOL> s = s . strip ( ) <EOL> rules = ( <EOL> ( r"<STR_LIT>" , <EOL> lambda m : m . group ( <EOL> <NUM_LIT:1> ) + translateFunction ( m . group ( <NUM_LIT:2> ) ) + parse ( m . group ( <NUM_LIT:3> ) ) ) , <EOL> ( r"<STR_LIT>" , <EOL> lambda m : parse ( m . group ( <NUM_LIT:1> ) ) + translateFunction ( <EOL> m . group ( <NUM_LIT:2> ) ) + parse ( m . group ( <NUM_LIT:3> ) ) ) , <EOL> ( r"<STR_LIT>" , <EOL> lambda m : translateFunction ( <EOL> m . group ( <NUM_LIT:1> ) ) + "<STR_LIT:(>" + parse ( m . group ( <NUM_LIT:2> ) ) + "<STR_LIT:)>" ) , <EOL> ( r"<STR_LIT>" , <EOL> lambda m : "<STR_LIT:(>" + parse ( m . group ( <NUM_LIT:1> ) ) + "<STR_LIT>" + parse ( m . group ( <NUM_LIT:2> ) ) + "<STR_LIT:)>" ) , <EOL> ( r"<STR_LIT>" , <EOL> lambda m : "<STR_LIT:(>" + parse ( m . group ( <NUM_LIT:1> ) ) + "<STR_LIT:)>" ) , <EOL> ( r"<STR_LIT>" , <EOL> lambda m : parse ( m . group ( <NUM_LIT:1> ) ) + "<STR_LIT>" + parse ( m . group ( <NUM_LIT:2> ) ) + "<STR_LIT:)>" ) , <EOL> ( r"<STR_LIT>" , <EOL> lambda m : "<STR_LIT:(>" + parse ( m . group ( <NUM_LIT:1> ) ) + "<STR_LIT>" + parse ( m . group ( <NUM_LIT:2> ) ) ) , <EOL> ( r"<STR_LIT>" , <EOL> lambda m : parse ( m . group ( <NUM_LIT:1> ) ) + "<STR_LIT:*>" + parse ( m . group ( <NUM_LIT:2> ) ) ) , <EOL> ( r"<STR_LIT>" , <EOL> lambda m : parse ( m . group ( <NUM_LIT:1> ) ) + translateOperator ( m . group ( <NUM_LIT:2> ) ) + parse ( m . group ( <NUM_LIT:3> ) ) ) ) <EOL> for rule , action in rules : <EOL> m = match ( rule , s ) <EOL> if m : <EOL> return action ( m ) <EOL> return s <EOL> def translateFunction ( s ) : <EOL> if s . startswith ( "<STR_LIT>" ) : <EOL> return "<STR_LIT:a>" + s [ <NUM_LIT:3> : ] <EOL> return s . lower ( ) <EOL> def translateOperator ( s ) : <EOL> dictionary = { '<STR_LIT>' : '<STR_LIT>' } <EOL> if s in dictionary : <EOL> return dictionary [ s ] <EOL> return s </s>
