Search code examples
query-optimizationparentheses

Is there an optimal method/algorithm to unnecessary extra parens in BOOL or arithmetic operations


I have this, I built it programmatically:

(( cat1:bobo AND ( ( cat2:jojo ) OR ( cat2:coco ) ) ))

For the sake of debugging, I am looking for a good method that would basically reduce it to least amount of parens needed:

cat1:bobo AND ( cat2:jojo  OR  cat2:coco ) 

I'm on C#, but if you have a good technique you've seen, I will port it.


Solution

  • Assuming you're parsing the expression into some sort of tree structure, then the approach I've taken in the past is just to navigate through the tree and remove any nodes that are just a container for other nodes.