11. Symbols I
Basic knowledge about symbols and the symbol table.
Variables
- 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
- How many symbol tables does the book suggest you use, and why? What advantage do they see over just using one?
- 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?
- 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 sym
s.
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 asym
with that name or#false
if no item with that name is in the symbol table.