2

Can anyone tell me how can i write a program where i have to read a c program from a text file and then count the number of if-else statements, excluding the nested if-else. Here in this program i have counted the number of if and else from a text file, but how do i exlude the nested if from this count? please help me with it.

package softwaretesting;

import java.io.*;
import java.util.Scanner;

public class SoftwareTesting {

public static void main(String[] args) throws IOException {
    int countIf = 0, countElse = 0;

    Scanner input;
    input = new Scanner(System.in);
    String fileName;
    System.out.println("Enter the path of the file from which no of if and else statements are to be counted");

    fileName = input.next();


    Scanner file;
    file = new Scanner(new File(fileName));

    int count=0;
    while (file.hasNextLine())
    {
        String line = file.nextLine();
        if (line.indexOf("if") != -1 && count%2==0 )
        {   
            countIf++;
        }


        if (line.indexOf("else") != -1  )
        {
            countElse++;
        }


    }


    {
        System.out.println("No of If statements: " + countIf);
        System.out.println("No of Else statements: " + countElse);
    }
}
}
skiwi
  • 66,971
  • 31
  • 131
  • 216
Aiman
  • 31
  • 1
  • 3
  • 2
    Hint: count brackets. – Martijn Courteaux Mar 30 '14 at 11:09
  • i did try that, but very often if there is just a single line in the statement we don't use the braces, what to do in sucha a case? – Aiman Mar 30 '14 at 11:10
  • You're going to need to maintain some state somewhere. That means track if you're currently in an if block. A boolean would work fine. Also, don't assume you won't have multiple if statements on a single line – crush Mar 30 '14 at 11:10
  • please explain a little. how will it solve the problem of excluding the nested if-else? – Aiman Mar 30 '14 at 11:12
  • 1
    Because you would know that you are already in an opened if statements context... – crush Mar 30 '14 at 11:15
  • 2
    I suppose you realize that your current code will also count lines containing variables that have 'if' anywhere in their name? If you REALLY need to do this, then you need something that parses the C language under programmatic control, so that your code can count the constructs that parsing finds. Then you'll avoid counting variables named "difference", and 'if' statements that have been commented out, and literal strings containing 'if', etc. – arcy Mar 30 '14 at 11:24
  • @rcook I'd say this is almost certainly a homework assignment. The input is probably fairly naive, but since the OP didn't tell us that it's homework, I'm just guessing. – crush Mar 30 '14 at 11:26
  • 1
    You may be right; if you are, then I hope part of the intent is to help the students realize that parsing a language is a little more complicated than scanning it for strings. We might even hope that they are learning that the parsing of 3G procedural languages is a problem that not only got solved, but for which there are canned solutions that can be configured to a particular language. One can hope. – arcy Mar 30 '14 at 11:30
  • well. it is in fact a homework assignment. I am a novice and i really am not able to understand how to count the constructs. if u can then pease give me a little hint regarding that with a piece of code or something – Aiman Mar 30 '14 at 11:38

3 Answers3

0

If you aren't stuck to Java I would suggest Python pycparser https://github.com/eliben/pycparser . If you have to use Java then check Parsing / reading C-Header files using Java

Community
  • 1
  • 1
user3159253
  • 16,836
  • 3
  • 30
  • 56
0

Without seeing the original file to read, I would use regex to count the total number of if/else and then count the number of nested if/else. The math for the result should be pretty simple... :)

Desorder
  • 1,549
  • 12
  • 16
0

In general counting if-else statements the way you've specified is wrong 'cause in a programming language if-else statement is not only line containing if or else (consider 'if' or 'else' words in comments and so on). It's exactly a statement in a language defined by a specific set of rules - grammar. Moreover your code will return successfully from not even a C program, which might not be correct...

So the ultimate way to solve the problem is to build an AST tree for the input program and traverse it counting only top-level if-else statements.

There are several tools that can help you with this.

  1. ANTLR
  2. JavaCC

Both of them can generate language parsers from the specified grammar. You can use these parsers to figure out what you input program consist of.

The main problem of such approach is finding (creating?) the correct grammar. For example there are lots of grammars for both ANTLR (https://github.com/antlr/grammars-v4) and JavaCC (https://java.net/projects/javacc/downloads/directory/contrib/grammars); but none of them can be used to generate AST - only plain parsing will be produced. On the other hand since you need to count only if-else statements you might be good with parsing only (without AST tree)...

So at this point there are 2 possible solutions:

  1. Manually update generated by ANTLR/JavaCC parser to count if-else statements.
  2. Find/create a C grammar for ANTLR/JavaCC to produce AST of the input program and traverse it in search for a top-level if-else statements.

ps: for more info on updating grammars to support AST trees see How to implement JJTree on grammar (JavaCC) and How to output the AST built using ANTLR? (ANTLR).

Community
  • 1
  • 1
FlasH from Ru
  • 1,165
  • 2
  • 13
  • 19