The Symbol Table Lecture 13 Wed, Feb 23, 2005. The Symbol Table When identifiers are found, they will be entered into a symbol table, which will hold.

Slides:



Advertisements
Similar presentations
CPSC 388 – Compiler Design and Construction
Advertisements

SYMBOL TABLES &CODE GENERATION FOR EXECUTABLES. SYMBOL TABLES Compilers that produce an executable (or the representation of an executable in object module.
CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.
Semantic Analysis and Symbol Tables
Symbol Table.
An introduction to pointers in c
Module R2 CS450. Next Week R1 is due next Friday ▫Bring manuals in a binder - make sure to have a cover page with group number, module, and date. You.
Lecture 6 Hashing. Motivating Example Want to store a list whose elements are integers between 1 and 5 Will define an array of size 5, and if the list.
Hash Tables Hash function h: search key  [0…B-1]. Buckets are blocks, numbered [0…B-1]. Big idea: If a record with search key K exists, then it must be.
©Silberschatz, Korth and Sudarshan12.1Database System Concepts Chapter 12: Indexing and Hashing Basic Concepts Ordered Indices B+-Tree Index Files B-Tree.
Semantic analysis Parsing only verifies that the program consists of tokens arranged in a syntactically-valid combination, we now move on to semantic analysis,
CPSC Compiler Tutorial 9 Review of Compiler.
Hash Tables Hash function h: search key  [0…B-1]. Buckets are blocks, numbered [0…B-1]. Big idea: If a record with search key K exists, then it must be.
Arrays Liang, Chpt 5. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
1 Chapter 9 Maps and Dictionaries. 2 A basic problem We have to store some records and perform the following: add new record add new record delete record.
External Memory Hashing. Hash Tables Hash function h: search key  [0…B-1]. Buckets are blocks, numbered [0…B-1]. Big idea: If a record with search key.
Tutorial 6 & 7 Symbol Table
Chapter 2 Chang Chi-Chung Lexical Analyzer The tasks of the lexical analyzer:  Remove white space and comments  Encode constants as tokens.
Hash Tables1 Part E Hash Tables  
Environments and Evaluation
Hash Tables1 Part E Hash Tables  
Chapter 2 Chang Chi-Chung Lexical Analyzer The tasks of the lexical analyzer:  Remove white space and comments  Encode constants as tokens.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Cs164 Prof. Bodik, Fall Symbol Tables and Static Checks Lecture 14.
Symbol Table (  ) Contents Map identifiers to the symbol with relevant information about the identifier All information is derived from syntax tree -
Abstract Syntax Trees Lecture 14 Wed, Mar 3, 2004.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
CSc 453 Semantic Analysis Saumya Debray The University of Arizona Tucson.
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
COMPILERS Semantic Analysis hussein suleman uct csc3005h 2006.
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
Lexical Analysis Hira Waseem Lecture
Chapter 6 Programming Languages (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Copyright © 2002, Systems and Computer Engineering, Carleton University Hashtable.ppt * Object-Oriented Software Development Unit 8.
D. M. Akbar Hussain: Department of Software & Media Technology 1 Compiler is tool: which translate notations from one system to another, usually from source.
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.
Compiler (scalac, gcc) Compiler (scalac, gcc) Id3 = 0 while (id3 < 10) { println(“”,id3); id3 = id3 + 1 } Id3 = 0 while (id3 < 10) { println(“”,id3); id3.
___________________________________________ COMPILER Theory___________________________________________ Fourth Year (First Semester) Dr. Hamdy M. Mousa.
CS30003: Compilers Lexical Analysis Lecture Date: 05/08/13 Submission By: DHANJIT DAS, 11CS10012.
Introduction Lecture 1 Wed, Jan 12, The Stages of Compilation Lexical analysis. Syntactic analysis. Semantic analysis. Intermediate code generation.
WEEK 1 Hashing CE222 Dr. Senem Kumova Metin
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.
Muhammad Idrees, Lecturer University of Lahore 1 Top-Down Parsing Top down parsing can be viewed as an attempt to find a leftmost derivation for an input.
Chapter 1 Introduction Major Data Structures in Compiler
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Compiler Construction By: Muhammad Nadeem Edited By: M. Bilal Qureshi.
ENEE150 – 0102 ANDREW GOFFIN Project 4 & Function Pointers.
What am I? while b != 0 if a > b a := a − b else b := b − a return a AST == Abstract Syntax Tree.
1 the hash table. hash table A hash table consists of two major components …
1 Data Structures CSCI 132, Spring 2014 Lecture 33 Hash Tables.
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
CS412/413 Introduction to Compilers Radu Rugina Lecture 11: Symbol Tables 13 Feb 02.
Programming Fundamentals. Today’s Lecture Array Fundamentals Arrays as Class Member Data Arrays of Objects C-Strings The Standard C++ string Class.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
A Simple Syntax-Directed Translator
Java Primer 1: Types, Classes and Operators
Semantic Analysis with Emphasis on Name Analysis
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
External Memory Hashing
Variables Numbers can be stored and retrieved while a program is running if they are given a home. The way that integers and decimal numbers are stored.
Local Variables, Global Variables and Variable Scope
Symbol Table 薛智文 (textbook ch#2.7 and 6.5) 薛智文 96 Spring.
COMPILERS Semantic Analysis
Presentation transcript:

The Symbol Table Lecture 13 Wed, Feb 23, 2005

The Symbol Table When identifiers are found, they will be entered into a symbol table, which will hold all relevant information about identifiers. This information will be used later by the semantic analyzer and the code generator. Lexical Analyzer Semantic Analyzer Code Generator Symbol Table Syntax Analyzer

Symbol Table Entries We will store the following information about identifiers. The name (as a string). The data type. The block level. Its scope (global, local, or parameter). Its offset from the base pointer (for local variables and parameters only).

Symbol Table Entries This information is stored in an object called an IdEntry. This information may not all be known at once. We may begin by knowing only the name and data type, and then later learn the block level, scope, and the offset.

Symbol Table Functions The two most basic symbol-table functions are the ones that insert a new symbol and lookup an old symbol. IdEntry install(String s, int blkLev) IdEntry idLookup(String s, int blkLev)

Inserting a Symbol The install() function will insert a new symbol into the symbol table. Each symbol has a block level. Block level 1 = Keywords. Block level 2 = Global variables. Block level 3 = Parameters and local variables. install() will create an IdEntry object and store it in the table.

Inserting a Symbol When the symbol is first encountered by the lexer, we do not yet know the scope or type. That is determined later by the parser. For example, we could first encounter the symbol count in any of the following contexts. int count; // Global variable int func(int sum, float count); int main() {int count…}

Looking up a Symbol Whenever a symbol is encountered, we must look it up in the symbol table. If it is the first encounter, then idLookup() will return null. If it is not the first encounter, then idLookup() will return a reference to the IdEntry for that identifier found in the table. Once we have the IdEntry object, we may add information to it.

Looking up a Symbol Since a variable should be declared when it first appears, If the parser is parsing a declaration, then it expects idLookup() to return null. If the parser is not parsing a declaration, then it expects idLookup() to return non-null. In each case, anything else is an error.

Block Levels Keywords, global variables, and local variables are stored at different block levels. C and C++ recognize further levels (blocks) within functions, delimited by braces { }. However, in C, variables local to a block must be declared at the beginning of the block. Every time we enter a block, the block level increases by 1 and every time we leave a block, it decreases by 1.

Structure of the Symbol Table We will implement the symbol table as a linked list of hash tables, one hash table for each block level. Level 3Level 1Level 2 Hash table of Locals Hash table of Globals Hash table of Keywords Level 0 null

Structure of the Symbol Table Initially, we create a null hash table at level 0. Level 0 null

Structure of the Symbol Table Then we increase the block level and install the keywords in the symbol table at level 1. Level 1 Hash table of Keywords Level 0 null

Structure of the Symbol Table Then we increase the block level and install the globals at level 2. Level 1Level 2 Hash table of Globals Hash table of Keywords Level 0 null

Structure of the Symbol Table When we enter a function, we create a level 3 hash table and store parameters and local variables there. Level 3Level 1Level 2 Hash table of Locals Hash table of Globals Hash table of Keywords Level 0 null

Structure of the Symbol Table When we leave the function, the hash table of local variables is deleted from the list. Level 1Level 2 Hash table of Globals Hash table of Keywords Level 0 null

Locating a Symbol If we enter another function, a new level 3 hash table is created. Level 3Level 1Level 2 Hash table of Locals Hash table of Globals Hash table of Keywords Level 0 null

Locating a Symbol When we look up an identifier, we begin the search at the head of the list. Level 3Level 1Level 2 Hash table of Locals Hash table of Globals Hash table of Keywords Level 0 null

Locating a Symbol If it is not found there, then the search continues at the lower levels. Level 3Level 1Level 2 Hash table of Locals Hash table of Globals Hash table of Keywords Level 0 null

Locating a Symbol Keywords are found in the level 1 hash table. Level 3Level 1Level 2 Hash table of Locals Hash table of Globals Hash table of Keywords Level 0 null

Looking up a Symbol If an identifier is declared both globally and locally, which one will be found when it is looked up? If an identifier is declared only globally and we are in a function, how will it be found? How do we prevent the use of a keyword as a variable name?

Distinguishing Between Keywords and Identifiers The keywords are installed at level 1 before the lexer begins. When the lexer finds an “identifier,” it looks it up in the symbol table. If it finds it at level 1, it returns the appropriate keyword token. Otherwise, it returns an identifier token.

Distinguishing Between Keywords and Identifiers The benefit of this is that it greatly simplifies the lexer. For example, imagine that a program has identifiers el, els, and elses. The regular expression for true identifiers would be immensely complicated. However, in JLex, we could list the regular expressions of the keywords first, then the regular expression for identifiers.

Hash Tables A hash table is a list in which each member is accessed through a key. The key is used to determine where to store the value in the table. The function that produces a location from the key is called the hash function. For example, if it were a hash table of strings, the hash function might compute the sum of the ASCII values of the first 5 characters of the string, modulo the size of the table.

Hash Tables The numerical value of the hashed key gives the location of the member. Thus, there is no need to search for the member; the hashed key tells where it is located. For example, if the string were "return", then the key would be ( ) % 100 = 62. Thus, "return" would be located in position 62 of the hash table.

Clashes and Buckets Clearly, there is the possibility of a clash: two members have the same hashed key. In that case, the hash table creates a list, called a “bucket,” of those values in the table with that same location. When that location comes up, the list is searched. However, it is generally a very short list, especially if the table size has been chosen well.

Hash Table Efficiency The two parameters that determine how efficiently the hash table performs are The capacity of the table, i.e., the total amount of memory allocated. The number of buckets, or equivalently, the size of a bucket. Clearly, the size of a bucket times the number of buckets equals the capacity of the table.

Hash Table Efficiency For a given hash table capacity, If there are too many buckets, then many buckets will not be used, leading to space inefficiency. If there are too few buckets, then there will be many clashes, causing the searches to degenerate into predominately sequential searches, leading to time inefficiency.

Hash Tables in Java Java has a Hashtable class. Thank you, thank you, thank you. Look it up on the web to see what its member functions are. The Java Hashtable class will use its own hash function to hash the keys. The two most important Hashtable functions (for us) are put() and get().

Hash Tables in Java When we make the function call put(key, value), the Hashtable will Hash key to compute the location. Insert value into the appropriate bucket. Handle any clashes. When we make the function call get(key), the Hashtable will Hash key to compute the location. Retrieve the value with the matching key.

String Tables Compilers generally create a table of strings. These strings are the “names” of the identifiers, keywords, and other strings used in the program. Thus, if the same string is used for several different identifiers, the string will be stored only once in the string table. Each symbol table entry will include a pointer to the string in the string table. For simplicity, we will not use a string table.