Symbol Table | Compiler Design - Computer Science Engineering (CSE) PDF Download

Symbol table:
A symbol table is a major data structure used in a compiler:  

  • Associates attributes with identifiers used in a program.  
  •  For instance, a type attribute is usually associated with each identifier.   

  A symbol table is a necessary component.  

Definition (declaration) of identifiers appears once in a program 

  • Use of identifiers may appear in many places of the program text 
  • Identifiers and attributes are entered by the analysis phases 
  • When processing a definition (declaration) of an identifier   
  • In simple languages with only global variables and implici t declarations:   

The scanner can enter an identifier into a symbol table if it is not already there 
In block-
structured languages with scopes and explicit declarations:  

The parser and/or semantic analyzer enter identifiers and corresponding attributes  

  • · Symbol table information is used by the analysis and synthesis phases  
  • ·To verify that used identifiers have been defined (declared)   
  • · To verify that expressions and assignments are semantically correct  type checking   
  • · To generate intermediate or target code  

    Symbol Table Interface: 

    The basic operations defined on a symbol table include: allocate – to allocate a new empty symbol table   
     
  • free – to remove all entries and free the storage of a symbol table  
  • insert – to insert a name in a symbol table and return a pointer to its entry  
  •  lookup – to search for a name and return a pointer to its entry  
  • set_attribute – to associate an attribute with a given entry  
  • get_attribute – to get an attribute associated with a given entry  
  •  Other operations can be added depending on requirement    

    For example, a delete operation removes a name previously inserted Some identifiers become invisible (out of scope) after exiting a block  
     
  • This interface provides an abstract view of a symbol table.  
  • Supports the simultaneous existence of multiple tables  
  • Implementation can vary without modifying the interface  

Basic Implementation Techniques:

First consideration is how to insert and lookup names
Variety of implementation techniques

Unordered List 
Simplest to implement
Implemented as an array or a linked list
Linked list can grow dynamically – alleviates problem of a fixed size array
Insertion is fast O(1), but lookup is slow for large tables – O(n) on average

Ordered List
If an array is sorted, it can be searched using binary search – O(log2 n)
Insertion into a sorted array is expensive – O(n) on average
Useful when set of names is known in advance – table of reserved words

Binary Search Tree
Can grow dynamically Insertion and lookup are O(log2 n) on average

 

The document Symbol Table | Compiler Design - Computer Science Engineering (CSE) is a part of the Computer Science Engineering (CSE) Course Compiler Design.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)
26 videos|66 docs|30 tests

Top Courses for Computer Science Engineering (CSE)

FAQs on Symbol Table - Compiler Design - Computer Science Engineering (CSE)

1. What is a symbol table in computer science?
Ans. A symbol table is a data structure used by compilers, interpreters, and other language processing systems to store and retrieve information about symbols (such as variables, functions, classes) in a program. It maps each symbol to its attributes, such as its data type, scope, and memory location.
2. Why is a symbol table important in programming languages?
Ans. A symbol table is important in programming languages because it enables efficient and accurate compilation or interpretation of code. It allows the compiler or interpreter to resolve symbols, such as variables or functions, by associating each symbol with its corresponding attributes. This helps in detecting errors, managing memory, and optimizing the execution of the program.
3. How does a symbol table work in a compiler?
Ans. In a compiler, a symbol table is typically implemented as a hash table or a balanced tree. As the compiler traverses the source code, it scans for symbols and their attributes. When a symbol is encountered, it is added to the symbol table or updated if it already exists. The symbol table is then used during subsequent phases of compilation to resolve symbols and perform necessary operations, such as type checking or code generation.
4. What are the common operations performed on a symbol table?
Ans. Common operations performed on a symbol table include inserting a symbol with its attributes, looking up a symbol to retrieve its attributes, updating the attributes of a symbol, and deleting a symbol from the table. Additionally, symbol tables may support operations like scope management, type checking, and alias resolution.
5. How is a symbol table used in the process of linking?
Ans. In the process of linking, symbol tables play a crucial role in resolving external references between different object files or libraries. During linking, the linker uses the symbol table to match symbols referenced in one object file with their definitions in another object file or library. This allows the linker to correctly connect different parts of the program and generate a complete executable or library. The symbol table helps ensure that all symbols are resolved and their addresses are correctly assigned.
26 videos|66 docs|30 tests
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

mock tests for examination

,

study material

,

Symbol Table | Compiler Design - Computer Science Engineering (CSE)

,

Sample Paper

,

shortcuts and tricks

,

MCQs

,

video lectures

,

ppt

,

Previous Year Questions with Solutions

,

Exam

,

Extra Questions

,

Objective type Questions

,

Symbol Table | Compiler Design - Computer Science Engineering (CSE)

,

Summary

,

practice quizzes

,

Viva Questions

,

Semester Notes

,

past year papers

,

Symbol Table | Compiler Design - Computer Science Engineering (CSE)

,

pdf

,

Free

,

Important questions

;