Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3. public class SymbolTable{
  4. //stack of symbols woman
  5. //stack of array list of symbols
  6. //ArrayList <Symbol> symbolList;
  7. private Stack<ArrayList<Symbol>> symbolTable;
  8.  
  9. public SymbolTable(){
  10.  
  11. //symbolList = new ArrayList<Symbol>();
  12. symbolTable = new Stack<ArrayList<Symbol>>();
  13. }
  14.  
  15. public void pushScope(){
  16. ArrayList<Symbol> s = new ArrayList<Symbol>();
  17. symbolTable.push(s);
  18.  
  19. }
  20. public ArrayList<Symbol> popScope(){
  21.  
  22. return symbolTable.pop();
  23.  
  24. }
  25. public boolean insertSymbol(Symbol s){
  26. // boolean flag = false;
  27. //first check to see if the array list, peek on the top of stack
  28. //see if that array List contains the symbol
  29.  
  30. ArrayList<Symbol> symbols = symbolTable.peek();
  31.  
  32. for (Symbol s1 : symbols){
  33. if (s1.equals(s))
  34. return true;
  35. }
  36. symbols.add(s);
  37. return false;
  38. //redifined error
  39.  
  40.  
  41. }
  42.  
  43. public void updateValue(Value val)
  44. {
  45. //save the pop of the stack
  46.  
  47. ArrayList <Symbol> temp = symbolTable.pop();
  48. symbolTable.peek().get(symbolTable.peek().size()-1).updateValue(val);
  49. symbolTable.push(temp);
  50.  
  51. }
  52. public void printTable(PrintStream outFile)
  53. {
  54. int depth = 0;
  55. for (ArrayList<Symbol> scope : symbolTable) {
  56. String indentation = new String();
  57. for (int i=0; i<depth; i++)
  58. indentation += " ";
  59. for (Symbol symbol : scope)
  60. outFile.println( indentation + symbol );
  61. depth++;
  62. }
  63.  
  64.  
  65. }
  66.  
  67.  
  68.  
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement