Symbol Table.

Slides:



Advertisements
Similar presentations
Chap 8 Symbol Table name attributes : :
Advertisements

CPSC 388 – Compiler Design and Construction
1 Symbol Tables. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
Intermediate Code Generation
1 Mooly Sagiv and Greta Yorsh School of Computer Science Tel-Aviv University Modern Compiler Design.
1 Compiler Construction Intermediate Code Generation.
Quick Review of Apr 10 material B+-Tree File Organization –similar to B+-tree index –leaf nodes store records, not pointers to records stored in an original.
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison- Wesley. All rights reserved. 1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
The Symbol Table Lecture 13 Wed, Feb 23, The Symbol Table When identifiers are found, they will be entered into a symbol table, which will hold.
ISBN Chapter 10 Implementing Subprograms.
Syntax Trees MathWorks Compiler Course – Day 5. Syntax Trees MathWorks Compiler Course – Day 5 Parser lexemes shift/reduce seq. Cfg tables Tree Symbols.
Lecture 11 March 5 Goals: hashing dictionary operations general idea of hashing hash functions chaining closed hashing.
Tutorial 6 & 7 Symbol Table
Semantic analysis Enforce context-dependent language rules that are not reflected in the BNF, e.g.a function must have a return statement. Decorate AST.
Context-sensitive Analysis, II Ad-hoc syntax-directed translation, Symbol Tables, andTypes.
Chapter 9: Subprogram Control
1 ES 314 Advanced Programming Lec 2 Sept 3 Goals: Complete the discussion of problem Review of C++ Object-oriented design Arrays and pointers.
Building An Interpreter After having done all of the analysis, it’s possible to run the program directly rather than compile it … and it may be worth it.
Cs164 Prof. Bodik, Fall Symbol Tables and Static Checks Lecture 14.
Chapter 61 Chapter 6 Index Structures for Files. Chapter 62 Indexes Indexes are additional auxiliary access structures with typically provide either faster.
Symbol Table (  ) Contents Map identifiers to the symbol with relevant information about the identifier All information is derived from syntax tree -
國立台灣大學 資訊工程學系 薛智文 98 Spring Symbol Table (textbook ch#2.7 and 6.5 )
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
Database Management 8. course. Query types Equality query – Each field has to be equal to a constant Range query – Not all the fields have to be equal.
COMPILERS Semantic Analysis hussein suleman uct csc3005h 2006.
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
Names and Scope. Scope Suppose that a name is used many times for different entities in text of the program, or in the course of execution. When the name.
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
1 Symbol Tables The symbol table contains information about –variables –functions –class names –type names –temporary variables –etc.
COMPILERS Symbol Tables hussein suleman uct csc3003s 2007.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Basic Semantics Associating meaning with language entities.
CS 153: Concepts of Compiler Design October 5 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
ISBN Chapter 10 Implementing Subprograms.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Run-Time Environments How do we allocate the space for the generated target code and the data object.
Introduction to Code Generation and Intermediate Representations
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
Lecture by: Prof. Pooja Vaishnav.  Language Processor implementations are highly influenced by the kind of storage structure used for program variables.
Chapter 1 Introduction Major Data Structures in Compiler
Review: Syntax directed translation. –Translation is done according to the parse tree. Each production (when used in the parsing) is a sub- structure of.
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
Bernd Fischer RW713: Compiler and Software Language Engineering.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
10-1 Chapter 10: Implementing Subprograms The General Semantics of Calls and Returns Implementing “Simple” Subprograms Implementing Subprograms with Stack-Dynamic.
CS412/413 Introduction to Compilers Radu Rugina Lecture 11: Symbol Tables 13 Feb 02.
1 Semantic Analysis  Semantic analysis includes  Dynamic Checking (Those checks for which to perform, compiler doesn’t have sufficient information) 
Chapter 5 Record Storage and Primary File Organizations
1 Compiler Construction Run-time Environments,. 2 Run-Time Environments (Chapter 7) Continued: Access to No-local Names.
ISBN Chapter 10 Implementing Subprograms.
©2004 Joel Jones 1 CS 403: Programming Languages Lecture 3 Fall 2004 Department of Computer Science University of Alabama Joel Jones.
Code Generation Instruction Selection Higher level instruction -> Low level instruction Register Allocation Which register to assign to hold which items?
COMPILERS Semantic Analysis hussein suleman uct csc3003s 2009.
Lecture 9 Symbol Table and Attributed Grammars
Names and Attributes Names are a key programming language feature
Context-Sensitive Analysis
Constructing Precedence Table
Compiler Construction (CS-636)
Semantic Analysis with Emphasis on Name Analysis
Semantic Analysis Semantic analysis includes
2018, Spring Pusan National University Ki-Joune Li
UNIT V Run Time Environments.
Symbol Table 薛智文 (textbook ch#2.7 and 6.5) 薛智文 96 Spring.
COMPILERS Semantic Analysis
SYMBOL TABLE Chuen-Liang Chen Department of Computer Science
RUN-TIME STORAGE Chuen-Liang Chen Department of Computer Science
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Presentation transcript:

Symbol Table

Role of Symbol Table Essential data structure for compiler Used for storing information about identifiers appearing in a source program Lexical Analyzer and Parser fill up symbol table Code generator and optimizer make use of symbol table Entities stored in a symbol table Variables, procedures, functions, defined constants, labels, structures, file identifications, compiler generated temporaries

Information in symbol table Name: May be stored directly in the table or the entry may point to another character string, possibly in an associated string table Type: type of identifier Whether variable / function / procedure name For variables, identify whether integer / real / array … Location: offset in the program where the identifier is defined Scope: identifies the region of the program in which the current symbol definition is valid Other attributes: Array limits, record fields / parameters / return values of functions

Usage of symbol table information Semantic Analysis: check correct semantic usage of language constructs May need checking types of identifiers Code generation: All program variables and temporaries need to be allocated some memory locations Symbol table provides information regarding memory size required for identifiers by their types Error Detection: Leave variables undefined Optimization: To reduce the total number of variables used in a program we need to reuse the temporaries generated by the compiler

Features of symbol tables Insert Delete Lookup Modify

Symbol Table Design Format of symbol table entries Access mechanism Linear Lists / arrays / trees…. Access mechanism Linear search / hashing / binary search .. Location of storage Primary Memory (RAM) / Secondary Memory (large symbol table) Scope issues Simple symbol table with nested scope Scoped symbol table with nested scopes

Simple symbol table: Operations Enter a new symbol into the table Lookup for a symbol Modify information about a symbol stored earlier

Simple Symbol Table Formats Linear Table Ordered List Tree Hash table

Linear Table int x, y; float z; ….. procedure abc L1:…

Linear Table Name Type Location x y z Abc L1 int float procedure label Offset of x Offset of y Offset of abc Offset of L1 Insert, Lookup and modify operations take O (n) time, n being the number of identifiers Insertion can be made in O (1) be remembering the pointer to the next free position

Ordered List Variation of linear table List may be sorted and a binary search may be used for access in O (log n) Insertion needs to be done at proper place to preserve the sorted nature Self-organizing list: Dynamically re-arrange list based on recency of reference

Tree Symbol Table Each entry is used represented as a node in a tree Based on string comparison of names, entries lesser than a reference node are kept in the left subtree and entries greater than a reference node are kept in the right subtree Average lookup time is O (log n) AVL trees may be used

Tree symbol table example abc y L1 z

Hash table symbol table Used in cases where access time is to be minimized Most common method implementing Symbol Table in compilers Hash function used to map identifier names to hash table locations, organized as an array To store a symbol table into the table, hash function is applied which results in the unique location in the table Symbol along with associated information stored Accessed using the hash function [O (1) time] Problem of hash tables Imperfect hash function – Collision Collision resolution (Chaining …)

Hash function properties Should depend on name of symbol Easy and quick to compute Should be uniform in mapping names to different parts of the table Computed value must always be within table range

Scoped Symbol Table Global Scope File-wide scope Global Variables File-wide scope Modules in more than one file (static variables / functions) Local scope within a procedure Function local variables Local scope within a block

Scoping rules Static or lexical scoping Dynamic or runtime scoping Procedure P1 is callable from P2 and P3 P1 has reference to a non-local variable x There exist two different definitions of x, one each in P2 and P3 When P1 is called from P2, x will refer to the definition in P2, while when called from P3, it will refer to the definition in P3

Nested Lexical Scoping Procedure P1 ….. Procedure P2 End Procedure Procedure P3 …. Procedure P4 x =

Scope resolution rule If a name is defined in more than one scope, the innermost definition closest to the reference is to be used to interpret the reference to that name When a scope is exited, all variables declared in that scope are deleted from the symbol table

Scope symbol table implementation One table per scope One table for all scopes Operations on symbol tables Insert: Insert a new symbol in current scope Delete Modify Create: Create a new scope Lookup

One table per scope List Tree Hash tables

One symbol table for all scopes Lists Trees Hash tables

Reference Compiler Design: Santanu Chattopadhay