Extract variable definitions and variable reference from a 'C' program
Hi everyone! I've been assigned a strange project at college. I've been asked to extract variable definitions and references from a given input 'C' program.
Each line in the input program contains respective line number at the beginning and followed by a space, and then the actual code begins.
Consider the following program..
And the input for the program which I'm developing is a 'C' program, in which single line consists of a single statement..
1 int main()
3 int a,b,c;
4 printf("Enter the values of a and b\n");
7 printf("The sum of two numbers is %d",c);
ie, we know that a whole program can be written in a single line. But not in my case, that is once there is a termination(semicolon), the lines following the semi colon is shifted to next line..
Anyways my job is to extract the variable definitions/declarations and variable use/reference in the given input C program..
Consider the above program,
In line number 3, variables a,b and c are declared, hence it has to be printed under the "definition" column of the output..
Similarly in the statement 5, values of a and b are being initialized using a scanf statement, hence variables a and b should be printed under the definition column of the output..
Now consider the statement 6, The value of variable c is being initialized/defined hence c must be printed under the definition column.. At the same time, values of a and b are being used to determine the value of c, hence variables a and b must be printed under the "reference" column of the output..
And lastly, the value of variable c is being referenced/used in the statement 7, hence the variable c has to be printed under the referenced column..
The sample output of the program is as shown below..
Line Number Defined Variable Referenced Variable
1 -- --
2 -- --
3 a,b,c --
4 -- --
5 a,b --
6 c a,b
7 -- c
8 -- --
Can anyone tell me how to solve the problem????
Remember, I need to write a C/C++ program for the project..
I need to consider the mathematical expressions, logical expressions, built in function calls, user defined function calls and function definitions as well..
Thanks in advance..