Semantic Analysis. Find 6 problems with this code. These issues go beyond syntax.

Slides:



Advertisements
Similar presentations
CPSC 388 – Compiler Design and Construction
Advertisements

Symbol Table.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
1 Compiler Construction Intermediate Code Generation.
Subprogram Control - Data sharing Mechanisms to exchange data Arguments - data objects sent to a subprogram to be processed. Obtained through  parameters.
CSE 425: Semantics II Implementing Scopes A symbol table is in essence a dictionary –I.e., every name appears in it, with the info known about it –Usually.
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison- Wesley. All rights reserved. 1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
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.
ISBN Chapter 10 Implementing Subprograms.
Tutorial 6 & 7 Symbol Table
1 Pertemuan 20 Run-Time Environment Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
Run time vs. Compile time
Chapter 9: Subprogram Control
Chapter 10 Implementing Subprograms. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Semantics of Call and Return The subprogram call and return.
Cs164 Prof. Bodik, Fall Symbol Tables and Static Checks Lecture 14.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
Symbol Table (  ) Contents Map identifiers to the symbol with relevant information about the identifier All information is derived from syntax tree -
ISBN Chapter 10 Implementing Subprograms.
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
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Names. 2 Variables  binding is an association between an entity (such as a variable) and a property (such as its value). A binding is static if the association.
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.
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
ISBN Chapter 10 Implementing Subprograms.
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.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
CSC 8505 Compiler Construction Runtime Environments.
Bernd Fischer RW713: Compiler and Software Language Engineering.
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
CS412/413 Introduction to Compilers Radu Rugina Lecture 13 : Static Semantics 18 Feb 02.
1 Programming Languages (CS 550) Lecture 2 Summary Mini Language Interpreter Jeremy R. Johnson.
10-1 Chapter 10: Implementing Subprograms The General Semantics of Calls and Returns Implementing “Simple” Subprograms Implementing Subprograms with Stack-Dynamic.
ISBN Chapter 10 Implementing Subprograms.
Implementing Subprograms
Scope of Variable. 2 Scope and visibility Scope (visibility) of identifier = portion of program where identifier can be referred to Lexical scope = textual.
ISBN Chapter 10 Implementing Subprograms.
Semantic Analysis. Find 6 problems with this code. These issues go beyond syntax.
Run-Time Environments Presented By: Seema Gupta 09MCA102.
Design issues for Object-Oriented Languages
Lecture 9 Symbol Table and Attributed Grammars
Run-Time Environments Chapter 7
Name Spaces: ALL versus OOL
Implementing Subprograms Chapter 10
Chapter 7: User-Defined Functions II
Names and Attributes Names are a key programming language feature
Constructing Precedence Table
CS 326 Programming Languages, Concepts and Implementation
Semantic Analysis with Emphasis on Name Analysis
Semantic Analysis Chapter 6.
CS 3304 Comparative Languages
Mini Language Interpreter Programming Languages (CS 550)
Scope of Variables.
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
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.
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
Chapter 10 Def: The subprogram call and return operations of
Presentation transcript:

Semantic Analysis

Find 6 problems with this code. These issues go beyond syntax.

What kinds of questions does the semantic analysis/code generator need to answer?

Semantic Analysis = Values/Meaning Context-Sensitive Analysis

