The basic operations defined on a symbol table include: free to remove all entries and free the storage of a symbol table

Similar documents
Run-time Environments - 2

Symbol Tables. ASU Textbook Chapter 7.6, 6.5 and 6.3. Tsan-sheng Hsu.

Informal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility

Concepts Introduced in Chapter 7

Today's Topics. CISC 458 Winter J.R. Cordy

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

UNIT-4 (COMPILER DESIGN)

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

Compiler Theory. (Semantic Analysis and Run-Time Environments)

Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004

CSE 504. Expression evaluation. Expression Evaluation, Runtime Environments. One possible semantics: Problem:

The role of semantic analysis in a compiler

Programming Language Concepts Scoping. Janyl Jumadinova January 31, 2017

CSE 307: Principles of Programming Languages

G Programming Languages - Fall 2012

! Those values must be stored somewhere! Therefore, variables must somehow be bound. ! How?

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

CA Compiler Construction

Run Time Environment. Procedure Abstraction. The Procedure as a Control Abstraction. The Procedure as a Control Abstraction

CSE 504: Compiler Design. Intermediate Representations Symbol Table

Programming Languages Third Edition. Chapter 7 Basic Semantics

Chapter 5. Names, Bindings, and Scopes

Programming Languages Third Edition. Chapter 10 Control II Procedures and Environments

Special Topics: Programming Languages

Programming Languages

The Compiler So Far. Lexical analysis Detects inputs with illegal tokens. Overview of Semantic Analysis

G Programming Languages - Fall 2012

The Procedure Abstraction

Runtime Environments I. Basilio B. Fraguela

CSE 504: Compiler Design. Runtime Environments

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

When do We Run a Compiler?

Acknowledgement. CS Compiler Design. Semantic Processing. Alternatives for semantic processing. Intro to Semantic Analysis. V.

Symbol Tables. For compile-time efficiency, compilers often use a symbol table: associates lexical names (symbols) with their attributes

The Structure of a Syntax-Directed Compiler


Run Time Environment. Activation Records Procedure Linkage Name Translation and Variable Access

Scope. CSC 4181 Compiler Construction. Static Scope. Static Scope Rules. Closest Nested Scope Rule

Chapter 3:: Names, Scopes, and Bindings (cont.)

Chapter 3:: Names, Scopes, and Bindings (cont.)

Run Time Environments

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

Symbol Table Information. Symbol Tables. Symbol table organization. Hash Tables. What kind of information might the compiler need?

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

Module 27 Switch-case statements and Run-time storage management

Run-time Environments

Run-time Environments

5. Semantic Analysis. Mircea Lungu Oscar Nierstrasz

UNIT 3

CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11

Principles of Compiler Design

Procedure and Object- Oriented Abstraction

COMPILER CONSTRUCTION LAB 2 THE SYMBOL TABLE. Tutorial 2 LABS. PHASES OF A COMPILER Source Program. Lab 2 Symbol table

Example. program sort; var a : array[0..10] of integer; procedure readarray; : function partition (y, z :integer) :integer; var i, j,x, v :integer; :

The Procedure Abstraction Part I: Basics

Chapter 9 Subprograms

Procedure and Function Calls, Part II. Comp 412 COMP 412 FALL Chapter 6 in EaC2e. target code. source code Front End Optimizer Back End

Scope. COMP 524: Programming Language Concepts Björn B. Brandenburg. The University of North Carolina at Chapel Hill

Separate compilation. Topic 6: Runtime Environments p.1/21. CS 526 Topic 6: Runtime Environments The linkage convention

CS 314 Principles of Programming Languages. Lecture 13

CS4215 Programming Language Implementation

Chapter 5 Names, Binding, Type Checking and Scopes

Scope, Functions, and Storage Management

Chapter 5 Names, Bindings, Type Checking, and Scopes

Names, Scopes, and Bindings II. Hwansoo Han

Attributes of Variable. Lecture 13: Run-Time Storage Management. Lifetime. Value & Location

Run-Time Environments

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

Run-Time Environements

Compilers. 8. Run-time Support. Laszlo Böszörmenyi Compilers Run-time - 1

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILERS

Static Semantics. Winter /3/ Hal Perkins & UW CSE I-1

Names, Scopes, and Bindings. CSE 307 Principles of Programming Languages Stony Brook University

