I would like a general solution to transforming strings that are in prefix notation into tuples. Here are three examples below that illustrate what I hope to achieve. Each string is an element in a list (only the second example has more than one element).
['neg(or(p,q))']
['imp(p,q)', 'imp(neg(r),neg(q))']
['and(and(p,q),r)']
Becomes
[('neg',('or','p','q'))]
[('imp','p','q'), ('imp',('neg','r'),('neg','q'))]
[('and',('and','p','q'),'r')]
More generally, the format I desire is ('connective', 'parameter', 'parameter') where connectives may be 'and', 'or', 'iff', 'imp', 'neg'. Each of these is then followed by its two parameters, except for 'neg' which takes just one. Parameters may be other tuples as shown above in the examples, which demonstrates nested formulae.
Another way to look at this is with the formulae that motivated the problem in the first place.
Ex. 1 ((p ∧ q) ∨ (¬p ∧ ¬q)) ultimately becomes
[('or',('and','p','q'),('and',('neg','p'),('neg','q')))]
Ex. 2 (p → q), (¬r → ¬q) ultimately becomes
[('imp','p','q'), ('imp',('neg','r'),('neg','q'))]
What I have tried:
import re
def tuplify(raw_sequent):
first_split = re.split('\(', raw_sequent, 1)
connective = first_split[0]
second_split = re.split('\,', first_split[1])
right_arg = second_split[-1]
second_split = second_split[:-1]
left_arg = ','.join(second_split)
tup = (connective, left_arg, right_arg[:-1])
return tup
However, this does not generalize well at all, only working for the input
'and(and(p,q),or(r))'
I imagine the solution might require a combination of regex and recursion.