Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.

Slides:



Advertisements
Similar presentations
CH4.1 Type Checking Md. Fahim Computer Engineering Department Jamia Millia Islamia (A Central University) New Delhi –
Advertisements

CS3012: Formal Languages and Compilers Static Analysis the last of the analysis phases of compilation type checking - is an operator applied to an incompatible.
CPSC 388 – Compiler Design and Construction
Chapter 6 Type Checking. The compiler should report an error if an operator is applied to an incompatible operand. Type checking can be performed without.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
1 Mooly Sagiv and Greta Yorsh School of Computer Science Tel-Aviv University Modern Compiler Design.
Winter Compiler Construction T7 – semantic analysis part II type-checking Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv.
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
Type Checking Compiler Design Lecture (02/25/98) Computer Science Rensselaer Polytechnic.
Type Checking.
Compiler Construction
Compiler Principle and Technology Prof. Dongming LU Mar. 28th, 2014.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Ch.2: Syntax and Semantics Fall 2005.
1 Names, Scopes and Bindings. 2 Names Kinds of names Kinds of names Variables, functions, classes, types, labels, blocks, operators, tasks, etc. Variables,
Chapter 3 Program translation1 Chapt. 3 Language Translation Syntax and Semantics Translation phases Formal translation models.
1 Type Type system for a programming language = –set of types AND – rules that specify how a typed program is allowed to behave Why? –to generate better.
Compiler Construction Semantic Analysis I Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.
Programming Languages 2nd edition Tucker and Noonan Chapter 6 Types I was eventually persuaded of the need to design programming notations so as to maximize.
Cs164 Prof. Bodik, Fall Symbol Tables and Static Checks Lecture 14.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
Imperative Programming
CSc 453 Semantic Analysis Saumya Debray The University of Arizona Tucson.
Semantic Analysis CS 671 February 5, CS 671 – Spring The Compiler So Far Lexical analysis Detects inputs with illegal tokens –e.g.: main$
COP4020 Programming Languages
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
Semantics CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
March 12, ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Programming Languages (ICE 1341) Lecture #6 Programming Languages (ICE 1341)
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 2.
COP4020 Programming Languages Semantics Prof. Xin Yuan.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 8: Semantic Analysis and Symbol Tables.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
410/510 1 of 18 Week 5 – Lecture 1 Semantic Analysis Compiler Construction.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
Compiler Construction Dr. Noam Rinetzky and Orr Tamir School of Computer Science Tel Aviv University
CPS 506 Comparative Programming Languages Syntax Specification.
Programming Languages 2nd edition Tucker and Noonan Chapter 6 Type Systems I was eventually persuaded of the need to design programming notations so as.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
Semantic Analysis Semantic Analysis v Lexically and syntactically correct programs may still contain other errors v Lexical and syntax analyses.
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
CS536 Types 1. Roadmap Back from our LR Parsing Detour Name analysis – Static v dynamic – Scope Today – Type checking 2 Scanner Parser Tokens Semantic.
Semantic Analysis II. Messages Please check lecturer notices in the Moodle Appeals  Legitimate: “I don’t have the bug you mentioned…”  Illegitimate:
Bernd Fischer RW713: Compiler and Software Language Engineering.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
CS412/413 Introduction to Compilers Radu Rugina Lecture 13 : Static Semantics 18 Feb 02.
C H A P T E R T H R E E Type Systems and Semantics Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
CS412/413 Introduction to Compilers Radu Rugina Lecture 11: Symbol Tables 13 Feb 02.
6. Semantic Analysis. Semantic Analysis Phase – Purpose: compute additional information needed for compilation that is beyond the capabilities of Context-
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
Chapter 1: Preliminaries Lecture # 2. Chapter 1: Preliminaries Reasons for Studying Concepts of Programming Languages Programming Domains Language Evaluation.
Compiler Design Lecture 10 Semantic Analysis. int aintegers int a[2][3]array(2, array(3, integer)) int f(int, float, char) int x float x char  int Int.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Compiler Design Lecture 10 Semantic Analysis. int aintegers int a[2][3]array(2, array(3, integer)) int f(int, float, char) int x float x char  int Int.
Dr. M. Al-Mulhem Introduction 1 Chapter 6 Type Systems.
LECTURE 10 Semantic Analysis. REVIEW So far, we’ve covered the following: Compilation methods: compilation vs. interpretation. The overall compilation.
Language-Based Security: Overview of Types Deepak Garg Foundations of Security and Privacy October 27, 2009.
Lecture 9 Symbol Table and Attributed Grammars
Chapter 3 – Describing Syntax
Names and Attributes Names are a key programming language feature
Semantic Analysis Type Checking
Constructing Precedence Table
Semantic Analysis with Emphasis on Name Analysis
Semantic Analysis Chapter 6.
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
CSCE 330 Programming Language Structures Ch.2: Syntax and Semantics
Semantic Analysis Chapter 6.
Compiler Construction
CSCE 314: Programming Languages Dr. Dylan Shell
Compiler Construction
Presentation transcript:

Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006

- 1 - Announcements v Updated schedule »Today: Semantic analysis II »Mon 10/23: MIRV Q/A session (Yuan Lin) »Wed 10/25: Semantic analysis III (Simon Chen) »Mon 10/30: Exam review »Wed 11/1: Exam 1 in class v Project 2 »Teams of 2  Please send Simon/I mail with names  Persons can work individually if really want to »No extensions on deadline due to Exam, so get started! v Reading