Run-time Environments - 3

THEORY OF COMPILATION

CSE 3302 Notes 5: Memory Management

Programming Languages, Summary CSC419; Odelia Schwartz

CS558 Programming Languages

Compiler Construction

Names, Scopes, and Bindings. CSE 307 Principles of Programming Languages Stony Brook University

Compiler Construction

Intermediate Representations & Symbol Tables

Compilation /15a Lecture 7. Activation Records Noam Rinetzky

PROCESS STATES AND TRANSITIONS:

PSD1C SYSTEM SOFTWAE UNIT: I - V PSD1C SYSTEM SOFTWARE

What about Object-Oriented Languages?

CS 480 Fall Runtime Environments. Mike Lam, Professor. (a.k.a. procedure calls and heap management)

Lecture 13: Complex Types and Garbage Collection

CA Compiler Construction

CSC 533: Organization of Programming Languages. Spring 2005

5. Semantic Analysis!

Announcements. Scope, Function Calls and Storage Management. Block Structured Languages. Topics. Examples. Simplified Machine Model.

Implementing Subprograms

An Overview to Compiler Design. 2008/2/14 \course\cpeg421-08s\topic-1a.ppt 1

Memory Allocation. Static Allocation. Dynamic Allocation. Dynamic Storage Allocation. CS 414: Operating Systems Spring 2008

CS558 Programming Languages

Compilers and Code Optimization EDOARDO FUSELLA

Heap, Variables, References, and Garbage. CS152. Chris Pollett. Oct. 13, 2008.

Transcription:

SYMBOL TABLE: A symbol table is a data structure used by a language translator such as a compiler or interpreter, where each identifier in a program's source code is associated with information relating to its declaration or appearance in the source, such as its type, scope level and sometimes its location. A symbol table is a major data structure used in a compiler which associates attributes with identifiers in a program. A 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. 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. Possible Implementations Techniques: Unordered list: for a very small set of variables. Ordered linear list: insertion is expensive, but implementation is relatively easy Binary search tree: O(log n) time per operation for n variables. Hash table: most commonly used, and very efficient provided the memory space is adequately larger than the number of variables. A common implementation technique is to use a hash table. A compiler may use one large symbol table for all symbols or use separated hierarchical symbol tables for different scopes. T.M. JAYA KRISHNA, M.Tech Assistant Professor CSE Dept. COMPILER DESIGN P a g e 1

There are also trees, linear lists and self-organizing lists which can be used to implement symbol table. It also simplifies the classification of literals in tabular format. The symbol table is accessed by most phases of a compiler, beginning with the lexical analysis to optimization. Implementation of Symbol Tables: There are many ways to implement symbol tables, but the most important distinction between these is how scopes are handled. This may be done using a persistent (or functional) data structure, or it may be done using an imperative (or destructively-updated) data structure. A persistent data structure has the property that no operation on the structure will destroy it. Conceptually, a new modified copy is made of the data structure whenever an operation updates it, hence preserving the old structure unchanged. In the imperative approach, only one copy of the symbol table exists, so explicit actions are required to store the information needed to restore the symbol table to a previous state. This can be done by using an auxiliary stack. When an update is made, the old binding of a name that is overwritten is recorded (pushed) on the auxiliary stack. When a new scope is entered, a marker is pushed on the auxiliary stack. When the scope is exited, the bindings on the auxiliary stack (down to the marker) are used to reestablish the old symbol table. The bindings and the marker are popped off the auxiliary stack in the process, returning the auxiliary stack to the state it was in before the scope was entered. Uses: An object file will contain a symbol table of the identifiers it contains that are externally visible. During the linking of different object files, a linker will use these symbol tables to resolve any unresolved references. A symbol table may only exist during the translation process, or it may be embedded in the output of that process for later exploitation, for example, during an interactive debugging session, or as a resource for formatting a diagnostic report during or after execution of a program. While reverse engineering an executable, many tools refer to the symbol table to check what addresses have been assigned to global variables and known functions. If the symbol table has been stripped or cleaned out before being converted into an executable, tools will find it harder to determine addresses or understand anything about the program. At that time of accessing variables and allocating memory dynamically, a compiler should perform many works and as such the extended stack model requires the symbol table. T.M. JAYA KRISHNA, M.Tech Assistant Professor CSE Dept. COMPILER DESIGN P a g e 2

