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

Similar documents
Lecture 16: Static Semantics Overview 1

Syntax Errors; Static Semantics

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.

1 Terminology. 2 Environments and Static Scoping. P. N. Hilfinger. Fall Static Analysis: Scope and Types

LECTURE 14. Names, Scopes, and Bindings: Scopes

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

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

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

The role of semantic analysis in a compiler

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

5. Semantic Analysis. Mircea Lungu Oscar Nierstrasz

5. Semantic Analysis!

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

Syntax-Directed Translation. Lecture 14

1 Lexical Considerations

Names, Scope, and Bindings

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

Compiler construction

Semantic Analysis. Compiler Architecture

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

Lexical Considerations

Semantic Analysis and Type Checking

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

Names, Scope, and Bindings

The Structure of a Syntax-Directed Compiler

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

5. Semantic Analysis. Mircea Lungu Oscar Nierstrasz

CSCE 314 Programming Languages. Type System

The SPL Programming Language Reference Manual

CSCI312 Principles of Programming Languages!

Names, Scope, and Bindings

Every language has its own scoping rules. For example, what is the scope of variable j in this Java program?

The compilation process is driven by the syntactic structure of the program as discovered by the parser

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

Lexical Considerations

CSE 307: Principles of Programming Languages

Types and Type Inference

4 Bindings and Scope. Bindings and environments. Scope, block structure, and visibility. Declarations. Blocks. 2004, D.A. Watt, University of Glasgow

JavaScript: Sort of a Big Deal,

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

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

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

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

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

Decaf Language Reference

Symbol Table Information. Symbol Tables. Symbol table organization. Hash Tables. What kind of information might the compiler need?

Type Inference Systems. Type Judgments. Deriving a Type Judgment. Deriving a Judgment. Hypothetical Type Judgments CS412/CS413

CMSC 330: Organization of Programming Languages

CS558 Programming Languages

Typescript on LLVM Language Reference Manual

Symbol Tables. For compile-time efficiency, compilers often use a symbol table: associates lexical names (symbols) with their attributes

When do We Run a Compiler?

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

Time : 1 Hour Max Marks : 30

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

The PCAT Programming Language Reference Manual

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

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

CS558 Programming Languages

LECTURE 3. Compiler Phases

Today. Assignments. Lecture Notes CPSC 326 (Spring 2019) Quiz 5. Exam 1 overview. Type checking basics. HW4 due. HW5 out, due in 2 Tuesdays

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

A Short Summary of Javali

Writing Evaluators MIF08. Laure Gonnord

IPCoreL. Phillip Duane Douglas, Jr. 11/3/2010

(Not Quite) Minijava

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division

Type Bindings. Static Type Binding

Visitors. Move functionality

The Structure of a Syntax-Directed Compiler

Types and Type Inference

Topic 6: Types COS 320. Compiling Techniques. Princeton University Spring Prof. David August. Adapted from slides by Aarne Ranta

Semantic Analysis. Lecture 9. February 7, 2018

CS558 Programming Languages

G Programming Languages - Fall 2012

Programmiersprachen (Programming Languages)

CS558 Programming Languages. Winter 2013 Lecture 3

Informatica 3 Syntax and Semantics

Properties of an identifier (and the object it represents) may be set at

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

Scope. Chapter Ten Modern Programming Languages 1

Run-time Environments - 2

Reading Assignment. Scanner. Read Chapter 3 of Crafting a Compiler.

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

CS /534 Compiler Construction University of Massachusetts Lowell

Project Compiler. CS031 TA Help Session November 28, 2011

Overview of Semantic Analysis. Lecture 9

CS558 Programming Languages

CS 314 Principles of Programming Languages

Special Topics: Programming Languages

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

About this exam review

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

CS558 Programming Languages

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

CA Compiler Construction

Array. Prepared By - Rifat Shahriyar

Transcription:

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

Current Status Lexical analysis Produces tokens Detects & eliminates illegal tokens Parsing Produces trees Detects & eliminates ill-formed parse trees Static semantic analysis we are here Produces decorated tree with additional information attached Detects & eliminates remaining static errors 2/29/08 Prof. Hilfinger, CS164 Lecture 15 2

