• Post Reply Bookmark Topic Watch Topic
  • New Topic

Cyclomatic Complexity  RSS feed

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Having successfully coded a program that measures the Cyclomatic Complexity of a Java Source code file using the String Tokenizer class. I am in the middle of coding another using the Java Regex class for learning purposes.

Is it possible by using the Java Regex class to Ignore comments from a file being read without using the slashSlash from the StreamTokenizer? I tried COMMENTS in the Regex class but had no luck.

Also:
Pattern pattern = Pattern.compile("if|for|while|case|switch",Pattern.COMMENTS);

How can the line above get sorted so that my count will only count the keyWords and not be greedy and count the "If" in " I found out " .

Please help !
Thanks
John Weir
 
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regular expressions aren't the way to go. What you want is a full-blown parser, the kind that you can build with a parser generator. There are a number of nice Java parser generators out there, ANTLR being one (Google it.)

But you probably should look at the open-source Java code analyzer PMD first, which already does this analysis, and more. If it's not enough for you, you can modify or enhance it. Why reinvent the wheel?
 
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Pattern.COMMENTS flag has nothing to do with comments in Java code - it's to allow comments and whitespace in your regular expression, which can be used to improve readability. E.g. the pattern

(\w+)\s+(\w+)

could be written

(\w+) # first name
\s+ # space(s)
(\w+) # last name

If you compile with Pattern.COMMENTS, the regex compiler will ignore the newlines, blanks, and comments indicated by #, converting it back to

(\w+)\s+(\w+)

That may not seem incredibly useful here, but imagine that the pattern had been somewhat more complex - the ability to insert whiteapce and comments can be very nice indeed. Though it's more effective in some other languages like Perl, which allow you to write a multi-line pattern expression withoutspecial escape sequesnces like "\\" and "\n". Oh well...

As for your general problem - yeah, I agree with EFH. (Always a good idea.)
[ November 20, 2004: Message edited by: Jim Yingst ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!