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

Slides:



Advertisements
Similar presentations
CPSC 388 – Compiler Design and Construction
Advertisements

1 Symbol Tables. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
Semantic Analysis and Symbol Tables
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.
Recap 1.Programmer enters expression 2.ML checks if expression is “well-typed” Using a precise set of rules, ML tries to find a unique type for the expression.
Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
Implementing Subprograms
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
ISBN Chapter 10 Implementing Subprograms.
1 Storage Registers vs. memory Access to registers is much faster than access to memory Goal: store as much data as possible in registers Limitations/considerations:
CS 536 Spring Run-time organization Lecture 19.
ISBN Chapter 10 Implementing Subprograms.
Tutorial 6 & 7 Symbol Table
Hash Tables1 Part E Hash Tables  
Run time vs. Compile time
Chapter 9: Subprogram Control
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Chapter 10 Implementing Subprograms. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Semantics of Call and Return The subprogram call and return.
Testing a program Remove syntax and link errors: Look at compiler comments where errors occurred and check program around these lines Run time errors:
Cs164 Prof. Bodik, Fall Symbol Tables and Static Checks Lecture 14.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
Review C++ exception handling mechanism Try-throw-catch block How does it work What is exception specification? What if a exception is not caught?
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
CS 2104 Prog. Lang. Concepts 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
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 9 Def: The subprogram call and return operations of a language are together called its subprogram.
1 Symbol Tables The symbol table contains information about –variables –functions –class names –type names –temporary variables –etc.
COMPILERS Symbol Tables hussein suleman uct csc3003s 2007.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 8: Semantic Analysis and Symbol Tables.
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.
1 Mooly Sagiv and Greta Yorsh School of Computer Science Tel-Aviv University Modern Compiler Design.
Semantic Analysis. Find 6 problems with this code. These issues go beyond syntax.
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.
CS 423 Compiler project notes Dept. of Comp. Sci. & Eng. Geunbae Lee.
For more notes and topics VISIT: IMPLEMENTATION OF STACKS eITnotes.com.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
Contextual Analysis (Chapter 5) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
Lecture by: Prof. Pooja Vaishnav.  Language Processor implementations are highly influenced by the kind of storage structure used for program variables.
CSC 8505 Compiler Construction Runtime Environments.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames.
1 Chapter 10 © 2002 by Addison Wesley Longman, Inc The General Semantics of Calls and Returns - Def: The subprogram call and return operations of.
Bernd Fischer RW713: Compiler and Software Language Engineering.
1 Chapter 9 Searching And Table. 2 OBJECTIVE Introduces: Basic searching concept Type of searching Hash function Collision problems.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
PZ09A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09A - Activation records Programming Language Design.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To introduce the basic concepts of linked lists ❏ To introduce the basic concepts.
1 Compiler Construction Run-time Environments,. 2 Run-Time Environments (Chapter 7) Continued: Access to No-local Names.
Lecture 9 Symbol Table and Attributed Grammars
Run-Time Environments Chapter 7
Implementing Subprograms Chapter 10
Chapter 12 – Data Structures
Constructing Precedence Table
CS 326 Programming Languages, Concepts and Implementation
Implementing Subprograms
Semantic Analysis with Emphasis on Name Analysis
Hashing CSE 2011 Winter July 2018.
Syntax Errors; Static Semantics
Chapter 15 Lists Objectives
CMPE 152: Compiler Design October 4 Class Meeting
Symbol Table 薛智文 (textbook ch#2.7 and 6.5) 薛智文 96 Spring.
COMPILERS Semantic Analysis
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

Search from bottom 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 P1 P2 P3 active 0 P1

Binary tree Search in current symbol table for id; if not found find the previous table and continue in this manner until the id is found P1 P2 P3

Hash table technique - Single hash table Link together different entries for the same identifier and associate nesting level with each occurrence of same name. The first one is the latest occurrence of the name, i.e., highest nesting level id nesting level h(i) h(k) i i k k nesting level 0 nesting level 1

Three ways to do this 1. Search for the correct items to remove - rehash expensive 2. Use extra pointer in each element to link all items of the same scope 3. Use a stack that has entries for each scope level 1 level 2 level 3 Pop entry and delete from hash table - keep stack entries around During exit from a procedure - delete all entries of the nesting level we are exiting - must be able to do this - Remove links to most recent scope