0

So I need to count the number of conditional statements in C++ source code:

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;

int main ()
{
  char str[] = "String variable";
  cout << strstr (str,"variable") << endl;
  return 0;
}

I need to do a string search rather than a semantic one since this is a course work and I don't think they expect something impressive. Problem is, how do I tell the strstr() function to look into the whole source code instead of a single variable passed as the first parameter? Also, is this a good approach to solving the assignment?

Carey Gregory
  • 6,685
  • 2
  • 23
  • 45
Sacred
  • 13
  • 5
  • Since you are working with C++ you should avoid using `strstr`. Actually if you have access to a C++11 compiler you should consider using some of the regex features, http://en.cppreference.com/w/cpp/regex – Jack Jul 02 '14 at 23:59
  • @Jack since we have a current standard [`std::strstr`](http://en.cppreference.com/w/cpp/string/byte/strstr) is also well defined. The question is pretty unclear anyway. – πάντα ῥεῖ Jul 03 '14 at 00:10
  • Well, I don't know how to lay it out better. I just need to find the number of occurrence of if and if/else statements. – Sacred Jul 03 '14 at 00:12
  • Simple string matching functions like `strstr` won't do the job at all. For example, it would find variables named `whatif` as instances of `if` statements. Your code needs to follow the parsing rules of C++. If you've got regular expressions available to you, those will do the job. Otherwise, you can lookup the standard and roll your own. Just be forewarned it's not as simple as it appears on the surface. – Carey Gregory Jul 03 '14 at 00:50
  • I'm taking back my statement that regular expressions will do the job. I can't imagine any regular expression that could handle comments. – Carey Gregory Jul 03 '14 at 03:16

2 Answers2

0

So I need to count the number of conditional statements in a C++ program.

Problem is, how do I tell the strstr() function to look into the whole source code instead of a single variable passed as the first parameter?

You can't tell strstr (or any other C/C++ function) to search a running/compiled program because the source code text no longer exists at that point.

Instead you need to write a program which can open your actual source files (.cpp files, .h files, etc), load the text into string variables in memory, and then search those for whatever you want to find.

Community
  • 1
  • 1
TheUndeadFish
  • 7,700
  • 1
  • 20
  • 16
  • I'm pretty sure the OP intends to read the source code, not the compiled results. – Carey Gregory Jul 03 '14 at 00:47
  • Hard to tell from the question, but it's not uncommon that people ask for things at runtime which are only available from the source, so I figured I would cover that aspect. – TheUndeadFish Jul 03 '14 at 03:13
0

I'm thinking what you would need to do is create a text parser, which is separate from the source you want to examine. To do this, create a new C++ project, then:

  1. Read in the file into memory
  2. Create a lookup table of strings or character arrays that hold conditional statements that exist in C++, like: "if", "else", "goto" etc.
  3. Loop through each character in memory, and compare the character you find with your lookup table. If you have found a match, increment a counter.
Community
  • 1
  • 1
Ospho
  • 2,538
  • 5
  • 23
  • 38
  • 1) With an algorithm like this you don't need to read the file into memory. You can just do a simple loop reading a char at a time and know that the underlying code is doing the buffering for you. 2) Seems like you're missing step 4. How does this parser distinguish `if` from `iffoo`? – Carey Gregory Jul 03 '14 at 00:55
  • Reading the file into memory all at once or reading portions of it is up to the op and how he wants to enhance the solution. My answer is just a guideline to achieving the goal, but he most likely would experience hiccups along the way which would require his/her own adjustments to make the solution better. The lookup table could include whites-spaces in the keyword strings. – Ospho Jul 03 '14 at 01:05
  • Yeah, I get that, but reading the whole file into memory is just an unnecessary complication for a student. Including white space in the keyword strings won't fix anything since `if(foo)` contains no whitespace and yet is a valid conditional expression, while `iffoo=1` is an equally valid assignment. – Carey Gregory Jul 03 '14 at 01:10
  • 1
    You need to have a state machine to handle comments. "// if" is not a conditional statement, but a comment. To make matters worse, you can have "/* if (help *) / 25 */". In other words the C-Style comments need to be removed as well. There is more to parsing C or C++ than just searching for words. – Thomas Matthews Jul 03 '14 at 02:09
  • @ThomasMatthews Exactly. Parsing source code is nowhere as simple as people tend to think it is. – Carey Gregory Jul 03 '14 at 03:15