Static vs. Dynamic We use the term static to describe properties that the compiler can determine without considering any particular execution. E.g., in def f(x) : x + 1 Both uses of x refer to same variable Dynamic properties are those that depend on particular executions in general. E.g., will x = x/y cause an arithmetic exception. 2/29/08 Prof. Hilfinger, CS164 Lecture 15 3

Static vs. Dynamic, contd. Actually, distinction is not that simple. E.g., after x = 3 y = x + 2 compiler could deduce that x and y are Ints But languages often designed to require that we treat variables only according to explicitly declared types, because deductions are difficult or impossible in general. 2/29/08 Prof. Hilfinger, CS164 Lecture 15 4

Typical Tasks of the Semantic Analyzer Find the declaration that defines each identifier instance Determine the static types of expressions Perform re-organizations of the AST that were inconvenient in parser, or required semantic information Detect errors and fix to allow further processing 2/29/08 Prof. Hilfinger, CS164 Lecture 15 5

Typical Semantic Errors: Java, C++ Multiple declarations: a variable should be declared (in the same region) at most once Undeclared variable: a variable should not be used before being declared. Type mismatch: type of the left-hand side of an assignment should match the type of the right-hand side. Wrong arguments: methods should be called with the right number and types of arguments. Definite-assignment check (Java): conservative check that simple variables assigned to before use. 2/29/08 Prof. Hilfinger, CS164 Lecture 15 6

Output from Static Semantic Analysis Input is AST; output is an annotated tree. x = 3 def f (x): return x+y y: Int y = 2 #1: x, Any, 0 #2: f, Any->Any, 0 #3: x, Any, 1 #4: y, Int, 0 x = def type_decl = 3 f return y Int y 2 #1 #2 Int 2/29/08 Prof. Hilfinger, CS164 Lecture 15 7 x #3 x + Any y #3 #4 #4 #4 Ids decorated with declarations. Expressions annotated with (static) types. Int

Output from Static Semantic Analysis (II) Analysis has added objects we ll call symbol entries to hold information about instances of identifiers. In this example, #1: x, Any, 0 denotes an entry for something named x occurring at the outer lexical level (level 0) and having static type Any. For other expressions, we annotate with static type information. 2/29/08 Prof. Hilfinger, CS164 Lecture 15 8

Output from Static Semantic Analysis (III) Symbol entries for classes (or modules, packages, namespaces and the like) must contain equivalent of a dictionary mapping names of members to symbol entries. So given an expression such as x.clear (), we find symbol entry for x find type (class) of x from its entry, and find clear in dictionary of entry associated with x s type. 2/29/08 Prof. Hilfinger, CS164 Lecture 15 9

Binding names to symbol entries: Scoping Scope of a declaration: section of text where it applies Declarative region: section of text that bounds scopes of declarations (we ll say region for short) In most languages, the same name can be declared multiple times if its declarations occur in different declarative regions, and/or involve different kinds of names. 2/29/08 Prof. Hilfinger, CS164 Lecture 15 10

Scoping: example Java: can use same name for a class, field of the class, a method of the class, and a local variable of the method legal Java program: class Test { int Test; Test( ) { double Test; } } 2/29/08 Prof. Hilfinger, CS164 Lecture 15 11

Scoping: general rules The scope rules of a language: determine which declaration of a named object corresponds to each use of the object. Scoping rules map uses of objects to their declarations (or symbol entries). C++ and Java use static scoping: mapping from uses to declarations is made at compile time. C++ uses Algol scope rules a use of variable x matches the declaration with the most closely enclosing scope. a deeply nested variable x hides x declared in an outer region. in Java: inner regions cannot define variables defined in outer regions 2/29/08 Prof. Hilfinger, CS164 Lecture 15 12

Scoping Options: Declarative Regions In Java, each function has one or more declarative regions: one for the function body, and possibly additional regions in the function for each for loop and each nested block (delimited by curly braces) In Pyth, each function has one per function (possibly plus more for nested functions) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 13

