COMPILERS Symbol Tables hussein suleman uct csc3003s 2007.

Slides:



Advertisements
Similar presentations
CPSC 388 – Compiler Design and Construction
Advertisements

Semantic Analysis and Symbol Tables
Symbol Table.
Intermediate Code Generation
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
CS7100 (Prasad)L16-7AG1 Attribute Grammars Attribute Grammar is a Framework for specifying semantics and enables Modular specification.
Names and Bindings.
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
Semantic analysis Parsing only verifies that the program consists of tokens arranged in a syntactically-valid combination, we now move on to semantic analysis,
Chapter 9 Subprogram Control Consider program as a tree- –Each parent calls (transfers control to) child –Parent resumes when child completes –Copy rule.
ISBN Chapter 10 Implementing Subprograms.
5. Semantic Analysis Prof. O. Nierstrasz Jorge Ressia Thanks to Jens Palsberg and Tony Hosking for their kind permission to reuse and adapt the CS132 and.
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
Honors Compilers Semantic Analysis and Attribute Grammars Mar 5th 2002.
Tutorial 6 & 7 Symbol Table
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.
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.
Context-sensitive Analysis, II Ad-hoc syntax-directed translation, Symbol Tables, andTypes.
Chapter 3 Program translation1 Chapt. 3 Language Translation Syntax and Semantics Translation phases Formal translation models.
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
Compiler Summary Mooly Sagiv html://
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.
Names and Bindings Introduction Names Variables The concept of binding Chapter 5-a.
Cs164 Prof. Bodik, Fall Symbol Tables and Static Checks Lecture 14.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
5. Semantic Analysis Prof. O. Nierstrasz Thanks to Jens Palsberg and Tony Hosking for their kind permission to reuse and adapt the CS132 and CS502 lecture.
Names Variables Type Checking Strong Typing Type Compatibility 1.
5-1 Chapter 5: Names, Bindings, Type Checking, and Scopes Variables The Concept of Binding Type Checking Strong Typing Type Compatibility Scope and Lifetime.
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.
1 Names, Scopes and Bindings Aaron Bloomfield CS 415 Fall
Activation Records CS 671 February 7, CS 671 – Spring The Compiler So Far Lexical analysis Detects inputs with illegal tokens Syntactic analysis.
1 Scope Rules (Section 3.3) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael Scott.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Basic Semantics Associating meaning with language entities.
Runtime Organization.
Activation Records (in Tiger) CS 471 October 24, 2007.
1 CIS 461 Compiler Design and Construction Winter 2012 Lecture-Module #16 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Run-Time Environments How do we allocate the space for the generated target code and the data object.
Joey Paquet, 2000, Lecture 10 Introduction to Code Generation and Intermediate Representations.
COMP3190: Principle of Programming Languages
Chapter 1 Introduction. Chapter 1 - Introduction 2 The Goal of Chapter 1 Introduce different forms of language translators Give a high level overview.
Introduction to Code Generation and Intermediate Representations
Chapter 1 Introduction Major Data Structures in Compiler
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
Programming Languages and Design Lecture 6 Names, Scopes and Binding Instructor: Li Ma Department of Computer Science Texas Southern University, Houston.
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
CS412/413 Introduction to Compilers Radu Rugina Lecture 11: Symbol Tables 13 Feb 02.
Names, Scope, and Bindings Programming Languages and Paradigms.
1 Compiler Construction Run-time Environments,. 2 Run-Time Environments (Chapter 7) Continued: Access to No-local Names.
Semantic Analysis. Find 6 problems with this code. These issues go beyond syntax.
COMPILERS Semantic Analysis hussein suleman uct csc3003s 2009.
Lecture 9 Symbol Table and Attributed Grammars
Context-Sensitive Analysis
Type Checking, and Scopes
Constructing Precedence Table
Compiler Construction (CS-636)
CS 326 Programming Languages, Concepts and Implementation
Implementing Subprograms
Chapter 6 Intermediate-Code Generation
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
Symbol Table 薛智文 (textbook ch#2.7 and 6.5) 薛智文 96 Spring.
Run Time Environments 薛智文
COMPILERS Semantic Analysis
Presentation transcript:

COMPILERS Symbol Tables hussein suleman uct csc3003s 2007

Semantic Analysis  The compilation process is driven by the syntactic structure of the program as discovered by the parser.  Semantic routines: interpret meaning of the program based on its syntactic structure it has two functions:  finish analysis by deriving context-sensitive information  begin synthesis by generating the IR or target code  Associated with individual productions of a context free grammar or subtrees of a syntax tree.

Context Sensitive Analysis - Why  What context-sensitive issues can be determined? Is X declared before it is used? Are any names declared but not used? Which declaration of X does this reference? Is an expression type-consistent? Do the dimensions of a reference match the declaration? Where can X be stored? (heap, stack,,,, ) Does *p reference the result of a malloc()? Is X defined before it is used? Is an array reference in bounds? Does function foo(…) produce a constant value?

Context Sensitive Analysis - How  How to check symbols and their semantics at various points in the program? Process program linearly (roughly, in-order tree traversal). Maintain a list of currently defined symbols and what they mean as the program is processed – this is called a Symbol Table.

Symbol Tables  Associate lexical names (symbols) with their attributes.  Can contain: variable names defined constants procedure/function/method names literal constants and strings source text labels compiler-generated temporaries subtables for structure layouts (types) (field offsets and lengths)

Symbol Table Attributes  The following attributes would be kept in a symbol table: textual name data type dimension information (for aggregates) declaring procedure lexical level of declaration storage class (base address) offset in storage if record, pointer to structure table if parameter, by-reference or by-value? can it be aliased? to what other names? number and type of arguments to functions/methods

Scope  An identifier has scope when it is visible and can be referenced.  An out-of-scope identifier cannot be referenced.  Identifiers in open scopes may override older/outer scopes temporarily.  2 Types of scope: Static scope is when visibility is due to the lexical nesting of subprograms/blocks. Dynamic scope is when visibility is due to the call sequence of subprograms.

Why Scope?  Scope is not necessary. Languages such as assembler have exactly one scope: the whole program.  Modern programming languages have more than one scope. Information hiding and modularity.  Goal of any language is to make the programmer’s job simpler. One way: keep things isolated. Make each thing only affect a limited area. Make it hard to break something far away.

Scope in a symbol table  Most modern programming languages have nested static scope. The symbol table must reflect this.  What additional information can reflect nested scope? A name query must access the most recent declaration, from the current scope or some enclosing scope. Innermost scope overrides declarations from outer scopes.

Symbol Table Implementation  Implemented as a collection of dictionaries in which each symbol is placed.  Two operations: insert adds a binding to a table and lookup locates the binding for a name.  Many different possible data structures: linked list hash table binary tree

Symbol Table Lookup  Basic operation is to find the entry for a given symbol.  Each symbol table may have a pointer to its parent scope.  Lookup: if symbol in current table, return it, otherwise look in parent.  Hash tables and binary trees can be used more efficiently.

Symbols vs. Names  Names are the textual entities found in the source code.  Symbols are entities assigned to each name for more efficient processing during compilation.  Example: Name: valueA  Symbol: V001 Name: valueB  Symbol: V002  Remember perfect hash functions?

Type Checking  Static semantics should be checked after/as the symbol table is populated. Is every name defined before it is used? Does the type of each subexpression conform to what is expected? Are the types on either side of an assignment compatible?  The tree can be walked/visited to perform these checks. May need multiple passes – so retain symbol table across passes.

Type Equivalence  Two approaches: Name equivalence: each type name is a distinct type. Structural equivalence: two types are equivalent iff. they have the same structure (after substituting type expressions for type names).  Example (structural): typedef int bignumber; int c; bignumber b =c;

Error Handling  If errors are detected, correct program representation and continue analysis to detect other errors.  Example: int a, b; String c; c = a; b = a;