- 2 - From Last Time – Hierarchical Symbol Table int x; void f(int m) { float x, y;... {int i, j;....; } {int x; l:...; } } int g(int n) { char t;... ; } xvarint ffuncint  void gfuncint  int margint xvarfloat yvarfloat nargint tvarchar ivarint jvarint xvarint llabel Global symtab func f symtab func g symtab

- 3 - Catching Semantic Errors int x; void f(int m) { float x, y;... {int i, j; x=1; } {int x; l: i=2; } } int g(int n) { char t; x=3; } xvarint ffuncint  void gfuncint  int margint xvarfloat yvarfloat nargint tvarchar ivarint jvarint xvarint llabel Global symtab i=2 Error! undefined variable

- 4 - Symbol Table Operations v Two operations: »To build symbol tables, we need to insert new identifiers in the table »In the subsequent stages of the compiler we need to access the information from the table: use lookup function v Cannot build symbol tables during lexical analysis »Hierarchy of scopes encoded in syntax v Build the symbol tables: »While parsing, using the semantic actions »After the AST is constructed

- 5 - Forward References v Use of an identifier within the scope of its declaration, but before it is declared v Any compiler phase that uses the information from the symbol table must be performed after the table is constructed v Cannot type-check and build symbol table at the same time v Example class A { int m() {return n(); } int n() {return 1; } }

- 6 - Back to Type Checking v What are types? »They describe the values computed during the execution of the program »Essentially they are a predicate on values  E.g., “int x” in C means –2^31 <= x < 2^31 v Type Errors: improper or inconsistent operations during program execution v Type-safety: absence of type errors

- 7 - How to Ensure Type-Safety v Bind (assign) types, then check types v Type binding: defines type of 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 v Type checking: determine if the program correctly uses the type bindings »Consists of a set of type-checking rules

- 8 - Type Checking v Semantic checks to enforce the type safety of the program v 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

Concepts Related to Types/Languages 1. Static vs dynamic checking »When to check types 2. Static vs dynamic typing »When to define types 3. Strong vs weak typing »How many type errors 4. Sound type systems »Statically catch all type errors

Static vs Dynamic Checking v Static type checking »Perform at compile time v Dynamic type checking »Perform at run time (as the program executes) v Examples of dynamic checking »Array bounds checking »Null pointer dereferences

Static vs Dynamic Typing v Static and dynamic typing refer to type definitions (i.e., bindings of types to variables, expressions, etc.) v Static typed language »Types defined at compile-time and do not change during the execution of the program  C, C++, Java, Pascal v Dynamically typed language »Types defined at run-time, as program executes  Lisp, Smalltalk

Strong vs Weak Typing v Refer to how much type consistency is enforced v Strongly typed languages »Guarantee accepted programs are type-safe v Weakly typed languages »Allow programs which contain type errors v These concepts refer to run-time »Can achieve strong typing using either static or dynamic typing

Soundness v Sound type systems: can statically ensure that the program is type-safe v Soundness implies strong typing v 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

Class Problem Strong TypingWeak Typing Static Typing Dynamic Typing Classify the following languages: C, C++, Pascal, Java, Scheme ML, Postscript, Modula-3, Smalltalk, assembly code

Why Static Checking? v Efficient code »Dynamic checks slow down the program v Guarantees that all executions will be safe »Dynamic checking gives safety guarantees only for some execution of the program v But is conservative for sound systems »Needs to be expressive: reject few type-safe programs

Type Systems v What are types? »They describe the values computed during the execution of the program »Essentially they are a predicate on values  E.g., “int x” in C means –2^31 <= x < 2^31 v Type expressions: Describe the possible types in the program »E.g., int, char*, array[], object, etc. v Type system: Defines types for language constructs »E.g., expressions, statements

Type Expressions v Language type systems have basic types (aka: primitive types or ground types) »E.g., int, char*, double v Build type expressions using basic types: »Type constructors  Array types  Structure/object types  Pointer types »Type aliases »Function types

Type Comparison v Option 1: Implement a method T1.Equals(T2) »Must compare type trees of T1 and T2 »For object-oriented languages: also need sub-typing, T1.SubtypeOf(T2) v 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

Creating Type Objects v Build types while parsing – use a syntax- directed definition v Type objects = AST nodes for type expressions non terminal Type type type : INTEGER {$$ = new IntType(id); } | ARRAY LBRACKET type RBRACKET {$$ = new ArrayType($3); } ;

Processing Type Declarations v Type declarations add new identifiers and their types in the symbol tables v Class definitions must be added to symbol table: »class_defn : CLASS ID {decls} ; v Forward references require multiple passes over AST to collect legal names »class A {B b; } »class B {... }

Type Checking v Type checking = verify typing rules »E.g., “Operands of + must be integer expressions; the result is an integer expression” v Option 1: Implement using syntax-directed definitions (type-check during the parsing) expr: expr PLUS expr { if ($1 == IntType && $3 == IntType) $$ = IntType else TypeCheckError(“+”); }

Type Checking (2) v Option 2: First build the AST, then implement type checking by recursive traversal of the AST nodes: class Add extends Expr { Type typeCheck() { Type t1 = e1.typeCheck(), t2 = e2.typeCheck(); if (t1 == Int && t2 == Int) return Int else TypeCheckError(“+”); }

Type Checking Identifiers v Identifier expressions: Lookup the type in the symbol table class IdExpr extends Expr { Identifier id; Type typeCheck() {return id.lookupType(); } }

Next Time: Static Semantics v Can describe the types used in a program v How to describe type checking v Static semantics: Formal description for the programming language v Is to type checking: »As grammar is to syntax analysis »As regular expression is to lexical analysis v Static semantics defines types for legal ASTs in the language