Semantic Analysis. Compiler Architecture

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

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

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

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

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

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

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

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

Syntax Errors; Static Semantics

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

Project Compiler. CS031 TA Help Session November 28, 2011

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

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

Type Bindings. Static Type Binding

Introduction to Programming Using Java (98-388)

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

CSE 401 Midterm Exam Sample Solution 11/4/11

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

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

LECTURE 3. Compiler Phases

Semantic Analysis. Lecture 9. February 7, 2018

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

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

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

Chapter 5. Names, Bindings, and Scopes

Syntax-Directed Translation. Lecture 14

5. Semantic Analysis!

Lecture 16: Static Semantics Overview 1

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

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

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

CSE450 Translation of Programming Languages. Lecture 4: Syntax Analysis

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

CSE 504: Compiler Design. Intermediate Representations Symbol Table

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

CS /534 Compiler Construction University of Massachusetts Lowell

Syntax-Directed Translation. Introduction

5. Semantic Analysis. Mircea Lungu Oscar Nierstrasz

Working of the Compilers

Chapter 5 Names, Binding, Type Checking and Scopes

Syntax-Directed Translation

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

A Simple Syntax-Directed Translator

Programming Languages Third Edition. Chapter 7 Basic Semantics

More On Syntax Directed Translation

Time : 1 Hour Max Marks : 30

CS 415 Midterm Exam Spring SOLUTION

CS 4201 Compilers 2014/2015 Handout: Lab 1

Chapter 10 Implementing Subprograms

5. Semantic Analysis. Mircea Lungu Oscar Nierstrasz

CA Compiler Construction

MIDTERM EXAM (Solutions)

Semantic Analysis and Type Checking

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

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

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILING

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

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

Semantic actions for declarations and expressions

Exception Handling. Sometimes when the computer tries to execute a statement something goes wrong:

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

Visitors. Move functionality

CS 314 Principles of Programming Languages. Lecture 13

CS558 Programming Languages

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

Exception Handling. Run-time Errors. Methods Failure. Sometimes when the computer tries to execute a statement something goes wrong:

UNIT-4 (COMPILER DESIGN)

G Programming Languages - Fall 2012

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

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

Syntax-Directed Translation

CS 321 IV. Overview of Compilation

CS558 Programming Languages

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

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

Compiler Lab. Introduction to tools Lex and Yacc

1 Lexical Considerations

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILERS

6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson

CSE 307: Principles of Programming Languages

2.2 Syntax Definition

Ambiguity and Errors Syntax-Directed Translation

Informatica 3 Syntax and Semantics

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

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

Semantic actions for declarations and expressions

An Overview of Compilation

Lecture 09: Data Abstraction ++ Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree.

Intermediate Code Generation

Programming Language Concepts, cs2104 Lecture 04 ( )

Theoretical Part. Chapter one:- - What are the Phases of compiler? Answer:

COP4020 Programming Languages. Compilers and Interpreters Robert van Engelen & Chris Lacher

Error Handling Syntax-Directed Translation Recursive Descent Parsing

LECTURE 14. Names, Scopes, and Bindings: Scopes

CSE 12 Abstract Syntax Trees

Chapter 9. Def: The subprogram call and return operations of a language are together called its subprogram linkage

Undergraduate Compilers in a Day

CS5363 Final Review. cs5363 1

Transcription:

Processing Systems Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan Source Compiler Architecture Front End Scanner (lexical tokens Parser (syntax Parse tree Semantic Analysis AST IC generator Back End OIL Optimizer Generator Target Source Scanner (lexical tokens Parser (syntax Parse tree AST Semantic IC Analysis Semantic generator Analysis OIL Optimizer Generator Target Error Handler Symbol Table Error Handler Symbol Table Meaning Type/Error Checking 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 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 ession must have the same type In a condition test ession must have boolean type Compilers examine code to find semantic problems. Easy: undeclared variables, tag matching Difficult: preventing execution errors Essential Issues: Abstract Syntax Trees (AST) Symbol tables Type checking Essential Issues: Abstract Syntax Trees (AST) Symbol tables Type checking Abstract Syntax Tree AST Parse Tree vs. AST Why AST? Consider the ession: 1+(2)+(3) 1 + ( 2 ) + ( 3 ) + + 1 2 3 A more useful representation of the syntax tree Actual level of details depends on your design Evaluate ession by AST traversal Basis for semantic analysis Later annotate AST Type information Computed values Parse Tree Abstract Syntax Tree

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) + (2) + (3) + () + (3) + (3) + () 1 + ( 2 ) + ( 3 ) : '+' { $$ = ($1,$3); '(' ')' { $$ = $2; INT_CONST { $$ = ($1); val = 1 e1 e2 val = 2 e1 e2 val = 3 AST Traversal Visitor Pattern Separate operations on objects of a data structure from object representation Each operation may be implemented as separate or Example AST traversal for type-checking AST Traversal 6 root left right 3 (left) 3 (right) left right 1 2 (left) (right) val = 1 val = 2 val = 3 ExprEvalVisitor ev = new ExprEvalVisitor(); Integer result = (Integer)root.accept(ev); class : public Expression { public: Object accept(visitor v) { return v.(this); Expression left, right; class : public Expression { public: Object accept(visitor v) { return v.(this); int val; class ExprEvalVisitor : Visitor { public: Object ( e) { int leftval= ((int)e.left.accept(this))).intvalue(); int rightval= ((Integer)e.right.accept(this)).intValue(); return leftval + rightval; Object ( e) { return e.val; Essential Issues: Abstract Syntax Trees (AST) Symbol tables Type checking

and visibility (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) rules: identifiers are defined no multiple definition of same identifier local variables are defined before used program conforms to scope rules 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. s 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; ; ; public class Bar { int value = 42; int setvalue(int c){ value = c; Consider the following example: scope of b scope of value scope of c scope of d scope of c scope of value Example 3: C s In most s, a complete program will contain several different scopes. Different s have different rules for scope definition Global a,b,c,d,... File scope static names x,y,z f1() variables parameters labels Block 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 s 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. s: 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 s Dynamic current call tree determines the relevant declaration of a variable use. Static : 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 Example Program: Static sub2 Main sub1 c,d sub3 a,b,c, sub1 a,d, sub2, sub3 a (globally)? a, b, c, sub1

Example Program: Static Example Program: Static (sub1)? b, c, d, a, sub2, sub3 (sub3)? b, c, d, a, sub2 Example Program: Static Dynamic (sub2)? b, c, d, a, sub3 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 Dynamic In a dynamic-scoped, the referencing environment is the local variables all visible variables in all active subprograms. A subprogram is active if its execution has begun but has not yet terminated. Evaluation of Dynamic Scoping: Advantage: convenience (easy to implement) Disadvantage: poor readability, unbounded search time

Example Example - declaration of x - declaration of x - call calls calls - declaration of x - declaration of x - call calls calls - reference to x - uses x - reference to x - uses x call Which x?? call For static scoping, it is main s x Example (x) (x) - declaration of x - declaration of x - call - reference to x - call calls calls uses x For dynamic scoping, it is sub1 s x