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

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

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

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University

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

CS558 Programming Languages

Lecture Overview. [Scott, chapter 7] [Sebesta, chapter 6]

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

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

CSE 431S Type Checking. Washington University Spring 2013

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

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

COMP 181. Agenda. Midterm topics. Today: type checking. Purpose of types. Type errors. Type checking

Types. What is a type?

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

INF 212/CS 253 Type Systems. Instructors: Harry Xu Crista Lopes

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

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

Type Checking and Type Equality

CS558 Programming Languages

CSE 307: Principles of Programming Languages

Lecture 12: Data Types (and Some Leftover ML)

INF 212 ANALYSIS OF PROG. LANGS Type Systems. Instructors: Crista Lopes Copyright Instructors.

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

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

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

Type systems. Static typing

Type Checking. Error Checking

Semantic Analysis and Type Checking

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

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

CSCE 314 Programming Languages. Type System

Outline. Introduction Concepts and terminology The case for static typing. Implementing a static type system Basic typing relations Adding context

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

Types. Chapter Six Modern Programming Languages, 2nd ed. 1

Compilers CS S-05 Semantic Analysis

Programming Languages, Summary CSC419; Odelia Schwartz

Compiling and Interpreting Programming. Overview of Compilers and Interpreters

The role of semantic analysis in a compiler

9/7/17. Outline. Name, Scope and Binding. Names. Introduction. Names (continued) Names (continued) In Text: Chapter 5

Data Types (cont.) Subset. subtype in Ada. Powerset. set of in Pascal. implementations. CSE 3302 Programming Languages 10/1/2007

Lexical Considerations

Data Types (cont.) Administrative Issues. Academic Dishonesty. How do we detect plagiarism? Strongly Typed Languages. Type System

CMSC 330: Organization of Programming Languages

Types and Type Inference

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

CS321 Languages and Compiler Design I Winter 2012 Lecture 13

Fundamentals of Programming Languages

Some instance messages and methods

CSCI-GA Scripting Languages

Chapter 7:: Data Types. Mid-Term Test. Mid-Term Test (cont.) Administrative Notes

Lecture 16: Static Semantics Overview 1

Intermediate Code Generation

CSc 520. Principles of Programming Languages 25: Types Introduction

More Lambda Calculus and Intro to Type Systems

Pierce Ch. 3, 8, 11, 15. Type Systems

Introduction to Programming Using Java (98-388)

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

1 Lexical Considerations

Programmiersprachen (Programming Languages)

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

COS 320. Compiling Techniques

22c:111 Programming Language Concepts. Fall Types I

COS 140: Foundations of Computer Science

Lecture 9: Parameter Passing, Generics and Polymorphism, Exceptions

Type Analysis. Type Checking vs. Type Inference

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

Type Checking. Outline. General properties of type systems. Types in programming languages. Notation for type rules.

CSc 520 Principles of Programming Languages. 26 : Control Structures Introduction

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

Outline. General properties of type systems. Types in programming languages. Notation for type rules. Common type rules. Logical rules of inference

Informatica 3 Syntax and Semantics

Compiler Passes. Semantic Analysis. Semantic Analysis/Checking. Symbol Tables. An Example

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

Final-Term Papers Solved MCQS with Reference

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

Announcements. Working on requirements this week Work on design, implementation. Types. Lecture 17 CS 169. Outline. Java Types

Chapter 5 Names, Binding, Type Checking and Scopes

Motivation was to facilitate development of systems software, especially OS development.

Imperative Programming

Programming Languages Third Edition. Chapter 7 Basic Semantics

Concepts of Programming Languages

CS 314 Principles of Programming Languages

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

Control Flow February 9, Lecture 7


Motivation was to facilitate development of systems software, especially OS development.

Note 3. Types. Yunheung Paek. Associate Professor Software Optimizations and Restructuring Lab. Seoul National University

Chapter 7 Control I Expressions and Statements

MIDTERM EXAMINATION - CS130 - Spring 2003

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

CA Compiler Construction

MIDTERM EXAMINATION - CS130 - Spring 2005

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

CSC 533: Organization of Programming Languages. Spring 2005

