4

I am running an evolutionary algorithm that automatically generates S-expressions that represent an abstract syntax tree. From there I generate C code to create a compilable program.

For each generated expression I need to calculate the cyclomatic complexity to be used in the fitness calculation. I have noticed that there are tools available to do so (such as the metrics Eclipse plugin), but I was hoping for something that could analyze a more generic program representation.

I could see calling an external tool, however I think that would significantly increase my execution time. Is there a simple way to calculate cyclomatic complexity via some sort of formula that takes into account S-expressions or abstract syntax trees?

erik
  • 3,810
  • 6
  • 32
  • 63

1 Answers1

1

If your generated programs are goto-free (e.g., "structured" programs), you can compute cyclomatic complexity by using a simple rule:

CC(p) =  #conditionals +1

This is a fact for structured programs. You only need the full definition if your programs are a spaghetti control flow tangle.

Note that conditionals should count while and for loops (as they contain conditionals) and the expression short-circuit branching operators && and ||.

Ira Baxter
  • 93,541
  • 22
  • 172
  • 341