Representing Scope Information
In the source program, every name possesses a region of validity, called the scope of that name.
The rules in a block-structured language are as follows:
- If a name declared within block B then it will be valid only within B.
- If B1 block is nested within B2 then the name that is valid for block B2 is also valid for B1 unless the name's identifier is re-declared in B1.
- These scope rules need a more complicated organization of symbol table than a list of associations between names and attributes.
- Tables are organized into stack and each table contains the list of names and their associated attributes.
- Whenever a new block is entered then a new table is entered onto the stack. The new table holds the name that is declared as local to this block.
- When the declaration is compiled then the table is searched for a name.
- If the name is not found in the table then the new name is inserted.
- When the name's reference is translated then each table is searched, starting from the each table on the stack.
Fig: Symbol table organization that complies with static scope information rules