8. Symbol Table Chih-Hung Wang

Slides:



Advertisements
Similar presentations
Compilers Course 379K, TTH 9:30-11:00 Instructor: Dr. Doron A. Peled Office Hours: Mon 11:00-12:00.
Advertisements

CPSC 388 – Compiler Design and Construction
SYMBOL TABLES &CODE GENERATION FOR EXECUTABLES. SYMBOL TABLES Compilers that produce an executable (or the representation of an executable in object module.
1 Symbol Tables. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
Symbol Table.
Semantic analysis Parsing only verifies that the program consists of tokens arranged in a syntactically-valid combination, we now move on to semantic analysis,
Reference Book: Modern Compiler Design by Grune, Bal, Jacobs and Langendoen Wiley 2000.
Environments and Evaluation
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 531 Compiler Construction Ch.1 Spring 2010 Marco Valtorta
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
CPSC 388 – Compiler Design and Construction Lecture: MWF 11:00am-12:20pm, Room 106 Colton.
Compiler course 1. Introduction. Outline Scope of the course Disciplines involved in it Abstract view for a compiler Front-end and back-end tasks Modules.
PART I SISTEM UTILITIES Lecture 6 Compilers Ştefan Stăncescu 1.
Chih-Hung Wang Chapter 0: Introduction 參考書目 Leland L. Beck, System Software: An Introduction to Systems Programming (3rd), Addison-Wesley,
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
Compiler design Lecture 1: Compiler Overview Sulaimany University 2 Oct
1. 2 Preface In the time since the 1986 edition of this book, the world of compiler design has changed significantly 3.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
1 Compiler Design (40-414)  Main Text Book: Compilers: Principles, Techniques & Tools, 2 nd ed., Aho, Lam, Sethi, and Ullman, 2007  Evaluation:  Midterm.
1 Using Yacc. 2 Introduction Grammar –CFG –Recursive Rules Shift/Reduce Parsing –See Figure 3-2. –LALR(1) –What Yacc Cannot Parse It cannot deal with.
. n COMPILERS n n AND n n INTERPRETERS. -Compilers nA compiler is a program thatt reads a program written in one language - the source language- and translates.
Chapter 1 Introduction Major Data Structures in Compiler
Course Overview for Compilers J. H. Wang Sep. 14, 2015.
4. Bottom-up Parsing Chih-Hung Wang
Course Overview for Compilers J. H. Wang Sep. 20, 2011.
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.
CSC 8505 Compiler Construction
Programming Fundamentals Enumerations and Functions.
©2004 Joel Jones 1 CS 403: Programming Languages Lecture 3 Fall 2004 Department of Computer Science University of Alabama Joel Jones.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture Ahmed Ezzat.
7. Symbol Table Chih-Hung Wang Compilers References 1. C. N. Fischer and R. J. LeBlanc. Crafting a Compiler with C. Pearson Education Inc., D.
Lecture 12 Intermediate Code Generation Translating Expressions
Lecture 7 Syntax Analysis (5) Operator-Precedence Parsing
Implementing Subprograms
Lecture 9 Symbol Table and Attributed Grammars
System Software Theory (5KS03).
Compiler Design (40-414) Main Text Book:
Introduction Chapter : Introduction.
Chapter 1 Introduction.
Context-Sensitive Analysis
Constructing Precedence Table
Compiler Construction (CS-636)
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Chapter 1 Introduction.
Parsing with Context Free Grammars
Chapter 1: Introduction to Compiling (Cont.)
Compiler Lecture 1 CS510.
CS416 Compiler Design lec00-outline September 19, 2018
Introduction to Compiler Construction
Course supervisor: Lubna Siddiqui
Compiler Design 4. Language Grammars
Implementing Subprograms
Introduction CI612 Compiler Design CI612 Compiler Design.
CMPE 152: Compiler Design October 4 Class Meeting
Compilers B V Sai Aravind (11CS10008).
Subject: Language Processor
3. Formal Grammars and and Top-down Parsing Chih-Hung Wang
Compiler Structures 0. Preliminaries
CS416 Compiler Design lec00-outline February 23, 2019
5. Bottom-Up Parsing Chih-Hung Wang
Introduction to Compiler Construction
CISC 7120X Programming Languages and Compilers
CMPE 152: Compiler Design February 7 Class Meeting
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Lec00-outline May 18, 2019 Compiler Design CS416 Compiler Design.
Introduction Chapter : Introduction.
Implementing Subprograms
Compiler Design 3. Lexical Analyzer, Flex
Presentation transcript:

8. Symbol Table Chih-Hung Wang Compilers 8. Symbol Table Chih-Hung Wang References 1. C. N. Fischer, R. K. Cytron and R. J. LeBlanc. Crafting a Compiler. Pearson Education Inc., 2010. 2. D. Grune, H. Bal, C. Jacobs, and K. Langendoen. Modern Compiler Design. John Wiley & Sons, 2000. 3. Alfred V. Aho, Ravi Sethi, and Jeffrey D. Ullman. Compilers: Principles, Techniques, and Tools. Addison-Wesley, 1986. (2nd Ed. 2006)

Symbol Table In any case, the symbol table is a useful abstraction to aid the compiler to ascertain and verify the semantics, or meaning, of a piece of code. It will keep track of the names, types, locations and properties of the symbols encountered in the program. It makes the compiler more efficient, since the file doesn’t need to be re-parsed to discover previously processed information.

What Should be Stored? Constants Variables Types (user defined) Sub-programs Classes Inheritance Arrays Records Module

Common Symbol Table

Complex Type

Symbol Table Organization (1) 1) Find a record by name (e.g. access a variable as in "x = 5") 2) Find a record by name in a specific scope (e.g. access a field of a record as in "a.x = 5") 3) Find a record by its relationship with another record (e.g. get the type of a variable or the parameters of a function) 4) Insert a new record 5) Update an existing record

