COMPILERS Semantic Analysis

Slides:



Advertisements
Similar presentations
Symbol Table.
Advertisements

CSC 4181 Compiler Construction Scope and Symbol Table.
Intermediate Code Generation
Programming Languages and Paradigms
CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
Chapter 9 Subprogram Control Consider program as a tree- –Each parent calls (transfers control to) child –Parent resumes when child completes –Copy rule.
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.
1 Pertemuan 20 Run-Time Environment Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
Run time vs. Compile time
Context-sensitive Analysis, II Ad-hoc syntax-directed translation, Symbol Tables, andTypes.
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
Chapter 9: Subprogram Control
Cs164 Prof. Bodik, Fall Symbol Tables and Static Checks Lecture 14.
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.
Compiler Construction
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.
1 Type Checking Type checking ensures that the operands and the operator are of compatible types Generalized to include subprograms and assignments Compatible.
A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University NESTED SUBPROGRAMS.
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.
7. Runtime Environments Zhang Zhizheng
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
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.
Run-Time Environments Presented By: Seema Gupta 09MCA102.
Code Generation Instruction Selection Higher level instruction -> Low level instruction Register Allocation Which register to assign to hold which items?
COMPILERS Semantic Analysis hussein suleman uct csc3003s 2009.
Implementing Subprograms
Lecture 9 Symbol Table and Attributed Grammars
Chapter 10 : Implementing Subprograms
Implementing Subprograms Chapter 10
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
Context-Sensitive Analysis
Type Checking, and Scopes
Constructing Precedence Table
Compiler Construction (CS-636)
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
CS 326 Programming Languages, Concepts and Implementation
Structure of Programming Languages
Implementing Subprograms
Semantic Analysis with Emphasis on Name Analysis
Subprograms The basic abstraction mechanism.
Implementing Subprograms
Chapter 10: Implementing Subprograms Sangho Ha
Implementing Subprograms
CMPE 152: Compiler Design October 4 Class Meeting
Chapter 6 Intermediate-Code Generation
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
Implementing Subprograms
PZ09A - Activation records
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
UNIT V Run Time Environments.
Names and Binding In Text: Chapter 5.
Compiler Construction
Run Time Environments 薛智文
Runtime Environments What is in the memory?.
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Implementing Subprograms
Compiler Construction
Types and Related Issues
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Implementing Subprograms
Chapter 10 Def: The subprogram call and return operations of
Presentation transcript:

COMPILERS Semantic Analysis hussein suleman uct csc305w 2004

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

Symbol Tables Associate lexical names (symbols) with their attributes. Contains: 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

Binding As the declarations of types, variables, and functions are processed, identifiers are bound to “meanings” in the symbol table. A symbol table is a set of bindings.

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.

Basic Static Scope Usually, a name begins life where it is declared and ends at the end of its block. void foo() { int k; ……. }

Why Scope? Scope is not necessary. Languages such as assembly have exactly one scope: the whole program. Modern progamming 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.

Changing Scope Identifiers come into scope at the beginning of a subprogram/block and go out of scope at the end. Example (in C++): void testfunc () { int a; // a enters scope; for ( int b=1; b<10; b++ ) // b in scope for for int c; // c enters scope … } // b,c leave scope } // a leaves scope

Static Scope Consider the Pascal program (which uses static scoping): program test; var a : integer; procedure proc1; var b : integer; begin end; procedure proc2; var a, c : integer; proc1; proc2; end. in scope: b (from proc1), a (from test) in scope: a, c (from proc2) in scope: a (from test)

Dynamic Scope Consider the Pascal-like code (assume dynamic scoping): program test; var a : integer; procedure proc1; var b : integer; begin end; procedure proc2; var a, c : integer; proc1; proc2; end. in scope: b (from proc1) a, c (from proc2) in scope: a, c (from proc2) in scope: a (from test)

Static vs. Dynamic Scope Dynamic scope makes it easier to access variables with lifetime, but it is difficult to understand the semantics of code outside the context of execution. Static scope is more restrictive – therefore easier to read – but may force the use of more subprogram parameters or global identifiers to enable visibility when required.

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.

Scope and Symbol Table Operations What symbol table operations do we need? void put (Symbol key, Object value) binds key to value Object get(Symbol key) returns value bound to key void beginScope() remembers current state of table void endScope() restores table to state at most recent scope that has not been ended

Attribute Information Attributes are internal representations of declarations. Symbol table associates names with attributes. Names may have different attributes depending on their meaning: variables: type, procedure level, frame offset types: type descriptor, data size/alignment constants: type, value methods: formals (names/types), result type, block information (local decls.), frame size

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.

Types of Implementation Imperative Auxiliary data structures are modified as the analysis progresses, always reflecting only the current state. Functional Auxiliary data structures are maintained intact as the analysis progresses, with new versions created when needed – thus previous and current states are all available at any time.

Hash Table put endScope Imperative - Chain new entries to beginning of table, thus overriding older entries. Functional - Create copy of hash table array. endScope Imperative – Remove entries from head of each linked list. Each entry can point to the next one that should be removed. Functional – Dispose of array.

Binary Tree put endScope Functional – Insert new entries into a new subtree, duplicating nodes up to the root. endScope Functional – Delete all nodes in new subtree. root before second “valueA” root after second “valueA” valueB 1 valueB 1 valueA 2 valueC 3 valueA 4

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.

Type Equivalence Two approaches: Example (structural): 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;