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

Similar documents
Semantic Analysis. Compiler Architecture

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

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

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

The role of semantic analysis in a compiler

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

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

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

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

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

Syntax Errors; Static Semantics

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

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

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

Project Compiler. CS031 TA Help Session November 28, 2011

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

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

LECTURE 3. Compiler Phases

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking

Introduction to Programming Using Java (98-388)

Type Bindings. Static Type Binding

Semantic Analysis. Lecture 9. February 7, 2018

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

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

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

A Simple Syntax-Directed Translator

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

CSE 504: Compiler Design. Intermediate Representations Symbol Table

Visitors. Move functionality

5. Semantic Analysis!

Static Semantics. Winter /3/ Hal Perkins & UW CSE I-1

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

Chapter 5. Names, Bindings, and Scopes

Semantic Analysis Wilhelm/Seidl/Hack: Compiler Design Syntactic and Semantic Analysis, Chapter 4

Syntax-Directed Translation. Lecture 14

Syntax-Directed Translation. Introduction

Lecture 16: Static Semantics Overview 1

Semantic actions for declarations and expressions

Answer: Early binding generally leads to greater efficiency (compilation approach) Late binding general leads to greater flexibility

Crafting a Compiler with C (II) Compiler V. S. Interpreter

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

Interpreters. Prof. Clarkson Fall Today s music: Step by Step by New Kids on the Block

CPS2000 Compiler Theory & Practice

R13 SET Discuss how producer-consumer problem and Dining philosopher s problem are solved using concurrency in ADA.

Syntax-Directed Translation

More On Syntax Directed Translation

Working of the Compilers

CSE 401 Midterm Exam Sample Solution 11/4/11

CS 4201 Compilers 2014/2015 Handout: Lab 1

5. Semantic Analysis. Mircea Lungu Oscar Nierstrasz

2.2 Syntax Definition

CS 415 Midterm Exam Spring SOLUTION

Programming Languages & Translators PARSING. Baishakhi Ray. Fall These slides are motivated from Prof. Alex Aiken: Compilers (Stanford)

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILING

Time : 1 Hour Max Marks : 30

Syntax and Grammars 1 / 21

Semantic actions for declarations and expressions

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

Compiler Lab. Introduction to tools Lex and Yacc

CS /534 Compiler Construction University of Massachusetts Lowell

Informatica 3 Syntax and Semantics

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

5. Semantic Analysis. Mircea Lungu Oscar Nierstrasz

CS558 Programming Languages

Semantic analysis and intermediate representations. Which methods / formalisms are used in the various phases during the analysis?

2. Reachability in garbage collection is just an approximation of garbage.

1 Lexical Considerations

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

EDAN65: Compilers, Lecture 04 Grammar transformations: Eliminating ambiguities, adapting to LL parsing. Görel Hedin Revised:

Structure of a compiler. More detailed overview of compiler front end. Today we ll take a quick look at typical parts of a compiler.

CS558 Programming Languages

Chapter 5 Names, Binding, Type Checking and Scopes

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

CS5363 Final Review. cs5363 1

Lecture 9: Procedures & Functions. CS 540 George Mason University

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

KU Compilerbau - Programming Assignment

10/18/18. Outline. Semantic Analysis. Two types of semantic rules. Syntax vs. Semantics. Static Semantics. Static Semantics.

CSE302: Compiler Design

CSE450 Translation of Programming Languages. Lecture 4: Syntax Analysis

Programming Languages Third Edition. Chapter 7 Basic Semantics

UNIT-4 (COMPILER DESIGN)

Syntax-Directed Translation

CS 321 IV. Overview of Compilation

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Chapter 10 Implementing Subprograms

Lecture 14 Sections Mon, Mar 2, 2009

CS152 Programming Language Paradigms Prof. Tom Austin, Fall Syntax & Semantics, and Language Design Criteria

CA Compiler Construction

CPS 506 Comparative Programming Languages. Syntax Specification

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILERS

CSE 401/M501 Compilers

MIDTERM EXAM (Solutions)

Semantic Analysis and Type Checking

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

Compiler principles, PS1

Informal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility

Intermediate Code Generation

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

Building Compilers with Phoenix

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 motivation Syntactically correct programs may still contain errors Lexical analysis does not distinguish between different variable names (same ID token) Syntax analysis does not correlate variable declaration with variable use, does not keep track of types int a; a = hello ; Assigning wrong type int a; b = 1; Assigning undeclared variable

Goals of semantic analysis Check correct use of programming constructs Provide information for subsequent phases Context-sensitive beyond context free grammars Lexical analysis and syntax analysis provide relatively shallow checks of program structure Semantic analysis goes deeper Correctness specified by semantic rules Scope rules Type-checking rules Specific rules Note: semantic analysis ensures only partial correctness of programs Runtime checks (pointer dereferencing, array access)

Example of semantic rules A variable must be declared before used A variable should not be declared multiple times A variable should be initialized before used Non-void method should contain return statement along all execution paths break/continue statements allowed only in loops this keyword cannot be used in static method main method should have specific signature Type rules are important class of semantic rules In an assignment statement, the variable and assigned expression must have the same type In a condition test expression must have boolean type

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

AST Abstract Syntax Tree

Parse Tree vs. AST Consider the expression: 1+(2)+(3) expr + expr + expr expr expr 1 + ( 2 ) + ( 3 ) 1 2 3 Parse Tree Abstract Syntax Tree

Why AST? A more useful representation of the syntax tree Actual level of details depends on your design Evaluate expression by AST traversal Basis for semantic analysis Later annotate AST Type information Computed values

AST Construction AST Nodes constructed during parsing Bottom-up parser Grammar rules annotated with actions for AST construction When node is constructed all children available (already constructed) Top-down parser More complicated

AST Construction 1 + (2) + (3) expr + (2) + (3) expr + (expr) + (3) expr + (3) expr + (expr) expr : expr '+' expr { $$ = plus($1,$3); } '(' expr ')' { $$ = $2; } INT_CONST { $$ = int_const($1); } expr expr plus e1 e2 expr plus e1 e2 expr expr expr 1 + ( 2 ) + ( 3 ) int_const val = 1 int_const val = 2 int_const val = 3

AST Traversal Visitor Pattern Separate operations on objects of a data structure from object representation Each operation may be implemented as separate visitor Example AST traversal for type-checking

1 AST Traversal 3 visit (left) int_const val = 1 visit (left) plus left right root plus left right int_const val = 2 int_const val = 3 ExprEvalVisitor ev = new ExprEvalVisitor(); Integer result = (Integer)root.accept(ev); 6 2 visit (right) 3 visit (right) class ExprEvalVisitor : Visitor { public: Object visit(plus e) { int leftval= } class plus : public Expression { public: Object accept(visitor v) { return v.visit(this); } } Expression left, right; class int_const : public Expression { public: Object accept(visitor v) { return v.visit(this); } } int val; ((int)e.left.accept(this))).intvalue(); int rightval= ((Integer)e.right.accept(this)).intValue(); return leftval + rightval; } Object visit(int_const e) { return e.val; }...

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

Scope

Scope and visibility Scope (visibility) of identifier = portion of program where identifier can be referred to Lexical scope = textual region in the program Statement block Method body Class body Module / package / file Whole program (multiple modules)

Scope Scope rules: identifiers are defined no multiple definition of same identifier local variables are defined before used program conforms to scope rules

Scope Each scope maps a set of variables to a set of meanings. The scope of a variable declaration is the part of the program where that variable is visible.

Scopes Example class Foo { int value = 39; int test(){ int b = 3; value =+ b; }; int setvalue(int c){ value = c; int d = c; c = c + d; value = c; }; }; Consider the following example: scope of b scope of value scope of c scope of d public class Bar { int value = 42; int setvalue(int c){ value = c; } } scope of c scope of value

Scope In most languages, a complete program will contain several different scopes. Different languages have different rules for scope definition

Example 3: C Scopes Global a,b,c,d,... File scope static names x,y,z f1() variables parameters labels Block Scope variables labels File scope static names w,x,y f2() variables f3() variables, param Block scope Block scope Global scope holds variables and functions No function nesting Block level scope introduces variables and labels File level scope with static variables that are not visible outside the file (global otherwise)

Example 4: Java Scopes Public Classes package p1 public class c1 fields: f1,f2 method: m1 locals method: m2 locals class c2 fields: f3 method: m3 package p2 package p3 Limited global name space with only public classes Fields and methods in a public class can be public è visible to classes in other packages Fields and methods in a class are visible to all classes in the same package unless declared private Class variables visible to all objects of the same class.

Scopes: Referencing Environment The referencing environment at a particular location in source code is the set of variables that are visible at that point. A variable is local to a procedure if the declaration occurs in that procedure. A variable is non-local to a procedure if it is visible inside the procedure but is not declared inside that procedure. A variable is global if it occurs in the outermost scope (special case of non-local).

Types of Scoping Static scope of a variable determined from the source code. Most Closely Nested Used by most languages Dynamic current call tree determines the relevant declaration of a variable use.

Static Scope: Most Closely Nested Rule The scope of a particular declaration is given by the most closely nested rule The scope of a variable declared in block B, includes B. If x is not declared in block B, then an occurrence of x in B is in the scope of a declaration of x in some enclosing block A, such that A has a declaration of x and A is more closely nested around B than any other block with a declaration of x.

Example Program: Static Scope Program main; a,b,c: real; procedure sub1(a: real); d: int; procedure sub2(c: int); d: real; body of sub2 procedure sub3(a:int) body of sub3 body of sub1 body of main sub2 Main sub1 c,d sub3 a,b,c, sub1 a,d, sub2, sub3 a

Example Program: Static Scope Program main; a,b,c: real; procedure sub1(a: real); d: int; procedure sub2(c: int); d: real; body of sub2 procedure sub3(a:int) body of sub3 body of sub1 body of main What is visible at this point (globally)? a, b, c, sub1

Example Program: Static Scope Program main; a,b,c: real; procedure sub1(a: real); d: int; procedure sub2(c: int); d: real; body of sub2 procedure sub3(a:int) body of sub3 body of sub1 body of main What is visible at this point (sub1)? b, c, d, a, sub2, sub3

Example Program: Static Scope Program main; a,b,c: real; procedure sub1(a: real); d: int; procedure sub2(c: int); d: real; body of sub2 procedure sub3(a:int) body of sub3 body of sub1 body of main What is visible at this point (sub3)? b, c, d, a, sub2

Example Program: Static Scope Program main; a,b,c: real; procedure sub1(a: real); d: int; procedure sub2(c: int); d: real; body of sub2 procedure sub3(a:int) body of sub3 body of sub1 body of main What is visible at this point (sub2)? b, c, d, a, sub3

Dynamic Scope Based on calling sequences of program units, not their textual layout (temporal versus spatial) References to variables are connected to declarations by searching the chain of subprogram calls (runtime stack) that forced execution to this point

Dynamic Scope In a dynamic-scoped language, the referencing environment is the local variables plus all visible variables in all active subprograms. A subprogram is active if its execution has begun but has not yet terminated.

Dynamic Scope Evaluation of Dynamic Scoping: Advantage: convenience (easy to implement) Disadvantage: poor readability, unbounded search time

Scope Example MAIN - declaration of x SUB1 - declaration of x -... call SUB2... SUB2... - reference to x -...... call SUB1 MAIN calls SUB1 SUB1 calls SUB2 SUB2 uses x Which x??

Scope Example MAIN - declaration of x SUB1 - declaration of x -... call SUB2... SUB2... - reference to x -...... call SUB1 MAIN calls SUB1 SUB1 calls SUB2 SUB2 uses x For static scoping, it is main s x

Scope Example MAIN (x) SUB1 (x) SUB2 MAIN - declaration of x SUB1 - declaration of x -... call SUB2... SUB2... - reference to x -...... call SUB1 MAIN calls SUB1 SUB1 calls SUB2 SUB2 uses x For dynamic scoping, it is sub1 s x