6 Replies - 991 Views - Last Post: 14 April 2011 - 07:09 AM Rate Topic: -----

#1 abk07  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-April 11

Extract variable definitions and variable use from a program

Posted 02 April 2011 - 02:31 AM

Hi! Im trying to write a C program to extract variable definitions and variable use in each line from a input C program.
That is, a variable is said to be defined when its value changes(including declarations) and it is said to be used when it is referenced. I need to consider this for all the cases and functions(built-in as well as user defined)..
For example consider the following input program
int main()
{
int a,b,c;
a=10;
b=20;
c=a+b;
printf(c=%d,c);
}

In the line 3, a,b,c are defined and nothing is referenced, hence in line number 3 under the defined column a,b,c must be printed, and a mere should be printed under the used column.
Similarly in line 6, a is defined and both b and c are used, hence in defined column a should be printed and under used column b,c should be printed.
I really dont know how to start.. can anyone please give me an idea??
Thanks in advance..

Is This A Good Question/Topic? 0
  • +

Replies To: Extract variable definitions and variable use from a program

#2 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Extract variable definitions and variable use from a program

Posted 02 April 2011 - 03:12 AM

Parsing.
Context free grammar.

Have fun.
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,436
  • Joined: 23-August 08

Re: Extract variable definitions and variable use from a program

Posted 02 April 2011 - 03:37 AM

Note, your quotes here:

printf(“c=%d”,c);


look like they came from a word processor rather than a text editor, and will likely cause problems for you.

Tutorial on parsing, and there are others by the same brilliant author, Martyn.Rae as well.
Was This Post Helpful? 1
  • +
  • -

#4 abk07  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-April 11

Re: Extract variable definitions and variable use from a program

Posted 02 April 2011 - 11:31 AM

View PostOler1s, on 02 April 2011 - 03:12 AM, said:



Hey how can parsing help me out??
Because I need to consider user defined and built in functions as well..

For example, consider this

strcpy(dest,src);


Here 'dest' is being defined and 'src' is being used..
So there are many built in functions! How can I extend this to all the cases??
Was This Post Helpful? 0
  • +
  • -

#5 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Extract variable definitions and variable use from a program

Posted 02 April 2011 - 01:14 PM

Quote

Hey how can parsing help me out??
Did you not read the Wikipedia entry? Here is the very first sentence:

"In computer science and linguistics, parsing, or, more formally, syntactic analysis, is the process of analyzing a text, made of a sequence of tokens (for example, words), to determine its grammatical structure with respect to a given (more or less) formal grammar." - Wikipedia

This is your question:

"Hi! Im trying to write a C program to extract variable definitions and variable use in each line from a input C program."

Do you not see the connection? Recognition definitions and expressions means analyzing the file. And since that analysis is called parsing, what you want to do here is write a C parser.

Quote

Because I need to consider user defined and built in functions as well..
Is there a difference?

char * strcpy(char *dest, char *src); // Standard lib
char * yourstrcpy(char *dest, char *src); // Your function



What is the syntactical difference?

Quote

Here 'dest' is being defined and 'src' is being used..
No. You don't even understand the meaning of definition and in C, it would seem. But even by your definition, you are wrong. Here is example code:

#include <stdio.h>
#include <string.h>

int main(void) {
	char dest[20];
	char src[] = "Example";
	printf("%p\n", dest);
	strcpy(dest, src);
	printf("%p\n", dest);
	return 0;
}



Did dest change for you when you compile and run this code?

But, your definition of defined is wrong. "That is, a variable is said to be defined when its value changes(including declarations) and it is said to be used when it is referenced."

No. Read the standard. A definition is a declaration which, for objects causes storage to be reserved, and for functions, includes the function body.

This: a = 5; is not a definition. It is an expression.
Was This Post Helpful? 1
  • +
  • -

#6 abk07  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-April 11

Re: Extract variable definitions and variable use from a program

Posted 14 April 2011 - 12:11 AM

Hi thanks for the reply..
Sorry couldn't post reply as I was on an educational tour..

I need to admit that I'm not an advanced programmer. And I don't know all the advanced concepts such as parser and all.
Can you please explain me in simple words?? So that it'll be very helpful for me to learn..
I really don't know how to apply parsing technique to my program..
I have written a C program to recognize a few of the built in functions.. It looks a bit crappy I know. Please can you suggest me an alternative method??
Thanks in advance..

Please note: The input to our program will be an executable C program.. Which has line numbers at the beginning of each line..
Attached File  Working1.zip (2.87K)
Number of downloads: 39

This post has been edited by JackOfAllTrades: 14 April 2011 - 12:32 PM
Reason for edit:: Removed unnecessary quote

Was This Post Helpful? 0
  • +
  • -

#7 abk07  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-April 11

Re: Extract variable definitions and variable use from a program

Posted 14 April 2011 - 07:09 AM

In short, I need to generate output as shown below
Lno        Definition       Referenced
-------------------------------------------------
1             --               --
2             --               --
3            a,b,c             --
4             a                --
5             b                --
6             c                a,b
7             --                c
8             --               --

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1