CS 3360 Design and Implementation of Programming Languages. Exam 1

CS S-06 Semantic Analysis 1

LECTURE 17. Expressions and Assignment

CS 3360 Design and Implementation of Programming Languages. Exam 1

Types and Type Inference

Transcription:

CS412/CS413 Introduction to Compilers Tim Teitelbaum Lecture 13: Types and Type-Checking 19 Feb 07 Semantic Analysis Last time: Semantic errors related to scopes Symbol tables Name resolution This lecture: Semantic errors related to types Type system concepts Types and type-checking CS 412/413 Spring 2007 Introduction to Compilers 1 CS 412/413 Spring 2007 Introduction to Compilers 2 What Are Types? Types describe the values computed during the execution of the program Essentially, types are predicate on values E.g., int x in Java means x [-2 31, 2 31 ) Think: type = set of possible values Type errors: improper, type-inconsistent operations during program execution Type-safety: absence of type errors at run time CS 412/413 Spring 2007 Introduction to Compilers 3 How to Ensure Type-Safety Bind (assign) types, then check types Type binding:defines type for constructs in the program (e.g., variables, functions) Can be either explicit (int x) or implicit (x = 1) Type consistency (safety) = correctness with respect to the type bindings Type checking: determine if the program correctly uses the type bindings Enforce a set of type-checking rules CS 412/413 Spring 2007 Introduction to Compilers 4 Type Checking Type checking: static semantic checks to enforce the type safety of the program Examples: Unary and binary operators (e.g., +, ==, [ ]) must receive operands of the proper type Functions must be invoked with the right number and type of arguments Return statements must agree with the return type In assignments, assigned value must be compatible with type of variable on LHS. Class members accessed appropriately CS 412/413 Spring 2007 Introduction to Compilers 5 Static vs. Dynamic Typing Static and dynamic typing refer to type definitions (i.e., bindings of types to variables, expressions, etc.) Statically typed language: types are defined and checked at compile-time, and do not change during the execution of the program E.g., C, Java, Pascal Dynamically typed language: types defined and checked at run-time, during program execution E.g., Lisp, Scheme, Smalltalk CS 412/413 Spring 2007 Introduction to Compilers 6 1

Strong vs. Weak Typing Strong and weak typing refer to how much type consistency is enforced Strongly typed languages: guarantees that accepted programs are type-safe Weakly typed languages: allow programs that contain type errors Can achieve strong typing using either static or dynamic typing Soundness Sound type systems: can statically ensure that the program is type-safe Soundness implies strong typing Static type safety requires a conservative approximation of the values that may occur during all possible executions May reject type-safe programs Need to be expressive: reject as few type-safe programs as possible CS 412/413 Spring 2007 Introduction to Compilers 7 CS 412/413 Spring 2007 Introduction to Compilers 8 Concept Summary Classification Strong Typing Weak Typing Static vs dynamic typing: when to define/check types? Strong vs weak typing: how many type errors? Sound type systems: statically catch all type errors Static Typing Dynamic Typing ML Java Pascal Modula-3 Scheme PostScript C C++ assembly code Smalltalk CS 412/413 Spring 2007 Introduction to Compilers 9 CS 412/413 Spring 2007 Introduction to Compilers 10 Why Static Checking? Efficient code Dynamic checks slow down the program Guarantees that all executions will be safe Dynamic checking gives safety guarantees only for some execution of the program But is conservative for sound systems Needs to be expressive: reject few type-safe programs Type Systems Type is predicate on value Type expressions: describe the possible types in the program: int, string, array[], Object, etc. Type system: defines types for language constructs (e.g., expressions, statements) CS 412/413 Spring 2007 Introduction to Compilers 11 CS 412/413 Spring 2007 Introduction to Compilers 12 2