Symbol Table Organization (2) We can split the symbol table implementation into two layers where each layer represents a certain data abstraction. The bottom layer is an associative array, and is concerned with storing records and retrieving records by name. The top layer organizes the records into groups and determines how the scoping of the language is maintained.

Symbol Table Organization (3) A library

Symbol Table Organization (4) Linked list and binary tree

Symbol Table Organization (5) Hash table

Scoping (1) Example

Scoping (2) Scope-by-number

Scoping (3) Scope-by-location

Filling the Symbol Table (1) In the first case, information about a construct may appear in the source code before the type of the construct can be identified. Consider the declaration int x,y; in C. Until the parser encounters the semicolon, the compiler cannot tell whether x is a variable or a function. By the time it has encountered the variable, the data type for that variable (int) has long since been processed. This means that some information will have to be stored in a temporary location until it can be used.

Filling the Symbol Table (2) In the second case, additional information about a construct may not appear until after the record for that construct has been created. In SAL and Pascal the syntax for variable declarations is var x,y :int;. We know that x is a variable as soon as its name is encountered, and its symbol table record should be created immediately. The data type will be encountered later in the source code, and x’s record will need to be updated with its type information. In this case, a reference to the variable should be stored so that it can be updated when more information becomes available.

Implementation in Lex & Yacc (0) -Simple

Implementation in Lex & Yacc (1) Declaration (1)

Implementation in Lex & Yacc (2) Declaration (2)

Implementation in Lex & Yacc (3) Declaration (3)

Implementation in Lex & Yacc (4) Parser (in Yacc) (1)

Implementation in Lex & Yacc (5) Parser (in Yacc) (2)

Implementation in Lex & Yacc (6) Token

Implementation in Lex & Yacc (7) Grammar rule (in Yacc) (1)

Implementation in Lex & Yacc (8) Scanner (in Lex)