Example (assume C++ rules) double y, k; void f(int y) { y = k; int k = 0; while (k) { int k = 1; } } // y and k global variables // y also used as a parameter // Uses global k and parameter y // k is now local variable // refers to 2nd decl of k // another local var, in a loop (not ok in Java) global k hidden starting here the outermost region includes decls of "f, k and y function f itself has two (nested) regions: 1. The outer region for f includes parameter y and local variable k. 2. The innermost region is for the body of the while loop, and includes the variable k that is initialized to 1. 2/29/08 Prof. Hilfinger, CS164 Lecture 15 14

Scoping Options: overloading Java and C++ (but not in Pascal, C, or Pyth): can use the same name for more than one method as long as the number and/or types of parameters are unique. int add(int a, int b); float add(float a, float b); 2/29/08 Prof. Hilfinger, CS164 Lecture 15 15

Scoping options: Use before declaration? Can names be used before they are defined? Java, C++: a method or field name can be used before the definition appears; not true for a variable, whose scope starts at its declaration In Pyth, almost anything can be used before declaration, where syntactically possible 2/29/08 Prof. Hilfinger, CS164 Lecture 15 16

Scoping Options: Dynamic scoping Not all languages use static scoping. Original Lisp, APL, and Snobol use dynamic scoping. Dynamic scoping: A use of a variable that has no corresponding declaration in the same function corresponds to the declaration in the most-recently-called still active function. With this rule, difficult for compiler to determine much about identifiers 2/29/08 Prof. Hilfinger, CS164 Lecture 15 17

Example For example, consider the following code: void main() { f1(); f2(); } void f1() { int x = 10; g(); } void f2() { String x = "hello"; f3();g(); } void f3() { double x = 30.5; } void g() { print(x); } With static scoping, illegal. With dynamic scoping, prints 10 and hello 2/29/08 Prof. Hilfinger, CS164 Lecture 15 18

So How do we Annotate with Declarations? Idea is to recursively navigate the AST, in effect executing the program in simplified fashion, extracting information that isn t data dependent. You saw it in CS61A (sort of). 2/29/08 Prof. Hilfinger, CS164 Lecture 15 19

Environment Diagrams and Symbol Entries In Scheme, a program like (set! x 7) (define (f x) (let ((y (+ x 39)) (+ x y))) (f 3) when executed would eventually give you something like this when computing (+ x y) X: 7 f: x: 3 y: 42 current environment global environment 2/29/08 Prof. Hilfinger, CS164 Lecture 15 20

Environment Diagrams to Symbol Entries Abstracting from this particular execution, X: 7 f: x: 3 y: 42 current environment we take away the values and replace with static info: #1. X: Any #2. f: Any->Any #3. x: Any #4. y: Any current environment and we re left with symbol entries and a data structure (a kind of symbol table) for looking them up. 2/29/08 Prof. Hilfinger, CS164 Lecture 15 21

Annotating Pyth with Symbol Entries Process requires several passes (traversals of AST). A rough outline: 1. Create symbol entries for all classes and record in global environment, plus entries for all class members stored in the class s entries. 2. For each declarative region, A. Create a new environment box for each declaration immediately inside the region. B. Use it to annotate all uses, and recursively do step 2 for all inner declarative regions. 2/29/08 Prof. Hilfinger, CS164 Lecture 15 22

Type Checking the job of the type-checking phase is to: Determine the type of each expression in the program (each node in the AST that corresponds to an expression) Find type errors The type rules of a language define how to determine expression types, and what is considered to be an error. The type rules specify, for every operator (including assignment), what types the operands can have, and what is the type of the result. 2/29/08 Prof. Hilfinger, CS164 Lecture 15 23

Type Errors The type checker must also 1. find type errors having to do with the context of expressions, e.g., the context of some operators must be boolean, 2. type errors having to do with method calls. Examples of the context errors: the condition of an if not boolean (Java) type of returned value not function s return type Examples of method errors: calling something that is not a method calling a method with the wrong number of arguments calling a method with arguments of the wrong types 2/29/08 Prof. Hilfinger, CS164 Lecture 15 24