Header Ads Widget

Representing Scope Information

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:

  1. If a name declared within block B then it will be valid only within B.
  2. 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.

For example:

  1. int x;  
  2. void f(int m) {  
  3.      float x, y;  
  4. {  
  5.             int i, j;  
  6.             int u, v;  
  7. }  
  8. }  
  9. int g (int n)  
  10. {  
  11.       bool t;  
  12. }  

Representing Scope Information

Fig: Symbol table organization that complies with static scope information rules

Post a Comment

0 Comments