Results 1 to 6 of 6
  1. #1
    Rohit429 is offline Member
    Join Date
    Jul 2010
    Posts
    3
    Rep Power
    0

    Default 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.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,016
    Blog Entries
    7
    Rep Power
    23

    Default

    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

  3. #3
    Rohit429 is offline Member
    Join Date
    Jul 2010
    Posts
    3
    Rep Power
    0

    Default

    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?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,016
    Blog Entries
    7
    Rep Power
    23

    Default

    Quote Originally Posted by Rohit429 View Post
    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?
    An API has nothing to do with a package; I already gave you directions to the relevant API documentation (see my previous reply). If you haven't downloaded and installed the documentation I advise you to do so.

    kind regards,

    Jos

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    27

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    10

    Default

    Quote Originally Posted by Rohit429 View Post
    Could someone please tell me whether the Regex expression is correct?
    Yes, and no.

    Yes someone can and no it is not.

    ;-)

Similar Threads

  1. JSF expression inside another JSF expression
    By barbarabxl in forum JavaServer Faces (JSF)
    Replies: 2
    Last Post: 05-21-2010, 03:03 PM
  2. Are my comments correct?
    By twiggy62 in forum New To Java
    Replies: 2
    Last Post: 02-08-2010, 06:34 AM
  3. Getting correct output
    By WarmRegards in forum New To Java
    Replies: 9
    Last Post: 11-01-2009, 05:41 PM
  4. Syntax correct?
    By dbashby in forum New To Java
    Replies: 5
    Last Post: 09-22-2009, 06:44 AM
  5. Is this the correct Output?
    By Teny in forum New To Java
    Replies: 17
    Last Post: 04-13-2009, 12:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •