Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

Similar documents
Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

Semantic Analysis. Compiler Architecture

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

The role of semantic analysis in a compiler

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

The Compiler So Far. Lexical analysis Detects inputs with illegal tokens. Overview of Semantic Analysis

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Introduction to Programming Using Java (98-388)

CSE 504: Compiler Design. Intermediate Representations Symbol Table

CSE P 501 Compilers. Static Semantics Hal Perkins Winter /22/ Hal Perkins & UW CSE I-1

Syntax Errors; Static Semantics

The basic operations defined on a symbol table include: free to remove all entries and free the storage of a symbol table

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

LECTURE 3. Compiler Phases

Semantic Analysis. How to Ensure Type-Safety. What Are Types? Static vs. Dynamic Typing. Type Checking. Last time: CS412/CS413

Project Compiler. CS031 TA Help Session November 28, 2011

Semantic actions for declarations and expressions

Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1

The Structure of a Syntax-Directed Compiler

Semantic actions for declarations and expressions

Homework 3 Semantic Analysis. Remi Meier Compiler Design

COMPILER CONSTRUCTION LAB 2 THE SYMBOL TABLE. Tutorial 2 LABS. PHASES OF A COMPILER Source Program. Lab 2 Symbol table

An Overview to Compiler Design. 2008/2/14 \course\cpeg421-08s\topic-1a.ppt 1

Semantic actions for declarations and expressions. Monday, September 28, 15

CSE 504. Expression evaluation. Expression Evaluation, Runtime Environments. One possible semantics: Problem:

Lecture 8 CS 412/413 Spring '00 -- Andrew Myers 2. Lecture 8 CS 412/413 Spring '00 -- Andrew Myers 4

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 17: Types and Type-Checking 25 Feb 08

Intermediate Formats. for object oriented languages

Undergraduate Compilers in a Day

SE352b: Roadmap. SE352b Software Engineering Design Tools. W3: Programming Paradigms

Decaf Language Reference

CSC 467 Lecture 13-14: Semantic Analysis

Intermediate Formats. for object oriented languages

Time : 1 Hour Max Marks : 30

Compiler Construction

5. Semantic Analysis!

Compiler Construction

Compiler construction

When do We Run a Compiler?

A programming language requires two major definitions A simple one pass compiler

CSE 504: Compiler Design. Runtime Environments

Names, Scope, and Bindings

Today's Topics. CISC 458 Winter J.R. Cordy

Run-time Environments

CPS2000 Compiler Theory & Practice

Run-time Environments

5. Semantic Analysis. Mircea Lungu Oscar Nierstrasz

Some Basic Definitions. Some Basic Definitions. Some Basic Definitions. Language Processing Systems. Syntax Analysis (Parsing) Prof.

5. Semantic Analysis. Mircea Lungu Oscar Nierstrasz

RYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 96 INSTRUCTIONS

CS 415 Midterm Exam Spring 2002

CSE302: Compiler Design

Compilers Project 3: Semantic Analyzer

Names, Scope, and Bindings

Syntax-Directed Translation. Lecture 14

Compiler Code Generation COMP360

Scope. CSC 4181 Compiler Construction. Static Scope. Static Scope Rules. Closest Nested Scope Rule

EECS483 D6: Project 3 Overview

UNIT-4 (COMPILER DESIGN)

Program Representations

Semantic Processing. Semantic Errors. Semantics - Part 1. Semantics - Part 1

Lexical Considerations

Intermediate Formats. Martin Rinard Laboratory for Computer Science

1 Lexical Considerations

EZ-ASCII A Language for ASCII-Art Manipulation

Lecture 9: Parameter Passing, Generics and Polymorphism, Exceptions

Programming Languages Third Edition. Chapter 7 Basic Semantics

Compilers. Type checking. Yannis Smaragdakis, U. Athens (original slides by Sam

Compiler Construction

Programming Languages

CS 4201 Compilers 2014/2015 Handout: Lab 1

Compiler Construction

Compiler Construction

CSE-304 Programming Assignment #4

Writing Evaluators MIF08. Laure Gonnord

Array. Prepared By - Rifat Shahriyar

9/5/17. The Design and Implementation of Programming Languages. Compilation. Interpretation. Compilation vs. Interpretation. Hybrid Implementation

Semantic Analysis. Lecture 9. February 7, 2018

CSCE 314 Programming Languages. Type System

CS453 CLASSES, VARIABLES, ASSIGNMENTS

Parsing Techniques. AST Review. AST Data Structures. LL AST Construction. AST Construction CS412/CS413. Introduction to Compilers Tim Teitelbaum

CS 415 Midterm Exam Spring SOLUTION

CSCE 531 Spring 2009 Final Exam

CMPT 379 Compilers. Parse trees

Ruby: Introduction, Basics

CS 406: Syntax Directed Translation

Semantic Analysis and Type Checking

Lexical Considerations

CSE 401 Midterm Exam Sample Solution 2/11/15

Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres

Intermediate Representations & Symbol Tables

Compiler Theory. (Semantic Analysis and Run-Time Environments)

A Short Summary of Javali

About this exam review

Chapter 11: Compiler II: Code Generation

Acknowledgement. CS Compiler Design. Semantic Processing. Alternatives for semantic processing. Intro to Semantic Analysis. V.

Transcription:

Language Processing Systems Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

Semantic Analysis

Compiler Architecture Front End Back End Source language Scanner (lexical analysis) tokens Parser (syntax analysis) Parse tree Semantic Analysis AST IC generator Intermediate Language Code Optimizer Intermediate Language OIL Code Generator Target language Error Handler Symbol Table

Semantic Analysis Source language Scanner (lexical analysis) tokens Parser (syntax analysis) Parse tree AST Semantic IC Analysis Semantic generator Analysis Intermediate Language Code Optimizer Intermediate Language OIL Code Generator Target language Error Handler Symbol Table Meaning Type/Error Checking Intermediate Code Generation abstract machine

Semantic Analysis Compilers examine code to find semantic problems. Easy: undeclared variables, tag matching Difficult: preventing execution errors Essential Issues: Abstract Syntax Trees (AST) Scope Symbol tables Type checking

Semantic Analysis Essential Issues: Abstract Syntax Trees (AST) Scope Symbol tables Type checking

Semantic Analysis Essential Issues: Abstract Syntax Trees (AST) Scope Symbol tables Type checking

Symbol Table

Symbol table A compile-time data structure used to map names into declarations An environment that stores information about identifiers A data structure that captures scope information Each entry in symbol table contains The name of an identifier Its kind (variable/method/field ) Type Additional properties, e.g, final, public One symbol table for each scope

Symbol Table Primary data structure inside a compiler Stores information about the symbols in the input program including: Type (or class) Size (if not implied by type) Scope Scope represented explicitly or implicitly (based on table structure) Classes can also be represented by structure one difference = information about classes must persist after have left scope Used in all phases of the compiler

Symbol table structure Assign variables to storage classes that prescribe scope, visibility, and lifetime scope rules prescribe the symbol table structure scope: unit of static program structure with one or more variable declarations scope may be nested Pascal: procedures are scoping units C: blocks, functions, files are scoping units Visibility, lifetimes, global variables Automatic or stack storage Static variables

Symbol Table Object Symbol table functions are called during parsing: Insert(x) A new symbol is defined Delete(x) The lifetime of a symbol ends Lookup(x) A symbol is used EnterScope(s) A new scope is entered ExitScope(s) A scope is left

Symbol Table Issues A major consideration in designing a symbol table is that insertion and retrieval should be as fast as possible One dimensional table: search is very slow Balanced binary tree: quick insertion, searching and retrieval; extra work required to keep the tree balanced Hash tables: quick insertion, searching and retrieval; extra work to compute hash keys Hashing with a chain of entries is generally a good approach

Symbol Table Implementation Variety of choices, including arrays, lists, trees, heaps, hash tables, Different structures may be used for local tables versus tables representing scope. Each table in the hierarchy could be implemented using java.util.hashmap

Symbol Table Implementation Scopes implemented using symbol tables Data-structure for look-up key identifier value type of identifier, other semantic properties

Hashed local symbol table

Symbol Table - Example 1 class Test { int a = 39; int test(){ int b = 3 ; a = a + b ; }; }; Symbol Kind Type Properties a var Int b var Int test method -> Int

Symbol Table - Example 1 class Test { int a = 39; int test(){ String a = hello ; int b = 3; b = +b; }; }; Symbol Kind Type Properties a var Int b var Int test method -> Int a var String

Implementing Scopes Int x = 0 ; e = e + x; before processing e add definition of x to current definitions override any other definition of x after processing e remove definition of x restore old definition of x

Symbol Table Example 2 class Foo { int value = 39; int test() { int b = 3; value =+ b; }; int setvalue(c:int) value = c; int d = c; c = c + d; value = c; }}; };

Symbol Table Example 2 (Foo) Symbol Kind Type Properties value var Int test method -> Int setvalue method Int -> Int (Test) Symbol Kind Type Properties b var Int Symbol Kind Type Properties c var Int (setvalue) Symbol Kind Type Properties d var Int (setvalue-block)

Symbol Table Lookup (Foo) Symbol Kind Type Properties value var int test method -> int setvalue method int -> void (Test) Symbol Kind Type Properties b var int Symbol Kind Type Properties c var int (setvalue) Int setvalue(c:int) {{ value = c; int d = c; { c ß c + d; value ß c; }; }}; Lookup(value) Symbol Kind Type Properties d var int (setvalue-block)

Symbol Table Lookup (Foo) Symbol Kind Type Properties value var int test method -> int setvalue method int -> void Error! (Test) Symbol Kind Type Properties b var int Symbol Kind Type Properties c var int (setvalue) Int setvalue(c:int) {{ value = c; int d = c ; { c = c + d; myvalue = c; }; }}; Lookup(myValue) Symbol Kind Type Properties d var int (setvalue-block)

Symbol Table Construction class Foo { int value = 39; int test() { int b = 3; value =+ b; }; int setvalue(c:int) value = c; int d = c; c = c + d; value = c; root Class name=foo Method name=test Method name=setvalue }}; }; Let id=b Expr Let id=d Expr Expr Expr Expr (some details omitted)

Symbol Table Construction via AST Traversal globals Class root name=foo Symbol Foo Foo Symbol kind class kind test setvalue method method Method name=test Method name=setvalue test Symbol setvalue Symbol b var c var Let id=b Expr Let id=d Let Symbol d var Expr Expr Expr Expr (some details omitted)

Symbol Table Construction via AST Traversal globals Class root name=foo Symbol Foo Foo Symbol kind class kind test setvalue method method Method name=test Method name=setvalue test Symbol setvalue Symbol b var c var Let id=b Expr Let id=d Let Symbol d var Expr Expr Expr Expr (some details omitted)

Symbol Tables (cont d) globals class A { foo() { } } bar() { } bar(); Method id=foo root Program Class id=a Method id=bar Symbol A A Symbol foo bar kind class kind method method dispatch id=bar() Undefined identifier bar()

Symbol Tables - Naïve solution Building visitor Propagates (at least) a reference to the symbol table of the current scope In some cases have to use type information (inherits) Checking visitor On visit to node perform check using symbol tables Resolve identifiers try to find symbol in table hierarchy In some cases have to use global type table and type information You may postpone these checks

Symbol Tables less naïve solution Use forward references And/or construct some of the symbol table during parsing

Symbol Tables (cont d) globals class A { foo() { } } bar() { } bar(); Method id=foo root Program Class id=a Method id=bar Symbol kind A class A Symbol kind FREF foo method bar method true Dispatch id=bar()

Next phase: type checking First, record all pre-defined types (string,int,boolean,void,null) Second, record all user-defined types (classes, methods, arrays) Store all types in table Now, run type-checking algorithm

Type table Keeps a single copy for each type Can compare types for equality by == Records primitive types: int, bool, string, void, null Initialize table with primitive types User-defined types: arrays, methods, classes Used to record inheritance relation Types should support subtypeof(type t) For IC enough to keep one global table Static field of some class (e.g., Type) In C/Java associate type table with scope

Possible type hierarchy Type VoidType IntType BoolType RefType MethodType RefType ArrayType Type : elemtype NullType StringType ClassType ICClass : c type ::= int boolean type `[` `]`