11. Symbols I

Basic knowledge about symbols and the symbol table.

Variables

  1. What segment and index number should each variable be mapped to based on the code below?
    var type kind index
    count
    id
    hours
    working
    days
    holder
    rate
    amount
class One {
  static int count;
  field int id;
  field int hours;
  field boolean working;

  constructor One new(int days) {
     var int holder;
     let count = count + 1;
     let id = count;
     let holder = days;
     let hours = holder * 8;
     let working = false;
     return this;
   }
   method int payMe(int rate) {
     var int amount;
     let amount = rate * hours;
     if (working) {
       return amount;
     } else {
       return 0;
    }
  }
}

Symbol Tables

  1. How many symbol tables does the book suggest you use, and why? What advantage do they see over just using one?
  2. If you wanted to use four symbol tables, what would you put in each one? What would be different in your process for using four vs the book’s recommended approach? Why do you think the book doesn’t suggest four?
  3. In question 1, you should have identified something the book thinks is easier with two symbol tables. How would you accomplish the same thing with just one symbol table?

I think that the real reason the book introduces two symbol tables is to give you an idea of the complexity of a real Java compiler, where new variables can be introduced at any line of the program. (I think Java only needs more than two symbol tables because of inheritance.)

One way to represent the information need is:

(struct sym (name type kind index) #:transparent)

A SymTable is a data structure storing information about syms. This note is written assuming that you chose (Listof Sym) to represent your symbol table, but there are other reasonable choices.

Write the functions:

  • sym-tbl-lookup : SymTable String(name) -> (or/c Sym False) The symbol table lookup function takes in a string name and returns either a sym with that name or #false if no item with that name is in the symbol table.
Last modified May 31, 2025: Chapter 11 web site updates. (58262d9)