SCOPING: Consider the following code segment in a language with nested function definitions in which all parameters are passed by reference. int a, b, c, d; function f(int a, int x) { int b = 0; function g() { int c = 3; print (“in g:”,a,b,c,d,x); call h(a,b,c); print(“in g:”,a,b,c,d,x); } { /*in f*/ print(“in f:”,a,b,c,d,x); call g(); print(“in f:”,a,b,c,d,x); a = 6; } function h(int x, int y, int z) { int d = 2; print(“in h:”,a,b,c,d,x,y,z); x = 3; y= 4; z = 5; } function main() { a = b = c = d = 99; call f(b,c); print(“in ma in”,a,b,c,d); call h(a,b,c); print(“in main”, a,b,c,d); } 1. Show the output of this program assuming static scoping. 2. Show the output of this program assuming dynamic scoping.

Exercise: Identifying the Scoping Rules of a PL Using the Decaf spec, 1.List how new names are introduced 2.List rules for visibility of names

Exercise: Semantic Checks Using Decaf handout, identify what semantic checks need to be done by the compiler. Runtime environment?

A Symbol Table Entry For each kind of named object, what information is needed to –Perform semantic checks –Generate code efficiently (not have to traverse AST for the information in faraway places

Symbol Table Operations What are the key operations to be performed on the symbol table?

Implementing a Symbol Table What possible data structures could be used for storing a single scope? Exercise: Assigned a given data structure, create arguments to “sell” yours as the one to be used, and arguments to “not buy” the others

A symbol table manager and data structure must implement: enter_scope() start a new nested scope lookup(x) finds current x (or null) via scoping rules insert_symbol(x) add a symbol x to the table local-lookup(x) determines if x in local scope exit_scope() exit current scope

But, we have to deal with scopes and scoping rules Separate table for each procedure (i.e., for each scope) At a given point have symbol table for each nesting level  current nesting level Info is deleted as well as created

Using a Symbol Table ● To process a portion of the program that creates a scope (block statements, function calls, classes, etc.) ● Enter a new scope. ● Add all variable declarations to the symbol table. ● Process the body of the block/function/class. ● Exit the scope. ● Much of semantic analysis is defined in terms of recursive AST traversals like this.

Search from top of stack When table not needed, delete the pointer to its symbol table else keep pointers to table - code generation, debugging - - delete only from active symbol table P3 1 P2 3 P4 2 P3 * symbol table P1 symbol table P2 linear list P3 P2 P1 active 0 P1 Top of stack:

Scoping with Inheritance Root Scope public class Base { public int publicBaseInt = 1; protected int baseInt = 2; }

Scoping with Inheritance Root Scope public class Base { public int publicBaseInt = 1; protected int baseInt = 2; Base } publicBaseInt 1 baseInt 2

Scoping with Inheritance Root Scope public class Base { public int publicBaseInt = 1; protected int baseInt = 2; } public class Derived extends Base { public int derivedInt = 3; public int publicBaseInt = 4; public void doSomething() { System.out.println(publicBaseInt); System.out.println(baseInt); System.out.println(derivedInt); int publicBaseInt = 6; System.out.println(publicBaseInt); } } Base publicBaseInt 1 baseInt 2

Scoping with Inheritance Root Scope public class Base { public int publicBaseInt = 1; protected int baseInt = 2; } public class Derived extends Base { public int derivedInt = 3; public int publicBaseInt = 4; public void doSomething() { System.out.println(publicBaseInt); System.out.println(baseInt); System.out.println(derivedInt); int publicBaseInt = 6; System.out.println(publicBaseInt); } } Base publicBaseInt 1 baseInt 2 Derived derivedInt 3 publicBaseInt 4

Scoping with Inheritance Root Scope public class Base { public int publicBaseInt = 1; protected int baseInt = 2; } public class Derived extends Base { public int derivedInt = 3; public int publicBaseInt = 4; public void doSomething() { System.out.println(publicBaseInt); System.out.println(baseInt); System.out.println(derivedInt); int publicBaseInt = 6; System.out.println(publicBaseInt); } }>}> Base publicBaseInt 1 baseInt 2 Derived derivedInt 3 publicBaseInt 4

Scoping with Inheritance Root Scope public class Base { public int publicBaseInt = 1; protected int baseInt = 2; } public class Derived extends Base { public int derivedInt = 3; public int publicBaseInt = 4; public void doSomething() { System.out.println(publicBaseInt); System.out.println(baseInt); System.out.println(derivedInt); int publicBaseInt = 6; System.out.println(publicBaseInt); } }>}> Base publicBaseInt 1 baseInt 2 Derived derivedInt 3 publicBaseInt 4

Scoping with Inheritance Root Scope public class Base { public int publicBaseInt = 1; protected int baseInt = 2; } public class Derived extends Base { public int derivedInt = 3; public int publicBaseInt = 4; public void doSomething() { System.out.println(publicBaseInt); System.out.println(baseInt); System.out.println(derivedInt); int publicBaseInt = 6; System.out.println(publicBaseInt); } }>}> Base publicBaseInt 1 baseInt 2 Derived derivedInt 3 publicBaseInt 4 doSomething

