Semantic Analysis Type Checking

Slides:



Advertisements
Similar presentations
CS3012: Formal Languages and Compilers Static Analysis the last of the analysis phases of compilation type checking - is an operator applied to an incompatible.
Advertisements

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.
1 Mooly Sagiv and Greta Yorsh School of Computer Science Tel-Aviv University Modern Compiler Design.
Overview of Previous Lesson(s) Over View  Front end analyzes a source program and creates an intermediate representation from which the back end generates.
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
CSE 341, Winter Type Systems Terms to learn about types: –Type –Type system –Statically typed language –Dynamically typed language –Type error –Strongly.
Lecture # 20 Type Systems. 2 A type system defines a set of types and rules to assign types to programming language constructs Informal type system rules,
Type Checking Compiler Design Lecture (02/25/98) Computer Science Rensselaer Polytechnic.
Chapter 6 Type Checking Section 0 Overview 1.Static Checking Check that the source program follows both the syntactic and semantic conventions of the source.
Type Checking.
Compiler Construction
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
1 Names, Scopes and Bindings. 2 Names Kinds of names Kinds of names Variables, functions, classes, types, labels, blocks, operators, tasks, etc. Variables,
Chapter 6 Type Checking Section 0 Overview
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.
Static checking and symbol table Chapter 6, Chapter 7.6 and Chapter 8.2 Static checking: check whether the program follows both the syntactic and semantic.
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
Sahar Mosleh California State University San MarcosPage 1 A for loop can contain multiple initialization actions separated with commas Caution must be.
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.
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
Static Checking and Type Systems Chapter 6. 2 Static versus Dynamic Checking Static checking: the compiler enforces programming language’s static semantics.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
Chapter 8: Semantic Analyzer1 Compiler Designs and Constructions Chapter 8: Semantic Analyzer Objectives: Syntax-Directed Translation Type Checking Dr.
Chapter 1: Preliminaries Lecture # 2. Chapter 1: Preliminaries Reasons for Studying Concepts of Programming Languages Programming Domains Language Evaluation.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Dr. M. Al-Mulhem Introduction 1 Chapter 6 Type Systems.
Chapter4 Syntax-Directed Translation Introduction : 1.In the lexical analysis step, each token has its attribute , e.g., the attribute of an id is a pointer.
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
Language-Based Security: Overview of Types Deepak Garg Foundations of Security and Privacy October 27, 2009.
Lecture 9 Symbol Table and Attributed Grammars
Types Type Errors Static and Dynamic Typing Basic Types NonBasic Types
Compiler Design – CSE 504 Type Checking
Type Checking and Type Inference
Programming Languages and Compilers (CS 421)
Chapter 7: Expressions and Assignment Statements
Chapter 6 – Data Types CSCE 343.
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
Context-Sensitive Analysis
Type Checking, and Scopes
Constructing Precedence Table
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Lecture 4: Type Systems.
Lecture 16: Introduction to Data Types
Representation, Syntax, Paradigms, Types
Chapter 7: Expressions and Assignment Statements
Type Systems Terms to learn about types: Related concepts: Type
Semantic Analysis Chapter 6.
Data Types.
Programming Languages and Compilers (CS 421)
Static Checking and Type Systems
CSE 3302 Programming Languages
Chapter 6 Intermediate-Code Generation
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
Representation, Syntax, Paradigms, Types
Representation, Syntax, Paradigms, Types
SYNTAX DIRECTED DEFINITION
Names and Binding In Text: Chapter 5.
Representation, Syntax, Paradigms, Types
Compiler Construction
Type Systems Terms to learn about types: Related concepts: Type
Compiler Construction
Types and Related Issues
Programming Languages and Compilers (CS 421)
Lecture 7: Types (Revised based on the Tucker’s slides) 10/4/2019
Presentation transcript:

Semantic Analysis Type Checking

Type Checking Type checking Example typing rules Check type safety against typing rules During the semantic analysis phase Example typing rules Type rule for built in arithmetic operators E.g., operands of % should be integer E.g., operands of + have to be consistent (all integers or all floats) A function has the same number of formal and actual parameters The types of the formal and actual parameters of a function have to match The array range is not violated

Type Checking -- Attribute Grammar First handle type definitions Statement for basic type definitions (discussed earlier) D  id L addtype (id.entry, L.type) L  , id L1 L.type := L1.type; addtype (id.entry, L1.type) L  : T L.type := T.type T  int T.type := integer T  real T.type := real … Then perform type checking within statements

Type Checking -- Attribute Grammar Type checking for the statements from bottom up L  S; L1 L.type  if (S.type = void and L1.type = void) then void else type error S  id := E S.type := if (id.type = E.type) then void else type error E  E1 + T if (E1.type = T.type) then E.type = E1.type else type error S  while C do S1 S.type := if C.type = Boolean then S1.type else type error S  if C then S1 else S2 S.type := if C.type = Boolean then ? else type error if (S1.Type = void and S2.Type = void) then return void

