Tutorial 6 & 7 Symbol Table

Slides:



Advertisements
Similar presentations
CPSC 388 – Compiler Design and Construction
Advertisements

Semantic Analysis and Symbol Tables
Symbol Table.
Intermediate Code Generation
CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
Programming Languages Marjan Sirjani 2 2. Language Design Issues Design to Run efficiently : early languages Easy to write correctly : new languages.
Subprogram Control - Data sharing Mechanisms to exchange data Arguments - data objects sent to a subprogram to be processed. Obtained through  parameters.
Compiler Principle and Technology Prof. Dongming LU Mar. 28th, 2014.
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
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.
CPSC Compiler Tutorial 9 Review of Compiler.
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
3/17/2008Prof. Hilfinger CS 164 Lecture 231 Run-time organization Lecture 23.
Run time vs. Compile time
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.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Chapter 9: Subprogram Control
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
Cs164 Prof. Bodik, Fall Symbol Tables and Static Checks Lecture 14.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
ITEC 352 Lecture 11 ISA - CPU. ISA (2) Review Questions? HW 2 due on Friday ISA –Machine language –Buses –Memory.
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
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.
Compiler Construction
1 Names, Scopes and Bindings Aaron Bloomfield CS 415 Fall
1 Scope Rules (Section 3.3) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael Scott.
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.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
1 Scope Scope describes the region where an identifier is known, and semantic rules for this.
COMPUTER PROGRAMMING. A Typical C++ Environment Phases of C++ Programs: 1- Edit 2- Preprocess 3- Compile 4- Link 5- Load 6- Execute Loader Primary Memory.
CS 153: Concepts of Compiler Design October 5 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
MD – Object Model Domain eSales Checker Presentation Régis Elling 26 th October 2005.
Runtime Organization (Chapter 6) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
Contextual Analysis (Chapter 5) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
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
Comp 249 Programming Methodology Chapter 15 Linked Data Structure – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Lecture 10: Modular Programming (functions) B Burlingame 13 April 2015.
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
Bernd Fischer RW713: Compiler and Software Language Engineering.
7. Runtime Environments Zhang Zhizheng
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
10-1 Chapter 10: Implementing Subprograms The General Semantics of Calls and Returns Implementing “Simple” Subprograms Implementing Subprograms with Stack-Dynamic.
Implementing Subprograms
Chapter – 8 Software Tools.
1 Compiler Construction Run-time Environments,. 2 Run-Time Environments (Chapter 7) Continued: Access to No-local Names.
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
©2004 Joel Jones 1 CS 403: Programming Languages Lecture 3 Fall 2004 Department of Computer Science University of Alabama Joel Jones.
Semantic Analysis. Find 6 problems with this code. These issues go beyond syntax.
COMPILERS Semantic Analysis hussein suleman uct csc3003s 2009.
Advanced Programming in C
Name Spaces: ALL versus OOL
CS 326 Programming Languages, Concepts and Implementation
Semantic Analysis with Emphasis on Name Analysis
Syntax Errors; Static Semantics
Names, Binding, and Scope
CMPE 152: Compiler Design October 4 Class Meeting
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
UNIT V Run Time Environments.
Symbol Table 薛智文 (textbook ch#2.7 and 6.5) 薛智文 96 Spring.
COMPILERS Semantic Analysis
Presentation transcript:

Tutorial 6 & 7 Symbol Table CPSC 325 - Compiler Tutorial 6 & 7 Symbol Table

What is Symbol Table?

Why Do We Need a Symbol Table? Bind names to program entities (e.g., variables, procedures). set of possible values, i.e. their type. storage associated with names, i.e., where they are stored. visibility, i.e., where they can be used. Usage of Symbol Table: verification (e.g., number and type of procedure arguments). code generation (e.g., types and instructions). debugging (e.g., memory - symbol association).

What Symbol table does? Given a declaration of a name, is there already a declaration of the same name in the current scope. (i.e., is it multiply declared?) Given a use of a name, to which declaration does it correspond (using the "most closely nested" rule), or is it undeclared?

Attention!!! Generally, symbol table is only needed to the two purposes of last slide; in other words, once all declarations have been processed to build the symbol table, and all uses have been processed to link each ID node in the abstract-syntax tree with the corresponding symbol-table entry, then the symbol table itself is no longer needed because no more lookups based on name will be performed.

Symbol Table Structure Associate Attributes with Symbols: Type Name Storage Class Scope, Visibility and Lifetimes Structure and Attributes Depend on Language Features: PASCAL allows Nested Lexical Scoping. Lisp allows Dynamic Scoping.

Scope, Visibility and Lifetime Scope: Unit of Static program structure that may have one or more variables declared in it. Visibility: Refers to what scopes a given variable’s name refers to a particular instance of that variable. Lifetime: Execution period from the point when a variable first becomes visible until it is last visible.

Scope, Visibility and Lifetime (cont.) Prescribe: Scope, Visibility and Lifetime of Symbols Global: Visible throughout the entire program execution; lifetime encompasses whole execution. Functions, files: Visible in all of a given file; lifetime encompasses the entire execution. Local: Visible and live with activation of a scope. Modifiers: How Values Can be Changed and Retained. Static: Retains Values across Lifetime Boundaries.

Assumptions In this tutorial, I assume: Use static scoping Require that all names be declared before they are used. Do not allow multiple declarations of a name in the same scope Even for different kinds of type Do allow the same name to be declared in multiple nested scope But only once per scope

What operations do we need? Given the above assumptions, we will need: Look up a name in the current scope - only to check if it is multiply declared Look up a name in the current and enclosing scopes to check for a use of an undeclared name, and to link a use with the corresponding symbol-table entry Insert a new name into the symbol table with its attributes Do what must be done when a new scope is entered Do what must be done when a scope is exited

Possible implementation a list of tables a table of lists My combination way For each approach, we will consider what must be done when entering and exiting a scope, when processing a declaration, and when processing a use Simplification: assume each symbol-table entry includes only: the symbol name its type the nesting level of its declaration and the location

Approach 1: List of Hashtables The idea: Symbol table = a list of hashtables One hashtable for each currently visible scope. When processing a scope S:

Example:

Example 2:

Symbol Table for Example 2:

Operations On scope entry: To process a declaration of x: Increment the current level number and add a new empty hashtable to the front of the list To process a declaration of x: Look up x in the first table in the list If it is there, then issue a “multiply declared variable” error; Otherwise, add x to the first table in the list.

Operations (cont.) To process a use of x: On scope exit, Look up x starting in the first table in the list; If it is not there, then look up x in each successive table in the list. (Note: sometime just look into the global table) If it is not in any table then issue an “undeclared variable” error On scope exit, Remove the first table from the list and decrement the current level number

Inserting Method/Function Names Method names belong in the hashtable for the outermost scope Not in the same table as the method’s variables For example, in the previous example Method name f is in the symbol table for the outermost scope Name f is not in the same scope as parameters a and b, and variable x This is so that when the use of name f in method g is processed, the name is found in an enclosing scope’s table.

End of Tutorial 6 Part 1   END

Approach 2: Hash Table of Lists The idea: When processing a scope S, the structure of the symbol table is:

Definition In this approach, there is only ONE big hashtable, which containing an entry for each variable for which there is Some declration in scope S or In a scope that enclose S Associated with each variable is a list of symbol-table entries The first list item corresponds to the most closely enclosing declaration; The other list items correspond to declarations in enclosing scopes.

Example

Nesting level information is crucial The level-number attribute stored in each list item enables us to determine whether the most closely enclosing declaration was made In the current scope or In an enclosing scope

Hash Table of List: Operations On scope entry: Increment the current level number To process a declaration of x: Look up x in the symbol table If x is there, fetch the level number from the first list item. If that level number = the current level then issue a “multiply declared variable” error; Otherwise, add a new item to the front of the list with the appropriate type and the current level number

Hash Table of List: Operations (cont.) To process a use of x: Look up x in the symbol table If it is not there, then issue an “undeclared variable” error. On scope exit: Scan all entries in the symbol table, looking at the first item on each list If that items’ level number = the current number, then remove it from its list (and if the list becomes empty, remove the entire symbol-table entry) Finally, decrement the current level number

Inserting Method/Function Names The method/functions will be treated exactly same way as the variables. From the example, we can easily see it.

Approach 3: my approach Combination of Lazy Approach 2 and some approach 1. Use the Hash table of the list algorithm, but there is not deletion of tables/variables. Refer to my sample output for example

Operations Insert – which insert the appropriate information into the hash table. Look up – which look up for the symbol and return the memory location LookupDecl – returns the tree node corresponding to the declaration of the given variable, or return NULL if not found incScope/decScope – increment and decrement of the scope count setMemLoc – set the memory location of the variable

Information stored Necessary Optional (but always good to have) Name Type Memory location Scope Optional (but always good to have) Scope name indicate which function the variable is in. (For debugging propose) Line Number (also for debugging propose) Date Type – variable, function, etc.

Conclusion Symbol table is one of the major component of compiler. A program without symbol table, can only be called as interpreter or converter, but not a “complete” program compiler. Symbol table is not that complicated. Once the implementation algorithm and concept is clear, it is easy to write a program which create a symbol table.

Graphical breakdown Name Set memory Location Memory Location Insert (with build in filter) Scope Scope function (increase/decrease) Type Look up Other information … Other functions … Symbol Table