⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 symboltable.java

📁 一个JAVA编写的简单编译器
💻 JAVA
字号:
package ast;

/**
 * The SymbolTable manage NameSpace
 *
 * @author group A606
 */
public class SymbolTable {
  // object on the class level:
  // static/nonstatic
  // Variable/Method
  // every class define it is one NameSpace.

  // Stack for the current NameSpace
  private java.util.Stack display;
  private NameSpace current;
  private NameSpace global;

/**
 * To submit to Method codegen.WriteStdIO.writeInteger and codegen.ReadStdIO.readInteger to add.  
 */ 
  private void addMethods () {
    ast.declaration.StaticMethod smd = new ast.declaration.StaticMethod ("codegen.WriteStdIO.writeInteger", true, ast.declaration.JSubObject.TVOID);
    ast.NameSpace s = new ast.NameSpace (current);
    ast.declaration.LocalVariable decl = new ast.declaration.LocalVariable ("p1", true);
    s.addObject (decl);
    smd.setNameSpace (s);
    current.addObject (smd);

    smd = new ast.declaration.StaticMethod ("codegen.ReadStdIO.readInteger", true, ast.declaration.JSubObject.TINT);
    s = new ast.NameSpace (current);
    smd.setNameSpace (s);
    
    current.addObject (smd);
  }
      
/**
 *  New Symboltabele 
 * 
 */ 
  public SymbolTable () {
    display = new java.util.Stack ();
    display.push (current = global = new NameSpace (null));
    addMethods ();
  }

/**
 * set Name in Symboltable.
 * 
 */ 
  public void setName (String name) {
    global.setName (name);
  }  
  
/**
 *a new NameSpace on top of  stack .
 * 
 * @return the NameSpace
 */ 
  public NameSpace push () {
    util.Trace.decl ("push");
    NameSpace s = new NameSpace (current);
    display.push (s);
    current = s;
    return s;
  }
  
/**
 * pop a new NameSpace on the top of stack for the NameSpace.
 * 
 */ 
  public void pop () {
    display.pop ();
    current = (NameSpace)display.peek ();
    util.Trace.decl ("pop ");
  }

/**
 * Submit a  current NameSpace.
 * local object : Variable
 * notice: the scope of name in Java
 * NameSpace is nested.
 * But  environment of NameSpace not allowed used Name repeat
 */ 
  public void addObject (ast.declaration.JSubObject obj) {
    ast.declaration.JSubObject d = getObject (obj.getName ());
    if (d != null && (d.getParent () != global || current == global)) {
      dump ();
      util.Error.e1 ("name already defined");
    }
    current.addObject (obj);
  }
  
/**
 * submit a int entering in the global NameSpace.
 * 
 * @param key of this entering will insert.
 */ 
  public ast.declaration.JSubObject addintObject (String key) {
    ast.declaration.JSubObject decl = global.getObject (key);
    if (decl == null) {
      decl = new ast.declaration.Int (key);
      global.addObject (decl);
    }
    return decl;
  }
  
/**
 * pop the entering of the Name
 * the NameSpace becomes the top of the stack bounded of the searching.
 * when the entering finded the searching end
 * @param key of this entering will be seached.
 */ 
  public ast.declaration.JSubObject getObject (String key) {
    for (int i = display.size() - 1; i >= 0; i--) {
      NameSpace s = (NameSpace)display.elementAt (i);
      ast.declaration.JSubObject d = s.getObject (key);
      if (d != null)
        return d;      
    }
    return null;
  }
  
 /**
 * output every object
 *
 */ 
  public void dump () {
    System.out.println ("Table of Symbols");
    for (int i = display.size() - 1; i >= 0; i--) {
      System.out.println ("start NameSpace " + i);
      NameSpace oe = (NameSpace)display.elementAt (i);
      oe .dump ();
      System.out.println ("stop NameSpace " + i);
    }
    System.out.println ("End SymbolsTable ");
  }
  /**
 * generate XML for this NameSpace 
 *
 * @param p output stream
 * @param prefix generate the line of output
 */ 
 public void genXML (java.io.PrintStream p) {
    p.println ("<symboltable>");
    for (int i = display.size() - 1; i >= 0; i--) {
      NameSpace oe = (NameSpace)display.elementAt (i);
      oe .genXML (p, "");
    }
    p.println ("</symboltable>");
    
  }

 /**
 * code-generation for the Symboltable.
 * code-generation for the upmost structured Element.
 * 
 */ 
  public void codegen () {
    NameSpace oe = (NameSpace)display.elementAt (0);
    oe.codegen ();  
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -