Skip to content
SWP-Comp-Ch3ck3r edited this page Apr 16, 2013 · 8 revisions

Document Status: Draft / Work in Progress

This article is a draft and work in progress. Feel free to comment and discuss on the issue tracker.

Content of Symbol Table

The symbol table contains entries for every declaration with

  • name of variable
  • type of variable
  • column for optimization (optional)

type of variable is a reference to BasicType, StructType, ArrayType

Also the symbol table has to keep track of

  • Stack of symbol tables
  • Reference to parent symbol table

Usage

The program node is the root node of every program. This root node has an attribute with a reference to the symbol table. This is the root symbol table. This root symbol table does not have any parent symbol table. The root symbol table is active.

Whenever a block node appears in the abstract syntax tree a new symbol table is generated. The parent of this newly generated syntax table is the syntax table that has been active beforehand. The new symbol table is now active. The newly created symbol table will be referenced in the block node.

Newly declared symbols are entered into the currently active symbol table with their type and the name of the identifier (+ optional data, see remarks).

The intermediate code generator will always find the currently active by investigating the next higher block or program node in the abstract syntax tree.

Example: Example of Symbol Table

Remarks

Symbol table could also store Line and Column number of all identifiers for better debug purpose / error messages.

Interface specification

package swp_compiler_ss13.common.parser;

import swp_compiler_ss13.common.optimization.Liveliness;
import swp_compiler_ss13.common.types.Type;

/**
 * Proposed interface for symbol table
 * 
 * @author "Frank Zechert", "Danny Maasch"
 * @version 1
 * @see <a target="_top" href="https://github.com/swp-uebersetzerbau-ss13/common/wiki/Symbol Table">Symbol Table Wiki</a>
 * @see <a target="_top" href="https://github.com/swp-uebersetzerbau-ss13/common/issues/6">Symbol Table Issue Tracker</a>
 */
public interface SymbolTable {
	/**
	 * Get the symbol table of the previous scope. If there is no previous scope returns null.
	 * @return The symbol table of the previous scope or null.
	 */
	public SymbolTable getParentSymbolTable();
	
	/**
	 * Checks if the given identifier is already declared.
	 * @param identifier The identifier to check.
	 * @return true if the identifier is declared, false otherwise
	 */
	public Boolean isDeclared(String identifier);
	
	/**
	 * Returns the Type of the given identifier. If the identifier is not defined returns null.
	 * @param identifier The identifier to lookup.
	 * @return The Type of the identifier.
	 */
	public Type lookupType(String identifier);
	
	/**
	 * Insert the identifier <code>identifier</code> of type <code>type</code> into the symbol table.
	 * @param identifier The identifier to insert into the symbol table.
	 * @param type The Type of the identifier.
	 */
	public void insert(String identifier, Type type);
	
	/**
	 * Remove the <code>identifier</code> from the symbol table.
	 * @param identifier The identifier to remove.
	 * @return true if the identifier was removed, false if it was not in the symbol table and therefore not removed.
	 */
	public Boolean remove(String identifier);
	
	/**
	 * Set the liveliness information of the identifier.
	 * @param identifier The identifier.
	 * @param liveliness New liveliness information.
	 */
	public void setLivelinessInformation(String identifier, Liveliness liveliness);
	
	/**
	 * Get the liveliness information of the identifier.
	 * @param identifier The identifier to get the liveliniess informations for.
	 * @return The liveliness information or null if none.
	 */
	public Liveliness getLivelinessInformation(String identifier);
}
Clone this wiki locally