SYMBOL TABLE FORMAT: Symbol table is an environment that stores information about identifiers, a data structure that captures scope information. Each entry in symbol table contains: Name, Kind, Type and additional properties. Symbol table contains the following format: Symbol Kind Type Properties Scope nesting mirrored hierarchy of symbol tables is given as below: Symbol table example: Consider the following program: T.M. JAYA KRISHNA, M.Tech Assistant Professor CSE Dept. COMPILER DESIGN P a g e 3

TREE STRUCTURE REPRESENTATION OF SCOPE INFORMATION: Scope information characterizes the declaration of identifiers and the portions of the program where use of each identifier is allowed, Example identifiers: variables, functions, objects, labels. Semantic Rules for Scopes: Main rules regarding scopes are; 1) Use an identifier only if defined in enclosing scope; 2) Do not declare identifiers of the same kind with identical names more than once in the same lexical scope. When the scope information is presented in hierarchical manner then it forms a tree structure representation. To overcome the drawback of the sequential representation being slow in searching the desired identifier, this representation is been designed. The representation of scope information in the symbol table is given in the example below: T.M. JAYA KRISHNA, M.Tech Assistant Professor CSE Dept. COMPILER DESIGN P a g e 4

The hierarchical structure of symbol tables automatically solves the problem of resolving name collisions (identifiers with the same name and overlapping scopes). BLOCK STRUCTURES AND NON BLOCK STRUCTURE STORAGE ALLOCATION: Block structured symbol tables are designed to support the scoping rules of block structured languages. In general the Storage allocation can be done for two types of data variables: 1) Local Data 2) Non-Local Data The local data can be handled using activation record where as non local data can be handled using scope information. The Block structured storage allocation can be done using static scope or lexical scope. The Non-Block structured storage allocation can be done using dynamic scope. Activation Record: Every execution of a procedure is called as Activation. It is a block of memory used for managing information needed by single execution of a procedure. FORTRAN uses static data area to store the activation record. In PASCAL and C, activation record is situated in stack area. Contents of activation record are as shown in figure given as follows: T.M. JAYA KRISHNA, M.Tech Assistant Professor CSE Dept. COMPILER DESIGN P a g e 5

Actual Parameter: - It points to activation record of calling procedure. Saved Machine Status: - Access Link: - It holds information regarding status of machine before the procedure is called. It refers to non local data in other activation record. Procedure calls and returns are usually managed by a runtime stack called control stack. Each live activation has an activation record (some times called frame) on the control stack, with the root of the activation tree at the bottom. Static Scope:- one of the basic reason for scoping is to keep variables in different parts of the program distinct from one another. With static scoping, a variable always refers to its top-level environment. A language uses static or lexical scope if it is possible to find the scope of a declaration by looking only at the program. A global identifier refers to the identifiers with the name i.e. declared in the closest enclosing scope of the program. It maintains static relationship between the blocks in the program text. T.M. JAYA KRISHNA, M.Tech Assistant Professor CSE Dept. COMPILER DESIGN P a g e 6

Dynamic scope:- A global identifier refers to the identifier associated with the most recent activation record. It uses the actual sequence of calls that are executed in the dynamic execution of the program. Both static and dynamic scope are identical when local variables are concerned. E.g. int x = 1; function g(z) = x + z; function f(y) = { int x = y + 1; return g(y*x) }; f(3); After call to g, static scope x = 1 and dynamic scope x = 4 Static Storage Allocation:- In static allocation, compiler makes the decision regarding storage allocation by looking only at the program text. Compiler allocates space for all variables (local and global) of all procedures at compile time. As it does not use stack/heap allocation, there will be no overheads. Variable access is fast since addresses are known at compile time. No recursion. Dynamic Storage Allocation: In dynamic storage allocation, decisions, Compiler allocates space only for global variables at compile time. Space for variables of procedures will be allocated at runtime. It uses stack / heap allocation. E.g. C, C++, Java, FORTRAN 8/9 Variable access is slow (compared to static allocation) since addresses are accessed through the stack / heap pointer. Recursion can be implemented. T.M. JAYA KRISHNA, M.Tech Assistant Professor CSE Dept. COMPILER DESIGN P a g e 7