7. Symbol Table Chih-Hung Wang Compilers References 1. C. N. Fischer and R. J. LeBlanc. Crafting a Compiler with C. Pearson Education Inc., 2009. 2. D.

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.
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 Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 531 Compiler Construction Final Lecture of the Semester Spring 2010 Marco.
Semantic analysis Parsing only verifies that the program consists of tokens arranged in a syntactically-valid combination, we now move on to semantic analysis,
Syntax Trees MathWorks Compiler Course – Day 5. Syntax Trees MathWorks Compiler Course – Day 5 Parser lexemes shift/reduce seq. Cfg tables Tree Symbols.
ISBN Chapter 4 Lexical and Syntax Analysis The Parsing Problem Recursive-Descent Parsing.
Reference Book: Modern Compiler Design by Grune, Bal, Jacobs and Langendoen Wiley 2000.
Environments and Evaluation
CS 310 – Fall 2006 Pacific University CS310 Parsing with Context Free Grammars Today’s reference: Compilers: Principles, Techniques, and Tools by: Aho,
Chapter 3 Program translation1 Chapt. 3 Language Translation Syntax and Semantics Translation phases Formal translation models.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 531 Compiler Construction Ch.1 Spring 2010 Marco Valtorta
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
Symbol Table (  ) Contents Map identifiers to the symbol with relevant information about the identifier All information is derived from syntax tree -
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.
CST 320 Compiler Methods Dr. Sherry Yang PV 171 (541)
Chih-Hung Wang Chapter 0: Introduction 參考書目 Leland L. Beck, System Software: An Introduction to Systems Programming (3rd), Addison-Wesley,
COMPILERS Symbol Tables hussein suleman uct csc3003s 2007.
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.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
Compiler design Lecture 1: Compiler Overview Sulaimany University 2 Oct
CS 423 Compiler project notes Dept. of Comp. Sci. & Eng. Geunbae Lee.
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.
4. Formal Grammars and Parsing and Top-down Parsing Chih-Hung Wang Compilers References 1. C. N. Fischer, R. K. Cytron and R. J. LeBlanc. Crafting a Compiler.
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.
Introduction CPSC 388 Ellen Walker Hiram College.
Chapter 1 Introduction Major Data Structures in Compiler
Course Overview for Compilers J. H. Wang Sep. 14, 2015.
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
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.
More yacc. What is yacc – Tool to produce a parser given a grammar – YACC (Yet Another Compiler Compiler) is a program designed to compile a LALR(1) grammar.
CSC 8505 Compiler Construction
Programming Fundamentals Enumerations and Functions.
CS416 Compiler Design1. 2 Course Information Instructor : Dr. Ilyas Cicekli –Office: EA504, –Phone: , – Course Web.
©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.
Lecture 12 Intermediate Code Generation Translating Expressions
Lecture 7 Syntax Analysis (5) Operator-Precedence Parsing
Lecture 9 Symbol Table and Attributed Grammars
System Software Theory (5KS03).
Compiler Design (40-414) Main Text Book:
Introduction Chapter : Introduction.
Constructing Precedence Table
8. Symbol Table Chih-Hung Wang
Parsing with Context Free Grammars
Chapter 1: Introduction to Compiling (Cont.)
Compiler Lecture 1 CS510.
CS416 Compiler Design lec00-outline September 19, 2018
Course supervisor: Lubna Siddiqui
Introduction CI612 Compiler Design CI612 Compiler Design.
CMPE 152: Compiler Design October 4 Class Meeting
CS416 Compiler Design lec00-outline February 23, 2019
5. Bottom-Up Parsing Chih-Hung Wang
CISC 7120X Programming Languages and Compilers
CMPE 152: Compiler Design February 7 Class Meeting
Lec00-outline May 18, 2019 Compiler Design CS416 Compiler Design.
Introduction Chapter : Introduction.
Compiler Design 3. Lexical Analyzer, Flex
Presentation transcript:

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. Grune, H. Bal, C. Jacobs, and K. Langendoen. Modern Compiler Design. John Wiley & Sons, Alfred V. Aho, Ravi Sethi, and Jeffrey D. Ullman. Compilers: Principles, Techniques, and Tools. Addison-Wesley, (2 nd Ed. 2006) 1

2 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.

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

4 Common Symbol Table

5 Complex Type

6 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

7 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.

8 Symbol Table Organization (3) A library

9 Symbol Table Organization (4) Linked list and binary tree

10 Symbol Table Organization (5) Hash table

11 Scoping (1) Example

12 Scoping (2) Scope-by-number

13 Scoping (3) Scope-by-location

14 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.

15 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.

16 Implementation in Lex & Yacc (0) - Simple

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

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

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

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

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

22 Implementation in Lex & Yacc (6) Token

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

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