wu :: forums
« wu :: forums - CS: VARIABLE RENAMER »

Welcome, Guest. Please Login or Register.
Dec 23rd, 2024, 5:43pm

RIDDLES SITE WRITE MATH! Home Home Help Help Search Search Members Members Login Login Register Register
   wu :: forums
   riddles
   cs
(Moderators: Icarus, william wu, Eigenray, Grimbal, SMQ, ThudnBlunder, towr)
   CS: VARIABLE RENAMER
« Previous topic | Next topic »
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print
   Author  Topic: CS: VARIABLE RENAMER  (Read 912 times)
Aaron
Newbie
*





   


Gender: male
Posts: 14
CS: VARIABLE RENAMER  
« on: Aug 9th, 2002, 2:30pm »
Quote Quote Modify Modify

question:  
You are writing a parser that reads a C program and translates all the variable names into new names of the form "VAR######", where ###### is an integer incremented for each unique variable name. Discuss what is needed for the case where the C program already contains a variable of the form "VAR######".
 
 
I must not understand the question correctly, since I don't see a difficulty here.  the VAR#### originally in the c program will be transformed as well thus no longer there to cause a conflict...  
 
 
eg.
 
this program:
 
void myProg () {
  int a, b c;
  int VAR01001001 = a + b + c;
  printf("%d\n", VAR01001001);
}
 
will become:
 
void myProg () {
  int VAR001, VAR002, VAR003;
  int VAR004 = VAR001 + VAR002 + VAR003;
  printf("%d\n", VAR004);
}
 
the transformation lookup is 1 to 1:
 
a => VAR001
b => VAR002
c => VAR003
VAR01001001 => VAR004
 
 
... or is this problem a lot more complex then I thought?
 
 
 
 
 
 
IP Logged
-D-
Guest

Email

Re: CS: VARIABLE RENAMER  
« Reply #1 on: Aug 9th, 2002, 4:36pm »
Quote Quote Modify Modify Remove Remove

Yes.  what if the VAR#### in the original program is say:  VAR0001 and it's the 5th variable.  By the time you get to the 5th variable and start to rename it, you've already renamed all the first ones VAR0001 and now you have to figure out which of the VAR0001's in the program are the 5th variable and which are the 1st.  
 
One solution is a pre-mangler pass through the file that tacks a "_temp" or similar to all variables of the form VAR#### so that they maintain their uniqueness.
-D-
IP Logged
-D-
Guest

Email

Re: CS: VARIABLE RENAMER  
« Reply #2 on: Aug 9th, 2002, 4:40pm »
Quote Quote Modify Modify Remove Remove

I need to ammend one part.  My point was assuming you do a pass for each variable, obviously that's not necessarily ideal.  
 
If you instead took a pass through the file for all variable declarations, put them in a lookup table paired with their new names and then went through the file and replaced all variables as soon as they were encountered you would not have this problem.
 
Basically the "trick" to the problem (if asked as an interview question for example) is ensuring you state either of these points to show that you considered it a possibility or discounted it as a problem.
-D-
IP Logged
Ankur
Guest

Email

Re: CS: VARIABLE RENAMER  
« Reply #3 on: Jun 9th, 2005, 6:00am »
Quote Quote Modify Modify Remove Remove

I guess i dont understand the issue:
 
if in the c prog there is something like this
 
int a, a001;
 
u convert it into
 
int a001, a001002;
 
so how do u map back:
 
strip the last 3 digits out of the var.
 
what is the problem?
IP Logged
River Phoenix
Guest

Email

Re: CS: VARIABLE RENAMER  
« Reply #4 on: Jun 10th, 2005, 10:07am »
Quote Quote Modify Modify Remove Remove

when you have
int b;
int a001;
 
they should be translated to
int a001;
int a002;
IP Logged
Grimbal
wu::riddles Moderator
Uberpuzzler
*****






   


Gender: male
Posts: 7527
Re: CS: VARIABLE RENAMER  
« Reply #5 on: Jun 10th, 2005, 3:48pm »
Quote Quote Modify Modify

It is like the problem of translating all A's in a text by B and all B's by A.  If you first replace all A's by B's and then all B's by A's you are in trouble.  But if you do on e pass exchanging all A's and B's, it works.  And it is faster.
 
For the renaming of variables, just parse the whole source as a compiler would do and whenever an identifier is found, if it is the first time, generate and store a name, if not, get the generated name.
IP Logged
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print

« Previous topic | Next topic »

Powered by YaBB 1 Gold - SP 1.4!
Forum software copyright © 2000-2004 Yet another Bulletin Board