This is my first question , please correct me if anything is wrong.
I have some set of old rules in one of document system , im trying to convert them into new document system.
I have lot of IF-ENDIF and IF-ELSE-ENDIF nested inside each other like below. Need some logic which works on converting below input to corresponding output.
Need help for algo. Thanks
INPUT:
IF (Cond 1)
IF(Cond 2)
ENDIF
IF(Cond3)
ELSE
ENDIF
ELSE
IF(Cond4)
ELSE
IF(Cond5)
ELSE
ENDIF
ENDIF
IF(Cond6)
ENDIF
ENDIF
Required OUTPUT:
IF(Cond1) AND (Cond2)
IF(Cond1) AND (Cond3)
IF(Cond1) AND !(Cond3)
IF!(Cond1) AND (Cond4)
IF!(Cond1) AND !(Cond4) AND (Cond5)
IF!(Cond1) AND !(Cond4) AND !(Cond5)
IF!(Cond1) AND (Cond6)
I will assume that you have logic that can parse the file in the first place. If so, then you should end up with an abstract syntax tree where each node looks something like this:
or
or
where Terminal represents a concrete statement. They are implicit in your original input file. For example, “IF(COND2) ENDIF” would be represented as follows:
In your case, your actual tree would look something like this:
To generate the output, you would simply recursively walk down the tree, building a stack of conditions along the way, then when you get to a statement, output the whole stack of conditions with ANDs between them. Here is some pseudocode:
Here I am using + to indicate appending an item to a list. It is assumed that !cond results in a condition that prints itself out with a “!” at the front.
I hope that helps!