| | |
Stack class, Symbol Table help
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Nov 2008
Posts: 16
Reputation:
Solved Threads: 0
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
I only posted the method in question
Thanks for any help
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
Java Syntax (Toggle Plain Text)
public void Check() { for(int j=0; j<tIndex; j++) { while(tokens[j].equals("var")||tokens[j].equals(",")) { j++; if(idcheck(tokens[j])) { if(reName(tokens[j])) { change(tokens[j], blockNumber, j); } if(!reDeclaredV(tokens[j])) vVariables.add(tokens[j]); else System.out.println("error"); } } if(tokens[j].equals("const")) { j++; if(idcheck(tokens[j])) { if(!reDeclaredC(tokens[j])) vConstatns.add(tokens[j]); else System.out.println("error"); } } if(tokens[j].equals("loop")||tokens[j].equals("if")) { Vector<String> temp= new Vector<String>(); temp=vVariables; variables.push(temp); blockNumber++; vVariables.clear(); } if(tokens[j].equals("end")) { vVariables=variables.pop(); blockNumber--; } } }
I only posted the method in question
Thanks for any help
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: with 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].
And as for your problem, replace:
Java Syntax (Toggle Plain Text)
Vector<String> temp= new Vector<String>(); temp =vVariables; variables.push(temp);
Java Syntax (Toggle Plain Text)
variables.push(new Vector<String>(vVariables));
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
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
![]() |
Other Threads in the Java Forum
- Previous Thread: Why am I getting a StackOverflowError?
- Next Thread: file splitter
| Thread Tools | Search this Thread |
Tag cloud for Java
-xlint 2dgraphics android api apple applet application applications arguments array arrays automation bank bidirectional binary bluetooth chat class classes client code collision component database db development draw eclipse eclipsedevelopment error event exception file fractal game givemetehcodez graphics gui helpwithhomework homework html ide image input integer integration j2me jarfile java javadesktopapplications javafx javaprojects jmf jni jpanel julia learningresources linux list loop map method methods mobile netbeans newbie number oracle print problem program programming project projectideas recursion researchinmotion scanner screen server service set size sms socket sort sorting sql sqlserver state string swing swt tcp test text-file threads time tree web windows