Type Expressions Languages have basic types (a.k.a. primitive types or ground types) E.g., int, char, boolean Build type expressions using basic types: Type constructors Type aliases Array Types Various kinds of array types in different programming languages array(t) : array with elements of type T and no bounds C, Java: int [ ], Modula-3: array of integer array(t, S) : array with size C: int[10], Modula-3: array[10] of integer May be indexed 0..size-1 array(t,l,u) : array with upper/lower bounds Pascal or Ada: array[2.. 5] of integer array(t, S 1,, S n ) : multi-dimensional arrays FORTRAN: real(3,5) CS 412/413 Spring 2007 Introduction to Compilers 13 CS 412/413 Spring 2007 Introduction to Compilers 14 Record Types A record is {id 1 : T 1,, id n : T n for some identifiers id i and types T i Supports access operations on each field, with corresponding type C: struct { int a; float b; Pascal: record a: integer; b: real; end Objects: generalize the notion of records Pointer Types Pointer types characterize values that are addresses of variables of other types Pointer(T) : pointer to an object of type T C pointers: Pascal pointers: Java: T* (e.g., int *x;) ^T (e.g., x: ^integer;) object references CS 412/413 Spring 2007 Introduction to Compilers 15 CS 412/413 Spring 2007 Introduction to Compilers 16 Function Types Type: T 1 T 2 T n T r Function value can be invoked with some argument expressions with types T i, returns return type T r C functions: int pow(int x, int y) type: int int int Java: methods have function types Some languages have first-class functions usually in functional languages, e.g., ML, LISP C and C++ have function pointers Java doesn t Type Aliases Some languages allow type aliases (type definitions, equates) C: typedef int int_array[ ]; Modula-3: type int_array = array of int; Java doesn t allow type aliases Aliases are not type constructors! int_array is the same type as int [ ] Different type expressions may denote the same type CS 412/413 Spring 2007 Introduction to Compilers 17 CS 412/413 Spring 2007 Introduction to Compilers 18 3

Implementation Use a separate class hierarchy for types: class BaseType extends Type { class IntType extends BaseType { class BoolType extends Base Type { class ArrayType extends Type { Type elemtype; class FunctionType extends Type { Semantic analysis translates all type expressions to type objects Symbol table binds name to type object Type Comparison Option 1: implement a method T1.Equals(T2) Must compare type trees of T1 and T2 For object-oriented language: also need sub-typing: T1.SubtypeOf(T2) Option 2: use unique objects for each distinct type each type expression (e.g., array[int] ) resolved to same type object everywhere Faster type comparison: can use == Object-oriented: check subtyping of type objects CS 412/413 Spring 2007 Introduction to Compilers 19 CS 412/413 Spring 2007 Introduction to Compilers 20 Creating Type Objects Build types while parsing use a syntaxdirected definition: non terminal Type type type ::= BOOLEAN {: RESULT = new BoolType(); : ARRAY LBRACKET type:t RBRACKET {: RESULT = new ArrayType(t); : Type objects = AST nodes for type expressions Processing Type Declarations Type declarations add new identifiers and their types in the symbol tables Class definitions must be added to symbol table: class_defn ::= CLASS ID:id { decls:d Forward references require multiple passes over AST to collect legal names class A { B b; class B { CS 412/413 Spring 2007 Introduction to Compilers 21 CS 412/413 Spring 2007 Introduction to Compilers 22 Type-Checking Type-checking = verify typing rules operands of + must be integer expressions; the result is an integer expression Option 1: Implement using syntax-directed definitions (type-check during the parsing) expr ::= expr:t1 PLUS expr:t2 {: if (t1 == IntType && t2 == IntType) RESULT = IntType; else throw new TypeCheckError( + ); : CS 412/413 Spring 2007 Introduction to Compilers 23 Type-Checking Option 2: implement type-checking by an AST visitor class typecheck implements Visitor { Object visit(add e, Object symboltable) { Type t1 = (int) e.e1.accept(this, symboltable); Type t2 = (int) e.e2.accept(this, symboltable); if (t1 == Int && t2 == Int) return Int; else throw new TypeCheckError( + ); Object visit(num e, Object symboltable) { return Int; Object visit(id e, Object symboltable) { return (SymbolTable)symbolTable.lookupType(e); CS 412/413 Spring 2007 Introduction to Compilers 24 4

Next Time: Static Semantics Static semantics = mathematical description of typing rules for the language Static semantics formally defines types for all legal language ASTs CS 412/413 Spring 2007 Introduction to Compilers 25 5