Like Tree1Likes
  • 1 Post By jimblumberg

Extract variable definitions and variable reference from a 'C' program

This is a discussion on Extract variable definitions and variable reference from a 'C' program within the C Programming forums, part of the General Programming Boards category; Hi everyone! I've been assigned a strange project at college. I've been asked to extract variable definitions and references from ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Location
    Bellary, India
    Posts
    4

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

    Code:
    1 int main()
    2 {
    3 int a,b,c;
    4 printf("Enter the values of a and b\n");
    5 scanf("%d%d",&a,&b);
    6 c=a+b;
    7 printf("The sum of two numbers is %d",c);
    8 }
    And the input for the program which I'm developing is a 'C' program, in which single line consists of a single statement..
    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..

    Code:
    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..
    Last edited by abk07; 04-16-2011 at 04:29 AM.

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,652
    So you've made little to no attempt in the past few days?

  3. #3
    Registered User
    Join Date
    Apr 2011
    Location
    Bellary, India
    Posts
    4
    Quote Originally Posted by rags_to_riches View Post
    So you've made little to no attempt in the past few days?
    I've made an attempt.. I've written a C program.. It looks crappy..
    Its around 1500 lines of code..
    Take a look..

    It recognizes only for a few built in functions..
    Doesn't recognize any user defined and expressions
    I'm a novice, don't know much of parsing and all..
    COMBINED12dt.c
    TESTIP.C

  4. #4
    Registered User
    Join Date
    Apr 2011
    Location
    Bellary, India
    Posts
    4
    Quote Originally Posted by abk07 View Post
    I've made an attempt.. I've written a C program.. It looks crappy..
    Its around 1500 lines of code..
    Take a look..

    It recognizes only for a few built in functions..
    Doesn't recognize any user defined and expressions
    I'm a novice, don't know much of parsing and all..
    COMBINED12dt.c
    TESTIP.C

    To be honest I've gone mad without much support..
    No support even from our college staff

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    Sorry, it's unlikely anyone on the boards is going to look through 1500 lines.

    Why not break the problem up into separate, testable parts and let us know what you have trouble with? Post the minimum code that compiles and exhibits the problem and you'll get all the help you can imagine.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Are you confusing 'defined' with 'reference' ? In your "defined variable" column you list variable 'c' twice. Defining and assigning (writing) and referencing (reading) are three different things.

    I agree there seems to be a lot of code there. How about filtering for variables in a relatively simple way. Then seeing if the context was 'define' or 'reference'.

    "I need to consider the mathematical expressions, logical expressions, built in function calls, user defined function calls and function definitions as well.." - no not really. You don't want to be put in a position where you need to anticipate every built-in library function, or possible user-written in other code modules that are not immediately visible. You need to work on a generic variable detector.
    Last edited by nonoob; 04-16-2011 at 08:32 AM.

  7. #7
    Registered User
    Join Date
    Apr 2011
    Location
    Bellary, India
    Posts
    4
    Thanks for your replies..

    @mike
    Hmm yes.. I'll post a smaller code..

    @nonoob
    "You need to work on a generic variable detector"..
    Exactly..

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    2,898
    You need to learn to use functions. Having a function with over 1000 lines is going to be almost impossible to debug and maintain.

    Jim
    Salem likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extract values and store in new variable
    By cosmiccomputing in forum C Programming
    Replies: 3
    Last Post: 06-01-2008, 02:45 PM
  2. Variable Reference Question
    By Rune Hunter in forum C++ Programming
    Replies: 3
    Last Post: 04-01-2007, 10:36 AM
  3. destroyed reference variable
    By Mehdi in forum C++ Programming
    Replies: 4
    Last Post: 08-14-2006, 01:54 AM
  4. variable definitions
    By dan20n in forum C Programming
    Replies: 10
    Last Post: 06-06-2006, 10:01 PM
  5. Passing the variable as a Reference
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 03-05-2002, 02:35 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21