Scoping with Inheritance Root Scope public class Base { public int publicBaseInt = 1; protected int baseInt = 2; } public class Derived extends Base { public int derivedInt = 3; public int publicBaseInt = 4; public void doSomething() { System.out.println(publicBaseInt); System.out.println(baseInt); System.out.println(derivedInt); int publicBaseInt = 6; System.out.println(publicBaseInt); } }>}> Base publicBaseInt 1 baseInt 2 Derived derivedInt 3 publicBaseInt 4 doSomething

Scoping with Inheritance Root Scope public class Base { public int publicBaseInt = 1; protected int baseInt = 2; } public class Derived extends Base { public int derivedInt = 3; public int publicBaseInt = 4; public void doSomething() { System.out.println(publicBaseInt); System.out.println(baseInt); System.out.println(derivedInt); int publicBaseInt = 6; System.out.println(publicBaseInt); } } > 4 Base publicBaseInt 1 baseInt 2 Derived derivedInt 3 publicBaseInt 4 doSomething

Scoping with Inheritance Root Scope public class Base { public int publicBaseInt = 1; protected int baseInt = 2; } public class Derived extends Base { public int derivedInt = 3; public int publicBaseInt = 4; public void doSomething() { System.out.println(publicBaseInt); System.out.println(baseInt); System.out.println(derivedInt); int publicBaseInt = 6; System.out.println(publicBaseInt); } } > 4 Base publicBaseInt 1 baseInt 2 Derived derivedInt 3 publicBaseInt 4 doSomething

Scoping with Inheritance Root Scope public class Base { public int publicBaseInt = 1; protected int baseInt = 2; } public class Derived extends Base { public int derivedInt = 3; public int publicBaseInt = 4; public void doSomething() { System.out.println(publicBaseInt); System.out.println(baseInt); System.out.println(derivedInt); int publicBaseInt = 6; System.out.println(publicBaseInt); } } > 4 2 Base publicBaseInt 1 baseInt 2 Derived derivedInt 3 publicBaseInt 4 doSomething

Scoping with Inheritance Root Scope public class Base { public int publicBaseInt = 1; protected int baseInt = 2; } public class Derived extends Base { public int derivedInt = 3; public int publicBaseInt = 4; public void doSomething() { System.out.println(publicBaseInt); System.out.println(baseInt); System.out.println(derivedInt); int publicBaseInt = 6; System.out.println(publicBaseInt); } } > 4 2 Base publicBaseInt 1 baseInt 2 Derived derivedInt 3 publicBaseInt 4 doSomething

Scoping with Inheritance Root Scope public class Base { public int publicBaseInt = 1; protected int baseInt = 2; } public class Derived extends Base { public int derivedInt = 3; public int publicBaseInt = 4; public void doSomething() { System.out.println(publicBaseInt); System.out.println(baseInt); System.out.println(derivedInt); int publicBaseInt = 6; System.out.println(publicBaseInt); } } > Base publicBaseInt 1 baseInt 2 Derived derivedInt 3 publicBaseInt 4 doSomething

Scoping with Inheritance Root Scope public class Base { public int publicBaseInt = 1; protected int baseInt = 2; } public class Derived extends Base { public int derivedInt = 3; public int publicBaseInt = 4; public void doSomething() { System.out.println(publicBaseInt); System.out.println(baseInt); System.out.println(derivedInt); int publicBaseInt = 6; System.out.println(publicBaseInt); } } > Base publicBaseInt 1 baseInt 2 Derived derivedInt 3 publicBaseInt 4 doSomething

Scoping with Inheritance Root Scope public class Base { public int publicBaseInt = 1; protected int baseInt = 2; } public class Derived extends Base { public int derivedInt = 3; public int publicBaseInt = 4; public void doSomething() { System.out.println(publicBaseInt); System.out.println(baseInt); System.out.println(derivedInt); int publicBaseInt = 6; System.out.println(publicBaseInt); } } > Base publicBaseInt 1 baseInt 2 Derived derivedInt 3 publicBaseInt 4 doSomething publicBaseInt 6

Scoping with Inheritance Root Scope public class Base { public int publicBaseInt = 1; protected int baseInt = 2; } public class Derived extends Base { public int derivedInt = 3; public int publicBaseInt = 4; public void doSomething() { System.out.println(publicBaseInt); System.out.println(baseInt); System.out.println(derivedInt); int publicBaseInt = 6; System.out.println(publicBaseInt); } } > Base publicBaseInt 1 baseInt 2 Derived derivedInt 3 publicBaseInt 4 doSomething publicBaseInt 6

Scoping with Inheritance Root Scope public class Base { public int publicBaseInt = 1; protected int baseInt = 2; } public class Derived extends Base { public int derivedInt = 3; public int publicBaseInt = 4; public void doSomething() { System.out.println(publicBaseInt); System.out.println(baseInt); System.out.println(derivedInt); int publicBaseInt = 6; System.out.println(publicBaseInt); } } > Base publicBaseInt 1 baseInt 2 Derived derivedInt 3 publicBaseInt 4 doSomething publicBaseInt 6

Scoping with Inheritance Root Scope public class Base { public int publicBaseInt = 1; protected int baseInt = 2; } public class Derived extends Base { public int derivedInt = 3; public int publicBaseInt = 4; public void doSomething() { System.out.println(publicBaseInt); System.out.println(baseInt); System.out.println(derivedInt); int publicBaseInt = 6; System.out.println(publicBaseInt); } } > Base publicBaseInt 1 baseInt 2 Derived derivedInt 3 publicBaseInt 4 doSomething publicBaseInt 6

Scoping with Inheritance Root Scope public class Base { public int publicBaseInt = 1; protect } public clas public public Typically the scope would also contain the names “Base” and “Derived,” along with the function name “doSomething.” For simplicity, I've left these out. Base publicBaseInt 1 baseInt 2 public v System.out.println(publicBaseInt); System.out.println(baseInt); System.out.println(derivedInt); int publicBaseInt = 6; System.out.println(publicBaseInt); } } > Derived derivedInt 3 publicBaseInt 4 doSomething publicBaseInt 6

Explicit Disambiguation Root Scope public class Base { public int value = 1; } public class Derived extends Base { public int value = 2; public void doSomething() { int value = 3; System.out.println(value); System.out.println(this.value); System.out.println(super.value); } } Base value 1 Derived value 2 doSomething value 3

Explicit Disambiguation Root Scope public class Base { public int value = 1; } public class Derived extends Base { public int value = 2; public void doSomething() { int value = 3; System.out.println( value ); System.out.println( this.value ); System.out.println( super.value ); } } Base value 1 Derived value 2 doSomething value 3

Explicit Disambiguation Root Scope public class Base { public int value = 1; } public class Derived extends Base { public int value = 2; public void doSomething() { int value = 3; System.out.println( value ); System.out.println( this.value ); System.out.println( super.value ); } } Base value 1 super thisDerived Locals value 2 doSomething value 3

Explicit Disambiguation Root Scope public class Base { public int value = 1; } public class Derived extends Base { public int value = 2; public void doSomething() { int value = 3; System.out.println(value); System.out.println(this.value); System.out.println(super.value); } } Base value 1 super thisDerived Locals value 2 doSomething value 3

Explicit Disambiguation Root Scope public class Base { public int value = 1; } public class Derived extends Base { public int value = 2; public void doSomething() { int value = 3; System.out.println(value); System.out.println(this.value); System.out.println(super.value); } } > Base value 1 super thisDerived Locals value 2 doSomething value 3

Explicit Disambiguation Root Scope public class Base { public int value = 1; } public class Derived extends Base { public int value = 2; public void doSomething() { int value = 3; System.out.println(value); System.out.println(this.value); System.out.println(super.value); } } > Base value 1 super thisDerived Locals value 2 doSomething value 3

Explicit Disambiguation Root Scope public class Base { public int value = 1; } public class Derived extends Base { public int value = 2; public void doSomething() { int value = 3; System.out.println(value); System.out.println(this.value); System.out.println(super.value); } } > 3 Base value 1 super thisDerived Locals value 2 doSomething value 3

