Results 1 to 6 of 6
- 07-27-2010, 08:26 AM #1
Member
- Join Date
- Jul 2010
- Posts
- 3
- Rep Power
- 0
Could someone please tell me whether the Regex expression is correct?
Hi,
I am trying to find the cyclomatic complexity of a method using regex. The code that I have written is given below:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.*;
class cyclomaticCalculator
{
// uses the java i/o.
static BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
protected String txtFileName;
protected int count = 1; // start count at one then no need to add 1 to count!
private static BufferedReader reader;
// Uses the java.io.*;
public void Cyclomatic() throws IOException
{
try {
// open the file
System.out.println("------------------------------" );
System.out.println("CYCLOMATIC COMPLEXITY PROGRAM " );
System.out.println("------------------------------" );
//System.out.println("Enter file name to be read: " );
// Create object to read textfile from keyboard
txtFileName = "C:\\Documents and Settings\\227951\\workspace\\Decompiler\\test\\And Or.java";
System.out.println("\n \n");
// the buffered reader object
reader = new BufferedReader(new FileReader(txtFileName));
// Pattern for keywords and the start of comments
Pattern p1 = Pattern.compile("(/\\*)|(//)|(\".*?\")|(if|for|while|case|switch)");
Matcher m1 = p1.matcher("");
// Pattern for the end of multiline comments
Pattern p2 = Pattern.compile("\\*/");
Matcher m2 = p2.matcher("");
//Pattern for logical operators
// Pattern for logical operators
/* Pattern p3 = Pattern.compile("(\\&&)|(\\||)");
Matcher m3 = p2.matcher("");
*/
boolean inComment = false;
int lineNum = 0;
String line = null;
while ((line = reader.readLine()) != null)
{
lineNum++;
int startAt = 0;
if (inComment)
{
// In multiline comment; see if it ends in this line
if (m2.reset(line).find())
{
inComment = false;
startAt = m2.end();
}
else
{
continue;
}
}
m1.reset(line);
while (m1.find(startAt))
{
if (m1.start(1) != -1)
{
// Start of multiline comment
if (m2.reset(line).find(m1.end()))
{
// If it ends in this line, we'll keep looking for keywords
startAt = m2.end();
}
else
{
// ...otherwise, just set the flag
inComment = true;
break;
}
}
else if (m1.start(2) != -1)
{
// End-of-line comment
break;
}
else if (m1.start(3) != -1)
{
// String literal
startAt = m1.end();
}
else
{
// It's a keyword
count++;
break;
}
}
}
System.out.println("Cyclomatic Complexity : "+count);
}
catch (IOException e)
{
e.printStackTrace();
}
}
public static void main (String[]args)
{
// Create object Complex
cyclomaticCalculator Complex = new cyclomaticCalculator();
try {
Complex.Cyclomatic();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
how do I check for logical && and logical || operator in the above pattern p1? Please help.
- 07-27-2010, 08:52 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
Don't use regular expressions for that; it is clumsy at best. Go to the top level index.html page of your API documentation; all technologies are shown there in a big block; on the second row, second block from the left you'll find 'javac', the Java compiler API. Click it and you'll see the Compiler Tree API; that's the one you need. You can parse source code and generate an AST (Abstract Syntax Tree). One type of node in the tree is the BinaryTree for the && or || operator. Start reading the API.
kind regards,
Jos
- 07-27-2010, 10:29 AM #3
Member
- Join Date
- Jul 2010
- Posts
- 3
- Rep Power
- 0
Hey JosAH,
I am not using an API currently. Just made this in a default package. Can you please provide me with a link to this Compiler tree API?
- 07-27-2010, 10:40 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
-
Double post closed. OP, please do not do this, and please read the forum FAQ's.
- 07-27-2010, 12:39 PM #6
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Similar Threads
-
JSF expression inside another JSF expression
By barbarabxl in forum JavaServer Faces (JSF)Replies: 2Last Post: 05-21-2010, 03:03 PM -
Are my comments correct?
By twiggy62 in forum New To JavaReplies: 2Last Post: 02-08-2010, 05:34 AM -
Getting correct output
By WarmRegards in forum New To JavaReplies: 9Last Post: 11-01-2009, 04:41 PM -
Syntax correct?
By dbashby in forum New To JavaReplies: 5Last Post: 09-22-2009, 06:44 AM -
Is this the correct Output?
By Teny in forum New To JavaReplies: 17Last Post: 04-13-2009, 12:52 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks