Lp 2

1,474 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,474
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
66
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Lp 2

  1. 1. July 12, 2011 1July 12, 2011 1 Symbol Table Handling Techniques Symbol Table Handling Techniques
  2. 2. July 12, 2011 2 Perspective and Motivation  Symbol table is generally a volatile table because entries are being added continually to and, in some cases, deleted from the table  maintaining a volatile symbol table is more difficult  Every occurrence of an identifier in the program requires interaction with the symbol table  It may consume a major portion of processor time  Different methods:  Linear Search  Tree based method  Hash table method Symbol Table Handling Techniques
  3. 3. Construction and Interaction  The point at which symbol table routines are invoked depends on  Number of Passes  Nature of Passes  Multi pass compiler:  Identifier names are entered during lexical analysis phase in first pass  Parser checks for syntax and generates parse tree in second pass  Attribute values are assigned during semantic analysis and code generation in third pass  Type of variable can be assigned only when it is recognized that a declaration statement is being compiled July 12, 2011 Symbol Table Handling Techniques 3
  4. 4. July 12, 2011 Symbol Table Handling Techniques 4
  5. 5. Construction and Interaction  Combined pass compiler:  Lexical, syntactic and code generation phase are completed in one pass  It is possible for code generation phase to detect that a declaration statement is being compiled before the entire source statement is scanned  Advantage:  Attributes of variables can be entered as soon as they are detected  Only phase that requires symbol table intraction is code generation July 12, 2011 Symbol Table Handling Techniques 5
  6. 6. July 12, 2011 Symbol Table Handling Techniques 6
  7. 7. Symbol Table Contents  List of attributes for a particular variable:  Variable name  Object-code address  Type  Dimension or number of parameters of function  Source line number where variable is declared  Source line numbers where variable is referenced  Link field for listing in alphabetical order July 12, 2011 Symbol Table Handling Techniques 7
  8. 8. July 12, 2011 Symbol Table Handling Techniques 8
  9. 9. Symbol Table Contents  Variable Name:  Must be present to let other phases know which is a particular variable  Major issue is variability of length of name  Two popular approaches  To set a fixed maximum length for variable name  To keep only a descriptor in the variable name field and keep the name in general string area referenced by this descriptor  First approach gives quick table access while the other supports efficient storage of variable names  First approach is inefficient in short named variables while second has slow table access due to referencing July 12, 2011 Symbol Table Handling Techniques 9
  10. 10. July 12, 2011 Symbol Table Handling Techniques 10
  11. 11. Symbol Table Contents  Object code address:  This address dictates the relative location for values of a variable at run time.  Type:  The type attribute is stored in the symbol table when compiling languages having either implicit or explicit data types.  Number of dimensions and parameters:  The number of parameters in a procedure call must also agree with the number used in the procedure heading or declaration.  Cross reference listing:  This listing contains many of the attributes discussed above, plus the source line number at which a variable is declared (If explicitly declared) or first referenced (if implicitly declared) and the source line numbers of all other references to the variable. July 12, 2011 Symbol Table Handling Techniques 11
  12. 12. July 12, 2011 Symbol Table Handling Techniques 12
  13. 13. Operations on Symbol Tables  Two basic operations required to be performed on symbol table are insert and lookup.  Languages that require explicit declarations:  Insertion is required when processing the declaration  If symbol table is ordered alphabetically, then insertion requires lookup to find the location where insertion has to be made. Retrieval is easier as the entries are ordered.  If it is unordered, lookup is not required, but the retrieval becomes expensive.  Languages that require implicit declarations:  If declarations are not explicit and mandatory, any variable reference must be treated as an initial reference  Any variable reference generates a lookup operation followed by an insertion if the variable's name is not found in the symbol table July 12, 2011 Symbol Table Handling Techniques 13
  14. 14. Operations on Symbol Tables  Block Structured Languages:  Two basic operations : set and reset  The set operation is invoked when the beginning of a block is recognized during compilation  The reset operation, is applied when the end of a block is encountered  Upon block entry, the set operation establishes a new sub table (within the symbol table) in which the attributes for the variables declared in the new block can be stored  Upon block exit, the reset operation removes the sub table entries for the variables of the completed block. The removal of these entries reflects the fact that the variables in the terminated block can no longer be referenced July 12, 2011 Symbol Table Handling Techniques 14
  15. 15. July 12, 2011 Symbol Table Handling Techniques 15
  16. 16. Symbol Table Organization for Non Block Structured Languages  Each unit of code is a single module that has no sub modules  The complexity of symbol table operation is determined by average length of search, which is average number of comparisons required to retrieve a record.  The collection of attributes stored in the table for a given variable is called symbol table record.  The name of the variable for which an insertion of lookup has to be performed is called search argument.  Four structures:  Unordered Symbol Table  Ordered Symbol Table  Tree Structured Symbol Tables  Hash Symbol Tables July 12, 2011 Symbol Table Handling Techniques 16
  17. 17. Symbol Table Organization for Non Block Structured Languages: Unordered Symbol Tables  Language with explicit declarations:  Insertion requires that if the variable is newly defined then its attributes have to be entered.  To check for duplicate declarations, a look up of entire table has to be done before insertion  Average length of search is (n+1)/2  Language with implicit declarations:  Insertion must be preceded by lookup operation  If the lookup fails, variable’s attribute must be inserted because they are not present July 12, 2011 Symbol Table Handling Techniques 17
  18. 18. Symbol Table Organization for Non Block Structured Languages: Ordered Symbol Tables  Variables are alphabetically ordered  Insertion requires to lookup where the attribute should be placed  If the search argument is not present in the table, this can be detected in less comparisons as the entries are lexicographically ordered  An alternative method to linear search is binary search July 12, 2011 Symbol Table Handling Techniques 18
  19. 19. July 12, 2011 Symbol Table Handling Techniques 19

×