Chapter 8: Intermediate Code Generation

Creating symbol table .

D proc id;

{t = mktable(top(tblptr));

push(t, tblptr); push(0, offset)}

D 1 ; S

{t = top(tblptr);

addwidth(t, top(offset));

pop(tblptr); pop(offset);;

enterproc(top(tblptr), id.name, t)}

D id: T

{enter(top(tblptr), id.name, T.type, top(offset));

top(offset) = top (offset) + T.width}

The action for M creates a symbol table for the outermost scope and hence a nil pointer is passed in place of previous. When the declaration, D proc id ; ND1 ; S is processed, the action corresponding to N causes the creation of a symbol table for the procedure; the pointer to symbol table of enclosing procedure is given by top(tblptr). The pointer to the new table is pushed on to the stack tblptr and 0 is pushed as the initial offset on the offset stack. When the actions corresponding to the subtrees of N, D1 and S have been executed, the offset corresponding to the current procedure i.e., top(offset) contains the total width of entries in it. Hence top(offset) is added to the header of symbol table of the current procedure. The top entries of tblptr and offset are popped so that the pointer and offset of the enclosing procedure are now on top of these stacks. The entry for id is added to the symbol table of the enclosing procedure. When the declaration D -> id :T is processed entry for id is created in the symbol table of current procedure. Pointer to the symbol table of current procedure is again obtained from top(tblptr). Offset corresponding to the current procedure i.e. top(offset) is incremented by the width required by type T to point to the next available location.