Stack class, Symbol Table help

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2008
Posts: 16
Reputation: vladdy191 is an unknown quantity at this point 
Solved Threads: 0
vladdy191 vladdy191 is offline Offline
Newbie Poster

Stack class, Symbol Table help

 
0
  #1
Nov 29th, 2008
I'm trying to make a symbol table, for a project. The project as a whole is a translator/compiler that translates an imaginary language into java. I have everything down except for the symbol table.

I am currently using a Stack of Lists. The Lists are vectors of Strings

so I have Stack<Vector<String>>

What I'm trying to do is make a List of the declared variables, then when a nested class occurs, I push the current list on top of the stack. Now my problem is that I need to clear the current List so that I can hold only the declared variables in the new inner class with the list. But if I clear the List it also clears it from the Stack, because its an object and thus they are aliases (I'm assuming). Could any one suggest a way to clear the current List without affecting what I have on the Stack?

Here's My code for my Symbol Table

  1. public void Check()
  2. {
  3. for(int j=0; j<tIndex; j++)
  4. {
  5. while(tokens[j].equals("var")||tokens[j].equals(","))
  6. {
  7. j++;
  8. if(idcheck(tokens[j]))
  9. {
  10. if(reName(tokens[j]))
  11. {
  12. change(tokens[j], blockNumber, j);
  13. }
  14. if(!reDeclaredV(tokens[j]))
  15. vVariables.add(tokens[j]);
  16. else
  17. System.out.println("error");
  18. }
  19. }
  20.  
  21. if(tokens[j].equals("const"))
  22. {
  23. j++;
  24. if(idcheck(tokens[j]))
  25. {
  26. if(!reDeclaredC(tokens[j]))
  27. vConstatns.add(tokens[j]);
  28. else
  29. System.out.println("error");
  30. }
  31. }
  32.  
  33. if(tokens[j].equals("loop")||tokens[j].equals("if"))
  34. {
  35. Vector<String> temp= new Vector<String>();
  36. temp=vVariables;
  37. variables.push(temp);
  38. blockNumber++;
  39. vVariables.clear();
  40. }
  41. if(tokens[j].equals("end"))
  42. {
  43. vVariables=variables.pop();
  44. blockNumber--;
  45.  
  46. }
  47. }
  48. }

I only posted the method in question

Thanks for any help
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,649
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Stack class, Symbol Table help

 
0
  #2
Nov 30th, 2008
Vector and hence Stack are legacy/by default thread safe classes. Consider using ArrayList and LinkedList in lieu of the above mentioned classes.

And as for your problem, replace:
  1. Vector<String> temp= new Vector<String>();
  2. temp =vVariables;
  3. variables.push(temp);
with
  1. variables.push(new Vector<String>(vVariables));
In your code, you create a new Vector instance referenced by `temp' but discard it by assigning `vVariables' to `temp' which effectively means, `vVariables' and `temp' now point to the same thing. In the code I posted, the list pushed onto the `variables' stack is effectively different from the list `vVariables'. It is also worth noticing that both the lists effectively point to the same data so modifying it will make the changes visible in both the lists [which isn't a problem here since Strings are immutable].
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC