Lecture 16: Static Semantics Overview 1

Similar documents
Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 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

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

LECTURE 14. Names, Scopes, and Bindings: Scopes

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

The role of semantic analysis in a compiler

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

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

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

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

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

1 Lexical Considerations

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

CMSC 330: Organization of Programming Languages

CS 415 Midterm Exam Spring 2002

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

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

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

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

CSCI312 Principles of Programming Languages!

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

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

CSE 307: Principles of Programming Languages

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

The Structure of a Syntax-Directed Compiler

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

CSCE 314 Programming Languages. Type System

Type Bindings. Static Type Binding

The SPL Programming Language Reference Manual

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

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

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

Names, Scope, and Bindings

Lexical Considerations

Semantic Analysis and Type Checking

5. Semantic Analysis!

5. Semantic Analysis. Mircea Lungu Oscar Nierstrasz

LECTURE 3. Compiler Phases

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

CS558 Programming Languages

Names, Scope, and Bindings

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

CS558 Programming Languages. Winter 2013 Lecture 3

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

Compiler construction

Programmiersprachen (Programming Languages)

CS 415 Midterm Exam Spring SOLUTION

CS558 Programming Languages

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

Lexical Considerations

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

Informatica 3 Syntax and Semantics

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

Semantic Analysis. Compiler Architecture

Names, Scope, and Bindings

The Structure of a Syntax-Directed Compiler

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

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

CSC 533: Organization of Programming Languages. Spring 2005

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

When do We Run a Compiler?

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

G Programming Languages - Fall 2012

5. Semantic Analysis. Mircea Lungu Oscar Nierstrasz

Weeks 6&7: Procedures and Parameter Passing

MIDTERM EXAMINATION - CS130 - Spring 2003

Scope. COMP 524: Programming Language Concepts Björn B. Brandenburg. The University of North Carolina at Chapel Hill

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

Types and Type Inference

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

EI326 ENGINEERING PRACTICE & TECHNICAL INNOVATION (III-G) Kenny Q. Zhu Dept. of Computer Science Shanghai Jiao Tong University

CS558 Programming Languages

Semantic Analysis. Lecture 9. February 7, 2018

Programming Languages Third Edition. Chapter 7 Basic Semantics

CS558 Programming Languages

Project Compiler. CS031 TA Help Session November 28, 2011

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

Questions? Static Semantics. Static Semantics. Static Semantics. Next week on Wednesday (5 th of October) no

Syntax-Directed Translation. Lecture 14

Chapter 9 Subprograms

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

Decaf Language Reference

CSCI B522 Lecture 11 Naming and Scope 8 Oct, 2009

Overview of Semantic Analysis. Lecture 9

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

A declaration may appear wherever a statement or expression is allowed. Limited scopes enhance readability.

CS558 Programming Languages

JavaScript: Sort of a Big Deal,

CS558 Programming Languages

PLAGIARISM. Administrivia. Course home page: Introduction to Programming Languages and Compilers

Introduction to Programming Languages and Compilers. CS164 11:00-12:00 MWF 306 Soda

CS 314 Principles of Programming Languages

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

Programming Languages, Summary CSC419; Odelia Schwartz

Building Compilers with Phoenix

The Pyth Language. Administrivia

Binding and Variables

Transcription:

Lecture 16: Static Semantics Overview 1 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 1 From material by R. Bodik and P. Hilfinger Last modified: Mon Feb 28 18:09:45 2011 CS164: Lecture #16 1

Static vs. Dynamic Weusethetermstaticto describepropertiesthatthecompilercan 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? Actually, distinction is not that simple. E.g., after x = 3 y = x + 2 compiler could deduce that x and y are integers. But languages often designed to require that we treat variables only according to explicitly declared types, because deductions are difficult or impossible in general. Last modified: Mon Feb 28 18:09:45 2011 CS164: Lecture #16 2

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 Last modified: Mon Feb 28 18:09:45 2011 CS164: Lecture #16 3

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 without being declared. Type mismatch: e.g., 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. Last modified: Mon Feb 28 18:09:45 2011 CS164: Lecture #16 4

Output from Static Semantic Analysis Input is AST; output is an annotated tree: identifiers decorated with declarations, other expressions with type information. x = 3 def f (x): stmt list return x+y y = 2 = def = x:#1 3:Int f:#2 id list return y:#4 2:Int x:#3 +:Any Id Type Nesting #1: x, Any, 0 #2: f, Any->Any, 0 #3: x, Any, 1 #4: y, Any, 0 x:#3 y:#4 Last modified: Mon Feb 28 18:09:45 2011 CS164: Lecture #16 5

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. Last modified: Mon Feb 28 18:09:45 2011 CS164: Lecture #16 6

Output from Static Semantic Analysis: Classes In Python (dynamically typed), can write class A(object): def f(self): return self.x a1 = A(); a2 = A() # Create two As a1.x = 3; print a1.x # OK print a2.x # Error; there is no x so can t say much about attributes (fields) of A. In Java, C, C++ (statically typed), analogous program is illegal, even without second print (the class definition itself is illegal). So in statically typed languages, symbol entries for classes would contain dictionaries mapping attribute names to types. Last modified: Mon Feb 28 18:09:45 2011 CS164: Lecture #16 7

Scope Rules: Binding Names to Symbol Entries Scope of a declaration: section of text or program execution in which declaration applies Declarative region: section of text or program execution that bounds scopes of declarations (we ll say region for short). If scope of a declaration defined entirely according to its position in source text of a program, we say language is statically scoped. If scope of a declaration depends on what statements get executed during a particular run of the program, we say language has dynamically scoped. Last modified: Mon Feb 28 18:09:45 2011 CS164: Lecture #16 8

Scope Rules: Name= Declaration is Many-to-One In most languages, can declare the same name multiple times, if its declarations occur in different declarative regions, or involve different kinds of names. Examples from Java?, C++? Last modified: Mon Feb 28 18:09:45 2011 CS164: Lecture #16 9

Scope Rules: Nesting Most statically scoped languages (including C, C++, Java) use: Algol scope rule: Where multiple declarations might apply, choose the one defined in the innermost (most deeply nested) declarative region. Often expressed as inner declarations hide outer ones. Variations on this: Java disallows attempts to hide local variables and parameters. Last modified: Mon Feb 28 18:09:45 2011 CS164: Lecture #16 10

Scope Rules: Declarative Regions Languages differ in their definitions of declarative regions. In Java, variable declaration s effect stops at the closing }, that is, each function body is a declarative region. What others? In Python, function header and body make up a declarative region, as does a lambda expression. But nothing smaller. Just one x in this program: def f(x): x = 3 L = [x for x in xrange(0,10)] Last modified: Mon Feb 28 18:09:45 2011 CS164: Lecture #16 11

Scope Rules: Use Before Definition Languages have taken various decisions on where scopes start. In Java, C++, scope of a member (field or method) includes the entire class (textual uses may precede declaration). But scope of a local variable starts at its declaration. As for non-member and class declarations in C++: must write extern int f(int); // Forward declarations class C; int x = f(3) // Would be illegal w/o forward decls. void g(c* x) {... } int f (int x) {... } // Full definitions class C {... } Last modified: Mon Feb 28 18:09:45 2011 CS164: Lecture #16 12

Scope Rules: Overloading In Java or C++ (not Python or C), can use the same name for more than one method, as long as the number or types of parameters are unique. int add(int a, int b); float add(float a, float b); The declaration applies to the signature name + argument types not just name. But return type not part of signature, so this won t work: int add (int a, int b); float add (int a, int b) In Ada, it will, because the return type is part of signature. Last modified: Mon Feb 28 18:09:45 2011 CS164: Lecture #16 13

Dynamic Scoping Original Lisp, APL, Snobol use dynamic scoping, rather than static: Use of a variable refers to most recently executed, and still active, declaration of that variable. Makes static determination of declaration generally impossible. Example: 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 Last modified: Mon Feb 28 18:09:45 2011 CS164: Lecture #16 14

Explicit vs. Implicit Declaration Java, C++ require explicit declarations of things. C is lenient: if you write foo(3) with no declaration of foo in scope, C will supply one. Python implicitly declares variables you assign to in a function to be local variables. Fortran implicitly declares any variables you use, and gives them a type depending on their first letter. But in all these cases, there is a declaration as far as the compiler is concerned. Last modified: Mon Feb 28 18:09:45 2011 CS164: Lecture #16 15

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). Last modified: Mon Feb 28 18:09:45 2011 CS164: Lecture #16 16

Environment Diagrams and Symbol Entries In Scheme, executing (set! x 7) (define (f x) (let ((y (+ x 39))) (+ x y))) (f 3) would eventually give this environment at (+ x y): global environment x: 7 f:... x: 3 y: 42 current environment Now abstract away values in favor of static type info: #1. x: Any #2. f: Any Any #3. x: Any #4. y: Any and voila! A data structure for mapping names to current declarations: a block-structured symbol table. Last modified: Mon Feb 28 18:09:45 2011 CS164: Lecture #16 17