DESKTOP

Algorithms for Compiler Design: REPRESENTING THE SCOPE INFORMATION IN THE SYMBOL TABLE

7/24/2010 8:06:38 PM
7.7 REPRESENTING THE SCOPE INFORMATION IN THE SYMBOL TABLE
Every name possesses a region of validity within the source program, called the "scope" of that name. The rules governing the scope of names in a block-structured language are as follows:
  1. A name declared within a block B is valid only within B.

  2. If block B1 is nested within B2, then any name that is valid for B2 is also valid for B1, unless the identifier for that name is re-declared in B1.

These scope rules require a more complicated symbol table organization than simply a list of associations between names and attributes. One technique that can be used is to keep multiple symbol tables, one for each active block, such as the block that the compiler is currently in. Each table is list of names and their associated attributes, and the tables are organized into a stack. Whenever a new block is entered, a new empty table is pushed onto the stack for holding the names that are declared as local to this block. And when a declaration is compiled, the table on the stack is searched for a name. If the name is not found, then the new name is inserted. When a reference to a name is translated, each table is searched, starting from the top table on the stack, ensuring compliance with static scope rules. For example, consider following program structure. The symbol table organization will be as shown in Figure 1.

Program main
Var x,y : integer : Procedure P : Var x,a : boolean; Procedure q Var x,y,z : real; Begin . . end begin : end begin : end
Click To expand
Figure 1: Symbol table organization that complies with static scope information rules.

Another technique can be used to represent scope information in the symbol table. We store the nesting depth of each procedure block in the symbol table and use the [procedure name, nesting depth] pair as the key to accessing the information from the table. A nesting depth of a procedure is a number that is obtained by starting with a value of one for the main and adding one to it every time we go from an enclosing to an enclosed procedure. This number is basically a count of how many procedures are there in the referencing environment of the procedure.

For example, refer to the program code structure above. The symbol table's contents are shown in Table 1.

Table 1: Symbol Table Contents Using a Nesting Depth Approach

X

1

real

Y

1

real

Z

1

real

q

3

proc

a

3

Boolean

X

3

Boolean

P

2

proc

Y

2

integer

X

2

integer


Other  
  •  Algorithms for Compiler Design: ACTIVATION OF THE PROCEDURE AND THE ACTIVATION RECORD
  •  Algorithms for Compiler Design: STACK ALLOCATION
  •  Algorithms for Compiler Design: ERROR RECOVERY IN LR PARSING
  •  Algorithms for Compiler Design: PREDICTIVE PARSING ERROR RECOVERY
  •  Algorithms for Compiler Design: LOOP OPTIMIZATION
  •  Algorithms for Compiler Design: ELIMINATING INDUCTION VARIABLES
  •  Algorithms for Compiler Design: ELIMINATING LOCAL COMMON SUBEXPRESSIONS
  •  Algorithms for Compiler Design:
  •  Algorithms for Compiler Design
  •  Algorithms for Compiler Design: THE MACHINE MODEL
  •  Algorithms for Compiler Design: STRAIGHTFORWARD CODE GENERATION
  •  Algorithms for Compiler Design: USING DAG FOR CODE GENERATION
  •  Algorithms for Compiler Design: USING ALGEBRAIC PROPERTIES TO REDUCE THE REGISTER REQUIREMENT
  •  Algorithms for Compiler Design: PEEPHOLE OPTIMIZATION
  •  Algorithms for Compiler Design: IMPLEMENTATION OF THE TRANSLATIONS SPECIFIED BY SYNTAX-DIRECTED DEFINITIONS
  •  Algorithms for Compiler Design: L-ATTRIBUTED DEFINITIONS
  •  Algorithms for Compiler Design: SYNTAX-DIRECTED TRANSLATION SCHEMES
  •  Algorithms for Compiler Design: INTERMEDIATE CODE GENERATION
  •  Algorithms for Compiler Design: REPRESENTING THREE-ADDRESS STATEMENTS
  •  Algorithms for Compiler Design: SYNTAX-DIRECTED TRANSLATION SCHEMES TO SPECIFY THE TRANSLATION OF VARIOUS PROGRAMMING LANGUAGE CONSTRUCTS
  •  
    Top 10
    -  Creating Link-Worthy Content and Link Marketing : Further Refining How Search Engines Judge Links
    -  SharePoint 2010 : Operations Management with the SharePoint Central Administration Tool (part 6)
    -  Maintaining Windows 7 with Backup and Restore (part 1) - Creating a Backup & Restoring Files from a Backup
    -  Parallel Programming : Parallel Loops
    -  Exchange Server 2007 : Work with Remote Domains
    -  Programming Microsoft SQL Server 2005: Using the Data Mining Wizard and Data Mining Designer (part 2) - Creating a Mining Model
    -  ASP.NET AJAX Extensions : Selective Page Updates with Partial Rendering
    -  Cloud Application Architectures : Database Management
    -  AJAX : Updating Progress
    -  SQL Server 2008 : Using @@IDENTITY and NEWID Functions in DML Statements
    Most View
    -  Microsoft XNA Game Studio 3.0 : Text and Computers
    -  ASP.NET 4 in VB 2010 : The Security Controls
    -  Ten Keys to Successful Microsoft Business Intelligence
    -  Programming the Mobile Web : Geolocation and Maps - Showing a Map
    -  jQuery 1.3 : Modifying table appearance (part 2) - Tooltips
    -  ASP.NET 4 : Getting More Advanced with the Entity Framework (part 2) - Updates, Inserts, and Deletes
    -  Silverlight : Build a Download and Playback Progress Bar
    -  Managing Exchange Server 2010 : Archiving and compliancy (part 3) - Discovery
    -  Programming the Mobile Web : Testing and Debugging (part 1) - Remote Labs
    -  Microsoft SQL Server 2005 : Report Management
    -  IUSR and IIS_USRS
    -  Deploying a Public Key Infrastructure with Windows Server 2008 R2
    -  Managing System Properties
    -  Programming .NET Components : Visual Studio 2005 and Security
    -  Programming .NET Security : Programming Asymmetrical Encryption
    -  iPhone Application Development : Working with Text, Keyboards, and Buttons (part 1) - Adding Text Fields
    -  Designing a Windows Server 2008 R2 Active Directory : Understanding AD DS Domain Design
    -  Get Network Card Information
    -  Building Your First Windows Phone 7 Application (part 2) - Using Your First Windows Phone Silverlight Controls
    -  Microsoft XNA Game Studio 3.0 : Writing Your First Program (part 1)