I'm trying to create a calculator, not for numbers, but for set operations. To illustrate the concept lets say you have a file with two columns.
keyword, userid
hello , john
hello , alice
world , alice
world , john
mars , john
pluto , dave
The goal is to read in expressions like
[hello]
and return the set of users who have that keyword. For example
[hello] -> ['john','alice']
[world] - [mars] -> ['alice'] // the - here is for the difference operation
[world] * [mars] -> ['john','alice'] // the * here is for the intersection operation
[world] + [pluto] -> ['john','alice','dave'] // the + here is for union operation
I used the plyplus
module in python to generate the following grammar to parse this requirement. The grammar is shown below
Grammar("""
start: tprog ;
@tprog: atom | expr u_symbol expr | expr i_symbol expr | expr d_symbol | expr | '\[' tprog '\]';
expr: atom | '\[' tprog '\]';
@atom: '\[' queryterm '\]' ;
u_symbol: '\+' ;
i_symbol: '\*' ;
d_symbol: '\-' ;
queryterm: '[\w ]+' ;
WS: '[ \t]+' (%ignore);
""")
However, I'm not able to find any good links on the web to take the parsed output to the next level where I can evaluate the parsed output step by step. I understand that I need to parse it into syntax tree of some sort and define functions to apply to each node & its children recursively. Any help appreciated.