Explicit Disambiguation Root Scope public class Base { public int value = 1; } public class Derived extends Base { public int value = 2; public void doSomething() { int value = 3; System.out.println(value); System.out.println(this.value); System.out.println(super.value); } } > 3 Base value 1 super thisDerived Locals value 2 doSomething value 3

Explicit Disambiguation Root Scope public class Base { public int value = 1; } public class Derived extends Base { public int value = 2; public void doSomething() { int value = 3; System.out.println(value); System.out.println(this.value); System.out.println(super.value); } } > 3 2 Base value 1 super thisDerived Locals value 2 doSomething value 3

Explicit Disambiguation Root Scope public class Base { public int value = 1; } public class Derived extends Base { public int value = 2; public void doSomething() { int value = 3; System.out.println(value); System.out.println(this.value); System.out.println(super.value); } } > 3 2 Base value 1 super thisDerived Locals value 2 doSomething value 3

Explicit Disambiguation Root Scope public class Base { public int value = 1; } public class Derived extends Base { public int value = 2; public void doSomething() { int value = 3; System.out.println(value); System.out.println(this.value); System.out.println(super.value); } } > Base value 1 super thisDerived Locals value 2 doSomething value 3

Explicit Disambiguation Root Scope public class Base { public int value = 1; } public class Derived extends Base { public int value = 2; public void doSomething() { int value = 3; System.out.println(value); System.out.println(this.value); System.out.println(super.value); } } > Base value 1 super thisDerived Locals value 2 doSomething value 3

Explicit Disambiguation – no value Declared in Derived Root Scope public class Base { public int value = 1; } public class Derived extends Base { public void doSomething() { int value = 3; System.out.println(value); System.out.println(this.value); System.out.println(super.value); } } Base value 1 super thisDerived Locals doSomething value 3

Explicit Disambiguation Root Scope public class Base { public int value = 1; } public class Derived extends Base { public void doSomething() { int value = 3; System.out.println(value); System.out.println(this.value); System.out.println(super.value); } } > Base value 1 super thisDerived Locals doSomething value 3

Explicit Disambiguation Root Scope public class Base { public int value = 1; } public class Derived extends Base { public void doSomething() { int value = 3; System.out.println(value); System.out.println(this.value); System.out.println(super.value); } } > Base value 1 super thisDerived Locals doSomething value 3

Explicit Disambiguation Root Scope public class Base { public int value = 1; } public class Derived extends Base { public void doSomething() { int value = 3; System.out.println(value); System.out.println(this.value); System.out.println(super.value); } } > 3 Base value 1 super thisDerived Locals doSomething value 3

Explicit Disambiguation Root Scope public class Base { public int value = 1; } public class Derived extends Base { public void doSomething() { int value = 3; System.out.println(value); System.out.println(this.value); System.out.println(super.value); } } > 3 Base value 1 super thisDerived Locals doSomething value 3

Explicit Disambiguation Root Scope public class Base { public int value = 1; } public class Derived extends Base { public void doSomething() { int value = 3; System.out.println(value); System.out.println(this.value); System.out.println(super.value); } } > 3 1 Base value 1 super thisDerived Locals doSomething value 3

Explicit Disambiguation Root Scope public class Base { public int value = 1; } public class Derived extends Base { public void doSomething() { int value = 3; System.out.println(value); System.out.println(this.value); System.out.println(super.value); } } > 3 1 Base value 1 super thisDerived Locals doSomething value 3

Explicit Disambiguation Root Scope public class Base { public int value = 1; } public class Derived extends Base { public void doSomething() { int value = 3; System.out.println(value); System.out.println(this.value); System.out.println(super.value); } } > Base value 1 super thisDerived Locals doSomething value 3

Explicit Disambiguation Root Scope public class Base { public int value = 1; } public class Derived extends Base { public void doSomething() { int value = 3; System.out.println(value); System.out.println(this.value); System.out.println(super.value); } } > Base value 1 super thisDerived Locals doSomething value 3

Disambiguating Scopes ● Maintain a second table of pointers into the scope stack. ● When looking up a value in a specific scope, begin the search from that scope. ● Some languages allow you to jump up to any arbitrary base class (for example, C++).