• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Cyclomatic Complexity

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic