My goal is to write a function that will take a logical expression (eg: A OR NOT(B AND C)) and convert that into disjunctive normal form. (A OR NOT B OR NOT C)
I have written a grammar that will generate logical expressions
S => !S
S => (S)
S => S op S
S => W
op => AND | OR
W => A | B | C | ... | Z
This is my algorithm
With a parse tree I can simplify NOT operators by checking the current node's parent, and pushing it down the tree or re-arranging the tree (in the case of NOT NOT). Then it is trivial to flatten the tree.
This works on paper, but now I'm stuck with the actual parser. How can I convert these rules into a parser class? I don't want to use external libraries and want to write the parser from scratch.
Have a look at Treetop, which might do what you want. http://treetop.rubyforge.org/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With