Tools looking for an overly complex Boolean expression that can be simplified?

Are there C ++ code parsers looking for Boolean expressions that can be simplified with Boolean algebra?

I know that compilers do this already, but it would be nice to have a tool that produces such things so that you can improve code readability.

+4
source share
6 answers

Humans

You want to improve readability, and since readability is mostly human, it should be taught to humans.

Ask more experienced developers to analyze your expressions and give tips.

For example, see my answer here: What is the best way (by performance) to check if a value falls into a threshold?

+3
source

Although it does not work directly with C / C ++ Boolean expressions, one tool that I found very useful for simplifying complex logic is Logic Friday . Unfortunately, this is only Windows, but, fortunately, it is free.

0
source

You can make the code more efficient by reducing the number of ifs you need to verify. but more simplified and improved readability cannot be done automatically.

0
source

http://www.freewarepalm.com/educational/booleanfunctionsimplificationtool.shtml

may be worth a try.

However, it’s usually best to do what you do for yourself — because readability and comprehensibility are more important — and let the compiler simplify it.

0
source

This is such a bad idea! Programmers write code that reflects their thought processes. Thus, logical expressions written by people are already automatically optimized for human understanding. Any attempt to improve this software is doomed to failure. The only context in which this may make sense is the post-processing of source code created with the tool.

0
source

What you need is a tool that can analyze C ++, determine the meaning of its symbols, select logical equations and apply logical simplification rules to them that do not violate semantics.

A tool that can do this is our DMS Software Reengineering Toolkit with its C ++ Front End . DMS is designed to perform software analysis and transform source to source by code. Using C ++ Front End, it can parse C ++ for AST, create character tables and deduce the type of expression, and apply rewrite rules.

You can rewrite rewrite rules as follows:

domain Cpp. -- tell DMS to use the C++ front end rule factor_common_and_term(e1: condition, e2:condition, e3: condition): disjunctive_expression -> disjunctive_expression = " \e1 && \e2 || \e1 && \e3 " -> " \e1 && ( \e2 || \e3 ) " if no_side_effects(e1) /\ no_side_effects(e2); 

to share the general condition. The rule has the name "factor_common_and_term" to distinguish it from often hundreds of other rules that could be written (for example, "distribute_term", etc.). E1, e2, e3 - meta-variables representing arbitrary subexpressions (of type “condition” in accordance with the rules of grammar). Rewriting only works on disjunction_expressions; you could make it "just an expression", but then you would not get the clauses embedded in other conditional expressions. Rewriting has a template (left) and a replacement (right), both wrapped in meta-quotes " to distinguish C ++ code in templates from the rules language syntax. \ E1 are escape sequences from C ++ syntax and indicate where it can metaparameter matches. Metavariables matches any syntax of the corresponding category, so where \ e1 is visible, there can be an arbitrarily complex "condition", the fact that e1 is mentioned twice in the template makes the places happen the same.

You can write a set of rewriting rules that encode knowledge about simplifying arbitrarily complex Boolean equations; these are several dozen rules. We applied them to systems of Boolean equations other than C ++ with hundreds of thousands of terms, as well as to conditional expressions of the C and C ++ preprocessor.

For C ++, you need to check if it is safe to rewrite, which is not, if e1 has a side effect, or e2 has a side effect. This check is performed when the auxiliary function is called, which should determine this answer in a conservative way. Determining the absence of a side effect is actually quite difficult for C ++: you should know that all elements of the expression, and that none of them have side effects.

You can perform this check using a DMS attribute grammar (traversal of an organized tree) that checks all elements of an expression. Simple constants and variables (need a character table for this). Function calls (including constructors, etc.) Can; their definition must be found (again the need for a symbol table) and handled in a similar way. It is possible that the expression element calls a separate compiled function; the conservative answer in this case is “I don’t know,” so “suppose it has a side effect.” A DMS can actually read several compilation units at a time, so you can find a separate compiled function, parse / resolution of the character, and scan if you want to go this far.

Thus, the logical part of rewriting is quite simple; side effect analysis is not.

We used DMS for massive changes to C ++ code; we often cheat by making assumptions about complex analyzes like this. Usually we get surprised just like programmers are surprised ("What do you mean, what has a side effect?"). It basically works very well. We performed a detailed analysis of side effects on C systems on 25 million lines; not quite there for C ++.

An analysis of side effects is only relevant if some subexpression can be evaluated more than once. The OP example provided in the comment does not need them and can be handled by the following rules:

 rule not_on_disjunction(e1:condition, e2:condition): condition -> condition = " ! (\e1 || \e2) " -> " !\e1 && !\e2"; rule double_not(e:condition): condition -> condition = " ! ! \e " --> " \e "; 

A complete but simple processed example with a more detailed description is this example of algebraic simplification of conditional algebra and some calculus .

There is clearly a contradiction as to whether a particular code conversion will make the code more readable. IMHO, because the form of the code is often an artistic judgment, and we all seem to disagree with art. This is nothing more than letting someone modify your code.

0
source

All Articles