I want to parse a logical expression like the following:
(f = '1' OR f = '2') AND (s = '3' OR s = '4' OR s = '5') AND (t = '6')
What I need, is a representation of this logical expression in the form of a expression tree. In the end I want to be able to create a JSON representation of this data structure in the form of:
{
...
nodes: [
{
id: 1,
operator: 'AND'
operands: [
2,
3,
4
]
},
{
id: 2,
operator: 'OR'
operands: [
5,
6
]
},
{
id: 3,
operator: 'OR'
operands: [
7,
8,
9
]
},
leafs: [
{
id: 4,
operator: '='
operands: [
t,
6
]
},
...
}
I am aware that this is not the best representation of the data structure, but this is just an example. How do I approach this problem using packages like pyparse or re?