Type Definitions Basic Types Compound types Integer, real, Boolean, char, etc. Compound types Pointers Type = pointer (basic-type) Array Type = array (dimension) Structure Type = structure with the types of all fields Function Type = function with the types of all parameters

Type Definitions Type equivalence Subtypes array(a,b) array(c,d) In some situations, like parameter passing, they are structurally equivalent, or they have a common super type of 2D array struct A { int ai; char ac; } struct B { int bi; char bc; } A and B are structurally equivalent Subtypes Define subtype relation e.g., A  B: A is a subtype of B A may be accepted anywhere B is expected

Static and Dynamic Type Checking Static typing Types can be determined and checked at compilation time A variable has a single type throughout its lifetime More conservative, correct program may be rejected Dynamic typing Types are defined and checked at execution time Type may change during execution Degrading performance for type checking at run time Combined Some type checking may not be done fully statically Do all possible checking statically, and leave the uncertain type checking till run time

Static and Dynamic Type Checking Example: static type checking is conservative Class A { cat: Category; a(): …; } Class B inherits A { cat = B; b(): …; } Class C inherits A { cat = C; c(): …; } B  A, C  A F (obj: A) { switch obj.cat: B: obj.a(); obj.b(); C: obj.a(); obj.c(); default: … endcase; } The program would be correct with dynamic type checking But will fail on static type checking No function b() in class A

Static and Dynamic Type Checking Example: static type checking is conservative function f(x) { return x < 10 ? y : z(); } if (…) then f(5); else f(15); The program would be correct with dynamic type checking Because only one branch is executed and type checked But will fail on static type checking Inconsistent function type Assume that y and z() are of different types

Static and Dynamic Type Checking Static type checking -- example rules Type matching for functions and expressions Type matching (Boolean) for conditional statements Java also checks Illegitimate type casting (only allowed between sub-super types) Accesses to private fields Dynamic type checking -- example rules Divide by 0, array bound OCaml combines static and dynamic type checking E.g., check array bound errors during dynamic type checking Script languages such as Python, Ruby only perform dynamic type checking: Use general static typing rules

Static Type Checking -- Correctness Soundness Given type rule X, a static checker is supposed to assure X A static checker is sound if it never accepts a program that may violate X during execution (given any input) No false negatives, never have: “a faulty behavior is not caught” Completeness A static checker is complete if it never rejects a program P if P will never violate X during its execution with any input No false positives In general, static checker is designed to be sound, but not complete

Static Type Checking -- Type Inference Language without type definitions Still can perform static type checking Need to perform type inference to assign types to variables First proposed and applied to -calculus, then ML, then Haskell Type inference method Build parse tree (or AST) Assign type variables to identifiers Generate constraints based on (1) Constants (2) Built-in operators +. *, %, etc., (3) Typed functions (e.g. in ML, first, tail) All the steps above can be done during parsing (attribute grammar) Solve constraints Undetermined type variables  Has polymorphic type  Use the closest super type for the variable Haskell uses bottom up type inference on parse tree Possible because it has very strict typing rules - Overloaded operators should be type consistent e.g., int = int + int, real = real + real - IO has strict type assignments

Static Type Checking -- Type Inference You can build a graph (matrix or list) to show the relations of variables. If a type variable has relation to another, mark the matrix. During inference, whenever one type variable changes value, check all those that has relations to it and propagate the check. Or, you can maintain another list to record the type variables that have changed in a round, and propagate the check round by round. Static Type Checking -- Type Inference Example Example type rules Operands of arithmetic operators have to be one of the number types (int, float, double) (sub-super type is fine), and the result should be a super type of the operands Operands and the result of % have to be integer Array index has to be natural numbers (integer) Example program Input (a, s, t, n); x := a + 2.5; y := s % t; L = (“this”, “is”, “a”, “list”); elem = L[n]; m := y / n; v = m * a; If / is overloaded for int and float/double, then “y / n” should be integer division Type error: y := s % x float  x.type  double a.type double y.type = s.type = t.type = int L.type = array of strings elem.type = string n.type = int m.type = int v.type  double

Strong Typing and Weak Typing A language is strongly typed Type errors are always detected, statically or dynamically There is strict enforcement of type rules with no exceptions The line between strong/week typing does not have consensus Ada, Java, and Haskell are strongly typed C is almost strongly typed, but union is the loophole Cannot assure type consistency with union

Strong Typing and Weak Typing Coercion and polymorphism blurs the line on strong or weak typing Coercion: such as type casting in C Especially dangerous for pointer type casting Coercion example x = 1; y = “2”; z = x + y JavaScript: z = 12 Visual Basic: z = 3 Polymorphism Earlier example of inconsistent return type  Make type checking difficult Python is strongly or weakly typed Still being debated, mostly consider it as strongly typed But how it handles polymorphism makes this debatable

Type Checking -- Summary Read Chapter 6 Sections 6.3, 6.5