Symbol Table Entries (Java)

SymbolTableEntry class is a base class. Several different subclasses of symbol table entries will extend this class, each designed to store and provide attribute information for the various identifiers that are encountered in the source program.

Symbol table entry base class

Data Members:
   String name;
 TokenType type;

Constructors:  
    public SymbolTableEntry () {;}
   
    public SymbolTableEntry (String name) {
        this.name = name;
    }
   
    public SymbolTableEntry (String name, TokenType type) {
        this.name = name;
        this.type = type;
    }

Public members:
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public TokenType getType() {
        return type;
    }
    public void setType(TokenType type) {
        this.type = type;
    }

Protected members:
      protected boolean isVariable() { return false; }
    protected boolean isKeyword() { return false; }
    protected boolean isProcedure() { return false; }
    protected boolean isFunction() { return false; }
    protected boolean isFunctionResult() { return false; } 
    protected boolean isParameter() { return false; }
   
protected boolean isArray() { return false; }

Derived classes (extend SymbolTableEntry)

You need classes for the following entry types. The required fields are indicated for each.

Note that simple variables can also be function result variables (i.e., they have the same fields), in which case the method is_function_result is defined differently.

Here is one example of an implementation for the VariableEntry class. Note that the methods isVariable, isFunctionResult, isParameter, and print are redefined.

public class VariableEntry extends SymbolTableEntry {

    int address;
    boolean parm = false, functionResult = false;
   
    public int getAddress() {
        return address;
    }

    public void setAddress(int address) {
        this.address = address;
    }

    public VariableEntry() {
    }

    public VariableEntry(String Name) {
        super(Name);
    }
   
    public VariableEntry(String Name, TokenType type) {
        super(Name, type);   
    }

    protected boolean isVariable() {
        return true;
    }

    protected boolean isFunctionResult() {
        return functionResult;
    }

    public void setFunctionResult() {
        this.functionResult = true;
    }

    public boolean isParameter() {
        return parm;
    }

    public void setParm() {
        this.parm = true;
    }
   
    public void print () {
       
        System.out.println("Variable Entry:");
        System.out.println("   Name    : " + this.getName());
        System.out.println("   Type    : " + this.getType());
        System.out.println("   Address : " + this.getAddress());
        System.out.println();
    }
   
}